blob: 82e85563bdf840763a2726c8491d6adf3564d389 [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
Mon P Wang3c81d352008-11-23 04:37:22 +000058static cl::opt<bool>
Mon P Wang9f22a4a2008-11-24 02:10:43 +000059DisableMMX("disable-mmx", cl::Hidden, cl::desc("Disable use of MMX"));
Mon P Wang3c81d352008-11-23 04:37:22 +000060
Evan Cheng10e86422008-04-25 19:11:04 +000061// Forward declarations.
Owen Andersone50ed302009-08-10 22:56:29 +000062static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +000063 SDValue V2);
Evan Cheng10e86422008-04-25 19:11:04 +000064
Chris Lattnerf0144122009-07-28 03:13:23 +000065static TargetLoweringObjectFile *createTLOF(X86TargetMachine &TM) {
Michael J. Spencerec38de22010-10-10 22:04:20 +000066
Eric Christopher62f35a22010-07-05 19:26:33 +000067 bool is64Bit = TM.getSubtarget<X86Subtarget>().is64Bit();
Michael J. Spencerec38de22010-10-10 22:04:20 +000068
Eric Christopher62f35a22010-07-05 19:26:33 +000069 if (TM.getSubtarget<X86Subtarget>().isTargetDarwin()) {
Chris Lattnere019ec12010-12-19 20:07:10 +000070 if (is64Bit)
71 return new X8664_MachoTargetObjectFile();
Anton Korobeynikov293d5922010-02-21 20:28:15 +000072 return new TargetLoweringObjectFileMachO();
Michael J. Spencerec38de22010-10-10 22:04:20 +000073 }
Chris Lattnere019ec12010-12-19 20:07:10 +000074
75 if (TM.getSubtarget<X86Subtarget>().isTargetELF() ){
76 if (is64Bit)
77 return new X8664_ELFTargetObjectFile(TM);
78 return new X8632_ELFTargetObjectFile(TM);
79 }
80 if (TM.getSubtarget<X86Subtarget>().isTargetCOFF())
81 return new TargetLoweringObjectFileCOFF();
Eric Christopher62f35a22010-07-05 19:26:33 +000082 llvm_unreachable("unknown subtarget type");
Chris Lattnerf0144122009-07-28 03:13:23 +000083}
84
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +000085X86TargetLowering::X86TargetLowering(X86TargetMachine &TM)
Chris Lattnerf0144122009-07-28 03:13:23 +000086 : TargetLowering(TM, createTLOF(TM)) {
Evan Cheng559806f2006-01-27 08:10:46 +000087 Subtarget = &TM.getSubtarget<X86Subtarget>();
Nate Begeman2ea8ee72010-12-10 00:26:57 +000088 X86ScalarSSEf64 = Subtarget->hasXMMInt();
89 X86ScalarSSEf32 = Subtarget->hasXMM();
Evan Cheng25ab6902006-09-08 06:48:29 +000090 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000091
Anton Korobeynikov2365f512007-07-14 14:06:15 +000092 RegInfo = TM.getRegisterInfo();
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000093 TD = getTargetData();
Anton Korobeynikov2365f512007-07-14 14:06:15 +000094
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000095 // Set up the TargetLowering object.
Chris Lattnera34b3cf2010-12-19 20:03:11 +000096 static MVT IntVTs[] = { MVT::i8, MVT::i16, MVT::i32, MVT::i64 };
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000097
98 // X86 is weird, it always uses i8 for shift amounts and setcc results.
Owen Anderson825b72b2009-08-11 20:47:22 +000099 setShiftAmountType(MVT::i8);
Duncan Sands03228082008-11-23 15:47:28 +0000100 setBooleanContents(ZeroOrOneBooleanContent);
Evan Cheng211ffa12010-05-19 20:19:50 +0000101 setSchedulingPreference(Sched::RegPressure);
Evan Cheng25ab6902006-09-08 06:48:29 +0000102 setStackPointerRegisterToSaveRestore(X86StackPtr);
Evan Cheng714554d2006-03-16 21:47:42 +0000103
Michael J. Spencer92bf38c2010-10-10 23:11:06 +0000104 if (Subtarget->isTargetWindows() && !Subtarget->isTargetCygMing()) {
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000105 // Setup Windows compiler runtime calls.
106 setLibcallName(RTLIB::SDIV_I64, "_alldiv");
Michael J. Spencer335b8062010-10-11 05:29:15 +0000107 setLibcallName(RTLIB::UDIV_I64, "_aulldiv");
108 setLibcallName(RTLIB::FPTOUINT_F64_I64, "_ftol2");
Michael J. Spencer94f7eeb2010-10-19 07:32:52 +0000109 setLibcallName(RTLIB::FPTOUINT_F32_I64, "_ftol2");
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000110 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::X86_StdCall);
Michael J. Spencer335b8062010-10-11 05:29:15 +0000111 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::X86_StdCall);
Michael J. Spencer6dad10e2010-10-27 18:52:38 +0000112 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I64, CallingConv::C);
113 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I64, CallingConv::C);
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000114 }
115
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000116 if (Subtarget->isTargetDarwin()) {
Evan Chengdf57fa02006-03-17 20:31:41 +0000117 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000118 setUseUnderscoreSetJmp(false);
119 setUseUnderscoreLongJmp(false);
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000120 } else if (Subtarget->isTargetMingw()) {
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000121 // MS runtime is weird: it exports _setjmp, but longjmp!
122 setUseUnderscoreSetJmp(true);
123 setUseUnderscoreLongJmp(false);
124 } else {
125 setUseUnderscoreSetJmp(true);
126 setUseUnderscoreLongJmp(true);
127 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000128
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000129 // Set up the register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000130 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
Dan Gohman71edb242010-04-30 18:30:26 +0000131 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000132 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
Evan Cheng25ab6902006-09-08 06:48:29 +0000133 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000134 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000135
Owen Anderson825b72b2009-08-11 20:47:22 +0000136 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Evan Chengc5484282006-10-04 00:56:09 +0000137
Scott Michelfdc40a02009-02-17 22:15:04 +0000138 // We don't accept any truncstore of integer registers.
Owen Anderson825b72b2009-08-11 20:47:22 +0000139 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000140 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000141 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000142 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000143 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
144 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
Evan Cheng7f042682008-10-15 02:05:31 +0000145
146 // SETOEQ and SETUNE require checking two conditions.
Owen Anderson825b72b2009-08-11 20:47:22 +0000147 setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand);
148 setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand);
149 setCondCodeAction(ISD::SETOEQ, MVT::f80, Expand);
150 setCondCodeAction(ISD::SETUNE, MVT::f32, Expand);
151 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
152 setCondCodeAction(ISD::SETUNE, MVT::f80, Expand);
Chris Lattnerddf89562008-01-17 19:59:44 +0000153
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000154 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
155 // operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000156 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
157 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
158 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +0000159
Evan Cheng25ab6902006-09-08 06:48:29 +0000160 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000161 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
162 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
Eli Friedman948e95a2009-05-23 09:59:16 +0000163 } else if (!UseSoftFloat) {
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000164 // We have an algorithm for SSE2->double, and we turn this into a
165 // 64-bit FILD followed by conditional FADD for other targets.
166 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Eli Friedman948e95a2009-05-23 09:59:16 +0000167 // We have an algorithm for SSE2, and we turn this into a 64-bit
168 // FILD for other targets.
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000169 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000170 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000171
172 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
173 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000174 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
175 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000176
Devang Patel6a784892009-06-05 18:48:29 +0000177 if (!UseSoftFloat) {
Bill Wendling105be5a2009-03-13 08:41:47 +0000178 // SSE has no i16 to fp conversion, only i32
179 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000180 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000181 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000182 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000183 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000184 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
185 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000186 }
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000187 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000188 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
189 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Promote);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000190 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000191
Dale Johannesen73328d12007-09-19 23:55:34 +0000192 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
193 // are Legal, f80 is custom lowered.
Owen Anderson825b72b2009-08-11 20:47:22 +0000194 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
195 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Evan Cheng6dab0532006-01-30 08:02:57 +0000196
Evan Cheng02568ff2006-01-30 22:13:22 +0000197 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
198 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000199 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
200 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
Evan Cheng02568ff2006-01-30 22:13:22 +0000201
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000202 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000203 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000204 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000205 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000206 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000207 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
208 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000209 }
210
211 // Handle FP_TO_UINT by promoting the destination to a larger signed
212 // conversion.
Owen Anderson825b72b2009-08-11 20:47:22 +0000213 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
214 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
215 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000216
Evan Cheng25ab6902006-09-08 06:48:29 +0000217 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000218 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
219 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Eli Friedman948e95a2009-05-23 09:59:16 +0000220 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000221 if (X86ScalarSSEf32 && !Subtarget->hasSSE3())
Evan Cheng25ab6902006-09-08 06:48:29 +0000222 // Expand FP_TO_UINT into a select.
223 // FIXME: We would like to use a Custom expander here eventually to do
224 // the optimal thing for SSE vs. the default expansion in the legalizer.
Owen Anderson825b72b2009-08-11 20:47:22 +0000225 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000226 else
Eli Friedman948e95a2009-05-23 09:59:16 +0000227 // With SSE3 we can use fisttpll to convert to a signed i64; without
228 // SSE, we're stuck with a fistpll.
Owen Anderson825b72b2009-08-11 20:47:22 +0000229 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000230 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000231
Chris Lattner399610a2006-12-05 18:22:22 +0000232 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Michael J. Spencerec38de22010-10-10 22:04:20 +0000233 if (!X86ScalarSSEf64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000234 setOperationAction(ISD::BITCAST , MVT::f32 , Expand);
235 setOperationAction(ISD::BITCAST , MVT::i32 , Expand);
Dale Johannesene39859a2010-05-21 18:40:15 +0000236 if (Subtarget->is64Bit()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000237 setOperationAction(ISD::BITCAST , MVT::f64 , Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000238 // Without SSE, i64->f64 goes through memory.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000239 setOperationAction(ISD::BITCAST , MVT::i64 , Expand);
Dale Johannesen7d07b482010-05-21 00:52:33 +0000240 }
Chris Lattnerf3597a12006-12-05 18:45:06 +0000241 }
Chris Lattner21f66852005-12-23 05:15:23 +0000242
Dan Gohmanb00ee212008-02-18 19:34:53 +0000243 // Scalar integer divide and remainder are lowered to use operations that
244 // produce two results, to match the available instructions. This exposes
245 // the two-result form to trivial CSE, which is able to combine x/y and x%y
246 // into a single instruction.
247 //
248 // Scalar integer multiply-high is also lowered to use two-result
249 // operations, to match the available instructions. However, plain multiply
250 // (low) operations are left as Legal, as there are single-result
251 // instructions for this in x86. Using the two-result multiply instructions
252 // when both high and low results are needed must be arranged by dagcombine.
Chris Lattnere019ec12010-12-19 20:07:10 +0000253 for (unsigned i = 0, e = 4; i != e; ++i) {
254 MVT VT = IntVTs[i];
255 setOperationAction(ISD::MULHS, VT, Expand);
256 setOperationAction(ISD::MULHU, VT, Expand);
257 setOperationAction(ISD::SDIV, VT, Expand);
258 setOperationAction(ISD::UDIV, VT, Expand);
259 setOperationAction(ISD::SREM, VT, Expand);
260 setOperationAction(ISD::UREM, VT, Expand);
261 }
Dan Gohmana37c9f72007-09-25 18:23:27 +0000262
Owen Anderson825b72b2009-08-11 20:47:22 +0000263 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
264 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
265 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
266 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000267 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000268 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
269 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
270 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
271 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
272 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
273 setOperationAction(ISD::FREM , MVT::f32 , Expand);
274 setOperationAction(ISD::FREM , MVT::f64 , Expand);
275 setOperationAction(ISD::FREM , MVT::f80 , Expand);
276 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000277
Owen Anderson825b72b2009-08-11 20:47:22 +0000278 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
279 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000280 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
281 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000282 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
283 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000284 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000285 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
286 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000287 }
288
Benjamin Kramer1292c222010-12-04 20:32:23 +0000289 if (Subtarget->hasPOPCNT()) {
290 setOperationAction(ISD::CTPOP , MVT::i8 , Promote);
291 } else {
292 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
293 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
294 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
295 if (Subtarget->is64Bit())
296 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
297 }
298
Owen Anderson825b72b2009-08-11 20:47:22 +0000299 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
300 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000301
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000302 // These should be promoted to a larger select which is supported.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000303 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000304 // X86 wants to expand cmov itself.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000305 setOperationAction(ISD::SELECT , MVT::i8 , Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000306 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000307 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
308 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
309 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
310 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
311 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000312 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000313 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
314 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
315 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
316 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000317 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000318 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
319 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000320 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000321 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000322
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000323 // Darwin ABI issue.
Owen Anderson825b72b2009-08-11 20:47:22 +0000324 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
325 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
326 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
327 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000328 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000329 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
330 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000331 setOperationAction(ISD::BlockAddress , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000332 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000333 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
334 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
335 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
336 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000337 setOperationAction(ISD::BlockAddress , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000338 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000339 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Owen Anderson825b72b2009-08-11 20:47:22 +0000340 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
341 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
342 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000343 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000344 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
345 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
346 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000347 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000348
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000349 if (Subtarget->hasXMM())
Owen Anderson825b72b2009-08-11 20:47:22 +0000350 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Cheng27b7db52008-03-08 00:58:38 +0000351
Eric Christopher9a9d2752010-07-22 02:48:34 +0000352 // We may not have a libcall for MEMBARRIER so we should lower this.
353 setOperationAction(ISD::MEMBARRIER , MVT::Other, Custom);
Michael J. Spencerec38de22010-10-10 22:04:20 +0000354
Jim Grosbachf1ab49e2010-06-23 16:25:07 +0000355 // On X86 and X86-64, atomic operations are lowered to locked instructions.
356 // Locked instructions, in turn, have implicit fence semantics (all memory
357 // operations are flushed before issuing the locked instruction, and they
358 // are not buffered), so we can fold away the common pattern of
359 // fence-atomic-fence.
360 setShouldFoldAtomicFences(true);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000361
Mon P Wang63307c32008-05-05 19:05:59 +0000362 // Expand certain atomics
Chris Lattnere019ec12010-12-19 20:07:10 +0000363 for (unsigned i = 0, e = 4; i != e; ++i) {
364 MVT VT = IntVTs[i];
365 setOperationAction(ISD::ATOMIC_CMP_SWAP, VT, Custom);
366 setOperationAction(ISD::ATOMIC_LOAD_SUB, VT, Custom);
367 }
368
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000369 if (!Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000370 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
371 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
372 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
373 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
374 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
375 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
376 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000377 }
378
Evan Cheng3c992d22006-03-07 02:02:57 +0000379 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000380 if (!Subtarget->isTargetDarwin() &&
381 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000382 !Subtarget->isTargetCygMing()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000383 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Dan Gohman44066042008-07-01 00:05:16 +0000384 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000385
Owen Anderson825b72b2009-08-11 20:47:22 +0000386 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
387 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
388 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
389 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000390 if (Subtarget->is64Bit()) {
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000391 setExceptionPointerRegister(X86::RAX);
392 setExceptionSelectorRegister(X86::RDX);
393 } else {
394 setExceptionPointerRegister(X86::EAX);
395 setExceptionSelectorRegister(X86::EDX);
396 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000397 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
398 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000399
Owen Anderson825b72b2009-08-11 20:47:22 +0000400 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000401
Owen Anderson825b72b2009-08-11 20:47:22 +0000402 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000403
Nate Begemanacc398c2006-01-25 18:21:52 +0000404 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson825b72b2009-08-11 20:47:22 +0000405 setOperationAction(ISD::VASTART , MVT::Other, Custom);
406 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000407 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000408 setOperationAction(ISD::VAARG , MVT::Other, Custom);
409 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000410 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000411 setOperationAction(ISD::VAARG , MVT::Other, Expand);
412 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000413 }
Evan Chengae642192007-03-02 23:16:35 +0000414
Owen Anderson825b72b2009-08-11 20:47:22 +0000415 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
416 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000417 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000418 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Michael J. Spencere9c253e2010-10-21 01:41:01 +0000419 if (Subtarget->isTargetCygMing() || Subtarget->isTargetWindows())
Owen Anderson825b72b2009-08-11 20:47:22 +0000420 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +0000421 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000422 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000423
Evan Chengc7ce29b2009-02-13 22:36:38 +0000424 if (!UseSoftFloat && X86ScalarSSEf64) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000425 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000426 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000427 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
428 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000429
Evan Cheng223547a2006-01-31 22:28:30 +0000430 // Use ANDPD to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000431 setOperationAction(ISD::FABS , MVT::f64, Custom);
432 setOperationAction(ISD::FABS , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000433
434 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000435 setOperationAction(ISD::FNEG , MVT::f64, Custom);
436 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000437
Evan Cheng68c47cb2007-01-05 07:55:56 +0000438 // Use ANDPD and ORPD to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000439 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
440 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000441
Evan Chengd25e9e82006-02-02 00:28:23 +0000442 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000443 setOperationAction(ISD::FSIN , MVT::f64, Expand);
444 setOperationAction(ISD::FCOS , MVT::f64, Expand);
445 setOperationAction(ISD::FSIN , MVT::f32, Expand);
446 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000447
Chris Lattnera54aa942006-01-29 06:26:08 +0000448 // Expand FP immediates into loads from the stack, except for the special
449 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000450 addLegalFPImmediate(APFloat(+0.0)); // xorpd
451 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Evan Chengc7ce29b2009-02-13 22:36:38 +0000452 } else if (!UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000453 // Use SSE for f32, x87 for f64.
454 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000455 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
456 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000457
458 // Use ANDPS to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000459 setOperationAction(ISD::FABS , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000460
461 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000462 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000463
Owen Anderson825b72b2009-08-11 20:47:22 +0000464 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000465
466 // Use ANDPS and ORPS to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000467 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
468 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000469
470 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000471 setOperationAction(ISD::FSIN , MVT::f32, Expand);
472 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000473
Nate Begemane1795842008-02-14 08:57:00 +0000474 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000475 addLegalFPImmediate(APFloat(+0.0f)); // xorps
476 addLegalFPImmediate(APFloat(+0.0)); // FLD0
477 addLegalFPImmediate(APFloat(+1.0)); // FLD1
478 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
479 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
480
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000481 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000482 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
483 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000484 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000485 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000486 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000487 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000488 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
489 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000490
Owen Anderson825b72b2009-08-11 20:47:22 +0000491 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
492 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
493 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
494 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000495
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000496 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000497 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
498 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000499 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000500 addLegalFPImmediate(APFloat(+0.0)); // FLD0
501 addLegalFPImmediate(APFloat(+1.0)); // FLD1
502 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
503 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000504 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
505 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
506 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
507 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000508 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000509
Dale Johannesen59a58732007-08-05 18:49:15 +0000510 // Long double always uses X87.
Evan Cheng92722532009-03-26 23:06:32 +0000511 if (!UseSoftFloat) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000512 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
513 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
514 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000515 {
Benjamin Kramer98383962010-12-04 14:22:24 +0000516 APFloat TmpFlt = APFloat::getZero(APFloat::x87DoubleExtended);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000517 addLegalFPImmediate(TmpFlt); // FLD0
518 TmpFlt.changeSign();
519 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
Benjamin Kramer98383962010-12-04 14:22:24 +0000520
521 bool ignored;
Evan Chengc7ce29b2009-02-13 22:36:38 +0000522 APFloat TmpFlt2(+1.0);
523 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
524 &ignored);
525 addLegalFPImmediate(TmpFlt2); // FLD1
526 TmpFlt2.changeSign();
527 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
528 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000529
Evan Chengc7ce29b2009-02-13 22:36:38 +0000530 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000531 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
532 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000533 }
Dale Johannesen2f429012007-09-26 21:10:55 +0000534 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000535
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000536 // Always use a library call for pow.
Owen Anderson825b72b2009-08-11 20:47:22 +0000537 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
538 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
539 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000540
Owen Anderson825b72b2009-08-11 20:47:22 +0000541 setOperationAction(ISD::FLOG, MVT::f80, Expand);
542 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
543 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
544 setOperationAction(ISD::FEXP, MVT::f80, Expand);
545 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000546
Mon P Wangf007a8b2008-11-06 05:31:54 +0000547 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000548 // (for widening) or expand (for scalarization). Then we will selectively
549 // turn on ones that can be effectively codegen'd.
Owen Anderson825b72b2009-08-11 20:47:22 +0000550 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
551 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
552 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
553 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
554 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
555 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
556 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
557 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
558 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
559 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
560 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
561 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
562 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
563 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
564 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
565 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
566 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
567 setOperationAction(ISD::EXTRACT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
568 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
569 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
570 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
571 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
572 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
573 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
574 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
575 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
576 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
577 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
578 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
579 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
580 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
581 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
582 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
583 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
584 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
585 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
586 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
587 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
588 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
589 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
590 setOperationAction(ISD::VSETCC, (MVT::SimpleValueType)VT, Expand);
591 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
592 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
593 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
594 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
595 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
596 setOperationAction(ISD::FP_TO_UINT, (MVT::SimpleValueType)VT, Expand);
597 setOperationAction(ISD::FP_TO_SINT, (MVT::SimpleValueType)VT, Expand);
598 setOperationAction(ISD::UINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
599 setOperationAction(ISD::SINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
Dan Gohman87862e72009-12-11 21:31:27 +0000600 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,Expand);
Dan Gohman2e141d72009-12-14 23:40:38 +0000601 setOperationAction(ISD::TRUNCATE, (MVT::SimpleValueType)VT, Expand);
602 setOperationAction(ISD::SIGN_EXTEND, (MVT::SimpleValueType)VT, Expand);
603 setOperationAction(ISD::ZERO_EXTEND, (MVT::SimpleValueType)VT, Expand);
604 setOperationAction(ISD::ANY_EXTEND, (MVT::SimpleValueType)VT, Expand);
605 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
606 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
607 setTruncStoreAction((MVT::SimpleValueType)VT,
608 (MVT::SimpleValueType)InnerVT, Expand);
609 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
610 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
611 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000612 }
613
Evan Chengc7ce29b2009-02-13 22:36:38 +0000614 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
615 // with -msoft-float, disable use of MMX as well.
Evan Cheng92722532009-03-26 23:06:32 +0000616 if (!UseSoftFloat && !DisableMMX && Subtarget->hasMMX()) {
Dale Johannesene93d99c2010-10-20 21:32:10 +0000617 addRegisterClass(MVT::x86mmx, X86::VR64RegisterClass);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000618 // No operations on x86mmx supported, everything uses intrinsics.
Evan Cheng470a6ad2006-02-22 02:26:30 +0000619 }
620
Dale Johannesen0488fb62010-09-30 23:57:10 +0000621 // MMX-sized vectors (other than x86mmx) are expected to be expanded
622 // into smaller operations.
623 setOperationAction(ISD::MULHS, MVT::v8i8, Expand);
624 setOperationAction(ISD::MULHS, MVT::v4i16, Expand);
625 setOperationAction(ISD::MULHS, MVT::v2i32, Expand);
626 setOperationAction(ISD::MULHS, MVT::v1i64, Expand);
627 setOperationAction(ISD::AND, MVT::v8i8, Expand);
628 setOperationAction(ISD::AND, MVT::v4i16, Expand);
629 setOperationAction(ISD::AND, MVT::v2i32, Expand);
630 setOperationAction(ISD::AND, MVT::v1i64, Expand);
631 setOperationAction(ISD::OR, MVT::v8i8, Expand);
632 setOperationAction(ISD::OR, MVT::v4i16, Expand);
633 setOperationAction(ISD::OR, MVT::v2i32, Expand);
634 setOperationAction(ISD::OR, MVT::v1i64, Expand);
635 setOperationAction(ISD::XOR, MVT::v8i8, Expand);
636 setOperationAction(ISD::XOR, MVT::v4i16, Expand);
637 setOperationAction(ISD::XOR, MVT::v2i32, Expand);
638 setOperationAction(ISD::XOR, MVT::v1i64, Expand);
639 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Expand);
640 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Expand);
641 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Expand);
642 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Expand);
643 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v1i64, Expand);
644 setOperationAction(ISD::SELECT, MVT::v8i8, Expand);
645 setOperationAction(ISD::SELECT, MVT::v4i16, Expand);
646 setOperationAction(ISD::SELECT, MVT::v2i32, Expand);
647 setOperationAction(ISD::SELECT, MVT::v1i64, Expand);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000648 setOperationAction(ISD::BITCAST, MVT::v8i8, Expand);
649 setOperationAction(ISD::BITCAST, MVT::v4i16, Expand);
650 setOperationAction(ISD::BITCAST, MVT::v2i32, Expand);
651 setOperationAction(ISD::BITCAST, MVT::v1i64, Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000652
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000653 if (!UseSoftFloat && Subtarget->hasXMM()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000654 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000655
Owen Anderson825b72b2009-08-11 20:47:22 +0000656 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
657 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
658 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
659 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
660 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
661 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
662 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
663 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
664 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
665 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
666 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
667 setOperationAction(ISD::VSETCC, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000668 }
669
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000670 if (!UseSoftFloat && Subtarget->hasXMMInt()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000671 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000672
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000673 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
674 // registers cannot be used even for integer operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000675 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
676 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
677 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
678 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000679
Owen Anderson825b72b2009-08-11 20:47:22 +0000680 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
681 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
682 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
683 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
684 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
685 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
686 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
687 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
688 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
689 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
690 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
691 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
692 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
693 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
694 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
695 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000696
Owen Anderson825b72b2009-08-11 20:47:22 +0000697 setOperationAction(ISD::VSETCC, MVT::v2f64, Custom);
698 setOperationAction(ISD::VSETCC, MVT::v16i8, Custom);
699 setOperationAction(ISD::VSETCC, MVT::v8i16, Custom);
700 setOperationAction(ISD::VSETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000701
Owen Anderson825b72b2009-08-11 20:47:22 +0000702 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
703 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
704 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
705 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
706 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000707
Mon P Wangeb38ebf2010-01-24 00:05:03 +0000708 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2f64, Custom);
709 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2i64, Custom);
710 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i8, Custom);
711 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i16, Custom);
712 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i32, Custom);
713
Evan Cheng2c3ae372006-04-12 21:21:57 +0000714 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Owen Anderson825b72b2009-08-11 20:47:22 +0000715 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
716 EVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000717 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000718 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000719 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000720 // Do not attempt to custom lower non-128-bit vectors
721 if (!VT.is128BitVector())
722 continue;
Owen Anderson825b72b2009-08-11 20:47:22 +0000723 setOperationAction(ISD::BUILD_VECTOR,
724 VT.getSimpleVT().SimpleTy, Custom);
725 setOperationAction(ISD::VECTOR_SHUFFLE,
726 VT.getSimpleVT().SimpleTy, Custom);
727 setOperationAction(ISD::EXTRACT_VECTOR_ELT,
728 VT.getSimpleVT().SimpleTy, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000729 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000730
Owen Anderson825b72b2009-08-11 20:47:22 +0000731 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
732 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
733 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
734 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
735 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
736 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000737
Nate Begemancdd1eec2008-02-12 22:51:28 +0000738 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000739 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
740 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000741 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000742
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000743 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Owen Anderson825b72b2009-08-11 20:47:22 +0000744 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; i++) {
745 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
Owen Andersone50ed302009-08-10 22:56:29 +0000746 EVT VT = SVT;
David Greene9b9838d2009-06-29 16:47:10 +0000747
748 // Do not attempt to promote non-128-bit vectors
Chris Lattner32b4b5a2010-07-05 05:53:14 +0000749 if (!VT.is128BitVector())
David Greene9b9838d2009-06-29 16:47:10 +0000750 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +0000751
Owen Andersond6662ad2009-08-10 20:46:15 +0000752 setOperationAction(ISD::AND, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000753 AddPromotedToType (ISD::AND, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000754 setOperationAction(ISD::OR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000755 AddPromotedToType (ISD::OR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000756 setOperationAction(ISD::XOR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000757 AddPromotedToType (ISD::XOR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000758 setOperationAction(ISD::LOAD, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000759 AddPromotedToType (ISD::LOAD, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000760 setOperationAction(ISD::SELECT, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000761 AddPromotedToType (ISD::SELECT, SVT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000762 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000763
Owen Anderson825b72b2009-08-11 20:47:22 +0000764 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000765
Evan Cheng2c3ae372006-04-12 21:21:57 +0000766 // Custom lower v2i64 and v2f64 selects.
Owen Anderson825b72b2009-08-11 20:47:22 +0000767 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
768 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
769 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
770 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000771
Owen Anderson825b72b2009-08-11 20:47:22 +0000772 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
773 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000774 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000775
Nate Begeman14d12ca2008-02-11 04:19:36 +0000776 if (Subtarget->hasSSE41()) {
Dale Johannesen54feef22010-05-27 20:12:41 +0000777 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
778 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
779 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
780 setOperationAction(ISD::FRINT, MVT::f32, Legal);
781 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
782 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
783 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
784 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
785 setOperationAction(ISD::FRINT, MVT::f64, Legal);
786 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
787
Nate Begeman14d12ca2008-02-11 04:19:36 +0000788 // FIXME: Do we need to handle scalar-to-vector here?
Owen Anderson825b72b2009-08-11 20:47:22 +0000789 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000790
Nate Begemanbdcb5af2010-07-27 22:37:06 +0000791 // Can turn SHL into an integer multiply.
792 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
Nate Begeman51409212010-07-28 00:21:48 +0000793 setOperationAction(ISD::SHL, MVT::v16i8, Custom);
Nate Begemanbdcb5af2010-07-27 22:37:06 +0000794
Nate Begeman14d12ca2008-02-11 04:19:36 +0000795 // i8 and i16 vectors are custom , because the source register and source
796 // source memory operand types are not the same width. f32 vectors are
797 // custom since the immediate controlling the insert encodes additional
798 // information.
Owen Anderson825b72b2009-08-11 20:47:22 +0000799 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
800 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
801 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
802 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000803
Owen Anderson825b72b2009-08-11 20:47:22 +0000804 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
805 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
806 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
807 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000808
809 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000810 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
811 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000812 }
813 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000814
Chris Lattnera34b3cf2010-12-19 20:03:11 +0000815 if (Subtarget->hasSSE42())
Owen Anderson825b72b2009-08-11 20:47:22 +0000816 setOperationAction(ISD::VSETCC, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000817
David Greene9b9838d2009-06-29 16:47:10 +0000818 if (!UseSoftFloat && Subtarget->hasAVX()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000819 addRegisterClass(MVT::v8f32, X86::VR256RegisterClass);
820 addRegisterClass(MVT::v4f64, X86::VR256RegisterClass);
821 addRegisterClass(MVT::v8i32, X86::VR256RegisterClass);
822 addRegisterClass(MVT::v4i64, X86::VR256RegisterClass);
Bruno Cardoso Lopes405f11b2010-08-10 01:43:16 +0000823 addRegisterClass(MVT::v32i8, X86::VR256RegisterClass);
David Greened94c1012009-06-29 22:50:51 +0000824
Owen Anderson825b72b2009-08-11 20:47:22 +0000825 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
826 setOperationAction(ISD::LOAD, MVT::v8i32, Legal);
827 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
828 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
829 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
830 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
831 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
832 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
833 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
834 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +0000835 setOperationAction(ISD::BUILD_VECTOR, MVT::v8f32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000836 //setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8f32, Custom);
837 //setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8f32, Custom);
838 //setOperationAction(ISD::SELECT, MVT::v8f32, Custom);
839 //setOperationAction(ISD::VSETCC, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000840
841 // Operations to consider commented out -v16i16 v32i8
Owen Anderson825b72b2009-08-11 20:47:22 +0000842 //setOperationAction(ISD::ADD, MVT::v16i16, Legal);
843 setOperationAction(ISD::ADD, MVT::v8i32, Custom);
844 setOperationAction(ISD::ADD, MVT::v4i64, Custom);
845 //setOperationAction(ISD::SUB, MVT::v32i8, Legal);
846 //setOperationAction(ISD::SUB, MVT::v16i16, Legal);
847 setOperationAction(ISD::SUB, MVT::v8i32, Custom);
848 setOperationAction(ISD::SUB, MVT::v4i64, Custom);
849 //setOperationAction(ISD::MUL, MVT::v16i16, Legal);
850 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
851 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
852 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
853 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
854 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
855 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000856
Owen Anderson825b72b2009-08-11 20:47:22 +0000857 setOperationAction(ISD::VSETCC, MVT::v4f64, Custom);
858 // setOperationAction(ISD::VSETCC, MVT::v32i8, Custom);
859 // setOperationAction(ISD::VSETCC, MVT::v16i16, Custom);
860 setOperationAction(ISD::VSETCC, MVT::v8i32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000861
Owen Anderson825b72b2009-08-11 20:47:22 +0000862 // setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v32i8, Custom);
863 // setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i16, Custom);
864 // setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i16, Custom);
865 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i32, Custom);
866 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000867
Owen Anderson825b72b2009-08-11 20:47:22 +0000868 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f64, Custom);
869 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i64, Custom);
870 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f64, Custom);
871 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i64, Custom);
872 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f64, Custom);
873 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000874
875#if 0
876 // Not sure we want to do this since there are no 256-bit integer
877 // operations in AVX
878
879 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
880 // This includes 256-bit vectors
Owen Anderson825b72b2009-08-11 20:47:22 +0000881 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v4i64; ++i) {
882 EVT VT = (MVT::SimpleValueType)i;
David Greene9b9838d2009-06-29 16:47:10 +0000883
884 // Do not attempt to custom lower non-power-of-2 vectors
885 if (!isPowerOf2_32(VT.getVectorNumElements()))
886 continue;
887
888 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
889 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
890 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
891 }
892
893 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000894 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i64, Custom);
895 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i64, Custom);
Eric Christopherfd179292009-08-27 18:07:15 +0000896 }
David Greene9b9838d2009-06-29 16:47:10 +0000897#endif
898
899#if 0
900 // Not sure we want to do this since there are no 256-bit integer
901 // operations in AVX
902
903 // Promote v32i8, v16i16, v8i32 load, select, and, or, xor to v4i64.
904 // Including 256-bit vectors
Owen Anderson825b72b2009-08-11 20:47:22 +0000905 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v4i64; i++) {
906 EVT VT = (MVT::SimpleValueType)i;
David Greene9b9838d2009-06-29 16:47:10 +0000907
908 if (!VT.is256BitVector()) {
909 continue;
910 }
911 setOperationAction(ISD::AND, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000912 AddPromotedToType (ISD::AND, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000913 setOperationAction(ISD::OR, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000914 AddPromotedToType (ISD::OR, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000915 setOperationAction(ISD::XOR, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000916 AddPromotedToType (ISD::XOR, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000917 setOperationAction(ISD::LOAD, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000918 AddPromotedToType (ISD::LOAD, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000919 setOperationAction(ISD::SELECT, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000920 AddPromotedToType (ISD::SELECT, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000921 }
922
Owen Anderson825b72b2009-08-11 20:47:22 +0000923 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
David Greene9b9838d2009-06-29 16:47:10 +0000924#endif
925 }
926
Evan Cheng6be2c582006-04-05 23:38:46 +0000927 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000928 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +0000929
Chris Lattnera34b3cf2010-12-19 20:03:11 +0000930
Eli Friedman962f5492010-06-02 19:35:46 +0000931 // Only custom-lower 64-bit SADDO and friends on 64-bit because we don't
932 // handle type legalization for these operations here.
Dan Gohman71c62a22010-06-02 19:13:40 +0000933 //
Eli Friedman962f5492010-06-02 19:35:46 +0000934 // FIXME: We really should do custom legalization for addition and
935 // subtraction on x86-32 once PR3203 is fixed. We really can't do much better
936 // than generic legalization for 64-bit multiplication-with-overflow, though.
Chris Lattnera34b3cf2010-12-19 20:03:11 +0000937 for (unsigned i = 0, e = 3+Subtarget->is64Bit(); i != e; ++i) {
938 // Add/Sub/Mul with overflow operations are custom lowered.
939 MVT VT = IntVTs[i];
940 setOperationAction(ISD::SADDO, VT, Custom);
941 setOperationAction(ISD::UADDO, VT, Custom);
942 setOperationAction(ISD::SSUBO, VT, Custom);
943 setOperationAction(ISD::USUBO, VT, Custom);
944 setOperationAction(ISD::SMULO, VT, Custom);
945 setOperationAction(ISD::UMULO, VT, Custom);
Eli Friedmana993f0a2010-06-02 00:27:18 +0000946 }
Chris Lattnera34b3cf2010-12-19 20:03:11 +0000947
948 // There are no 8-bit 3-address imul/mul instructions
949 setOperationAction(ISD::SMULO, MVT::i8, Expand);
950 setOperationAction(ISD::UMULO, MVT::i8, Expand);
Bill Wendling41ea7e72008-11-24 19:21:46 +0000951
Evan Chengd54f2d52009-03-31 19:38:51 +0000952 if (!Subtarget->is64Bit()) {
953 // These libcalls are not available in 32-bit.
954 setLibcallName(RTLIB::SHL_I128, 0);
955 setLibcallName(RTLIB::SRL_I128, 0);
956 setLibcallName(RTLIB::SRA_I128, 0);
957 }
958
Evan Cheng206ee9d2006-07-07 08:33:52 +0000959 // We have target-specific dag combine patterns for the following nodes:
960 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Dan Gohman1bbf72b2010-03-15 23:23:03 +0000961 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Evan Chengd880b972008-05-09 21:53:03 +0000962 setTargetDAGCombine(ISD::BUILD_VECTOR);
Chris Lattner83e6c992006-10-04 06:57:07 +0000963 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +0000964 setTargetDAGCombine(ISD::SHL);
965 setTargetDAGCombine(ISD::SRA);
966 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +0000967 setTargetDAGCombine(ISD::OR);
Nate Begemanb65c1752010-12-17 22:55:37 +0000968 setTargetDAGCombine(ISD::AND);
Chris Lattner149a4e52008-02-22 02:09:43 +0000969 setTargetDAGCombine(ISD::STORE);
Evan Cheng2e489c42009-12-16 00:53:11 +0000970 setTargetDAGCombine(ISD::ZERO_EXTEND);
Evan Cheng0b0cd912009-03-28 05:57:29 +0000971 if (Subtarget->is64Bit())
972 setTargetDAGCombine(ISD::MUL);
Evan Cheng206ee9d2006-07-07 08:33:52 +0000973
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000974 computeRegisterProperties();
975
Evan Cheng87ed7162006-02-14 08:25:08 +0000976 // FIXME: These should be based on subtarget info. Plus, the values should
977 // be smaller when we are in optimizing for size mode.
Dan Gohman87060f52008-06-30 21:00:56 +0000978 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
Evan Cheng255f20f2010-04-01 06:04:33 +0000979 maxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
Dan Gohman87060f52008-06-30 21:00:56 +0000980 maxStoresPerMemmove = 3; // For @llvm.memmove -> sequence of stores
Evan Chengfb8075d2008-02-28 00:43:03 +0000981 setPrefLoopAlignment(16);
Evan Cheng6ebf7bc2009-05-13 21:42:09 +0000982 benefitFromCodePlacementOpt = true;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000983}
984
Scott Michel5b8f82e2008-03-10 15:42:14 +0000985
Owen Anderson825b72b2009-08-11 20:47:22 +0000986MVT::SimpleValueType X86TargetLowering::getSetCCResultType(EVT VT) const {
987 return MVT::i8;
Scott Michel5b8f82e2008-03-10 15:42:14 +0000988}
989
990
Evan Cheng29286502008-01-23 23:17:41 +0000991/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
992/// the desired ByVal argument alignment.
993static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
994 if (MaxAlign == 16)
995 return;
996 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
997 if (VTy->getBitWidth() == 128)
998 MaxAlign = 16;
Evan Cheng29286502008-01-23 23:17:41 +0000999 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
1000 unsigned EltAlign = 0;
1001 getMaxByValAlign(ATy->getElementType(), EltAlign);
1002 if (EltAlign > MaxAlign)
1003 MaxAlign = EltAlign;
1004 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
1005 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1006 unsigned EltAlign = 0;
1007 getMaxByValAlign(STy->getElementType(i), EltAlign);
1008 if (EltAlign > MaxAlign)
1009 MaxAlign = EltAlign;
1010 if (MaxAlign == 16)
1011 break;
1012 }
1013 }
1014 return;
1015}
1016
1017/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1018/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001019/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1020/// are at 4-byte boundaries.
Evan Cheng29286502008-01-23 23:17:41 +00001021unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001022 if (Subtarget->is64Bit()) {
1023 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001024 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001025 if (TyAlign > 8)
1026 return TyAlign;
1027 return 8;
1028 }
1029
Evan Cheng29286502008-01-23 23:17:41 +00001030 unsigned Align = 4;
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001031 if (Subtarget->hasXMM())
Dale Johannesen0c191872008-02-08 19:48:20 +00001032 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001033 return Align;
1034}
Chris Lattner2b02a442007-02-25 08:29:00 +00001035
Evan Chengf0df0312008-05-15 08:39:06 +00001036/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengc3b0c342010-04-08 07:37:57 +00001037/// and store operations as a result of memset, memcpy, and memmove
1038/// lowering. If DstAlign is zero that means it's safe to destination
1039/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1040/// means there isn't a need to check it against alignment requirement,
1041/// probably because the source does not need to be loaded. If
1042/// 'NonScalarIntSafe' is true, that means it's safe to return a
1043/// non-scalar-integer type, e.g. empty string source, constant, or loaded
1044/// from memory. 'MemcpyStrSrc' indicates whether the memcpy source is
1045/// constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001046/// It returns EVT::Other if the type should be determined using generic
1047/// target-independent logic.
Owen Andersone50ed302009-08-10 22:56:29 +00001048EVT
Evan Cheng255f20f2010-04-01 06:04:33 +00001049X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1050 unsigned DstAlign, unsigned SrcAlign,
Evan Chengf28f8bc2010-04-02 19:36:14 +00001051 bool NonScalarIntSafe,
Evan Chengc3b0c342010-04-08 07:37:57 +00001052 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00001053 MachineFunction &MF) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001054 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
1055 // linux. This is because the stack realignment code can't handle certain
1056 // cases like PR2962. This should be removed when PR2962 is fixed.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001057 const Function *F = MF.getFunction();
Evan Chengf28f8bc2010-04-02 19:36:14 +00001058 if (NonScalarIntSafe &&
1059 !F->hasFnAttr(Attribute::NoImplicitFloat)) {
Evan Cheng255f20f2010-04-01 06:04:33 +00001060 if (Size >= 16 &&
1061 (Subtarget->isUnalignedMemAccessFast() ||
Chandler Carruthae1d41c2010-04-02 01:31:24 +00001062 ((DstAlign == 0 || DstAlign >= 16) &&
1063 (SrcAlign == 0 || SrcAlign >= 16))) &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001064 Subtarget->getStackAlignment() >= 16) {
1065 if (Subtarget->hasSSE2())
1066 return MVT::v4i32;
Evan Chengf28f8bc2010-04-02 19:36:14 +00001067 if (Subtarget->hasSSE1())
Evan Cheng255f20f2010-04-01 06:04:33 +00001068 return MVT::v4f32;
Evan Chengc3b0c342010-04-08 07:37:57 +00001069 } else if (!MemcpyStrSrc && Size >= 8 &&
Evan Cheng3ea97552010-04-01 20:27:45 +00001070 !Subtarget->is64Bit() &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001071 Subtarget->getStackAlignment() >= 8 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001072 Subtarget->hasXMMInt()) {
Evan Chengc3b0c342010-04-08 07:37:57 +00001073 // Do not use f64 to lower memcpy if source is string constant. It's
1074 // better to use i32 to avoid the loads.
Evan Cheng255f20f2010-04-01 06:04:33 +00001075 return MVT::f64;
Evan Chengc3b0c342010-04-08 07:37:57 +00001076 }
Chris Lattner4002a1b2008-10-28 05:49:35 +00001077 }
Evan Chengf0df0312008-05-15 08:39:06 +00001078 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001079 return MVT::i64;
1080 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001081}
1082
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001083/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1084/// current function. The returned value is a member of the
1085/// MachineJumpTableInfo::JTEntryKind enum.
1086unsigned X86TargetLowering::getJumpTableEncoding() const {
1087 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1088 // symbol.
1089 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1090 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001091 return MachineJumpTableInfo::EK_Custom32;
Michael J. Spencerec38de22010-10-10 22:04:20 +00001092
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001093 // Otherwise, use the normal jump table encoding heuristics.
1094 return TargetLowering::getJumpTableEncoding();
1095}
1096
Chris Lattnerc64daab2010-01-26 05:02:42 +00001097const MCExpr *
1098X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1099 const MachineBasicBlock *MBB,
1100 unsigned uid,MCContext &Ctx) const{
1101 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1102 Subtarget->isPICStyleGOT());
1103 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1104 // entries.
Daniel Dunbar4e815f82010-03-15 23:51:06 +00001105 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1106 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001107}
1108
Evan Chengcc415862007-11-09 01:32:10 +00001109/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1110/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001111SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001112 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001113 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001114 // This doesn't have DebugLoc associated with it, but is not really the
1115 // same as a Register.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001116 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001117 return Table;
1118}
1119
Chris Lattner589c6f62010-01-26 06:28:43 +00001120/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1121/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1122/// MCExpr.
1123const MCExpr *X86TargetLowering::
1124getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1125 MCContext &Ctx) const {
1126 // X86-64 uses RIP relative addressing based on the jump table label.
1127 if (Subtarget->isPICStyleRIPRel())
1128 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1129
1130 // Otherwise, the reference is relative to the PIC base.
Chris Lattner142b5312010-11-14 22:48:15 +00001131 return MCSymbolRefExpr::Create(MF->getPICBaseSymbol(), Ctx);
Chris Lattner589c6f62010-01-26 06:28:43 +00001132}
1133
Bill Wendlingb4202b82009-07-01 18:50:55 +00001134/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +00001135unsigned X86TargetLowering::getFunctionAlignment(const Function *F) const {
Dan Gohman25103a22009-08-18 00:20:06 +00001136 return F->hasFnAttr(Attribute::OptimizeForSize) ? 0 : 4;
Bill Wendling20c568f2009-06-30 22:38:32 +00001137}
1138
Evan Chengdee81012010-07-26 21:50:05 +00001139std::pair<const TargetRegisterClass*, uint8_t>
1140X86TargetLowering::findRepresentativeClass(EVT VT) const{
1141 const TargetRegisterClass *RRC = 0;
1142 uint8_t Cost = 1;
1143 switch (VT.getSimpleVT().SimpleTy) {
1144 default:
1145 return TargetLowering::findRepresentativeClass(VT);
1146 case MVT::i8: case MVT::i16: case MVT::i32: case MVT::i64:
1147 RRC = (Subtarget->is64Bit()
1148 ? X86::GR64RegisterClass : X86::GR32RegisterClass);
1149 break;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001150 case MVT::x86mmx:
Evan Chengdee81012010-07-26 21:50:05 +00001151 RRC = X86::VR64RegisterClass;
1152 break;
1153 case MVT::f32: case MVT::f64:
1154 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1155 case MVT::v4f32: case MVT::v2f64:
1156 case MVT::v32i8: case MVT::v8i32: case MVT::v4i64: case MVT::v8f32:
1157 case MVT::v4f64:
1158 RRC = X86::VR128RegisterClass;
1159 break;
1160 }
1161 return std::make_pair(RRC, Cost);
1162}
1163
Evan Cheng70017e42010-07-24 00:39:05 +00001164unsigned
1165X86TargetLowering::getRegPressureLimit(const TargetRegisterClass *RC,
1166 MachineFunction &MF) const {
Anton Korobeynikovd0c38172010-11-18 21:19:35 +00001167 const TargetFrameInfo *TFI = MF.getTarget().getFrameInfo();
1168
1169 unsigned FPDiff = TFI->hasFP(MF) ? 1 : 0;
Evan Cheng70017e42010-07-24 00:39:05 +00001170 switch (RC->getID()) {
1171 default:
1172 return 0;
1173 case X86::GR32RegClassID:
1174 return 4 - FPDiff;
1175 case X86::GR64RegClassID:
1176 return 8 - FPDiff;
1177 case X86::VR128RegClassID:
1178 return Subtarget->is64Bit() ? 10 : 4;
1179 case X86::VR64RegClassID:
1180 return 4;
1181 }
1182}
1183
Eric Christopherf7a0c7b2010-07-06 05:18:56 +00001184bool X86TargetLowering::getStackCookieLocation(unsigned &AddressSpace,
1185 unsigned &Offset) const {
1186 if (!Subtarget->isTargetLinux())
1187 return false;
1188
1189 if (Subtarget->is64Bit()) {
1190 // %fs:0x28, unless we're using a Kernel code model, in which case it's %gs:
1191 Offset = 0x28;
1192 if (getTargetMachine().getCodeModel() == CodeModel::Kernel)
1193 AddressSpace = 256;
1194 else
1195 AddressSpace = 257;
1196 } else {
1197 // %gs:0x14 on i386
1198 Offset = 0x14;
1199 AddressSpace = 256;
1200 }
1201 return true;
1202}
1203
1204
Chris Lattner2b02a442007-02-25 08:29:00 +00001205//===----------------------------------------------------------------------===//
1206// Return Value Calling Convention Implementation
1207//===----------------------------------------------------------------------===//
1208
Chris Lattner59ed56b2007-02-28 04:55:35 +00001209#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001210
Michael J. Spencerec38de22010-10-10 22:04:20 +00001211bool
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001212X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv, bool isVarArg,
Dan Gohman84023e02010-07-10 09:00:22 +00001213 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001214 LLVMContext &Context) const {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001215 SmallVector<CCValAssign, 16> RVLocs;
1216 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001217 RVLocs, Context);
Dan Gohman84023e02010-07-10 09:00:22 +00001218 return CCInfo.CheckReturn(Outs, RetCC_X86);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001219}
1220
Dan Gohman98ca4f22009-08-05 01:29:28 +00001221SDValue
1222X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001223 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001224 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001225 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001226 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001227 MachineFunction &MF = DAG.getMachineFunction();
1228 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001229
Chris Lattner9774c912007-02-27 05:28:59 +00001230 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001231 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1232 RVLocs, *DAG.getContext());
1233 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001234
Evan Chengdcea1632010-02-04 02:40:39 +00001235 // Add the regs to the liveout set for the function.
1236 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1237 for (unsigned i = 0; i != RVLocs.size(); ++i)
1238 if (RVLocs[i].isRegLoc() && !MRI.isLiveOut(RVLocs[i].getLocReg()))
1239 MRI.addLiveOut(RVLocs[i].getLocReg());
Scott Michelfdc40a02009-02-17 22:15:04 +00001240
Dan Gohman475871a2008-07-27 21:46:04 +00001241 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001242
Dan Gohman475871a2008-07-27 21:46:04 +00001243 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001244 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1245 // Operand #1 = Bytes To Pop
Dan Gohman1e93df62010-04-17 14:41:14 +00001246 RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1247 MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001248
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001249 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001250 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1251 CCValAssign &VA = RVLocs[i];
1252 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohmanc9403652010-07-07 15:54:55 +00001253 SDValue ValToCopy = OutVals[i];
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001254 EVT ValVT = ValToCopy.getValueType();
1255
Dale Johannesenc4510512010-09-24 19:05:48 +00001256 // If this is x86-64, and we disabled SSE, we can't return FP values,
1257 // or SSE or MMX vectors.
1258 if ((ValVT == MVT::f32 || ValVT == MVT::f64 ||
1259 VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001260 (Subtarget->is64Bit() && !Subtarget->hasXMM())) {
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001261 report_fatal_error("SSE register return with SSE disabled");
1262 }
1263 // Likewise we can't return F64 values with SSE1 only. gcc does so, but
1264 // llvm-gcc has never done it right and no one has noticed, so this
1265 // should be OK for now.
1266 if (ValVT == MVT::f64 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001267 (Subtarget->is64Bit() && !Subtarget->hasXMMInt()))
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001268 report_fatal_error("SSE2 register return with SSE2 disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001269
Chris Lattner447ff682008-03-11 03:23:40 +00001270 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1271 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001272 if (VA.getLocReg() == X86::ST0 ||
1273 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001274 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1275 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001276 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001277 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001278 RetOps.push_back(ValToCopy);
1279 // Don't emit a copytoreg.
1280 continue;
1281 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001282
Evan Cheng242b38b2009-02-23 09:03:22 +00001283 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1284 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001285 if (Subtarget->is64Bit()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001286 if (ValVT == MVT::x86mmx) {
Chris Lattner97a2a562010-08-26 05:24:29 +00001287 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001288 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ValToCopy);
Eric Christopher90eb4022010-07-22 00:26:08 +00001289 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
1290 ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001291 // If we don't have SSE2 available, convert to v4f32 so the generated
1292 // register is legal.
1293 if (!Subtarget->hasSSE2())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001294 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32,ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001295 }
Evan Cheng242b38b2009-02-23 09:03:22 +00001296 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001297 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00001298
Dale Johannesendd64c412009-02-04 00:33:20 +00001299 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001300 Flag = Chain.getValue(1);
1301 }
Dan Gohman61a92132008-04-21 23:59:07 +00001302
1303 // The x86-64 ABI for returning structs by value requires that we copy
1304 // the sret argument into %rax for the return. We saved the argument into
1305 // a virtual register in the entry block, so now we copy the value out
1306 // and into %rax.
1307 if (Subtarget->is64Bit() &&
1308 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1309 MachineFunction &MF = DAG.getMachineFunction();
1310 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1311 unsigned Reg = FuncInfo->getSRetReturnReg();
Michael J. Spencerec38de22010-10-10 22:04:20 +00001312 assert(Reg &&
Zhongxing Xuc2798a12010-05-26 08:10:02 +00001313 "SRetReturnReg should have been set in LowerFormalArguments().");
Dale Johannesendd64c412009-02-04 00:33:20 +00001314 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001315
Dale Johannesendd64c412009-02-04 00:33:20 +00001316 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001317 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001318
1319 // RAX now acts like a return value.
Evan Chengdcea1632010-02-04 02:40:39 +00001320 MRI.addLiveOut(X86::RAX);
Dan Gohman61a92132008-04-21 23:59:07 +00001321 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001322
Chris Lattner447ff682008-03-11 03:23:40 +00001323 RetOps[0] = Chain; // Update chain.
1324
1325 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001326 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001327 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001328
1329 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001330 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001331}
1332
Evan Cheng3d2125c2010-11-30 23:55:39 +00001333bool X86TargetLowering::isUsedByReturnOnly(SDNode *N) const {
1334 if (N->getNumValues() != 1)
1335 return false;
1336 if (!N->hasNUsesOfValue(1, 0))
1337 return false;
1338
1339 SDNode *Copy = *N->use_begin();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001340 if (Copy->getOpcode() != ISD::CopyToReg &&
1341 Copy->getOpcode() != ISD::FP_EXTEND)
Evan Cheng3d2125c2010-11-30 23:55:39 +00001342 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001343
1344 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001345 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001346 UI != UE; ++UI) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001347 if (UI->getOpcode() != X86ISD::RET_FLAG)
1348 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001349 HasRet = true;
1350 }
Evan Cheng3d2125c2010-11-30 23:55:39 +00001351
Evan Cheng1bf891a2010-12-01 22:59:46 +00001352 return HasRet;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001353}
1354
Dan Gohman98ca4f22009-08-05 01:29:28 +00001355/// LowerCallResult - Lower the result values of a call into the
1356/// appropriate copies out of appropriate physical registers.
1357///
1358SDValue
1359X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001360 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001361 const SmallVectorImpl<ISD::InputArg> &Ins,
1362 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001363 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001364
Chris Lattnere32bbf62007-02-28 07:09:55 +00001365 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001366 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001367 bool Is64Bit = Subtarget->is64Bit();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001368 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +00001369 RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001370 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001371
Chris Lattner3085e152007-02-25 08:59:22 +00001372 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001373 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001374 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001375 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001376
Torok Edwin3f142c32009-02-01 18:15:56 +00001377 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001378 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001379 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasXMM())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001380 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001381 }
1382
Evan Cheng79fb3b42009-02-20 20:43:02 +00001383 SDValue Val;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001384
1385 // If this is a call to a function that returns an fp value on the floating
1386 // point stack, we must guarantee the the value is popped from the stack, so
1387 // a CopyFromReg is not good enough - the copy instruction may be eliminated
1388 // if the return value is not used. We use the FpGET_ST0 instructions
1389 // instead.
1390 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1) {
1391 // If we prefer to use the value in xmm registers, copy it out as f80 and
1392 // use a truncate to move it from fp stack reg to xmm reg.
1393 if (isScalarFPTypeInSSEReg(VA.getValVT())) CopyVT = MVT::f80;
1394 bool isST0 = VA.getLocReg() == X86::ST0;
1395 unsigned Opc = 0;
1396 if (CopyVT == MVT::f32) Opc = isST0 ? X86::FpGET_ST0_32:X86::FpGET_ST1_32;
1397 if (CopyVT == MVT::f64) Opc = isST0 ? X86::FpGET_ST0_64:X86::FpGET_ST1_64;
1398 if (CopyVT == MVT::f80) Opc = isST0 ? X86::FpGET_ST0_80:X86::FpGET_ST1_80;
1399 SDValue Ops[] = { Chain, InFlag };
1400 Chain = SDValue(DAG.getMachineNode(Opc, dl, CopyVT, MVT::Other, MVT::Flag,
1401 Ops, 2), 1);
1402 Val = Chain.getValue(0);
1403
1404 // Round the f80 to the right size, which also moves it to the appropriate
1405 // xmm register.
1406 if (CopyVT != VA.getValVT())
1407 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
1408 // This truncation won't change the value.
1409 DAG.getIntPtrConstant(1));
1410 } else if (Is64Bit && CopyVT.isVector() && CopyVT.getSizeInBits() == 64) {
Evan Cheng242b38b2009-02-23 09:03:22 +00001411 // For x86-64, MMX values are returned in XMM0 / XMM1 except for v1i64.
1412 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
1413 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001414 MVT::v2i64, InFlag).getValue(1);
Evan Cheng242b38b2009-02-23 09:03:22 +00001415 Val = Chain.getValue(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00001416 Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64,
1417 Val, DAG.getConstant(0, MVT::i64));
Evan Cheng242b38b2009-02-23 09:03:22 +00001418 } else {
1419 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001420 MVT::i64, InFlag).getValue(1);
Evan Cheng242b38b2009-02-23 09:03:22 +00001421 Val = Chain.getValue(0);
1422 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001423 Val = DAG.getNode(ISD::BITCAST, dl, CopyVT, Val);
Evan Cheng79fb3b42009-02-20 20:43:02 +00001424 } else {
1425 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1426 CopyVT, InFlag).getValue(1);
1427 Val = Chain.getValue(0);
1428 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001429 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001430 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001431 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001432
Dan Gohman98ca4f22009-08-05 01:29:28 +00001433 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001434}
1435
1436
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001437//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001438// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001439//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001440// StdCall calling convention seems to be standard for many Windows' API
1441// routines and around. It differs from C calling convention just a little:
1442// callee should clean up the stack, not caller. Symbols should be also
1443// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001444// For info on fast calling convention see Fast Calling Convention (tail call)
1445// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001446
Dan Gohman98ca4f22009-08-05 01:29:28 +00001447/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001448/// semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001449static bool CallIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
1450 if (Outs.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001451 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001452
Dan Gohman98ca4f22009-08-05 01:29:28 +00001453 return Outs[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001454}
1455
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001456/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001457/// return semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001458static bool
1459ArgsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
1460 if (Ins.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001461 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001462
Dan Gohman98ca4f22009-08-05 01:29:28 +00001463 return Ins[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001464}
1465
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001466/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1467/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001468/// the specific parameter attribute. The copy will be passed as a byval
1469/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001470static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001471CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001472 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1473 DebugLoc dl) {
Chris Lattnere72f2022010-09-21 05:40:29 +00001474 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Michael J. Spencerec38de22010-10-10 22:04:20 +00001475
Dale Johannesendd64c412009-02-04 00:33:20 +00001476 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Mon P Wang20adc9d2010-04-04 03:10:48 +00001477 /*isVolatile*/false, /*AlwaysInline=*/true,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001478 MachinePointerInfo(), MachinePointerInfo());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001479}
1480
Chris Lattner29689432010-03-11 00:22:57 +00001481/// IsTailCallConvention - Return true if the calling convention is one that
1482/// supports tail call optimization.
1483static bool IsTailCallConvention(CallingConv::ID CC) {
1484 return (CC == CallingConv::Fast || CC == CallingConv::GHC);
1485}
1486
Evan Cheng0c439eb2010-01-27 00:07:07 +00001487/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1488/// a tailcall target by changing its ABI.
1489static bool FuncIsMadeTailCallSafe(CallingConv::ID CC) {
Chris Lattner29689432010-03-11 00:22:57 +00001490 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001491}
1492
Dan Gohman98ca4f22009-08-05 01:29:28 +00001493SDValue
1494X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001495 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001496 const SmallVectorImpl<ISD::InputArg> &Ins,
1497 DebugLoc dl, SelectionDAG &DAG,
1498 const CCValAssign &VA,
1499 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00001500 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00001501 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001502 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Evan Cheng0c439eb2010-01-27 00:07:07 +00001503 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001504 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001505 EVT ValVT;
1506
1507 // If value is passed by pointer we have address passed instead of the value
1508 // itself.
1509 if (VA.getLocInfo() == CCValAssign::Indirect)
1510 ValVT = VA.getLocVT();
1511 else
1512 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001513
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001514 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001515 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001516 // In case of tail call optimization mark all arguments mutable. Since they
1517 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001518 if (Flags.isByVal()) {
1519 int FI = MFI->CreateFixedObject(Flags.getByValSize(),
Evan Chenged2ae132010-07-03 00:40:23 +00001520 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001521 return DAG.getFrameIndex(FI, getPointerTy());
1522 } else {
1523 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00001524 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001525 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1526 return DAG.getLoad(ValVT, dl, Chain, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001527 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00001528 false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001529 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001530}
1531
Dan Gohman475871a2008-07-27 21:46:04 +00001532SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001533X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001534 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001535 bool isVarArg,
1536 const SmallVectorImpl<ISD::InputArg> &Ins,
1537 DebugLoc dl,
1538 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001539 SmallVectorImpl<SDValue> &InVals)
1540 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00001541 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001542 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001543
Gordon Henriksen86737662008-01-05 16:56:59 +00001544 const Function* Fn = MF.getFunction();
1545 if (Fn->hasExternalLinkage() &&
1546 Subtarget->isTargetCygMing() &&
1547 Fn->getName() == "main")
1548 FuncInfo->setForceFramePointer(true);
1549
Evan Cheng1bc78042006-04-26 01:20:17 +00001550 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001551 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001552 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001553
Chris Lattner29689432010-03-11 00:22:57 +00001554 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1555 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001556
Chris Lattner638402b2007-02-28 07:00:42 +00001557 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001558 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001559 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1560 ArgLocs, *DAG.getContext());
Duncan Sands45907662010-10-31 13:21:44 +00001561 CCInfo.AnalyzeFormalArguments(Ins, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001562
Chris Lattnerf39f7712007-02-28 05:46:49 +00001563 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001564 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001565 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1566 CCValAssign &VA = ArgLocs[i];
1567 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1568 // places.
1569 assert(VA.getValNo() != LastVal &&
1570 "Don't support value assigned to multiple locs yet");
1571 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001572
Chris Lattnerf39f7712007-02-28 05:46:49 +00001573 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001574 EVT RegVT = VA.getLocVT();
Devang Patel8a84e442009-01-05 17:31:22 +00001575 TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001576 if (RegVT == MVT::i32)
Chris Lattnerf39f7712007-02-28 05:46:49 +00001577 RC = X86::GR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001578 else if (Is64Bit && RegVT == MVT::i64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001579 RC = X86::GR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001580 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001581 RC = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001582 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001583 RC = X86::FR64RegisterClass;
Bruno Cardoso Lopesac098352010-08-05 23:35:51 +00001584 else if (RegVT.isVector() && RegVT.getSizeInBits() == 256)
1585 RC = X86::VR256RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001586 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001587 RC = X86::VR128RegisterClass;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001588 else if (RegVT == MVT::x86mmx)
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001589 RC = X86::VR64RegisterClass;
1590 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001591 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001592
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001593 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001594 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001595
Chris Lattnerf39f7712007-02-28 05:46:49 +00001596 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1597 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1598 // right size.
1599 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001600 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001601 DAG.getValueType(VA.getValVT()));
1602 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001603 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001604 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001605 else if (VA.getLocInfo() == CCValAssign::BCvt)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001606 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001607
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001608 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001609 // Handle MMX values passed in XMM regs.
1610 if (RegVT.isVector()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001611 ArgValue = DAG.getNode(X86ISD::MOVDQ2Q, dl, VA.getValVT(),
1612 ArgValue);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001613 } else
1614 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001615 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001616 } else {
1617 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001618 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001619 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001620
1621 // If value is passed via pointer - do a load.
1622 if (VA.getLocInfo() == CCValAssign::Indirect)
Chris Lattner51abfe42010-09-21 06:02:19 +00001623 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue,
1624 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001625
Dan Gohman98ca4f22009-08-05 01:29:28 +00001626 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001627 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001628
Dan Gohman61a92132008-04-21 23:59:07 +00001629 // The x86-64 ABI for returning structs by value requires that we copy
1630 // the sret argument into %rax for the return. Save the argument into
1631 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001632 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001633 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1634 unsigned Reg = FuncInfo->getSRetReturnReg();
1635 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001636 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001637 FuncInfo->setSRetReturnReg(Reg);
1638 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001639 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001640 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001641 }
1642
Chris Lattnerf39f7712007-02-28 05:46:49 +00001643 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00001644 // Align stack specially for tail calls.
1645 if (FuncIsMadeTailCallSafe(CallConv))
Gordon Henriksenae636f82008-01-03 16:47:34 +00001646 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001647
Evan Cheng1bc78042006-04-26 01:20:17 +00001648 // If the function takes variable number of arguments, make a frame index for
1649 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001650 if (isVarArg) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001651 if (!IsWin64 && (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
1652 CallConv != CallingConv::X86_ThisCall))) {
Jakob Stoklund Olesenb2eeed72010-07-29 17:42:27 +00001653 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,true));
Gordon Henriksen86737662008-01-05 16:56:59 +00001654 }
1655 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001656 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1657
1658 // FIXME: We should really autogenerate these arrays
1659 static const unsigned GPR64ArgRegsWin64[] = {
1660 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001661 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001662 static const unsigned GPR64ArgRegs64Bit[] = {
1663 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1664 };
1665 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001666 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1667 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1668 };
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001669 const unsigned *GPR64ArgRegs;
1670 unsigned NumXMMRegs = 0;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001671
1672 if (IsWin64) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001673 // The XMM registers which might contain var arg parameters are shadowed
1674 // in their paired GPR. So we only need to save the GPR to their home
1675 // slots.
1676 TotalNumIntRegs = 4;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001677 GPR64ArgRegs = GPR64ArgRegsWin64;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001678 } else {
1679 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1680 GPR64ArgRegs = GPR64ArgRegs64Bit;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001681
1682 NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs64Bit, TotalNumXMMRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001683 }
1684 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1685 TotalNumIntRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001686
Devang Patel578efa92009-06-05 21:57:13 +00001687 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001688 assert(!(NumXMMRegs && !Subtarget->hasXMM()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001689 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001690 assert(!(NumXMMRegs && UseSoftFloat && NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00001691 "SSE register cannot be used when SSE is disabled!");
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001692 if (UseSoftFloat || NoImplicitFloatOps || !Subtarget->hasXMM())
Torok Edwin3f142c32009-02-01 18:15:56 +00001693 // Kernel mode asks for SSE to be disabled, so don't push them
1694 // on the stack.
1695 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001696
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001697 if (IsWin64) {
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001698 const TargetFrameInfo &TFI = *getTargetMachine().getFrameInfo();
1699 // Get to the caller-allocated home save location. Add 8 to account
1700 // for the return address.
1701 int HomeOffset = TFI.getOffsetOfLocalArea() + 8;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001702 FuncInfo->setRegSaveFrameIndex(
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001703 MFI->CreateFixedObject(1, NumIntRegs * 8 + HomeOffset, false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001704 FuncInfo->setVarArgsFrameIndex(FuncInfo->getRegSaveFrameIndex());
1705 } else {
1706 // For X86-64, if there are vararg parameters that are passed via
1707 // registers, then we must store them to their spots on the stack so they
1708 // may be loaded by deferencing the result of va_next.
1709 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
1710 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
1711 FuncInfo->setRegSaveFrameIndex(
1712 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
Dan Gohman1e93df62010-04-17 14:41:14 +00001713 false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001714 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001715
Gordon Henriksen86737662008-01-05 16:56:59 +00001716 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001717 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00001718 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
1719 getPointerTy());
1720 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001721 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00001722 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
1723 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00001724 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
1725 X86::GR64RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00001726 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00001727 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001728 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001729 MachinePointerInfo::getFixedStack(
1730 FuncInfo->getRegSaveFrameIndex(), Offset),
1731 false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001732 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001733 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001734 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001735
Dan Gohmanface41a2009-08-16 21:24:25 +00001736 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
1737 // Now store the XMM (fp + vector) parameter registers.
1738 SmallVector<SDValue, 11> SaveXMMOps;
1739 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001740
Dan Gohmanface41a2009-08-16 21:24:25 +00001741 unsigned AL = MF.addLiveIn(X86::AL, X86::GR8RegisterClass);
1742 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
1743 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001744
Dan Gohman1e93df62010-04-17 14:41:14 +00001745 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1746 FuncInfo->getRegSaveFrameIndex()));
1747 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1748 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00001749
Dan Gohmanface41a2009-08-16 21:24:25 +00001750 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001751 unsigned VReg = MF.addLiveIn(XMMArgRegs64Bit[NumXMMRegs],
Dan Gohmanface41a2009-08-16 21:24:25 +00001752 X86::VR128RegisterClass);
1753 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
1754 SaveXMMOps.push_back(Val);
1755 }
1756 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
1757 MVT::Other,
1758 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00001759 }
Dan Gohmanface41a2009-08-16 21:24:25 +00001760
1761 if (!MemOps.empty())
1762 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1763 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001764 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001765 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001766
Gordon Henriksen86737662008-01-05 16:56:59 +00001767 // Some CCs need callee pop.
Dan Gohman4d3d6e12010-05-27 18:43:40 +00001768 if (Subtarget->IsCalleePop(isVarArg, CallConv)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001769 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001770 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00001771 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001772 // If this is an sret function, the return should pop the hidden pointer.
Chris Lattner29689432010-03-11 00:22:57 +00001773 if (!Is64Bit && !IsTailCallConvention(CallConv) && ArgsAreStructReturn(Ins))
Dan Gohman1e93df62010-04-17 14:41:14 +00001774 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001775 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001776
Gordon Henriksen86737662008-01-05 16:56:59 +00001777 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001778 // RegSaveFrameIndex is X86-64 only.
1779 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Anton Korobeynikovded05e32010-05-16 09:08:45 +00001780 if (CallConv == CallingConv::X86_FastCall ||
1781 CallConv == CallingConv::X86_ThisCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00001782 // fastcc functions can't have varargs.
1783 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00001784 }
Evan Cheng25caf632006-05-23 21:06:34 +00001785
Dan Gohman98ca4f22009-08-05 01:29:28 +00001786 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001787}
1788
Dan Gohman475871a2008-07-27 21:46:04 +00001789SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001790X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
1791 SDValue StackPtr, SDValue Arg,
1792 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00001793 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001794 ISD::ArgFlagsTy Flags) const {
Anton Korobeynikovc7c62bb2010-09-02 22:31:32 +00001795 const unsigned FirstStackArgOffset = (Subtarget->isTargetWin64() ? 32 : 0);
1796 unsigned LocMemOffset = FirstStackArgOffset + VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00001797 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00001798 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001799 if (Flags.isByVal())
Dale Johannesendd64c412009-02-04 00:33:20 +00001800 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001801
1802 return DAG.getStore(Chain, dl, Arg, PtrOff,
1803 MachinePointerInfo::getStack(LocMemOffset),
David Greene67c9d422010-02-15 16:53:33 +00001804 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00001805}
1806
Bill Wendling64e87322009-01-16 19:25:27 +00001807/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001808/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00001809SDValue
1810X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00001811 SDValue &OutRetAddr, SDValue Chain,
1812 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00001813 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001814 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00001815 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001816 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00001817
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001818 // Load the "old" Return address.
Chris Lattner51abfe42010-09-21 06:02:19 +00001819 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, MachinePointerInfo(),
1820 false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001821 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001822}
1823
1824/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1825/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00001826static SDValue
1827EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00001828 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00001829 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001830 // Store the return address to the appropriate stack slot.
1831 if (!FPDiff) return Chain;
1832 // Calculate the new stack slot for the return address.
1833 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00001834 int NewReturnAddrFI =
Evan Chenged2ae132010-07-03 00:40:23 +00001835 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false);
Owen Anderson825b72b2009-08-11 20:47:22 +00001836 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00001837 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001838 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00001839 MachinePointerInfo::getFixedStack(NewReturnAddrFI),
David Greene67c9d422010-02-15 16:53:33 +00001840 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001841 return Chain;
1842}
1843
Dan Gohman98ca4f22009-08-05 01:29:28 +00001844SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001845X86TargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001846 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001847 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001848 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001849 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001850 const SmallVectorImpl<ISD::InputArg> &Ins,
1851 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001852 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001853 MachineFunction &MF = DAG.getMachineFunction();
1854 bool Is64Bit = Subtarget->is64Bit();
1855 bool IsStructRet = CallIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00001856 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001857
Evan Cheng5f941932010-02-05 02:21:12 +00001858 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00001859 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00001860 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1861 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001862 Outs, OutVals, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00001863
1864 // Sibcalls are automatically detected tailcalls which do not require
1865 // ABI changes.
Dan Gohman1797ed52010-02-08 20:27:50 +00001866 if (!GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00001867 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00001868
1869 if (isTailCall)
1870 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00001871 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00001872
Chris Lattner29689432010-03-11 00:22:57 +00001873 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1874 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001875
Chris Lattner638402b2007-02-28 07:00:42 +00001876 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00001877 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001878 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1879 ArgLocs, *DAG.getContext());
Duncan Sands45907662010-10-31 13:21:44 +00001880 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001881
Chris Lattner423c5f42007-02-28 05:31:48 +00001882 // Get a count of how many bytes are to be pushed on the stack.
1883 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00001884 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00001885 // This is a sibcall. The memory operands are available in caller's
1886 // own caller's stack.
1887 NumBytes = 0;
Chris Lattner29689432010-03-11 00:22:57 +00001888 else if (GuaranteedTailCallOpt && IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00001889 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001890
Gordon Henriksen86737662008-01-05 16:56:59 +00001891 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00001892 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001893 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00001894 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00001895 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1896 FPDiff = NumBytesCallerPushed - NumBytes;
1897
1898 // Set the delta of movement of the returnaddr stackslot.
1899 // But only set if delta is greater than previous delta.
1900 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1901 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1902 }
1903
Evan Chengf22f9b32010-02-06 03:28:46 +00001904 if (!IsSibcall)
1905 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001906
Dan Gohman475871a2008-07-27 21:46:04 +00001907 SDValue RetAddrFrIdx;
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001908 // Load return adress for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00001909 if (isTailCall && FPDiff)
1910 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
1911 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00001912
Dan Gohman475871a2008-07-27 21:46:04 +00001913 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
1914 SmallVector<SDValue, 8> MemOpChains;
1915 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00001916
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001917 // Walk the register/memloc assignments, inserting copies/loads. In the case
1918 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00001919 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1920 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001921 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00001922 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00001923 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00001924 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00001925
Chris Lattner423c5f42007-02-28 05:31:48 +00001926 // Promote the value if needed.
1927 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001928 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00001929 case CCValAssign::Full: break;
1930 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001931 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001932 break;
1933 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001934 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001935 break;
1936 case CCValAssign::AExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001937 if (RegVT.isVector() && RegVT.getSizeInBits() == 128) {
1938 // Special case: passing MMX values in XMM registers.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001939 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00001940 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
1941 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001942 } else
1943 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
1944 break;
1945 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001946 Arg = DAG.getNode(ISD::BITCAST, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001947 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001948 case CCValAssign::Indirect: {
1949 // Store the argument.
1950 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00001951 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001952 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00001953 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00001954 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001955 Arg = SpillSlot;
1956 break;
1957 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00001958 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001959
Chris Lattner423c5f42007-02-28 05:31:48 +00001960 if (VA.isRegLoc()) {
1961 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00001962 if (isVarArg && Subtarget->isTargetWin64()) {
1963 // Win64 ABI requires argument XMM reg to be copied to the corresponding
1964 // shadow reg if callee is a varargs function.
1965 unsigned ShadowReg = 0;
1966 switch (VA.getLocReg()) {
1967 case X86::XMM0: ShadowReg = X86::RCX; break;
1968 case X86::XMM1: ShadowReg = X86::RDX; break;
1969 case X86::XMM2: ShadowReg = X86::R8; break;
1970 case X86::XMM3: ShadowReg = X86::R9; break;
1971 }
1972 if (ShadowReg)
1973 RegsToPass.push_back(std::make_pair(ShadowReg, Arg));
1974 }
Evan Chengf22f9b32010-02-06 03:28:46 +00001975 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00001976 assert(VA.isMemLoc());
1977 if (StackPtr.getNode() == 0)
1978 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
1979 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1980 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001981 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001982 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001983
Evan Cheng32fe1032006-05-25 00:59:30 +00001984 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001985 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001986 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001987
Evan Cheng347d5f72006-04-28 21:29:37 +00001988 // Build a sequence of copy-to-reg nodes chained together with token chain
1989 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001990 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001991 // Tail call byval lowering might overwrite argument registers so in case of
1992 // tail call optimization the copies to registers are lowered later.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001993 if (!isTailCall)
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001994 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001995 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00001996 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001997 InFlag = Chain.getValue(1);
1998 }
Gordon Henriksen86737662008-01-05 16:56:59 +00001999
Chris Lattner88e1fd52009-07-09 04:24:46 +00002000 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002001 // ELF / PIC requires GOT in the EBX register before function calls via PLT
2002 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002003 if (!isTailCall) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002004 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
2005 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00002006 DebugLoc(), getPointerTy()),
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002007 InFlag);
2008 InFlag = Chain.getValue(1);
2009 } else {
2010 // If we are tail calling and generating PIC/GOT style code load the
2011 // address of the callee into ECX. The value in ecx is used as target of
2012 // the tail jump. This is done to circumvent the ebx/callee-saved problem
2013 // for tail calls on PIC/GOT architectures. Normally we would just put the
2014 // address of GOT into ebx and then call target@PLT. But for tail calls
2015 // ebx would be restored (since ebx is callee saved) before jumping to the
2016 // target@PLT.
2017
2018 // Note: The actual moving to ECX is done further down.
2019 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
2020 if (G && !G->getGlobal()->hasHiddenVisibility() &&
2021 !G->getGlobal()->hasProtectedVisibility())
2022 Callee = LowerGlobalAddress(Callee, DAG);
2023 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00002024 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002025 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002026 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002027
Nate Begemanc8ea6732010-07-21 20:49:52 +00002028 if (Is64Bit && isVarArg && !Subtarget->isTargetWin64()) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002029 // From AMD64 ABI document:
2030 // For calls that may call functions that use varargs or stdargs
2031 // (prototype-less calls or calls to functions containing ellipsis (...) in
2032 // the declaration) %al is used as hidden argument to specify the number
2033 // of SSE registers used. The contents of %al do not need to match exactly
2034 // the number of registers, but must be an ubound on the number of SSE
2035 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002036
Gordon Henriksen86737662008-01-05 16:56:59 +00002037 // Count the number of XMM registers allocated.
2038 static const unsigned XMMArgRegs[] = {
2039 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2040 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2041 };
2042 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00002043 assert((Subtarget->hasXMM() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00002044 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00002045
Dale Johannesendd64c412009-02-04 00:33:20 +00002046 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Owen Anderson825b72b2009-08-11 20:47:22 +00002047 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002048 InFlag = Chain.getValue(1);
2049 }
2050
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002051
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002052 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002053 if (isTailCall) {
2054 // Force all the incoming stack arguments to be loaded from the stack
2055 // before any new outgoing arguments are stored to the stack, because the
2056 // outgoing stack slots may alias the incoming argument stack slots, and
2057 // the alias isn't otherwise explicit. This is slightly more conservative
2058 // than necessary, because it means that each store effectively depends
2059 // on every argument instead of just those arguments it would clobber.
2060 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2061
Dan Gohman475871a2008-07-27 21:46:04 +00002062 SmallVector<SDValue, 8> MemOpChains2;
2063 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002064 int FI = 0;
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002065 // Do not flag preceeding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00002066 InFlag = SDValue();
Dan Gohman1797ed52010-02-08 20:27:50 +00002067 if (GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002068 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2069 CCValAssign &VA = ArgLocs[i];
2070 if (VA.isRegLoc())
2071 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002072 assert(VA.isMemLoc());
Dan Gohmanc9403652010-07-07 15:54:55 +00002073 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002074 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002075 // Create frame index.
2076 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002077 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002078 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002079 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002080
Duncan Sands276dcbd2008-03-21 09:14:45 +00002081 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002082 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002083 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002084 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00002085 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00002086 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002087 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002088
Dan Gohman98ca4f22009-08-05 01:29:28 +00002089 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2090 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002091 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002092 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002093 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002094 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002095 DAG.getStore(ArgChain, dl, Arg, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002096 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002097 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002098 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002099 }
2100 }
2101
2102 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002103 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002104 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002105
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002106 // Copy arguments to their registers.
2107 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002108 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002109 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002110 InFlag = Chain.getValue(1);
2111 }
Dan Gohman475871a2008-07-27 21:46:04 +00002112 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002113
Gordon Henriksen86737662008-01-05 16:56:59 +00002114 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002115 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00002116 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002117 }
2118
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002119 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2120 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2121 // In the 64-bit large code model, we have to make all calls
2122 // through a register, since the call instruction's 32-bit
2123 // pc-relative offset may not be large enough to hold the whole
2124 // address.
2125 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002126 // If the callee is a GlobalAddress node (quite common, every direct call
2127 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2128 // it.
2129
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002130 // We should use extra load for direct calls to dllimported functions in
2131 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002132 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002133 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002134 unsigned char OpFlags = 0;
Eric Christopherfd179292009-08-27 18:07:15 +00002135
Chris Lattner48a7d022009-07-09 05:02:21 +00002136 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2137 // external symbols most go through the PLT in PIC mode. If the symbol
2138 // has hidden or protected visibility, or if it is static or local, then
2139 // we don't need to use the PLT - we can directly call it.
2140 if (Subtarget->isTargetELF() &&
2141 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002142 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002143 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002144 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner80945782010-09-27 06:34:01 +00002145 (GV->isDeclaration() || GV->isWeakForLinker()) &&
2146 Subtarget->getDarwinVers() < 9) {
Chris Lattner74e726e2009-07-09 05:27:35 +00002147 // PC-relative references to external symbols should go through $stub,
2148 // unless we're building with the leopard linker or later, which
2149 // automatically synthesizes these stubs.
2150 OpFlags = X86II::MO_DARWIN_STUB;
2151 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002152
Devang Patel0d881da2010-07-06 22:08:15 +00002153 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002154 G->getOffset(), OpFlags);
2155 }
Bill Wendling056292f2008-09-16 21:48:12 +00002156 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002157 unsigned char OpFlags = 0;
2158
Evan Cheng1bf891a2010-12-01 22:59:46 +00002159 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to
2160 // external symbols should go through the PLT.
2161 if (Subtarget->isTargetELF() &&
2162 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
2163 OpFlags = X86II::MO_PLT;
2164 } else if (Subtarget->isPICStyleStubAny() &&
2165 Subtarget->getDarwinVers() < 9) {
2166 // PC-relative references to external symbols should go through $stub,
2167 // unless we're building with the leopard linker or later, which
2168 // automatically synthesizes these stubs.
2169 OpFlags = X86II::MO_DARWIN_STUB;
Chris Lattner74e726e2009-07-09 05:27:35 +00002170 }
Eric Christopherfd179292009-08-27 18:07:15 +00002171
Chris Lattner48a7d022009-07-09 05:02:21 +00002172 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2173 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002174 }
2175
Chris Lattnerd96d0722007-02-25 06:40:16 +00002176 // Returns a chain & a flag for retval copy to use.
Owen Anderson825b72b2009-08-11 20:47:22 +00002177 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00002178 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002179
Evan Chengf22f9b32010-02-06 03:28:46 +00002180 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002181 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2182 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002183 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002184 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002185
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002186 Ops.push_back(Chain);
2187 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002188
Dan Gohman98ca4f22009-08-05 01:29:28 +00002189 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002190 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002191
Gordon Henriksen86737662008-01-05 16:56:59 +00002192 // Add argument registers to the end of the list so that they are known live
2193 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002194 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2195 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2196 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002197
Evan Cheng586ccac2008-03-18 23:36:35 +00002198 // Add an implicit use GOT pointer in EBX.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002199 if (!isTailCall && Subtarget->isPICStyleGOT())
Evan Cheng586ccac2008-03-18 23:36:35 +00002200 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
2201
Anton Korobeynikov3a1e54a2010-08-17 21:06:07 +00002202 // Add an implicit use of AL for non-Windows x86 64-bit vararg functions.
2203 if (Is64Bit && isVarArg && !Subtarget->isTargetWin64())
Owen Anderson825b72b2009-08-11 20:47:22 +00002204 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
Evan Cheng586ccac2008-03-18 23:36:35 +00002205
Gabor Greifba36cb52008-08-28 21:40:38 +00002206 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002207 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002208
Dan Gohman98ca4f22009-08-05 01:29:28 +00002209 if (isTailCall) {
Dale Johannesen88004c22010-06-05 00:30:45 +00002210 // We used to do:
2211 //// If this is the first return lowered for this function, add the regs
2212 //// to the liveout set for the function.
2213 // This isn't right, although it's probably harmless on x86; liveouts
2214 // should be computed from returns not tail calls. Consider a void
2215 // function making a tail call to a function returning int.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002216 return DAG.getNode(X86ISD::TC_RETURN, dl,
2217 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002218 }
2219
Dale Johannesenace16102009-02-03 19:33:06 +00002220 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002221 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002222
Chris Lattner2d297092006-05-23 18:50:38 +00002223 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002224 unsigned NumBytesForCalleeToPush;
Dan Gohman4d3d6e12010-05-27 18:43:40 +00002225 if (Subtarget->IsCalleePop(isVarArg, CallConv))
Gordon Henriksen86737662008-01-05 16:56:59 +00002226 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Chris Lattner29689432010-03-11 00:22:57 +00002227 else if (!Is64Bit && !IsTailCallConvention(CallConv) && IsStructRet)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002228 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002229 // pops the hidden struct pointer, so we have to push it back.
2230 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002231 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002232 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002233 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002234
Gordon Henriksenae636f82008-01-03 16:47:34 +00002235 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002236 if (!IsSibcall) {
2237 Chain = DAG.getCALLSEQ_END(Chain,
2238 DAG.getIntPtrConstant(NumBytes, true),
2239 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2240 true),
2241 InFlag);
2242 InFlag = Chain.getValue(1);
2243 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002244
Chris Lattner3085e152007-02-25 08:59:22 +00002245 // Handle result values, copying them out of physregs into vregs that we
2246 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002247 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2248 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002249}
2250
Evan Cheng25ab6902006-09-08 06:48:29 +00002251
2252//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002253// Fast Calling Convention (tail call) implementation
2254//===----------------------------------------------------------------------===//
2255
2256// Like std call, callee cleans arguments, convention except that ECX is
2257// reserved for storing the tail called function address. Only 2 registers are
2258// free for argument passing (inreg). Tail call optimization is performed
2259// provided:
2260// * tailcallopt is enabled
2261// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002262// On X86_64 architecture with GOT-style position independent code only local
2263// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002264// To keep the stack aligned according to platform abi the function
2265// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2266// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002267// If a tail called function callee has more arguments than the caller the
2268// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002269// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002270// original REtADDR, but before the saved framepointer or the spilled registers
2271// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2272// stack layout:
2273// arg1
2274// arg2
2275// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002276// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002277// move area ]
2278// (possible EBP)
2279// ESI
2280// EDI
2281// local1 ..
2282
2283/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2284/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002285unsigned
2286X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2287 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002288 MachineFunction &MF = DAG.getMachineFunction();
2289 const TargetMachine &TM = MF.getTarget();
2290 const TargetFrameInfo &TFI = *TM.getFrameInfo();
2291 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002292 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002293 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002294 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002295 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2296 // Number smaller than 12 so just add the difference.
2297 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2298 } else {
2299 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002300 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002301 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002302 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002303 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002304}
2305
Evan Cheng5f941932010-02-05 02:21:12 +00002306/// MatchingStackOffset - Return true if the given stack call argument is
2307/// already available in the same position (relatively) of the caller's
2308/// incoming argument stack.
2309static
2310bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2311 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2312 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002313 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2314 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002315 if (Arg.getOpcode() == ISD::CopyFromReg) {
2316 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
2317 if (!VR || TargetRegisterInfo::isPhysicalRegister(VR))
2318 return false;
2319 MachineInstr *Def = MRI->getVRegDef(VR);
2320 if (!Def)
2321 return false;
2322 if (!Flags.isByVal()) {
2323 if (!TII->isLoadFromStackSlot(Def, FI))
2324 return false;
2325 } else {
2326 unsigned Opcode = Def->getOpcode();
2327 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2328 Def->getOperand(1).isFI()) {
2329 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002330 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002331 } else
2332 return false;
2333 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002334 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2335 if (Flags.isByVal())
2336 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002337 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002338 // define @foo(%struct.X* %A) {
2339 // tail call @bar(%struct.X* byval %A)
2340 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002341 return false;
2342 SDValue Ptr = Ld->getBasePtr();
2343 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2344 if (!FINode)
2345 return false;
2346 FI = FINode->getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002347 } else
2348 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002349
Evan Cheng4cae1332010-03-05 08:38:04 +00002350 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002351 if (!MFI->isFixedObjectIndex(FI))
2352 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002353 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002354}
2355
Dan Gohman98ca4f22009-08-05 01:29:28 +00002356/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2357/// for tail call optimization. Targets which want to do tail call
2358/// optimization should implement this function.
2359bool
2360X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002361 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002362 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002363 bool isCalleeStructRet,
2364 bool isCallerStructRet,
Evan Chengb1712452010-01-27 06:25:16 +00002365 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002366 const SmallVectorImpl<SDValue> &OutVals,
Evan Chengb1712452010-01-27 06:25:16 +00002367 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002368 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002369 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002370 CalleeCC != CallingConv::C)
2371 return false;
2372
Evan Cheng7096ae42010-01-29 06:45:59 +00002373 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002374 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002375 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Cheng13617962010-04-30 01:12:32 +00002376 CallingConv::ID CallerCC = CallerF->getCallingConv();
2377 bool CCMatch = CallerCC == CalleeCC;
2378
Dan Gohman1797ed52010-02-08 20:27:50 +00002379 if (GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002380 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002381 return true;
2382 return false;
2383 }
2384
Dale Johannesen2f05cc02010-05-28 23:24:28 +00002385 // Look for obvious safe cases to perform tail call optimization that do not
2386 // require ABI changes. This is what gcc calls sibcall.
Evan Chengb2c92902010-02-02 02:22:50 +00002387
Evan Cheng2c12cb42010-03-26 16:26:03 +00002388 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2389 // emit a special epilogue.
2390 if (RegInfo->needsStackRealignment(MF))
2391 return false;
2392
Eric Christopher90eb4022010-07-22 00:26:08 +00002393 // Do not sibcall optimize vararg calls unless the call site is not passing
2394 // any arguments.
Evan Cheng3c262ee2010-03-26 02:13:13 +00002395 if (isVarArg && !Outs.empty())
Evan Cheng843bd692010-01-31 06:44:49 +00002396 return false;
2397
Evan Chenga375d472010-03-15 18:54:48 +00002398 // Also avoid sibcall optimization if either caller or callee uses struct
2399 // return semantics.
2400 if (isCalleeStructRet || isCallerStructRet)
2401 return false;
2402
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002403 // If the call result is in ST0 / ST1, it needs to be popped off the x87 stack.
2404 // Therefore if it's not used by the call it is not safe to optimize this into
2405 // a sibcall.
2406 bool Unused = false;
2407 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2408 if (!Ins[i].Used) {
2409 Unused = true;
2410 break;
2411 }
2412 }
2413 if (Unused) {
2414 SmallVector<CCValAssign, 16> RVLocs;
2415 CCState CCInfo(CalleeCC, false, getTargetMachine(),
2416 RVLocs, *DAG.getContext());
2417 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002418 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002419 CCValAssign &VA = RVLocs[i];
2420 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2421 return false;
2422 }
2423 }
2424
Evan Cheng13617962010-04-30 01:12:32 +00002425 // If the calling conventions do not match, then we'd better make sure the
2426 // results are returned in the same way as what the caller expects.
2427 if (!CCMatch) {
2428 SmallVector<CCValAssign, 16> RVLocs1;
2429 CCState CCInfo1(CalleeCC, false, getTargetMachine(),
2430 RVLocs1, *DAG.getContext());
2431 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2432
2433 SmallVector<CCValAssign, 16> RVLocs2;
2434 CCState CCInfo2(CallerCC, false, getTargetMachine(),
2435 RVLocs2, *DAG.getContext());
2436 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2437
2438 if (RVLocs1.size() != RVLocs2.size())
2439 return false;
2440 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2441 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2442 return false;
2443 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2444 return false;
2445 if (RVLocs1[i].isRegLoc()) {
2446 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2447 return false;
2448 } else {
2449 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2450 return false;
2451 }
2452 }
2453 }
2454
Evan Chenga6bff982010-01-30 01:22:00 +00002455 // If the callee takes no arguments then go on to check the results of the
2456 // call.
2457 if (!Outs.empty()) {
2458 // Check if stack adjustment is needed. For now, do not do this if any
2459 // argument is passed on the stack.
2460 SmallVector<CCValAssign, 16> ArgLocs;
2461 CCState CCInfo(CalleeCC, isVarArg, getTargetMachine(),
2462 ArgLocs, *DAG.getContext());
Duncan Sands45907662010-10-31 13:21:44 +00002463 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Evan Chengb2c92902010-02-02 02:22:50 +00002464 if (CCInfo.getNextStackOffset()) {
2465 MachineFunction &MF = DAG.getMachineFunction();
2466 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2467 return false;
2468 if (Subtarget->isTargetWin64())
2469 // Win64 ABI has additional complications.
2470 return false;
2471
2472 // Check if the arguments are already laid out in the right way as
2473 // the caller's fixed stack objects.
2474 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002475 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2476 const X86InstrInfo *TII =
2477 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002478 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2479 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00002480 SDValue Arg = OutVals[i];
Evan Chengb2c92902010-02-02 02:22:50 +00002481 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002482 if (VA.getLocInfo() == CCValAssign::Indirect)
2483 return false;
2484 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002485 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2486 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002487 return false;
2488 }
2489 }
2490 }
Evan Cheng9c044672010-05-29 01:35:22 +00002491
2492 // If the tailcall address may be in a register, then make sure it's
2493 // possible to register allocate for it. In 32-bit, the call address can
2494 // only target EAX, EDX, or ECX since the tail call must be scheduled after
Evan Chengdedd9742010-07-14 06:44:01 +00002495 // callee-saved registers are restored. These happen to be the same
2496 // registers used to pass 'inreg' arguments so watch out for those.
2497 if (!Subtarget->is64Bit() &&
2498 !isa<GlobalAddressSDNode>(Callee) &&
Evan Cheng9c044672010-05-29 01:35:22 +00002499 !isa<ExternalSymbolSDNode>(Callee)) {
Evan Cheng9c044672010-05-29 01:35:22 +00002500 unsigned NumInRegs = 0;
2501 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2502 CCValAssign &VA = ArgLocs[i];
Evan Chengdedd9742010-07-14 06:44:01 +00002503 if (!VA.isRegLoc())
2504 continue;
2505 unsigned Reg = VA.getLocReg();
2506 switch (Reg) {
2507 default: break;
2508 case X86::EAX: case X86::EDX: case X86::ECX:
2509 if (++NumInRegs == 3)
Evan Cheng9c044672010-05-29 01:35:22 +00002510 return false;
Evan Chengdedd9742010-07-14 06:44:01 +00002511 break;
Evan Cheng9c044672010-05-29 01:35:22 +00002512 }
2513 }
2514 }
Evan Chenga6bff982010-01-30 01:22:00 +00002515 }
Evan Chengb1712452010-01-27 06:25:16 +00002516
Dale Johannesend155d7e2010-10-25 22:17:05 +00002517 // An stdcall caller is expected to clean up its arguments; the callee
Dale Johannesen0e034562010-11-12 00:43:18 +00002518 // isn't going to do that.
Dale Johannesend155d7e2010-10-25 22:17:05 +00002519 if (!CCMatch && CallerCC==CallingConv::X86_StdCall)
2520 return false;
2521
Evan Cheng86809cc2010-02-03 03:28:02 +00002522 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002523}
2524
Dan Gohman3df24e62008-09-03 23:12:08 +00002525FastISel *
Dan Gohmana4160c32010-07-07 16:29:44 +00002526X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
2527 return X86::createFastISel(funcInfo);
Dan Gohmand9f3c482008-08-19 21:32:53 +00002528}
2529
2530
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002531//===----------------------------------------------------------------------===//
2532// Other Lowering Hooks
2533//===----------------------------------------------------------------------===//
2534
Bruno Cardoso Lopese654b562010-09-01 00:51:36 +00002535static bool MayFoldLoad(SDValue Op) {
2536 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
2537}
2538
2539static bool MayFoldIntoStore(SDValue Op) {
2540 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
2541}
2542
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002543static bool isTargetShuffle(unsigned Opcode) {
2544 switch(Opcode) {
2545 default: return false;
2546 case X86ISD::PSHUFD:
2547 case X86ISD::PSHUFHW:
2548 case X86ISD::PSHUFLW:
2549 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002550 case X86ISD::PALIGN:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002551 case X86ISD::SHUFPS:
2552 case X86ISD::MOVLHPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002553 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002554 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002555 case X86ISD::MOVLPS:
2556 case X86ISD::MOVLPD:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002557 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002558 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002559 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002560 case X86ISD::MOVSS:
2561 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002562 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002563 case X86ISD::UNPCKLPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002564 case X86ISD::PUNPCKLWD:
2565 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002566 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002567 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002568 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002569 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002570 case X86ISD::PUNPCKHWD:
2571 case X86ISD::PUNPCKHBW:
2572 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002573 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002574 return true;
2575 }
2576 return false;
2577}
2578
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002579static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002580 SDValue V1, SelectionDAG &DAG) {
2581 switch(Opc) {
2582 default: llvm_unreachable("Unknown x86 shuffle node");
2583 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002584 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002585 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002586 return DAG.getNode(Opc, dl, VT, V1);
2587 }
2588
2589 return SDValue();
2590}
2591
2592static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00002593 SDValue V1, unsigned TargetMask, SelectionDAG &DAG) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002594 switch(Opc) {
2595 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002596 case X86ISD::PSHUFD:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002597 case X86ISD::PSHUFHW:
2598 case X86ISD::PSHUFLW:
2599 return DAG.getNode(Opc, dl, VT, V1, DAG.getConstant(TargetMask, MVT::i8));
2600 }
2601
2602 return SDValue();
2603}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002604
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002605static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2606 SDValue V1, SDValue V2, unsigned TargetMask, SelectionDAG &DAG) {
2607 switch(Opc) {
2608 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002609 case X86ISD::PALIGN:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002610 case X86ISD::SHUFPD:
2611 case X86ISD::SHUFPS:
2612 return DAG.getNode(Opc, dl, VT, V1, V2,
2613 DAG.getConstant(TargetMask, MVT::i8));
2614 }
2615 return SDValue();
2616}
2617
2618static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2619 SDValue V1, SDValue V2, SelectionDAG &DAG) {
2620 switch(Opc) {
2621 default: llvm_unreachable("Unknown x86 shuffle node");
2622 case X86ISD::MOVLHPS:
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00002623 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002624 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002625 case X86ISD::MOVLPS:
2626 case X86ISD::MOVLPD:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002627 case X86ISD::MOVSS:
2628 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002629 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002630 case X86ISD::UNPCKLPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002631 case X86ISD::PUNPCKLWD:
2632 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002633 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002634 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002635 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002636 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002637 case X86ISD::PUNPCKHWD:
2638 case X86ISD::PUNPCKHBW:
2639 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002640 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002641 return DAG.getNode(Opc, dl, VT, V1, V2);
2642 }
2643 return SDValue();
2644}
2645
Dan Gohmand858e902010-04-17 15:26:15 +00002646SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002647 MachineFunction &MF = DAG.getMachineFunction();
2648 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2649 int ReturnAddrIndex = FuncInfo->getRAIndex();
2650
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002651 if (ReturnAddrIndex == 0) {
2652 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002653 uint64_t SlotSize = TD->getPointerSize();
David Greene3f2bf852009-11-12 20:49:22 +00002654 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00002655 false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002656 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002657 }
2658
Evan Cheng25ab6902006-09-08 06:48:29 +00002659 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002660}
2661
2662
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002663bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
2664 bool hasSymbolicDisplacement) {
2665 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00002666 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002667 return false;
2668
2669 // If we don't have a symbolic displacement - we don't have any extra
2670 // restrictions.
2671 if (!hasSymbolicDisplacement)
2672 return true;
2673
2674 // FIXME: Some tweaks might be needed for medium code model.
2675 if (M != CodeModel::Small && M != CodeModel::Kernel)
2676 return false;
2677
2678 // For small code model we assume that latest object is 16MB before end of 31
2679 // bits boundary. We may also accept pretty large negative constants knowing
2680 // that all objects are in the positive half of address space.
2681 if (M == CodeModel::Small && Offset < 16*1024*1024)
2682 return true;
2683
2684 // For kernel code model we know that all object resist in the negative half
2685 // of 32bits address space. We may not accept negative offsets, since they may
2686 // be just off and we may accept pretty large positive ones.
2687 if (M == CodeModel::Kernel && Offset > 0)
2688 return true;
2689
2690 return false;
2691}
2692
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002693/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
2694/// specific condition code, returning the condition code and the LHS/RHS of the
2695/// comparison to make.
2696static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2697 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00002698 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002699 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2700 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2701 // X > -1 -> X == 0, jump !sign.
2702 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002703 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002704 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2705 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002706 return X86::COND_S;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002707 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00002708 // X < 1 -> X <= 0
2709 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002710 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002711 }
Chris Lattnerf9570512006-09-13 03:22:10 +00002712 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002713
Evan Chengd9558e02006-01-06 00:43:03 +00002714 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002715 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002716 case ISD::SETEQ: return X86::COND_E;
2717 case ISD::SETGT: return X86::COND_G;
2718 case ISD::SETGE: return X86::COND_GE;
2719 case ISD::SETLT: return X86::COND_L;
2720 case ISD::SETLE: return X86::COND_LE;
2721 case ISD::SETNE: return X86::COND_NE;
2722 case ISD::SETULT: return X86::COND_B;
2723 case ISD::SETUGT: return X86::COND_A;
2724 case ISD::SETULE: return X86::COND_BE;
2725 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00002726 }
Chris Lattner4c78e022008-12-23 23:42:27 +00002727 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002728
Chris Lattner4c78e022008-12-23 23:42:27 +00002729 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00002730
Chris Lattner4c78e022008-12-23 23:42:27 +00002731 // If LHS is a foldable load, but RHS is not, flip the condition.
2732 if ((ISD::isNON_EXTLoad(LHS.getNode()) && LHS.hasOneUse()) &&
2733 !(ISD::isNON_EXTLoad(RHS.getNode()) && RHS.hasOneUse())) {
2734 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
2735 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00002736 }
2737
Chris Lattner4c78e022008-12-23 23:42:27 +00002738 switch (SetCCOpcode) {
2739 default: break;
2740 case ISD::SETOLT:
2741 case ISD::SETOLE:
2742 case ISD::SETUGT:
2743 case ISD::SETUGE:
2744 std::swap(LHS, RHS);
2745 break;
2746 }
2747
2748 // On a floating point condition, the flags are set as follows:
2749 // ZF PF CF op
2750 // 0 | 0 | 0 | X > Y
2751 // 0 | 0 | 1 | X < Y
2752 // 1 | 0 | 0 | X == Y
2753 // 1 | 1 | 1 | unordered
2754 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002755 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00002756 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002757 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00002758 case ISD::SETOLT: // flipped
2759 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002760 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00002761 case ISD::SETOLE: // flipped
2762 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002763 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002764 case ISD::SETUGT: // flipped
2765 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002766 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00002767 case ISD::SETUGE: // flipped
2768 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002769 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002770 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002771 case ISD::SETNE: return X86::COND_NE;
2772 case ISD::SETUO: return X86::COND_P;
2773 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00002774 case ISD::SETOEQ:
2775 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00002776 }
Evan Chengd9558e02006-01-06 00:43:03 +00002777}
2778
Evan Cheng4a460802006-01-11 00:33:36 +00002779/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2780/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00002781/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00002782static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00002783 switch (X86CC) {
2784 default:
2785 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00002786 case X86::COND_B:
2787 case X86::COND_BE:
2788 case X86::COND_E:
2789 case X86::COND_P:
2790 case X86::COND_A:
2791 case X86::COND_AE:
2792 case X86::COND_NE:
2793 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00002794 return true;
2795 }
2796}
2797
Evan Chengeb2f9692009-10-27 19:56:55 +00002798/// isFPImmLegal - Returns true if the target can instruction select the
2799/// specified FP immediate natively. If false, the legalizer will
2800/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00002801bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00002802 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
2803 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
2804 return true;
2805 }
2806 return false;
2807}
2808
Nate Begeman9008ca62009-04-27 18:41:29 +00002809/// isUndefOrInRange - Return true if Val is undef or if its value falls within
2810/// the specified range (L, H].
2811static bool isUndefOrInRange(int Val, int Low, int Hi) {
2812 return (Val < 0) || (Val >= Low && Val < Hi);
2813}
2814
2815/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
2816/// specified value.
2817static bool isUndefOrEqual(int Val, int CmpVal) {
2818 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00002819 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00002820 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00002821}
2822
Nate Begeman9008ca62009-04-27 18:41:29 +00002823/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
2824/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
2825/// the second operand.
Owen Andersone50ed302009-08-10 22:56:29 +00002826static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00002827 if (VT == MVT::v4f32 || VT == MVT::v4i32 )
Nate Begeman9008ca62009-04-27 18:41:29 +00002828 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002829 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00002830 return (Mask[0] < 2 && Mask[1] < 2);
2831 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002832}
2833
Nate Begeman9008ca62009-04-27 18:41:29 +00002834bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002835 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002836 N->getMask(M);
2837 return ::isPSHUFDMask(M, N->getValueType(0));
2838}
Evan Cheng0188ecb2006-03-22 18:59:22 +00002839
Nate Begeman9008ca62009-04-27 18:41:29 +00002840/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
2841/// is suitable for input to PSHUFHW.
Owen Andersone50ed302009-08-10 22:56:29 +00002842static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002843 if (VT != MVT::v8i16)
Evan Cheng0188ecb2006-03-22 18:59:22 +00002844 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002845
Nate Begeman9008ca62009-04-27 18:41:29 +00002846 // Lower quadword copied in order or undef.
2847 for (int i = 0; i != 4; ++i)
2848 if (Mask[i] >= 0 && Mask[i] != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00002849 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002850
Evan Cheng506d3df2006-03-29 23:07:14 +00002851 // Upper quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002852 for (int i = 4; i != 8; ++i)
2853 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Evan Cheng506d3df2006-03-29 23:07:14 +00002854 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002855
Evan Cheng506d3df2006-03-29 23:07:14 +00002856 return true;
2857}
2858
Nate Begeman9008ca62009-04-27 18:41:29 +00002859bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002860 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002861 N->getMask(M);
2862 return ::isPSHUFHWMask(M, N->getValueType(0));
2863}
Evan Cheng506d3df2006-03-29 23:07:14 +00002864
Nate Begeman9008ca62009-04-27 18:41:29 +00002865/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
2866/// is suitable for input to PSHUFLW.
Owen Andersone50ed302009-08-10 22:56:29 +00002867static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002868 if (VT != MVT::v8i16)
Evan Cheng506d3df2006-03-29 23:07:14 +00002869 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002870
Rafael Espindola15684b22009-04-24 12:40:33 +00002871 // Upper quadword copied in order.
Nate Begeman9008ca62009-04-27 18:41:29 +00002872 for (int i = 4; i != 8; ++i)
2873 if (Mask[i] >= 0 && Mask[i] != i)
Rafael Espindola15684b22009-04-24 12:40:33 +00002874 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002875
Rafael Espindola15684b22009-04-24 12:40:33 +00002876 // Lower quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002877 for (int i = 0; i != 4; ++i)
2878 if (Mask[i] >= 4)
Rafael Espindola15684b22009-04-24 12:40:33 +00002879 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002880
Rafael Espindola15684b22009-04-24 12:40:33 +00002881 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002882}
2883
Nate Begeman9008ca62009-04-27 18:41:29 +00002884bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002885 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002886 N->getMask(M);
2887 return ::isPSHUFLWMask(M, N->getValueType(0));
2888}
2889
Nate Begemana09008b2009-10-19 02:17:23 +00002890/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
2891/// is suitable for input to PALIGNR.
2892static bool isPALIGNRMask(const SmallVectorImpl<int> &Mask, EVT VT,
2893 bool hasSSSE3) {
2894 int i, e = VT.getVectorNumElements();
Michael J. Spencerec38de22010-10-10 22:04:20 +00002895
Nate Begemana09008b2009-10-19 02:17:23 +00002896 // Do not handle v2i64 / v2f64 shuffles with palignr.
2897 if (e < 4 || !hasSSSE3)
2898 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00002899
Nate Begemana09008b2009-10-19 02:17:23 +00002900 for (i = 0; i != e; ++i)
2901 if (Mask[i] >= 0)
2902 break;
Michael J. Spencerec38de22010-10-10 22:04:20 +00002903
Nate Begemana09008b2009-10-19 02:17:23 +00002904 // All undef, not a palignr.
2905 if (i == e)
2906 return false;
2907
2908 // Determine if it's ok to perform a palignr with only the LHS, since we
2909 // don't have access to the actual shuffle elements to see if RHS is undef.
2910 bool Unary = Mask[i] < (int)e;
2911 bool NeedsUnary = false;
2912
2913 int s = Mask[i] - i;
Michael J. Spencerec38de22010-10-10 22:04:20 +00002914
Nate Begemana09008b2009-10-19 02:17:23 +00002915 // Check the rest of the elements to see if they are consecutive.
2916 for (++i; i != e; ++i) {
2917 int m = Mask[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00002918 if (m < 0)
Nate Begemana09008b2009-10-19 02:17:23 +00002919 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00002920
Nate Begemana09008b2009-10-19 02:17:23 +00002921 Unary = Unary && (m < (int)e);
2922 NeedsUnary = NeedsUnary || (m < s);
2923
2924 if (NeedsUnary && !Unary)
2925 return false;
2926 if (Unary && m != ((s+i) & (e-1)))
2927 return false;
2928 if (!Unary && m != (s+i))
2929 return false;
2930 }
2931 return true;
2932}
2933
2934bool X86::isPALIGNRMask(ShuffleVectorSDNode *N) {
2935 SmallVector<int, 8> M;
2936 N->getMask(M);
2937 return ::isPALIGNRMask(M, N->getValueType(0), true);
2938}
2939
Evan Cheng14aed5e2006-03-24 01:18:28 +00002940/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2941/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Owen Andersone50ed302009-08-10 22:56:29 +00002942static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002943 int NumElems = VT.getVectorNumElements();
2944 if (NumElems != 2 && NumElems != 4)
2945 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002946
Nate Begeman9008ca62009-04-27 18:41:29 +00002947 int Half = NumElems / 2;
2948 for (int i = 0; i < Half; ++i)
2949 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002950 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002951 for (int i = Half; i < NumElems; ++i)
2952 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002953 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002954
Evan Cheng14aed5e2006-03-24 01:18:28 +00002955 return true;
2956}
2957
Nate Begeman9008ca62009-04-27 18:41:29 +00002958bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
2959 SmallVector<int, 8> M;
2960 N->getMask(M);
2961 return ::isSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002962}
2963
Evan Cheng213d2cf2007-05-17 18:45:50 +00002964/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00002965/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2966/// half elements to come from vector 1 (which would equal the dest.) and
2967/// the upper half to come from vector 2.
Owen Andersone50ed302009-08-10 22:56:29 +00002968static bool isCommutedSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002969 int NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00002970
2971 if (NumElems != 2 && NumElems != 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00002972 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002973
Nate Begeman9008ca62009-04-27 18:41:29 +00002974 int Half = NumElems / 2;
2975 for (int i = 0; i < Half; ++i)
2976 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002977 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002978 for (int i = Half; i < NumElems; ++i)
2979 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002980 return false;
2981 return true;
2982}
2983
Nate Begeman9008ca62009-04-27 18:41:29 +00002984static bool isCommutedSHUFP(ShuffleVectorSDNode *N) {
2985 SmallVector<int, 8> M;
2986 N->getMask(M);
2987 return isCommutedSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002988}
2989
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002990/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2991/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00002992bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
2993 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002994 return false;
2995
Evan Cheng2064a2b2006-03-28 06:50:32 +00002996 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman9008ca62009-04-27 18:41:29 +00002997 return isUndefOrEqual(N->getMaskElt(0), 6) &&
2998 isUndefOrEqual(N->getMaskElt(1), 7) &&
2999 isUndefOrEqual(N->getMaskElt(2), 2) &&
3000 isUndefOrEqual(N->getMaskElt(3), 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00003001}
3002
Nate Begeman0b10b912009-11-07 23:17:15 +00003003/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
3004/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
3005/// <2, 3, 2, 3>
3006bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
3007 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Michael J. Spencerec38de22010-10-10 22:04:20 +00003008
Nate Begeman0b10b912009-11-07 23:17:15 +00003009 if (NumElems != 4)
3010 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003011
Nate Begeman0b10b912009-11-07 23:17:15 +00003012 return isUndefOrEqual(N->getMaskElt(0), 2) &&
3013 isUndefOrEqual(N->getMaskElt(1), 3) &&
3014 isUndefOrEqual(N->getMaskElt(2), 2) &&
3015 isUndefOrEqual(N->getMaskElt(3), 3);
3016}
3017
Evan Cheng5ced1d82006-04-06 23:23:56 +00003018/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
3019/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman9008ca62009-04-27 18:41:29 +00003020bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
3021 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003022
Evan Cheng5ced1d82006-04-06 23:23:56 +00003023 if (NumElems != 2 && NumElems != 4)
3024 return false;
3025
Evan Chengc5cdff22006-04-07 21:53:05 +00003026 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003027 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003028 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003029
Evan Chengc5cdff22006-04-07 21:53:05 +00003030 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003031 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003032 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003033
3034 return true;
3035}
3036
Nate Begeman0b10b912009-11-07 23:17:15 +00003037/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
3038/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
3039bool X86::isMOVLHPSMask(ShuffleVectorSDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003040 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003041
Evan Cheng5ced1d82006-04-06 23:23:56 +00003042 if (NumElems != 2 && NumElems != 4)
3043 return false;
3044
Evan Chengc5cdff22006-04-07 21:53:05 +00003045 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003046 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003047 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003048
Nate Begeman9008ca62009-04-27 18:41:29 +00003049 for (unsigned i = 0; i < NumElems/2; ++i)
3050 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003051 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003052
3053 return true;
3054}
3055
Evan Cheng0038e592006-03-28 00:39:58 +00003056/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
3057/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Owen Andersone50ed302009-08-10 22:56:29 +00003058static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003059 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003060 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003061 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng0038e592006-03-28 00:39:58 +00003062 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003063
Nate Begeman9008ca62009-04-27 18:41:29 +00003064 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
3065 int BitI = Mask[i];
3066 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00003067 if (!isUndefOrEqual(BitI, j))
3068 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00003069 if (V2IsSplat) {
Mon P Wang7bcaefa2009-02-04 01:16:59 +00003070 if (!isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003071 return false;
3072 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00003073 if (!isUndefOrEqual(BitI1, j + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003074 return false;
3075 }
Evan Cheng0038e592006-03-28 00:39:58 +00003076 }
Evan Cheng0038e592006-03-28 00:39:58 +00003077 return true;
3078}
3079
Nate Begeman9008ca62009-04-27 18:41:29 +00003080bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3081 SmallVector<int, 8> M;
3082 N->getMask(M);
3083 return ::isUNPCKLMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003084}
3085
Evan Cheng4fcb9222006-03-28 02:43:26 +00003086/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3087/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Eric Christopherfd179292009-08-27 18:07:15 +00003088static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003089 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003090 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003091 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng4fcb9222006-03-28 02:43:26 +00003092 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003093
Nate Begeman9008ca62009-04-27 18:41:29 +00003094 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
3095 int BitI = Mask[i];
3096 int BitI1 = Mask[i+1];
Chris Lattner5a88b832007-02-25 07:10:00 +00003097 if (!isUndefOrEqual(BitI, j + NumElts/2))
Evan Chengc5cdff22006-04-07 21:53:05 +00003098 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00003099 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00003100 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003101 return false;
3102 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00003103 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003104 return false;
3105 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003106 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003107 return true;
3108}
3109
Nate Begeman9008ca62009-04-27 18:41:29 +00003110bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3111 SmallVector<int, 8> M;
3112 N->getMask(M);
3113 return ::isUNPCKHMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003114}
3115
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003116/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3117/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3118/// <0, 0, 1, 1>
Owen Andersone50ed302009-08-10 22:56:29 +00003119static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003120 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003121 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003122 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003123
Nate Begeman9008ca62009-04-27 18:41:29 +00003124 for (int i = 0, j = 0; i != NumElems; i += 2, ++j) {
3125 int BitI = Mask[i];
3126 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00003127 if (!isUndefOrEqual(BitI, j))
3128 return false;
3129 if (!isUndefOrEqual(BitI1, j))
3130 return false;
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003131 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003132 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003133}
3134
Nate Begeman9008ca62009-04-27 18:41:29 +00003135bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
3136 SmallVector<int, 8> M;
3137 N->getMask(M);
3138 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
3139}
3140
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003141/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3142/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3143/// <2, 2, 3, 3>
Owen Andersone50ed302009-08-10 22:56:29 +00003144static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003145 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003146 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
3147 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003148
Nate Begeman9008ca62009-04-27 18:41:29 +00003149 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
3150 int BitI = Mask[i];
3151 int BitI1 = Mask[i+1];
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003152 if (!isUndefOrEqual(BitI, j))
3153 return false;
3154 if (!isUndefOrEqual(BitI1, j))
3155 return false;
3156 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003157 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003158}
3159
Nate Begeman9008ca62009-04-27 18:41:29 +00003160bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
3161 SmallVector<int, 8> M;
3162 N->getMask(M);
3163 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
3164}
3165
Evan Cheng017dcc62006-04-21 01:05:10 +00003166/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3167/// specifies a shuffle of elements that is suitable for input to MOVSS,
3168/// MOVSD, and MOVD, i.e. setting the lowest element.
Owen Andersone50ed302009-08-10 22:56:29 +00003169static bool isMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003170 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003171 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003172
3173 int NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003174
Nate Begeman9008ca62009-04-27 18:41:29 +00003175 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003176 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003177
Nate Begeman9008ca62009-04-27 18:41:29 +00003178 for (int i = 1; i < NumElts; ++i)
3179 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003180 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003181
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003182 return true;
3183}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003184
Nate Begeman9008ca62009-04-27 18:41:29 +00003185bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
3186 SmallVector<int, 8> M;
3187 N->getMask(M);
3188 return ::isMOVLMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003189}
3190
Evan Cheng017dcc62006-04-21 01:05:10 +00003191/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
3192/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003193/// element of vector 2 and the other elements to come from vector 1 in order.
Owen Andersone50ed302009-08-10 22:56:29 +00003194static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003195 bool V2IsSplat = false, bool V2IsUndef = false) {
3196 int NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003197 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
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 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003201 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003202
Nate Begeman9008ca62009-04-27 18:41:29 +00003203 for (int i = 1; i < NumOps; ++i)
3204 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3205 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3206 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003207 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003208
Evan Cheng39623da2006-04-20 08:58:49 +00003209 return true;
3210}
3211
Nate Begeman9008ca62009-04-27 18:41:29 +00003212static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00003213 bool V2IsUndef = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003214 SmallVector<int, 8> M;
3215 N->getMask(M);
3216 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00003217}
3218
Evan Chengd9539472006-04-14 21:59:03 +00003219/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3220/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003221bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N) {
3222 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003223 return false;
3224
3225 // Expect 1, 1, 3, 3
Rafael Espindola15684b22009-04-24 12:40:33 +00003226 for (unsigned i = 0; i < 2; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003227 int Elt = N->getMaskElt(i);
3228 if (Elt >= 0 && Elt != 1)
3229 return false;
Rafael Espindola15684b22009-04-24 12:40:33 +00003230 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003231
3232 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003233 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003234 int Elt = N->getMaskElt(i);
3235 if (Elt >= 0 && Elt != 3)
3236 return false;
3237 if (Elt == 3)
3238 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003239 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003240 // Don't use movshdup if it can be done with a shufps.
Nate Begeman9008ca62009-04-27 18:41:29 +00003241 // FIXME: verify that matching u, u, 3, 3 is what we want.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003242 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003243}
3244
3245/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3246/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003247bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N) {
3248 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003249 return false;
3250
3251 // Expect 0, 0, 2, 2
Nate Begeman9008ca62009-04-27 18:41:29 +00003252 for (unsigned i = 0; i < 2; ++i)
3253 if (N->getMaskElt(i) > 0)
3254 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003255
3256 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003257 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003258 int Elt = N->getMaskElt(i);
3259 if (Elt >= 0 && Elt != 2)
3260 return false;
3261 if (Elt == 2)
3262 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003263 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003264 // Don't use movsldup if it can be done with a shufps.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003265 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003266}
3267
Evan Cheng0b457f02008-09-25 20:50:48 +00003268/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3269/// specifies a shuffle of elements that is suitable for input to MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003270bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
3271 int e = N->getValueType(0).getVectorNumElements() / 2;
Eric Christopherfd179292009-08-27 18:07:15 +00003272
Nate Begeman9008ca62009-04-27 18:41:29 +00003273 for (int i = 0; i < e; ++i)
3274 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003275 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003276 for (int i = 0; i < e; ++i)
3277 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003278 return false;
3279 return true;
3280}
3281
Evan Cheng63d33002006-03-22 08:01:21 +00003282/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003283/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Evan Cheng63d33002006-03-22 08:01:21 +00003284unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003285 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3286 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
3287
Evan Chengb9df0ca2006-03-22 02:53:00 +00003288 unsigned Shift = (NumOperands == 4) ? 2 : 1;
3289 unsigned Mask = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00003290 for (int i = 0; i < NumOperands; ++i) {
3291 int Val = SVOp->getMaskElt(NumOperands-i-1);
3292 if (Val < 0) Val = 0;
Evan Cheng14aed5e2006-03-24 01:18:28 +00003293 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00003294 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00003295 if (i != NumOperands - 1)
3296 Mask <<= Shift;
3297 }
Evan Cheng63d33002006-03-22 08:01:21 +00003298 return Mask;
3299}
3300
Evan Cheng506d3df2006-03-29 23:07:14 +00003301/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003302/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003303unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003304 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003305 unsigned Mask = 0;
3306 // 8 nodes, but we only care about the last 4.
3307 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003308 int Val = SVOp->getMaskElt(i);
3309 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00003310 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00003311 if (i != 4)
3312 Mask <<= 2;
3313 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003314 return Mask;
3315}
3316
3317/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003318/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003319unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003320 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003321 unsigned Mask = 0;
3322 // 8 nodes, but we only care about the first 4.
3323 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003324 int Val = SVOp->getMaskElt(i);
3325 if (Val >= 0)
3326 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00003327 if (i != 0)
3328 Mask <<= 2;
3329 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003330 return Mask;
3331}
3332
Nate Begemana09008b2009-10-19 02:17:23 +00003333/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
3334/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
3335unsigned X86::getShufflePALIGNRImmediate(SDNode *N) {
3336 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3337 EVT VVT = N->getValueType(0);
3338 unsigned EltSize = VVT.getVectorElementType().getSizeInBits() >> 3;
3339 int Val = 0;
3340
3341 unsigned i, e;
3342 for (i = 0, e = VVT.getVectorNumElements(); i != e; ++i) {
3343 Val = SVOp->getMaskElt(i);
3344 if (Val >= 0)
3345 break;
3346 }
3347 return (Val - i) * EltSize;
3348}
3349
Evan Cheng37b73872009-07-30 08:33:02 +00003350/// isZeroNode - Returns true if Elt is a constant zero or a floating point
3351/// constant +0.0.
3352bool X86::isZeroNode(SDValue Elt) {
3353 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmane368b462010-06-18 14:22:04 +00003354 cast<ConstantSDNode>(Elt)->isNullValue()) ||
Evan Cheng37b73872009-07-30 08:33:02 +00003355 (isa<ConstantFPSDNode>(Elt) &&
3356 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
3357}
3358
Nate Begeman9008ca62009-04-27 18:41:29 +00003359/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
3360/// their permute mask.
3361static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
3362 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003363 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003364 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00003365 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00003366
Nate Begeman5a5ca152009-04-29 05:20:52 +00003367 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003368 int idx = SVOp->getMaskElt(i);
3369 if (idx < 0)
3370 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003371 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003372 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003373 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003374 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003375 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003376 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
3377 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003378}
3379
Evan Cheng779ccea2007-12-07 21:30:01 +00003380/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3381/// the two vector operands have swapped position.
Owen Andersone50ed302009-08-10 22:56:29 +00003382static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00003383 unsigned NumElems = VT.getVectorNumElements();
3384 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003385 int idx = Mask[i];
3386 if (idx < 0)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003387 continue;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003388 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003389 Mask[i] = idx + NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003390 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003391 Mask[i] = idx - NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003392 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003393}
3394
Evan Cheng533a0aa2006-04-19 20:35:22 +00003395/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
3396/// match movhlps. The lower half elements should come from upper half of
3397/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003398/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00003399static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
3400 if (Op->getValueType(0).getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00003401 return false;
3402 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003403 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003404 return false;
3405 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003406 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003407 return false;
3408 return true;
3409}
3410
Evan Cheng5ced1d82006-04-06 23:23:56 +00003411/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00003412/// is promoted to a vector. It also returns the LoadSDNode by reference if
3413/// required.
3414static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00003415 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
3416 return false;
3417 N = N->getOperand(0).getNode();
3418 if (!ISD::isNON_EXTLoad(N))
3419 return false;
3420 if (LD)
3421 *LD = cast<LoadSDNode>(N);
3422 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003423}
3424
Evan Cheng533a0aa2006-04-19 20:35:22 +00003425/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
3426/// match movlp{s|d}. The lower half elements should come from lower half of
3427/// V1 (and in order), and the upper half elements should come from the upper
3428/// half of V2 (and in order). And since V1 will become the source of the
3429/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003430static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
3431 ShuffleVectorSDNode *Op) {
Evan Cheng466685d2006-10-09 20:57:25 +00003432 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003433 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00003434 // Is V2 is a vector load, don't do this transformation. We will try to use
3435 // load folding shufps op.
3436 if (ISD::isNON_EXTLoad(V2))
3437 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003438
Nate Begeman5a5ca152009-04-29 05:20:52 +00003439 unsigned NumElems = Op->getValueType(0).getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003440
Evan Cheng533a0aa2006-04-19 20:35:22 +00003441 if (NumElems != 2 && NumElems != 4)
3442 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003443 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003444 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003445 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003446 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003447 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003448 return false;
3449 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003450}
3451
Evan Cheng39623da2006-04-20 08:58:49 +00003452/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
3453/// all the same.
3454static bool isSplatVector(SDNode *N) {
3455 if (N->getOpcode() != ISD::BUILD_VECTOR)
3456 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003457
Dan Gohman475871a2008-07-27 21:46:04 +00003458 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00003459 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
3460 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003461 return false;
3462 return true;
3463}
3464
Evan Cheng213d2cf2007-05-17 18:45:50 +00003465/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00003466/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00003467/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00003468static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00003469 SDValue V1 = N->getOperand(0);
3470 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003471 unsigned NumElems = N->getValueType(0).getVectorNumElements();
3472 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003473 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003474 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003475 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00003476 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
3477 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003478 if (Opc != ISD::BUILD_VECTOR ||
3479 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00003480 return false;
3481 } else if (Idx >= 0) {
3482 unsigned Opc = V1.getOpcode();
3483 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
3484 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003485 if (Opc != ISD::BUILD_VECTOR ||
3486 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00003487 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00003488 }
3489 }
3490 return true;
3491}
3492
3493/// getZeroVector - Returns a vector of specified type with all zero elements.
3494///
Owen Andersone50ed302009-08-10 22:56:29 +00003495static SDValue getZeroVector(EVT VT, bool HasSSE2, SelectionDAG &DAG,
Dale Johannesenace16102009-02-03 19:33:06 +00003496 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003497 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003498
Dale Johannesen0488fb62010-09-30 23:57:10 +00003499 // Always build SSE zero vectors as <4 x i32> bitcasted
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003500 // to their dest type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00003501 SDValue Vec;
Dale Johannesen0488fb62010-09-30 23:57:10 +00003502 if (VT.getSizeInBits() == 128) { // SSE
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003503 if (HasSSE2) { // SSE2
3504 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3505 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
3506 } else { // SSE1
3507 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
3508 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
3509 }
3510 } else if (VT.getSizeInBits() == 256) { // AVX
3511 // 256-bit logic and arithmetic instructions in AVX are
3512 // all floating-point, no support for integer ops. Default
3513 // to emitting fp zeroed vectors then.
Owen Anderson825b72b2009-08-11 20:47:22 +00003514 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003515 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
3516 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops, 8);
Evan Chengf0df0312008-05-15 08:39:06 +00003517 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003518 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00003519}
3520
Chris Lattner8a594482007-11-25 00:24:49 +00003521/// getOnesVector - Returns a vector of specified type with all bits set.
3522///
Owen Andersone50ed302009-08-10 22:56:29 +00003523static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003524 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003525
Chris Lattner8a594482007-11-25 00:24:49 +00003526 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
3527 // type. This ensures they get CSE'd.
Owen Anderson825b72b2009-08-11 20:47:22 +00003528 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00003529 SDValue Vec;
Dale Johannesen0488fb62010-09-30 23:57:10 +00003530 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003531 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00003532}
3533
3534
Evan Cheng39623da2006-04-20 08:58:49 +00003535/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
3536/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00003537static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003538 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003539 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003540
Evan Cheng39623da2006-04-20 08:58:49 +00003541 bool Changed = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003542 SmallVector<int, 8> MaskVec;
3543 SVOp->getMask(MaskVec);
Eric Christopherfd179292009-08-27 18:07:15 +00003544
Nate Begeman5a5ca152009-04-29 05:20:52 +00003545 for (unsigned i = 0; i != NumElems; ++i) {
3546 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003547 MaskVec[i] = NumElems;
3548 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00003549 }
Evan Cheng39623da2006-04-20 08:58:49 +00003550 }
Evan Cheng39623da2006-04-20 08:58:49 +00003551 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00003552 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
3553 SVOp->getOperand(1), &MaskVec[0]);
3554 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00003555}
3556
Evan Cheng017dcc62006-04-21 01:05:10 +00003557/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
3558/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00003559static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003560 SDValue V2) {
3561 unsigned NumElems = VT.getVectorNumElements();
3562 SmallVector<int, 8> Mask;
3563 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00003564 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003565 Mask.push_back(i);
3566 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00003567}
3568
Nate Begeman9008ca62009-04-27 18:41:29 +00003569/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003570static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003571 SDValue V2) {
3572 unsigned NumElems = VT.getVectorNumElements();
3573 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00003574 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003575 Mask.push_back(i);
3576 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00003577 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003578 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00003579}
3580
Nate Begeman9008ca62009-04-27 18:41:29 +00003581/// getUnpackhMask - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003582static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003583 SDValue V2) {
3584 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00003585 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00003586 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00003587 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003588 Mask.push_back(i + Half);
3589 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00003590 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003591 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00003592}
3593
Bruno Cardoso Lopesbb0a9482010-08-13 17:50:47 +00003594/// PromoteSplat - Promote a splat of v4i32, v8i16 or v16i8 to v4f32.
3595static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003596 EVT PVT = MVT::v4f32;
Owen Andersone50ed302009-08-10 22:56:29 +00003597 EVT VT = SV->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00003598 DebugLoc dl = SV->getDebugLoc();
3599 SDValue V1 = SV->getOperand(0);
3600 int NumElems = VT.getVectorNumElements();
3601 int EltNo = SV->getSplatIndex();
Rafael Espindola15684b22009-04-24 12:40:33 +00003602
Nate Begeman9008ca62009-04-27 18:41:29 +00003603 // unpack elements to the correct location
3604 while (NumElems > 4) {
3605 if (EltNo < NumElems/2) {
3606 V1 = getUnpackl(DAG, dl, VT, V1, V1);
3607 } else {
3608 V1 = getUnpackh(DAG, dl, VT, V1, V1);
3609 EltNo -= NumElems/2;
3610 }
3611 NumElems >>= 1;
3612 }
Eric Christopherfd179292009-08-27 18:07:15 +00003613
Nate Begeman9008ca62009-04-27 18:41:29 +00003614 // Perform the splat.
3615 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003616 V1 = DAG.getNode(ISD::BITCAST, dl, PVT, V1);
Nate Begeman9008ca62009-04-27 18:41:29 +00003617 V1 = DAG.getVectorShuffle(PVT, dl, V1, DAG.getUNDEF(PVT), &SplatMask[0]);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003618 return DAG.getNode(ISD::BITCAST, dl, VT, V1);
Evan Chengc575ca22006-04-17 20:43:08 +00003619}
3620
Evan Chengba05f722006-04-21 23:03:30 +00003621/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00003622/// vector of zero or undef vector. This produces a shuffle where the low
3623/// element of V2 is swizzled into the zero/undef vector, landing at element
3624/// 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 +00003625static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Chengf0df0312008-05-15 08:39:06 +00003626 bool isZero, bool HasSSE2,
3627 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003628 EVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00003629 SDValue V1 = isZero
Nate Begeman9008ca62009-04-27 18:41:29 +00003630 ? getZeroVector(VT, HasSSE2, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
3631 unsigned NumElems = VT.getVectorNumElements();
3632 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00003633 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003634 // If this is the insertion idx, put the low elt of V2 here.
3635 MaskVec.push_back(i == Idx ? NumElems : i);
3636 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00003637}
3638
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003639/// getShuffleScalarElt - Returns the scalar element that will make up the ith
3640/// element of the result of the vector shuffle.
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003641SDValue getShuffleScalarElt(SDNode *N, int Index, SelectionDAG &DAG,
3642 unsigned Depth) {
3643 if (Depth == 6)
3644 return SDValue(); // Limit search depth.
3645
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003646 SDValue V = SDValue(N, 0);
3647 EVT VT = V.getValueType();
3648 unsigned Opcode = V.getOpcode();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003649
3650 // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
3651 if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
3652 Index = SV->getMaskElt(Index);
3653
3654 if (Index < 0)
3655 return DAG.getUNDEF(VT.getVectorElementType());
3656
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003657 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003658 SDValue NewV = (Index < NumElems) ? SV->getOperand(0) : SV->getOperand(1);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003659 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG, Depth+1);
Evan Chengf26ffe92008-05-29 08:22:04 +00003660 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003661
3662 // Recurse into target specific vector shuffles to find scalars.
3663 if (isTargetShuffle(Opcode)) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003664 int NumElems = VT.getVectorNumElements();
3665 SmallVector<unsigned, 16> ShuffleMask;
3666 SDValue ImmN;
3667
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003668 switch(Opcode) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003669 case X86ISD::SHUFPS:
3670 case X86ISD::SHUFPD:
3671 ImmN = N->getOperand(N->getNumOperands()-1);
3672 DecodeSHUFPSMask(NumElems,
3673 cast<ConstantSDNode>(ImmN)->getZExtValue(),
3674 ShuffleMask);
3675 break;
3676 case X86ISD::PUNPCKHBW:
3677 case X86ISD::PUNPCKHWD:
3678 case X86ISD::PUNPCKHDQ:
3679 case X86ISD::PUNPCKHQDQ:
3680 DecodePUNPCKHMask(NumElems, ShuffleMask);
3681 break;
3682 case X86ISD::UNPCKHPS:
3683 case X86ISD::UNPCKHPD:
3684 DecodeUNPCKHPMask(NumElems, ShuffleMask);
3685 break;
3686 case X86ISD::PUNPCKLBW:
3687 case X86ISD::PUNPCKLWD:
3688 case X86ISD::PUNPCKLDQ:
3689 case X86ISD::PUNPCKLQDQ:
3690 DecodePUNPCKLMask(NumElems, ShuffleMask);
3691 break;
3692 case X86ISD::UNPCKLPS:
3693 case X86ISD::UNPCKLPD:
3694 DecodeUNPCKLPMask(NumElems, ShuffleMask);
3695 break;
3696 case X86ISD::MOVHLPS:
3697 DecodeMOVHLPSMask(NumElems, ShuffleMask);
3698 break;
3699 case X86ISD::MOVLHPS:
3700 DecodeMOVLHPSMask(NumElems, ShuffleMask);
3701 break;
3702 case X86ISD::PSHUFD:
3703 ImmN = N->getOperand(N->getNumOperands()-1);
3704 DecodePSHUFMask(NumElems,
3705 cast<ConstantSDNode>(ImmN)->getZExtValue(),
3706 ShuffleMask);
3707 break;
3708 case X86ISD::PSHUFHW:
3709 ImmN = N->getOperand(N->getNumOperands()-1);
3710 DecodePSHUFHWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
3711 ShuffleMask);
3712 break;
3713 case X86ISD::PSHUFLW:
3714 ImmN = N->getOperand(N->getNumOperands()-1);
3715 DecodePSHUFLWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
3716 ShuffleMask);
3717 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003718 case X86ISD::MOVSS:
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00003719 case X86ISD::MOVSD: {
3720 // The index 0 always comes from the first element of the second source,
3721 // this is why MOVSS and MOVSD are used in the first place. The other
3722 // elements come from the other positions of the first source vector.
3723 unsigned OpNum = (Index == 0) ? 1 : 0;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003724 return getShuffleScalarElt(V.getOperand(OpNum).getNode(), Index, DAG,
3725 Depth+1);
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00003726 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003727 default:
3728 assert("not implemented for target shuffle node");
3729 return SDValue();
3730 }
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003731
3732 Index = ShuffleMask[Index];
3733 if (Index < 0)
3734 return DAG.getUNDEF(VT.getVectorElementType());
3735
3736 SDValue NewV = (Index < NumElems) ? N->getOperand(0) : N->getOperand(1);
3737 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG,
3738 Depth+1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003739 }
3740
3741 // Actual nodes that may contain scalar elements
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003742 if (Opcode == ISD::BITCAST) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003743 V = V.getOperand(0);
3744 EVT SrcVT = V.getValueType();
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003745 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003746
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003747 if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003748 return SDValue();
3749 }
3750
3751 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
3752 return (Index == 0) ? V.getOperand(0)
3753 : DAG.getUNDEF(VT.getVectorElementType());
3754
3755 if (V.getOpcode() == ISD::BUILD_VECTOR)
3756 return V.getOperand(Index);
3757
3758 return SDValue();
3759}
3760
3761/// getNumOfConsecutiveZeros - Return the number of elements of a vector
3762/// shuffle operation which come from a consecutively from a zero. The
3763/// search can start in two diferent directions, from left or right.
3764static
3765unsigned getNumOfConsecutiveZeros(SDNode *N, int NumElems,
3766 bool ZerosFromLeft, SelectionDAG &DAG) {
3767 int i = 0;
3768
3769 while (i < NumElems) {
3770 unsigned Index = ZerosFromLeft ? i : NumElems-i-1;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003771 SDValue Elt = getShuffleScalarElt(N, Index, DAG, 0);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003772 if (!(Elt.getNode() &&
3773 (Elt.getOpcode() == ISD::UNDEF || X86::isZeroNode(Elt))))
3774 break;
3775 ++i;
3776 }
3777
3778 return i;
3779}
3780
3781/// isShuffleMaskConsecutive - Check if the shuffle mask indicies from MaskI to
3782/// MaskE correspond consecutively to elements from one of the vector operands,
3783/// starting from its index OpIdx. Also tell OpNum which source vector operand.
3784static
3785bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp, int MaskI, int MaskE,
3786 int OpIdx, int NumElems, unsigned &OpNum) {
3787 bool SeenV1 = false;
3788 bool SeenV2 = false;
3789
3790 for (int i = MaskI; i <= MaskE; ++i, ++OpIdx) {
3791 int Idx = SVOp->getMaskElt(i);
3792 // Ignore undef indicies
3793 if (Idx < 0)
3794 continue;
3795
3796 if (Idx < NumElems)
3797 SeenV1 = true;
3798 else
3799 SeenV2 = true;
3800
3801 // Only accept consecutive elements from the same vector
3802 if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
3803 return false;
3804 }
3805
3806 OpNum = SeenV1 ? 0 : 1;
3807 return true;
3808}
3809
3810/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
3811/// logical left shift of a vector.
3812static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
3813 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
3814 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
3815 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
3816 false /* check zeros from right */, DAG);
3817 unsigned OpSrc;
3818
3819 if (!NumZeros)
3820 return false;
3821
3822 // Considering the elements in the mask that are not consecutive zeros,
3823 // check if they consecutively come from only one of the source vectors.
3824 //
3825 // V1 = {X, A, B, C} 0
3826 // \ \ \ /
3827 // vector_shuffle V1, V2 <1, 2, 3, X>
3828 //
3829 if (!isShuffleMaskConsecutive(SVOp,
3830 0, // Mask Start Index
3831 NumElems-NumZeros-1, // Mask End Index
3832 NumZeros, // Where to start looking in the src vector
3833 NumElems, // Number of elements in vector
3834 OpSrc)) // Which source operand ?
3835 return false;
3836
3837 isLeft = false;
3838 ShAmt = NumZeros;
3839 ShVal = SVOp->getOperand(OpSrc);
3840 return true;
3841}
3842
3843/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
3844/// logical left shift of a vector.
3845static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
3846 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
3847 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
3848 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
3849 true /* check zeros from left */, DAG);
3850 unsigned OpSrc;
3851
3852 if (!NumZeros)
3853 return false;
3854
3855 // Considering the elements in the mask that are not consecutive zeros,
3856 // check if they consecutively come from only one of the source vectors.
3857 //
3858 // 0 { A, B, X, X } = V2
3859 // / \ / /
3860 // vector_shuffle V1, V2 <X, X, 4, 5>
3861 //
3862 if (!isShuffleMaskConsecutive(SVOp,
3863 NumZeros, // Mask Start Index
3864 NumElems-1, // Mask End Index
3865 0, // Where to start looking in the src vector
3866 NumElems, // Number of elements in vector
3867 OpSrc)) // Which source operand ?
3868 return false;
3869
3870 isLeft = true;
3871 ShAmt = NumZeros;
3872 ShVal = SVOp->getOperand(OpSrc);
3873 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00003874}
3875
3876/// isVectorShift - Returns true if the shuffle can be implemented as a
3877/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003878static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00003879 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003880 if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
3881 isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
3882 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00003883
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003884 return false;
Evan Chengf26ffe92008-05-29 08:22:04 +00003885}
3886
Evan Chengc78d3b42006-04-24 18:01:45 +00003887/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
3888///
Dan Gohman475871a2008-07-27 21:46:04 +00003889static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00003890 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00003891 SelectionDAG &DAG,
3892 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003893 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00003894 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00003895
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003896 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003897 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003898 bool First = true;
3899 for (unsigned i = 0; i < 16; ++i) {
3900 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
3901 if (ThisIsNonZero && First) {
3902 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003903 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00003904 else
Owen Anderson825b72b2009-08-11 20:47:22 +00003905 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00003906 First = false;
3907 }
3908
3909 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00003910 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003911 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
3912 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003913 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003914 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00003915 }
3916 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003917 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
3918 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
3919 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00003920 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003921 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00003922 } else
3923 ThisElt = LastElt;
3924
Gabor Greifba36cb52008-08-28 21:40:38 +00003925 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00003926 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00003927 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00003928 }
3929 }
3930
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003931 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00003932}
3933
Bill Wendlinga348c562007-03-22 18:42:45 +00003934/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00003935///
Dan Gohman475871a2008-07-27 21:46:04 +00003936static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00003937 unsigned NumNonZero, unsigned NumZero,
3938 SelectionDAG &DAG,
3939 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003940 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00003941 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00003942
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003943 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003944 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003945 bool First = true;
3946 for (unsigned i = 0; i < 8; ++i) {
3947 bool isNonZero = (NonZeros & (1 << i)) != 0;
3948 if (isNonZero) {
3949 if (First) {
3950 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003951 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00003952 else
Owen Anderson825b72b2009-08-11 20:47:22 +00003953 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00003954 First = false;
3955 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003956 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003957 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00003958 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00003959 }
3960 }
3961
3962 return V;
3963}
3964
Evan Chengf26ffe92008-05-29 08:22:04 +00003965/// getVShift - Return a vector logical shift node.
3966///
Owen Andersone50ed302009-08-10 22:56:29 +00003967static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00003968 unsigned NumBits, SelectionDAG &DAG,
3969 const TargetLowering &TLI, DebugLoc dl) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00003970 EVT ShVT = MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00003971 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003972 SrcOp = DAG.getNode(ISD::BITCAST, dl, ShVT, SrcOp);
3973 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00003974 DAG.getNode(Opc, dl, ShVT, SrcOp,
Gabor Greif327ef032008-08-28 23:19:51 +00003975 DAG.getConstant(NumBits, TLI.getShiftAmountTy())));
Evan Chengf26ffe92008-05-29 08:22:04 +00003976}
3977
Dan Gohman475871a2008-07-27 21:46:04 +00003978SDValue
Evan Chengc3630942009-12-09 21:00:30 +00003979X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00003980 SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00003981
Evan Chengc3630942009-12-09 21:00:30 +00003982 // Check if the scalar load can be widened into a vector load. And if
3983 // the address is "base + cst" see if the cst can be "absorbed" into
3984 // the shuffle mask.
3985 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
3986 SDValue Ptr = LD->getBasePtr();
3987 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
3988 return SDValue();
3989 EVT PVT = LD->getValueType(0);
3990 if (PVT != MVT::i32 && PVT != MVT::f32)
3991 return SDValue();
3992
3993 int FI = -1;
3994 int64_t Offset = 0;
3995 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
3996 FI = FINode->getIndex();
3997 Offset = 0;
3998 } else if (Ptr.getOpcode() == ISD::ADD &&
3999 isa<ConstantSDNode>(Ptr.getOperand(1)) &&
4000 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4001 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4002 Offset = Ptr.getConstantOperandVal(1);
4003 Ptr = Ptr.getOperand(0);
4004 } else {
4005 return SDValue();
4006 }
4007
4008 SDValue Chain = LD->getChain();
4009 // Make sure the stack object alignment is at least 16.
4010 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
4011 if (DAG.InferPtrAlignment(Ptr) < 16) {
4012 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00004013 // Can't change the alignment. FIXME: It's possible to compute
4014 // the exact stack offset and reference FI + adjust offset instead.
4015 // If someone *really* cares about this. That's the way to implement it.
4016 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004017 } else {
4018 MFI->setObjectAlignment(FI, 16);
4019 }
4020 }
4021
4022 // (Offset % 16) must be multiple of 4. Then address is then
4023 // Ptr + (Offset & ~15).
4024 if (Offset < 0)
4025 return SDValue();
4026 if ((Offset % 16) & 3)
4027 return SDValue();
4028 int64_t StartOffset = Offset & ~15;
4029 if (StartOffset)
4030 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
4031 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
4032
4033 int EltNo = (Offset - StartOffset) >> 2;
4034 int Mask[4] = { EltNo, EltNo, EltNo, EltNo };
4035 EVT VT = (PVT == MVT::i32) ? MVT::v4i32 : MVT::v4f32;
Chris Lattner51abfe42010-09-21 06:02:19 +00004036 SDValue V1 = DAG.getLoad(VT, dl, Chain, Ptr,
4037 LD->getPointerInfo().getWithOffset(StartOffset),
David Greene67c9d422010-02-15 16:53:33 +00004038 false, false, 0);
Evan Chengc3630942009-12-09 21:00:30 +00004039 // Canonicalize it to a v4i32 shuffle.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004040 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, V1);
4041 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Chengc3630942009-12-09 21:00:30 +00004042 DAG.getVectorShuffle(MVT::v4i32, dl, V1,
Chris Lattner51abfe42010-09-21 06:02:19 +00004043 DAG.getUNDEF(MVT::v4i32),&Mask[0]));
Evan Chengc3630942009-12-09 21:00:30 +00004044 }
4045
4046 return SDValue();
4047}
4048
Michael J. Spencerec38de22010-10-10 22:04:20 +00004049/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
4050/// vector of type 'VT', see if the elements can be replaced by a single large
Nate Begeman1449f292010-03-24 22:19:06 +00004051/// load which has the same value as a build_vector whose operands are 'elts'.
4052///
4053/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
Michael J. Spencerec38de22010-10-10 22:04:20 +00004054///
Nate Begeman1449f292010-03-24 22:19:06 +00004055/// FIXME: we'd also like to handle the case where the last elements are zero
4056/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
4057/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004058static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
Chris Lattner88641552010-09-22 00:34:38 +00004059 DebugLoc &DL, SelectionDAG &DAG) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00004060 EVT EltVT = VT.getVectorElementType();
4061 unsigned NumElems = Elts.size();
Michael J. Spencerec38de22010-10-10 22:04:20 +00004062
Nate Begemanfdea31a2010-03-24 20:49:50 +00004063 LoadSDNode *LDBase = NULL;
4064 unsigned LastLoadedElt = -1U;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004065
Nate Begeman1449f292010-03-24 22:19:06 +00004066 // For each element in the initializer, see if we've found a load or an undef.
Michael J. Spencerec38de22010-10-10 22:04:20 +00004067 // If we don't find an initial load element, or later load elements are
Nate Begeman1449f292010-03-24 22:19:06 +00004068 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004069 for (unsigned i = 0; i < NumElems; ++i) {
4070 SDValue Elt = Elts[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00004071
Nate Begemanfdea31a2010-03-24 20:49:50 +00004072 if (!Elt.getNode() ||
4073 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
4074 return SDValue();
4075 if (!LDBase) {
4076 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
4077 return SDValue();
4078 LDBase = cast<LoadSDNode>(Elt.getNode());
4079 LastLoadedElt = i;
4080 continue;
4081 }
4082 if (Elt.getOpcode() == ISD::UNDEF)
4083 continue;
4084
4085 LoadSDNode *LD = cast<LoadSDNode>(Elt);
4086 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
4087 return SDValue();
4088 LastLoadedElt = i;
4089 }
Nate Begeman1449f292010-03-24 22:19:06 +00004090
4091 // If we have found an entire vector of loads and undefs, then return a large
4092 // load of the entire vector width starting at the base pointer. If we found
4093 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004094 if (LastLoadedElt == NumElems - 1) {
4095 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
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(), 0);
Chris Lattner88641552010-09-22 00:34:38 +00004099 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004100 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004101 LDBase->isVolatile(), LDBase->isNonTemporal(),
4102 LDBase->getAlignment());
4103 } else if (NumElems == 4 && LastLoadedElt == 1) {
4104 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
4105 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
Chris Lattner88641552010-09-22 00:34:38 +00004106 SDValue ResNode = DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys,
4107 Ops, 2, MVT::i32,
4108 LDBase->getMemOperand());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004109 return DAG.getNode(ISD::BITCAST, DL, VT, ResNode);
Nate Begemanfdea31a2010-03-24 20:49:50 +00004110 }
4111 return SDValue();
4112}
4113
Evan Chengc3630942009-12-09 21:00:30 +00004114SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004115X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004116 DebugLoc dl = Op.getDebugLoc();
Chris Lattner6e80e442010-08-28 17:15:43 +00004117 // All zero's are handled with pxor in SSE2 and above, xorps in SSE1.
4118 // All one's are handled with pcmpeqd. In AVX, zero's are handled with
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004119 // vpxor in 128-bit and xor{pd,ps} in 256-bit, but no 256 version of pcmpeqd
4120 // is present, so AllOnes is ignored.
4121 if (ISD::isBuildVectorAllZeros(Op.getNode()) ||
4122 (Op.getValueType().getSizeInBits() != 256 &&
4123 ISD::isBuildVectorAllOnes(Op.getNode()))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004124 // Canonicalize this to <4 x i32> (SSE) to
Chris Lattner8a594482007-11-25 00:24:49 +00004125 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
4126 // eliminated on x86-32 hosts.
Dale Johannesen0488fb62010-09-30 23:57:10 +00004127 if (Op.getValueType() == MVT::v4i32)
Chris Lattner8a594482007-11-25 00:24:49 +00004128 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004129
Gabor Greifba36cb52008-08-28 21:40:38 +00004130 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00004131 return getOnesVector(Op.getValueType(), DAG, dl);
4132 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00004133 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004134
Owen Andersone50ed302009-08-10 22:56:29 +00004135 EVT VT = Op.getValueType();
4136 EVT ExtVT = VT.getVectorElementType();
4137 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004138
4139 unsigned NumElems = Op.getNumOperands();
4140 unsigned NumZero = 0;
4141 unsigned NumNonZero = 0;
4142 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004143 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00004144 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004145 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00004146 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00004147 if (Elt.getOpcode() == ISD::UNDEF)
4148 continue;
4149 Values.insert(Elt);
4150 if (Elt.getOpcode() != ISD::Constant &&
4151 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004152 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00004153 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00004154 NumZero++;
4155 else {
4156 NonZeros |= (1 << i);
4157 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004158 }
4159 }
4160
Chris Lattner97a2a562010-08-26 05:24:29 +00004161 // All undef vector. Return an UNDEF. All zero vectors were handled above.
4162 if (NumNonZero == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00004163 return DAG.getUNDEF(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004164
Chris Lattner67f453a2008-03-09 05:42:06 +00004165 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00004166 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004167 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00004168 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00004169
Chris Lattner62098042008-03-09 01:05:04 +00004170 // If this is an insertion of an i64 value on x86-32, and if the top bits of
4171 // the value are obviously zero, truncate the value to i32 and do the
4172 // insertion that way. Only do this if the value is non-constant or if the
4173 // value is a constant being inserted into element 0. It is cheaper to do
4174 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00004175 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00004176 (!IsAllConstants || Idx == 0)) {
4177 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004178 // Handle SSE only.
4179 assert(VT == MVT::v2i64 && "Expected an SSE value type!");
4180 EVT VecVT = MVT::v4i32;
4181 unsigned VecElts = 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00004182
Chris Lattner62098042008-03-09 01:05:04 +00004183 // Truncate the value (which may itself be a constant) to i32, and
4184 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00004185 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00004186 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00004187 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
4188 Subtarget->hasSSE2(), DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00004189
Chris Lattner62098042008-03-09 01:05:04 +00004190 // Now we have our 32-bit value zero extended in the low element of
4191 // a vector. If Idx != 0, swizzle it into place.
4192 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004193 SmallVector<int, 4> Mask;
4194 Mask.push_back(Idx);
4195 for (unsigned i = 1; i != VecElts; ++i)
4196 Mask.push_back(i);
4197 Item = DAG.getVectorShuffle(VecVT, dl, Item,
Eric Christopherfd179292009-08-27 18:07:15 +00004198 DAG.getUNDEF(Item.getValueType()),
Nate Begeman9008ca62009-04-27 18:41:29 +00004199 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00004200 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004201 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00004202 }
4203 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004204
Chris Lattner19f79692008-03-08 22:59:52 +00004205 // If we have a constant or non-constant insertion into the low element of
4206 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
4207 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00004208 // depending on what the source datatype is.
4209 if (Idx == 0) {
4210 if (NumZero == 0) {
4211 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Owen Anderson825b72b2009-08-11 20:47:22 +00004212 } else if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
4213 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Eli Friedman10415532009-06-06 06:05:10 +00004214 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
4215 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
4216 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget->hasSSE2(),
4217 DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00004218 } else if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
4219 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
Dale Johannesen0488fb62010-09-30 23:57:10 +00004220 assert(VT.getSizeInBits() == 128 && "Expected an SSE value type!");
4221 EVT MiddleVT = MVT::v4i32;
Eli Friedman10415532009-06-06 06:05:10 +00004222 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MiddleVT, Item);
4223 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
4224 Subtarget->hasSSE2(), DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004225 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Eli Friedman10415532009-06-06 06:05:10 +00004226 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004227 }
Evan Chengf26ffe92008-05-29 08:22:04 +00004228
4229 // Is it a vector logical left shift?
4230 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00004231 X86::isZeroNode(Op.getOperand(0)) &&
4232 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004233 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00004234 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00004235 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004236 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00004237 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004238 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004239
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004240 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00004241 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004242
Chris Lattner19f79692008-03-08 22:59:52 +00004243 // Otherwise, if this is a vector with i32 or f32 elements, and the element
4244 // is a non-constant being inserted into an element other than the low one,
4245 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
4246 // movd/movss) to move this into the low element, then shuffle it into
4247 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004248 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00004249 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00004250
Evan Cheng0db9fe62006-04-25 20:13:52 +00004251 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00004252 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
4253 Subtarget->hasSSE2(), DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00004254 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004255 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00004256 MaskVec.push_back(i == Idx ? 0 : 1);
4257 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004258 }
4259 }
4260
Chris Lattner67f453a2008-03-09 05:42:06 +00004261 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00004262 if (Values.size() == 1) {
4263 if (EVTBits == 32) {
4264 // Instead of a shuffle like this:
4265 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
4266 // Check if it's possible to issue this instead.
4267 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
4268 unsigned Idx = CountTrailingZeros_32(NonZeros);
4269 SDValue Item = Op.getOperand(Idx);
4270 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
4271 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
4272 }
Dan Gohman475871a2008-07-27 21:46:04 +00004273 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004274 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004275
Dan Gohmana3941172007-07-24 22:55:08 +00004276 // A vector full of immediates; various special cases are already
4277 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004278 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00004279 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00004280
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00004281 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00004282 if (EVTBits == 64) {
4283 if (NumNonZero == 1) {
4284 // One half is zero or undef.
4285 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00004286 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00004287 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00004288 return getShuffleVectorZeroOrUndef(V2, Idx, true,
4289 Subtarget->hasSSE2(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00004290 }
Dan Gohman475871a2008-07-27 21:46:04 +00004291 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00004292 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004293
4294 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00004295 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00004296 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00004297 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004298 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004299 }
4300
Bill Wendling826f36f2007-03-28 00:57:11 +00004301 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00004302 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Chris Lattner97a2a562010-08-26 05:24:29 +00004303 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004304 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004305 }
4306
4307 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00004308 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00004309 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004310 if (NumElems == 4 && NumZero > 0) {
4311 for (unsigned i = 0; i < 4; ++i) {
4312 bool isZero = !(NonZeros & (1 << i));
4313 if (isZero)
Dale Johannesenace16102009-02-03 19:33:06 +00004314 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004315 else
Dale Johannesenace16102009-02-03 19:33:06 +00004316 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004317 }
4318
4319 for (unsigned i = 0; i < 2; ++i) {
4320 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
4321 default: break;
4322 case 0:
4323 V[i] = V[i*2]; // Must be a zero vector.
4324 break;
4325 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00004326 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004327 break;
4328 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00004329 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004330 break;
4331 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00004332 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004333 break;
4334 }
4335 }
4336
Nate Begeman9008ca62009-04-27 18:41:29 +00004337 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004338 bool Reverse = (NonZeros & 0x3) == 2;
4339 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004340 MaskVec.push_back(Reverse ? 1-i : i);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004341 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
4342 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004343 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
4344 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004345 }
4346
Nate Begemanfdea31a2010-03-24 20:49:50 +00004347 if (Values.size() > 1 && VT.getSizeInBits() == 128) {
4348 // Check for a build vector of consecutive loads.
4349 for (unsigned i = 0; i < NumElems; ++i)
4350 V[i] = Op.getOperand(i);
Michael J. Spencerec38de22010-10-10 22:04:20 +00004351
Nate Begemanfdea31a2010-03-24 20:49:50 +00004352 // Check for elements which are consecutive loads.
4353 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
4354 if (LD.getNode())
4355 return LD;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004356
4357 // For SSE 4.1, use insertps to put the high elements into the low element.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004358 if (getSubtarget()->hasSSE41()) {
Chris Lattner24faf612010-08-28 17:59:08 +00004359 SDValue Result;
4360 if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
4361 Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
4362 else
4363 Result = DAG.getUNDEF(VT);
Michael J. Spencerec38de22010-10-10 22:04:20 +00004364
Chris Lattner24faf612010-08-28 17:59:08 +00004365 for (unsigned i = 1; i < NumElems; ++i) {
4366 if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
4367 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
Nate Begeman9008ca62009-04-27 18:41:29 +00004368 Op.getOperand(i), DAG.getIntPtrConstant(i));
Chris Lattner24faf612010-08-28 17:59:08 +00004369 }
4370 return Result;
Nate Begeman9008ca62009-04-27 18:41:29 +00004371 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00004372
Chris Lattner6e80e442010-08-28 17:15:43 +00004373 // Otherwise, expand into a number of unpckl*, start by extending each of
4374 // our (non-undef) elements to the full vector width with the element in the
4375 // bottom slot of the vector (which generates no code for SSE).
4376 for (unsigned i = 0; i < NumElems; ++i) {
4377 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
4378 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
4379 else
4380 V[i] = DAG.getUNDEF(VT);
4381 }
4382
4383 // Next, we iteratively mix elements, e.g. for v4f32:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004384 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
4385 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
4386 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Chris Lattner6e80e442010-08-28 17:15:43 +00004387 unsigned EltStride = NumElems >> 1;
4388 while (EltStride != 0) {
Chris Lattner3ddcc432010-08-28 17:28:30 +00004389 for (unsigned i = 0; i < EltStride; ++i) {
4390 // If V[i+EltStride] is undef and this is the first round of mixing,
4391 // then it is safe to just drop this shuffle: V[i] is already in the
4392 // right place, the one element (since it's the first round) being
4393 // inserted as undef can be dropped. This isn't safe for successive
4394 // rounds because they will permute elements within both vectors.
4395 if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
4396 EltStride == NumElems/2)
4397 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004398
Chris Lattner6e80e442010-08-28 17:15:43 +00004399 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
Chris Lattner3ddcc432010-08-28 17:28:30 +00004400 }
Chris Lattner6e80e442010-08-28 17:15:43 +00004401 EltStride >>= 1;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004402 }
4403 return V[0];
4404 }
Dan Gohman475871a2008-07-27 21:46:04 +00004405 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004406}
4407
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004408SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004409X86TargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const {
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004410 // We support concatenate two MMX registers and place them in a MMX
4411 // register. This is better than doing a stack convert.
4412 DebugLoc dl = Op.getDebugLoc();
4413 EVT ResVT = Op.getValueType();
4414 assert(Op.getNumOperands() == 2);
4415 assert(ResVT == MVT::v2i64 || ResVT == MVT::v4i32 ||
4416 ResVT == MVT::v8i16 || ResVT == MVT::v16i8);
4417 int Mask[2];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004418 SDValue InVec = DAG.getNode(ISD::BITCAST,dl, MVT::v1i64, Op.getOperand(0));
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004419 SDValue VecOp = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
4420 InVec = Op.getOperand(1);
4421 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4422 unsigned NumElts = ResVT.getVectorNumElements();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004423 VecOp = DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004424 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, ResVT, VecOp,
4425 InVec.getOperand(0), DAG.getIntPtrConstant(NumElts/2+1));
4426 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004427 InVec = DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, InVec);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004428 SDValue VecOp2 = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
4429 Mask[0] = 0; Mask[1] = 2;
4430 VecOp = DAG.getVectorShuffle(MVT::v2i64, dl, VecOp, VecOp2, Mask);
4431 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004432 return DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004433}
4434
Nate Begemanb9a47b82009-02-23 08:49:38 +00004435// v8i16 shuffles - Prefer shuffles in the following order:
4436// 1. [all] pshuflw, pshufhw, optional move
4437// 2. [ssse3] 1 x pshufb
4438// 3. [ssse3] 2 x pshufb + 1 x por
4439// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004440SDValue
4441X86TargetLowering::LowerVECTOR_SHUFFLEv8i16(SDValue Op,
4442 SelectionDAG &DAG) const {
4443 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00004444 SDValue V1 = SVOp->getOperand(0);
4445 SDValue V2 = SVOp->getOperand(1);
4446 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00004447 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00004448
Nate Begemanb9a47b82009-02-23 08:49:38 +00004449 // Determine if more than 1 of the words in each of the low and high quadwords
4450 // of the result come from the same quadword of one of the two inputs. Undef
4451 // mask values count as coming from any quadword, for better codegen.
4452 SmallVector<unsigned, 4> LoQuad(4);
4453 SmallVector<unsigned, 4> HiQuad(4);
4454 BitVector InputQuads(4);
4455 for (unsigned i = 0; i < 8; ++i) {
4456 SmallVectorImpl<unsigned> &Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00004457 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004458 MaskVals.push_back(EltIdx);
4459 if (EltIdx < 0) {
4460 ++Quad[0];
4461 ++Quad[1];
4462 ++Quad[2];
4463 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00004464 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004465 }
4466 ++Quad[EltIdx / 4];
4467 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00004468 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004469
Nate Begemanb9a47b82009-02-23 08:49:38 +00004470 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00004471 unsigned MaxQuad = 1;
4472 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004473 if (LoQuad[i] > MaxQuad) {
4474 BestLoQuad = i;
4475 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00004476 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004477 }
4478
Nate Begemanb9a47b82009-02-23 08:49:38 +00004479 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00004480 MaxQuad = 1;
4481 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004482 if (HiQuad[i] > MaxQuad) {
4483 BestHiQuad = i;
4484 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00004485 }
4486 }
4487
Nate Begemanb9a47b82009-02-23 08:49:38 +00004488 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00004489 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00004490 // single pshufb instruction is necessary. If There are more than 2 input
4491 // quads, disable the next transformation since it does not help SSSE3.
4492 bool V1Used = InputQuads[0] || InputQuads[1];
4493 bool V2Used = InputQuads[2] || InputQuads[3];
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004494 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004495 if (InputQuads.count() == 2 && V1Used && V2Used) {
4496 BestLoQuad = InputQuads.find_first();
4497 BestHiQuad = InputQuads.find_next(BestLoQuad);
4498 }
4499 if (InputQuads.count() > 2) {
4500 BestLoQuad = -1;
4501 BestHiQuad = -1;
4502 }
4503 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004504
Nate Begemanb9a47b82009-02-23 08:49:38 +00004505 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
4506 // the shuffle mask. If a quad is scored as -1, that means that it contains
4507 // words from all 4 input quadwords.
4508 SDValue NewV;
4509 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004510 SmallVector<int, 8> MaskV;
4511 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
4512 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
Eric Christopherfd179292009-08-27 18:07:15 +00004513 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004514 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V1),
4515 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V2), &MaskV[0]);
4516 NewV = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00004517
Nate Begemanb9a47b82009-02-23 08:49:38 +00004518 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
4519 // source words for the shuffle, to aid later transformations.
4520 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00004521 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00004522 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004523 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00004524 if (idx != (int)i)
4525 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004526 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00004527 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004528 AllWordsInNewV = false;
4529 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00004530 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004531
Nate Begemanb9a47b82009-02-23 08:49:38 +00004532 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
4533 if (AllWordsInNewV) {
4534 for (int i = 0; i != 8; ++i) {
4535 int idx = MaskVals[i];
4536 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00004537 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004538 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004539 if ((idx != i) && idx < 4)
4540 pshufhw = false;
4541 if ((idx != i) && idx > 3)
4542 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00004543 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00004544 V1 = NewV;
4545 V2Used = false;
4546 BestLoQuad = 0;
4547 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004548 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004549
Nate Begemanb9a47b82009-02-23 08:49:38 +00004550 // If we've eliminated the use of V2, and the new mask is a pshuflw or
4551 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00004552 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00004553 unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
4554 unsigned TargetMask = 0;
4555 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00004556 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00004557 TargetMask = pshufhw ? X86::getShufflePSHUFHWImmediate(NewV.getNode()):
4558 X86::getShufflePSHUFLWImmediate(NewV.getNode());
4559 V1 = NewV.getOperand(0);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00004560 return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
Evan Cheng14b32e12007-12-11 01:46:18 +00004561 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004562 }
Eric Christopherfd179292009-08-27 18:07:15 +00004563
Nate Begemanb9a47b82009-02-23 08:49:38 +00004564 // If we have SSSE3, and all words of the result are from 1 input vector,
4565 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
4566 // is present, fall back to case 4.
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004567 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004568 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00004569
Nate Begemanb9a47b82009-02-23 08:49:38 +00004570 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00004571 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00004572 // mask, and elements that come from V1 in the V2 mask, so that the two
4573 // results can be OR'd together.
4574 bool TwoInputs = V1Used && V2Used;
4575 for (unsigned i = 0; i != 8; ++i) {
4576 int EltIdx = MaskVals[i] * 2;
4577 if (TwoInputs && (EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004578 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4579 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004580 continue;
4581 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004582 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
4583 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004584 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004585 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004586 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00004587 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004588 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004589 if (!TwoInputs)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004590 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004591
Nate Begemanb9a47b82009-02-23 08:49:38 +00004592 // Calculate the shuffle mask for the second input, shuffle it, and
4593 // OR it with the first shuffled input.
4594 pshufbMask.clear();
4595 for (unsigned i = 0; i != 8; ++i) {
4596 int EltIdx = MaskVals[i] * 2;
4597 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004598 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4599 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004600 continue;
4601 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004602 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
4603 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004604 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004605 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00004606 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00004607 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004608 MVT::v16i8, &pshufbMask[0], 16));
4609 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004610 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004611 }
4612
4613 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
4614 // and update MaskVals with new element order.
4615 BitVector InOrder(8);
4616 if (BestLoQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004617 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004618 for (int i = 0; i != 4; ++i) {
4619 int idx = MaskVals[i];
4620 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004621 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004622 InOrder.set(i);
4623 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004624 MaskV.push_back(idx & 3);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004625 InOrder.set(i);
4626 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004627 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004628 }
4629 }
4630 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004631 MaskV.push_back(i);
Owen Anderson825b72b2009-08-11 20:47:22 +00004632 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00004633 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00004634
4635 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3())
4636 NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
4637 NewV.getOperand(0),
4638 X86::getShufflePSHUFLWImmediate(NewV.getNode()),
4639 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004640 }
Eric Christopherfd179292009-08-27 18:07:15 +00004641
Nate Begemanb9a47b82009-02-23 08:49:38 +00004642 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
4643 // and update MaskVals with the new element order.
4644 if (BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004645 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004646 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004647 MaskV.push_back(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004648 for (unsigned i = 4; i != 8; ++i) {
4649 int idx = MaskVals[i];
4650 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004651 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004652 InOrder.set(i);
4653 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004654 MaskV.push_back((idx & 3) + 4);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004655 InOrder.set(i);
4656 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004657 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004658 }
4659 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004660 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00004661 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00004662
4663 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3())
4664 NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
4665 NewV.getOperand(0),
4666 X86::getShufflePSHUFHWImmediate(NewV.getNode()),
4667 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004668 }
Eric Christopherfd179292009-08-27 18:07:15 +00004669
Nate Begemanb9a47b82009-02-23 08:49:38 +00004670 // In case BestHi & BestLo were both -1, which means each quadword has a word
4671 // from each of the four input quadwords, calculate the InOrder bitvector now
4672 // before falling through to the insert/extract cleanup.
4673 if (BestLoQuad == -1 && BestHiQuad == -1) {
4674 NewV = V1;
4675 for (int i = 0; i != 8; ++i)
4676 if (MaskVals[i] < 0 || MaskVals[i] == i)
4677 InOrder.set(i);
4678 }
Eric Christopherfd179292009-08-27 18:07:15 +00004679
Nate Begemanb9a47b82009-02-23 08:49:38 +00004680 // The other elements are put in the right place using pextrw and pinsrw.
4681 for (unsigned i = 0; i != 8; ++i) {
4682 if (InOrder[i])
4683 continue;
4684 int EltIdx = MaskVals[i];
4685 if (EltIdx < 0)
4686 continue;
4687 SDValue ExtOp = (EltIdx < 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00004688 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004689 DAG.getIntPtrConstant(EltIdx))
Owen Anderson825b72b2009-08-11 20:47:22 +00004690 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004691 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00004692 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004693 DAG.getIntPtrConstant(i));
4694 }
4695 return NewV;
4696}
4697
4698// v16i8 shuffles - Prefer shuffles in the following order:
4699// 1. [ssse3] 1 x pshufb
4700// 2. [ssse3] 2 x pshufb + 1 x por
4701// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
4702static
Nate Begeman9008ca62009-04-27 18:41:29 +00004703SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00004704 SelectionDAG &DAG,
4705 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004706 SDValue V1 = SVOp->getOperand(0);
4707 SDValue V2 = SVOp->getOperand(1);
4708 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00004709 SmallVector<int, 16> MaskVals;
Nate Begeman9008ca62009-04-27 18:41:29 +00004710 SVOp->getMask(MaskVals);
Eric Christopherfd179292009-08-27 18:07:15 +00004711
Nate Begemanb9a47b82009-02-23 08:49:38 +00004712 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00004713 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00004714 // present, fall back to case 3.
4715 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
4716 bool V1Only = true;
4717 bool V2Only = true;
4718 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004719 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00004720 if (EltIdx < 0)
4721 continue;
4722 if (EltIdx < 16)
4723 V2Only = false;
4724 else
4725 V1Only = false;
4726 }
Eric Christopherfd179292009-08-27 18:07:15 +00004727
Nate Begemanb9a47b82009-02-23 08:49:38 +00004728 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
4729 if (TLI.getSubtarget()->hasSSSE3()) {
4730 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00004731
Nate Begemanb9a47b82009-02-23 08:49:38 +00004732 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00004733 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00004734 //
4735 // Otherwise, we have elements from both input vectors, and must zero out
4736 // elements that come from V2 in the first mask, and V1 in the second mask
4737 // so that we can OR them together.
4738 bool TwoInputs = !(V1Only || V2Only);
4739 for (unsigned i = 0; i != 16; ++i) {
4740 int EltIdx = MaskVals[i];
4741 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004742 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004743 continue;
4744 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004745 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004746 }
4747 // If all the elements are from V2, assign it to V1 and return after
4748 // building the first pshufb.
4749 if (V2Only)
4750 V1 = V2;
Owen Anderson825b72b2009-08-11 20:47:22 +00004751 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00004752 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004753 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004754 if (!TwoInputs)
4755 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00004756
Nate Begemanb9a47b82009-02-23 08:49:38 +00004757 // Calculate the shuffle mask for the second input, shuffle it, and
4758 // OR it with the first shuffled input.
4759 pshufbMask.clear();
4760 for (unsigned i = 0; i != 16; ++i) {
4761 int EltIdx = MaskVals[i];
4762 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004763 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004764 continue;
4765 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004766 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004767 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004768 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00004769 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004770 MVT::v16i8, &pshufbMask[0], 16));
4771 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004772 }
Eric Christopherfd179292009-08-27 18:07:15 +00004773
Nate Begemanb9a47b82009-02-23 08:49:38 +00004774 // No SSSE3 - Calculate in place words and then fix all out of place words
4775 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
4776 // the 16 different words that comprise the two doublequadword input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004777 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
4778 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004779 SDValue NewV = V2Only ? V2 : V1;
4780 for (int i = 0; i != 8; ++i) {
4781 int Elt0 = MaskVals[i*2];
4782 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00004783
Nate Begemanb9a47b82009-02-23 08:49:38 +00004784 // This word of the result is all undef, skip it.
4785 if (Elt0 < 0 && Elt1 < 0)
4786 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004787
Nate Begemanb9a47b82009-02-23 08:49:38 +00004788 // This word of the result is already in the correct place, skip it.
4789 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
4790 continue;
4791 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
4792 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004793
Nate Begemanb9a47b82009-02-23 08:49:38 +00004794 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
4795 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
4796 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00004797
4798 // If Elt0 and Elt1 are defined, are consecutive, and can be load
4799 // using a single extract together, load it and store it.
4800 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004801 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00004802 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00004803 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00004804 DAG.getIntPtrConstant(i));
4805 continue;
4806 }
4807
Nate Begemanb9a47b82009-02-23 08:49:38 +00004808 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00004809 // source byte is not also odd, shift the extracted word left 8 bits
4810 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00004811 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004812 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004813 DAG.getIntPtrConstant(Elt1 / 2));
4814 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004815 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004816 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00004817 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004818 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
4819 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004820 }
4821 // If Elt0 is defined, extract it from the appropriate source. If the
4822 // source byte is not also even, shift the extracted word right 8 bits. If
4823 // Elt1 was also defined, OR the extracted values together before
4824 // inserting them in the result.
4825 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004826 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004827 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
4828 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004829 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004830 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00004831 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004832 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
4833 DAG.getConstant(0x00FF, MVT::i16));
4834 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00004835 : InsElt0;
4836 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004837 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004838 DAG.getIntPtrConstant(i));
4839 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004840 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00004841}
4842
Evan Cheng7a831ce2007-12-15 03:00:47 +00004843/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00004844/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00004845/// done when every pair / quad of shuffle mask elements point to elements in
4846/// the right sequence. e.g.
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00004847/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
Evan Cheng14b32e12007-12-11 01:46:18 +00004848static
Nate Begeman9008ca62009-04-27 18:41:29 +00004849SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00004850 SelectionDAG &DAG, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00004851 EVT VT = SVOp->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00004852 SDValue V1 = SVOp->getOperand(0);
4853 SDValue V2 = SVOp->getOperand(1);
4854 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00004855 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00004856 EVT NewVT;
Owen Anderson825b72b2009-08-11 20:47:22 +00004857 switch (VT.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004858 default: assert(false && "Unexpected!");
Owen Anderson825b72b2009-08-11 20:47:22 +00004859 case MVT::v4f32: NewVT = MVT::v2f64; break;
4860 case MVT::v4i32: NewVT = MVT::v2i64; break;
4861 case MVT::v8i16: NewVT = MVT::v4i32; break;
4862 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00004863 }
4864
Nate Begeman9008ca62009-04-27 18:41:29 +00004865 int Scale = NumElems / NewWidth;
4866 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00004867 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004868 int StartIdx = -1;
4869 for (int j = 0; j < Scale; ++j) {
4870 int EltIdx = SVOp->getMaskElt(i+j);
4871 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00004872 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00004873 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00004874 StartIdx = EltIdx - (EltIdx % Scale);
4875 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00004876 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00004877 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004878 if (StartIdx == -1)
4879 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00004880 else
Nate Begeman9008ca62009-04-27 18:41:29 +00004881 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004882 }
4883
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004884 V1 = DAG.getNode(ISD::BITCAST, dl, NewVT, V1);
4885 V2 = DAG.getNode(ISD::BITCAST, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00004886 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004887}
4888
Evan Chengd880b972008-05-09 21:53:03 +00004889/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00004890///
Owen Andersone50ed302009-08-10 22:56:29 +00004891static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00004892 SDValue SrcOp, SelectionDAG &DAG,
4893 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004894 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004895 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00004896 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00004897 LD = dyn_cast<LoadSDNode>(SrcOp);
4898 if (!LD) {
4899 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
4900 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00004901 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Duncan Sandscdfad362010-11-03 12:17:33 +00004902 if ((ExtVT != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00004903 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004904 SrcOp.getOperand(0).getOpcode() == ISD::BITCAST &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00004905 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004906 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00004907 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004908 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004909 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
4910 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
4911 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00004912 SrcOp.getOperand(0)
4913 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00004914 }
4915 }
4916 }
4917
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004918 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004919 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004920 DAG.getNode(ISD::BITCAST, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00004921 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00004922}
4923
Evan Chengace3c172008-07-22 21:13:36 +00004924/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
4925/// shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00004926static SDValue
Nate Begeman9008ca62009-04-27 18:41:29 +00004927LowerVECTOR_SHUFFLE_4wide(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
4928 SDValue V1 = SVOp->getOperand(0);
4929 SDValue V2 = SVOp->getOperand(1);
4930 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00004931 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00004932
Evan Chengace3c172008-07-22 21:13:36 +00004933 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00004934 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00004935 SmallVector<int, 8> Mask1(4U, -1);
4936 SmallVector<int, 8> PermMask;
4937 SVOp->getMask(PermMask);
4938
Evan Chengace3c172008-07-22 21:13:36 +00004939 unsigned NumHi = 0;
4940 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00004941 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004942 int Idx = PermMask[i];
4943 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00004944 Locs[i] = std::make_pair(-1, -1);
4945 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004946 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
4947 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00004948 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00004949 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004950 NumLo++;
4951 } else {
4952 Locs[i] = std::make_pair(1, NumHi);
4953 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00004954 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004955 NumHi++;
4956 }
4957 }
4958 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004959
Evan Chengace3c172008-07-22 21:13:36 +00004960 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004961 // If no more than two elements come from either vector. This can be
4962 // implemented with two shuffles. First shuffle gather the elements.
4963 // The second shuffle, which takes the first shuffle as both of its
4964 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00004965 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004966
Nate Begeman9008ca62009-04-27 18:41:29 +00004967 SmallVector<int, 8> Mask2(4U, -1);
Eric Christopherfd179292009-08-27 18:07:15 +00004968
Evan Chengace3c172008-07-22 21:13:36 +00004969 for (unsigned i = 0; i != 4; ++i) {
4970 if (Locs[i].first == -1)
4971 continue;
4972 else {
4973 unsigned Idx = (i < 2) ? 0 : 4;
4974 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00004975 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004976 }
4977 }
4978
Nate Begeman9008ca62009-04-27 18:41:29 +00004979 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004980 } else if (NumLo == 3 || NumHi == 3) {
4981 // Otherwise, we must have three elements from one vector, call it X, and
4982 // one element from the other, call it Y. First, use a shufps to build an
4983 // intermediate vector with the one element from Y and the element from X
4984 // that will be in the same half in the final destination (the indexes don't
4985 // matter). Then, use a shufps to build the final vector, taking the half
4986 // containing the element from Y from the intermediate, and the other half
4987 // from X.
4988 if (NumHi == 3) {
4989 // Normalize it so the 3 elements come from V1.
Nate Begeman9008ca62009-04-27 18:41:29 +00004990 CommuteVectorShuffleMask(PermMask, VT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004991 std::swap(V1, V2);
4992 }
4993
4994 // Find the element from V2.
4995 unsigned HiIndex;
4996 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004997 int Val = PermMask[HiIndex];
4998 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004999 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005000 if (Val >= 4)
5001 break;
5002 }
5003
Nate Begeman9008ca62009-04-27 18:41:29 +00005004 Mask1[0] = PermMask[HiIndex];
5005 Mask1[1] = -1;
5006 Mask1[2] = PermMask[HiIndex^1];
5007 Mask1[3] = -1;
5008 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005009
5010 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005011 Mask1[0] = PermMask[0];
5012 Mask1[1] = PermMask[1];
5013 Mask1[2] = HiIndex & 1 ? 6 : 4;
5014 Mask1[3] = HiIndex & 1 ? 4 : 6;
5015 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005016 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005017 Mask1[0] = HiIndex & 1 ? 2 : 0;
5018 Mask1[1] = HiIndex & 1 ? 0 : 2;
5019 Mask1[2] = PermMask[2];
5020 Mask1[3] = PermMask[3];
5021 if (Mask1[2] >= 0)
5022 Mask1[2] += 4;
5023 if (Mask1[3] >= 0)
5024 Mask1[3] += 4;
5025 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005026 }
Evan Chengace3c172008-07-22 21:13:36 +00005027 }
5028
5029 // Break it into (shuffle shuffle_hi, shuffle_lo).
5030 Locs.clear();
Nate Begeman9008ca62009-04-27 18:41:29 +00005031 SmallVector<int,8> LoMask(4U, -1);
5032 SmallVector<int,8> HiMask(4U, -1);
5033
5034 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00005035 unsigned MaskIdx = 0;
5036 unsigned LoIdx = 0;
5037 unsigned HiIdx = 2;
5038 for (unsigned i = 0; i != 4; ++i) {
5039 if (i == 2) {
5040 MaskPtr = &HiMask;
5041 MaskIdx = 1;
5042 LoIdx = 0;
5043 HiIdx = 2;
5044 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005045 int Idx = PermMask[i];
5046 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00005047 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00005048 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00005049 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00005050 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005051 LoIdx++;
5052 } else {
5053 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00005054 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005055 HiIdx++;
5056 }
5057 }
5058
Nate Begeman9008ca62009-04-27 18:41:29 +00005059 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
5060 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
5061 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00005062 for (unsigned i = 0; i != 4; ++i) {
5063 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005064 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00005065 } else {
5066 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00005067 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00005068 }
5069 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005070 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00005071}
5072
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005073static bool MayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005074 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005075 V = V.getOperand(0);
5076 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
5077 V = V.getOperand(0);
5078 if (MayFoldLoad(V))
5079 return true;
5080 return false;
5081}
5082
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005083// FIXME: the version above should always be used. Since there's
5084// a bug where several vector shuffles can't be folded because the
5085// DAG is not updated during lowering and a node claims to have two
5086// uses while it only has one, use this version, and let isel match
5087// another instruction if the load really happens to have more than
5088// one use. Remove this version after this bug get fixed.
Evan Cheng835580f2010-10-07 20:50:20 +00005089// rdar://8434668, PR8156
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005090static bool RelaxedMayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005091 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005092 V = V.getOperand(0);
5093 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
5094 V = V.getOperand(0);
5095 if (ISD::isNormalLoad(V.getNode()))
5096 return true;
5097 return false;
5098}
5099
5100/// CanFoldShuffleIntoVExtract - Check if the current shuffle is used by
5101/// a vector extract, and if both can be later optimized into a single load.
5102/// This is done in visitEXTRACT_VECTOR_ELT and the conditions are checked
5103/// here because otherwise a target specific shuffle node is going to be
5104/// emitted for this shuffle, and the optimization not done.
5105/// FIXME: This is probably not the best approach, but fix the problem
5106/// until the right path is decided.
5107static
5108bool CanXFormVExtractWithShuffleIntoLoad(SDValue V, SelectionDAG &DAG,
5109 const TargetLowering &TLI) {
5110 EVT VT = V.getValueType();
5111 ShuffleVectorSDNode *SVOp = dyn_cast<ShuffleVectorSDNode>(V);
5112
5113 // Be sure that the vector shuffle is present in a pattern like this:
5114 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), c) -> (f32 load $addr)
5115 if (!V.hasOneUse())
5116 return false;
5117
5118 SDNode *N = *V.getNode()->use_begin();
5119 if (N->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
5120 return false;
5121
5122 SDValue EltNo = N->getOperand(1);
5123 if (!isa<ConstantSDNode>(EltNo))
5124 return false;
5125
5126 // If the bit convert changed the number of elements, it is unsafe
5127 // to examine the mask.
5128 bool HasShuffleIntoBitcast = false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005129 if (V.getOpcode() == ISD::BITCAST) {
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005130 EVT SrcVT = V.getOperand(0).getValueType();
5131 if (SrcVT.getVectorNumElements() != VT.getVectorNumElements())
5132 return false;
5133 V = V.getOperand(0);
5134 HasShuffleIntoBitcast = true;
5135 }
5136
5137 // Select the input vector, guarding against out of range extract vector.
5138 unsigned NumElems = VT.getVectorNumElements();
5139 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
5140 int Idx = (Elt > NumElems) ? -1 : SVOp->getMaskElt(Elt);
5141 V = (Idx < (int)NumElems) ? V.getOperand(0) : V.getOperand(1);
5142
5143 // Skip one more bit_convert if necessary
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005144 if (V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005145 V = V.getOperand(0);
5146
5147 if (ISD::isNormalLoad(V.getNode())) {
5148 // Is the original load suitable?
5149 LoadSDNode *LN0 = cast<LoadSDNode>(V);
5150
5151 // FIXME: avoid the multi-use bug that is preventing lots of
5152 // of foldings to be detected, this is still wrong of course, but
5153 // give the temporary desired behavior, and if it happens that
5154 // the load has real more uses, during isel it will not fold, and
5155 // will generate poor code.
5156 if (!LN0 || LN0->isVolatile()) // || !LN0->hasOneUse()
5157 return false;
5158
5159 if (!HasShuffleIntoBitcast)
5160 return true;
5161
5162 // If there's a bitcast before the shuffle, check if the load type and
5163 // alignment is valid.
5164 unsigned Align = LN0->getAlignment();
5165 unsigned NewAlign =
5166 TLI.getTargetData()->getABITypeAlignment(
5167 VT.getTypeForEVT(*DAG.getContext()));
5168
5169 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
5170 return false;
5171 }
5172
5173 return true;
5174}
5175
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005176static
Evan Cheng835580f2010-10-07 20:50:20 +00005177SDValue getMOVDDup(SDValue &Op, DebugLoc &dl, SDValue V1, SelectionDAG &DAG) {
5178 EVT VT = Op.getValueType();
5179
5180 // Canonizalize to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005181 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1);
5182 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng835580f2010-10-07 20:50:20 +00005183 getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64,
5184 V1, DAG));
5185}
5186
5187static
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005188SDValue getMOVLowToHigh(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG,
5189 bool HasSSE2) {
5190 SDValue V1 = Op.getOperand(0);
5191 SDValue V2 = Op.getOperand(1);
5192 EVT VT = Op.getValueType();
5193
5194 assert(VT != MVT::v2i64 && "unsupported shuffle type");
5195
5196 if (HasSSE2 && VT == MVT::v2f64)
5197 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
5198
5199 // v4f32 or v4i32
5200 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V2, DAG);
5201}
5202
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00005203static
5204SDValue getMOVHighToLow(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG) {
5205 SDValue V1 = Op.getOperand(0);
5206 SDValue V2 = Op.getOperand(1);
5207 EVT VT = Op.getValueType();
5208
5209 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
5210 "unsupported shuffle type");
5211
5212 if (V2.getOpcode() == ISD::UNDEF)
5213 V2 = V1;
5214
5215 // v4i32 or v4f32
5216 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
5217}
5218
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005219static
5220SDValue getMOVLP(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG, bool HasSSE2) {
5221 SDValue V1 = Op.getOperand(0);
5222 SDValue V2 = Op.getOperand(1);
5223 EVT VT = Op.getValueType();
5224 unsigned NumElems = VT.getVectorNumElements();
5225
5226 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
5227 // operand of these instructions is only memory, so check if there's a
5228 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
5229 // same masks.
5230 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005231
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00005232 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005233 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005234 CanFoldLoad = true;
5235
5236 // When V1 is a load, it can be folded later into a store in isel, example:
5237 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
5238 // turns into:
5239 // (MOVLPSmr addr:$src1, VR128:$src2)
5240 // So, recognize this potential and also use MOVLPS or MOVLPD
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005241 if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005242 CanFoldLoad = true;
5243
5244 if (CanFoldLoad) {
5245 if (HasSSE2 && NumElems == 2)
5246 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
5247
5248 if (NumElems == 4)
5249 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
5250 }
5251
5252 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5253 // movl and movlp will both match v2i64, but v2i64 is never matched by
5254 // movl earlier because we make it strict to avoid messing with the movlp load
5255 // folding logic (see the code above getMOVLP call). Match it here then,
5256 // this is horrible, but will stay like this until we move all shuffle
5257 // matching to x86 specific nodes. Note that for the 1st condition all
5258 // types are matched with movsd.
5259 if ((HasSSE2 && NumElems == 2) || !X86::isMOVLMask(SVOp))
5260 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
5261 else if (HasSSE2)
5262 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
5263
5264
5265 assert(VT != MVT::v4i32 && "unsupported shuffle type");
5266
5267 // Invert the operand order and use SHUFPS to match it.
5268 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V2, V1,
5269 X86::getShuffleSHUFImmediate(SVOp), DAG);
5270}
5271
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005272static inline unsigned getUNPCKLOpcode(EVT VT) {
5273 switch(VT.getSimpleVT().SimpleTy) {
5274 case MVT::v4i32: return X86ISD::PUNPCKLDQ;
5275 case MVT::v2i64: return X86ISD::PUNPCKLQDQ;
5276 case MVT::v4f32: return X86ISD::UNPCKLPS;
5277 case MVT::v2f64: return X86ISD::UNPCKLPD;
5278 case MVT::v16i8: return X86ISD::PUNPCKLBW;
5279 case MVT::v8i16: return X86ISD::PUNPCKLWD;
5280 default:
5281 llvm_unreachable("Unknow type for unpckl");
5282 }
5283 return 0;
5284}
5285
5286static inline unsigned getUNPCKHOpcode(EVT VT) {
5287 switch(VT.getSimpleVT().SimpleTy) {
5288 case MVT::v4i32: return X86ISD::PUNPCKHDQ;
5289 case MVT::v2i64: return X86ISD::PUNPCKHQDQ;
5290 case MVT::v4f32: return X86ISD::UNPCKHPS;
5291 case MVT::v2f64: return X86ISD::UNPCKHPD;
5292 case MVT::v16i8: return X86ISD::PUNPCKHBW;
5293 case MVT::v8i16: return X86ISD::PUNPCKHWD;
5294 default:
5295 llvm_unreachable("Unknow type for unpckh");
5296 }
5297 return 0;
5298}
5299
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005300static
5301SDValue NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG,
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005302 const TargetLowering &TLI,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005303 const X86Subtarget *Subtarget) {
5304 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5305 EVT VT = Op.getValueType();
5306 DebugLoc dl = Op.getDebugLoc();
5307 SDValue V1 = Op.getOperand(0);
5308 SDValue V2 = Op.getOperand(1);
5309
5310 if (isZeroShuffle(SVOp))
5311 return getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
5312
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005313 // Handle splat operations
5314 if (SVOp->isSplat()) {
5315 // Special case, this is the only place now where it's
5316 // allowed to return a vector_shuffle operation without
5317 // using a target specific node, because *hopefully* it
5318 // will be optimized away by the dag combiner.
5319 if (VT.getVectorNumElements() <= 4 &&
5320 CanXFormVExtractWithShuffleIntoLoad(Op, DAG, TLI))
5321 return Op;
5322
5323 // Handle splats by matching through known masks
5324 if (VT.getVectorNumElements() <= 4)
5325 return SDValue();
5326
Evan Cheng835580f2010-10-07 20:50:20 +00005327 // Canonicalize all of the remaining to v4f32.
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005328 return PromoteSplat(SVOp, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005329 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005330
5331 // If the shuffle can be profitably rewritten as a narrower shuffle, then
5332 // do it!
5333 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
5334 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5335 if (NewOp.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005336 return DAG.getNode(ISD::BITCAST, dl, VT, NewOp);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005337 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
5338 // FIXME: Figure out a cleaner way to do this.
5339 // Try to make use of movq to zero out the top part.
5340 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
5341 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5342 if (NewOp.getNode()) {
5343 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
5344 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
5345 DAG, Subtarget, dl);
5346 }
5347 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
5348 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5349 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
5350 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
5351 DAG, Subtarget, dl);
5352 }
5353 }
5354 return SDValue();
5355}
5356
Dan Gohman475871a2008-07-27 21:46:04 +00005357SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005358X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00005359 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00005360 SDValue V1 = Op.getOperand(0);
5361 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00005362 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005363 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00005364 unsigned NumElems = VT.getVectorNumElements();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005365 bool isMMX = VT.getSizeInBits() == 64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005366 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
5367 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00005368 bool V1IsSplat = false;
5369 bool V2IsSplat = false;
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005370 bool HasSSE2 = Subtarget->hasSSE2() || Subtarget->hasAVX();
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00005371 bool HasSSE3 = Subtarget->hasSSE3() || Subtarget->hasAVX();
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00005372 bool HasSSSE3 = Subtarget->hasSSSE3() || Subtarget->hasAVX();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005373 MachineFunction &MF = DAG.getMachineFunction();
5374 bool OptForSize = MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005375
Dale Johannesen0488fb62010-09-30 23:57:10 +00005376 // Shuffle operations on MMX not supported.
5377 if (isMMX)
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00005378 return Op;
5379
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005380 // Vector shuffle lowering takes 3 steps:
5381 //
5382 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
5383 // narrowing and commutation of operands should be handled.
5384 // 2) Matching of shuffles with known shuffle masks to x86 target specific
5385 // shuffle nodes.
5386 // 3) Rewriting of unmatched masks into new generic shuffle operations,
5387 // so the shuffle can be broken into other shuffles and the legalizer can
5388 // try the lowering again.
5389 //
5390 // The general ideia is that no vector_shuffle operation should be left to
5391 // be matched during isel, all of them must be converted to a target specific
5392 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00005393
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005394 // Normalize the input vectors. Here splats, zeroed vectors, profitable
5395 // narrowing and commutation of operands should be handled. The actual code
5396 // doesn't include all of those, work in progress...
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005397 SDValue NewOp = NormalizeVectorShuffle(Op, DAG, *this, Subtarget);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005398 if (NewOp.getNode())
5399 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00005400
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005401 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
5402 // unpckh_undef). Only use pshufd if speed is more important than size.
5403 if (OptForSize && X86::isUNPCKL_v_undef_Mask(SVOp))
5404 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5405 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V1, DAG);
5406 if (OptForSize && X86::isUNPCKH_v_undef_Mask(SVOp))
5407 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5408 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00005409
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005410 if (X86::isMOVDDUPMask(SVOp) && HasSSE3 && V2IsUndef &&
Dale Johannesen0488fb62010-09-30 23:57:10 +00005411 RelaxedMayFoldVectorLoad(V1))
Evan Cheng835580f2010-10-07 20:50:20 +00005412 return getMOVDDup(Op, dl, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005413
Dale Johannesen0488fb62010-09-30 23:57:10 +00005414 if (X86::isMOVHLPS_v_undef_Mask(SVOp))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005415 return getMOVHighToLow(Op, dl, DAG);
5416
5417 // Use to match splats
5418 if (HasSSE2 && X86::isUNPCKHMask(SVOp) && V2IsUndef &&
5419 (VT == MVT::v2f64 || VT == MVT::v2i64))
5420 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
5421
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005422 if (X86::isPSHUFDMask(SVOp)) {
5423 // The actual implementation will match the mask in the if above and then
5424 // during isel it can match several different instructions, not only pshufd
5425 // as its name says, sad but true, emulate the behavior for now...
5426 if (X86::isMOVDDUPMask(SVOp) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
5427 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
5428
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005429 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
5430
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005431 if (HasSSE2 && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005432 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
5433
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005434 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005435 return getTargetShuffleNode(X86ISD::SHUFPD, dl, VT, V1, V1,
5436 TargetMask, DAG);
5437
5438 if (VT == MVT::v4f32)
5439 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V1, V1,
5440 TargetMask, DAG);
5441 }
Eric Christopherfd179292009-08-27 18:07:15 +00005442
Evan Chengf26ffe92008-05-29 08:22:04 +00005443 // Check if this can be converted into a logical shift.
5444 bool isLeft = false;
5445 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00005446 SDValue ShVal;
Nate Begeman9008ca62009-04-27 18:41:29 +00005447 bool isShift = getSubtarget()->hasSSE2() &&
Evan Chengc3630942009-12-09 21:00:30 +00005448 isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00005449 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00005450 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00005451 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00005452 EVT EltVT = VT.getVectorElementType();
5453 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00005454 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005455 }
Eric Christopherfd179292009-08-27 18:07:15 +00005456
Nate Begeman9008ca62009-04-27 18:41:29 +00005457 if (X86::isMOVLMask(SVOp)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005458 if (V1IsUndef)
5459 return V2;
Gabor Greifba36cb52008-08-28 21:40:38 +00005460 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00005461 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Dale Johannesen0488fb62010-09-30 23:57:10 +00005462 if (!X86::isMOVLPMask(SVOp)) {
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005463 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00005464 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
5465
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005466 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00005467 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
5468 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00005469 }
Eric Christopherfd179292009-08-27 18:07:15 +00005470
Nate Begeman9008ca62009-04-27 18:41:29 +00005471 // FIXME: fold these into legal mask.
Dale Johannesen0488fb62010-09-30 23:57:10 +00005472 if (X86::isMOVLHPSMask(SVOp) && !X86::isUNPCKLMask(SVOp))
5473 return getMOVLowToHigh(Op, dl, DAG, HasSSE2);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005474
Dale Johannesen0488fb62010-09-30 23:57:10 +00005475 if (X86::isMOVHLPSMask(SVOp))
5476 return getMOVHighToLow(Op, dl, DAG);
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00005477
Dale Johannesen0488fb62010-09-30 23:57:10 +00005478 if (X86::isMOVSHDUPMask(SVOp) && HasSSE3 && V2IsUndef && NumElems == 4)
5479 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00005480
Dale Johannesen0488fb62010-09-30 23:57:10 +00005481 if (X86::isMOVSLDUPMask(SVOp) && HasSSE3 && V2IsUndef && NumElems == 4)
5482 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00005483
Dale Johannesen0488fb62010-09-30 23:57:10 +00005484 if (X86::isMOVLPMask(SVOp))
5485 return getMOVLP(Op, dl, DAG, HasSSE2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005486
Nate Begeman9008ca62009-04-27 18:41:29 +00005487 if (ShouldXformToMOVHLPS(SVOp) ||
5488 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
5489 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005490
Evan Chengf26ffe92008-05-29 08:22:04 +00005491 if (isShift) {
5492 // No better options. Use a vshl / vsrl.
Dan Gohman8a55ce42009-09-23 21:02:20 +00005493 EVT EltVT = VT.getVectorElementType();
5494 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00005495 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005496 }
Eric Christopherfd179292009-08-27 18:07:15 +00005497
Evan Cheng9eca5e82006-10-25 21:49:50 +00005498 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00005499 // FIXME: This should also accept a bitcast of a splat? Be careful, not
5500 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00005501 V1IsSplat = isSplatVector(V1.getNode());
5502 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00005503
Chris Lattner8a594482007-11-25 00:24:49 +00005504 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00005505 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005506 Op = CommuteVectorShuffle(SVOp, DAG);
5507 SVOp = cast<ShuffleVectorSDNode>(Op);
5508 V1 = SVOp->getOperand(0);
5509 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00005510 std::swap(V1IsSplat, V2IsSplat);
5511 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00005512 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00005513 }
5514
Nate Begeman9008ca62009-04-27 18:41:29 +00005515 if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) {
5516 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00005517 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00005518 return V1;
5519 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
5520 // the instruction selector will not match, so get a canonical MOVL with
5521 // swapped operands to undo the commute.
5522 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00005523 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005524
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005525 if (X86::isUNPCKLMask(SVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005526 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005527
5528 if (X86::isUNPCKHMask(SVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005529 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V2, DAG);
Evan Chenge1113032006-10-04 18:33:38 +00005530
Evan Cheng9bbbb982006-10-25 20:48:19 +00005531 if (V2IsSplat) {
5532 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005533 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00005534 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00005535 SDValue NewMask = NormalizeMask(SVOp, DAG);
5536 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
5537 if (NSVOp != SVOp) {
5538 if (X86::isUNPCKLMask(NSVOp, true)) {
5539 return NewMask;
5540 } else if (X86::isUNPCKHMask(NSVOp, true)) {
5541 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005542 }
5543 }
5544 }
5545
Evan Cheng9eca5e82006-10-25 21:49:50 +00005546 if (Commuted) {
5547 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00005548 // FIXME: this seems wrong.
5549 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
5550 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005551
5552 if (X86::isUNPCKLMask(NewSVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005553 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V2, V1, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005554
5555 if (X86::isUNPCKHMask(NewSVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005556 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V2, V1, DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00005557 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005558
Nate Begeman9008ca62009-04-27 18:41:29 +00005559 // Normalize the node to match x86 shuffle ops if needed
Dale Johannesen0488fb62010-09-30 23:57:10 +00005560 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp))
Nate Begeman9008ca62009-04-27 18:41:29 +00005561 return CommuteVectorShuffle(SVOp, DAG);
5562
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00005563 // The checks below are all present in isShuffleMaskLegal, but they are
5564 // inlined here right now to enable us to directly emit target specific
5565 // nodes, and remove one by one until they don't return Op anymore.
5566 SmallVector<int, 16> M;
5567 SVOp->getMask(M);
5568
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00005569 if (isPALIGNRMask(M, VT, HasSSSE3))
5570 return getTargetShuffleNode(X86ISD::PALIGN, dl, VT, V1, V2,
5571 X86::getShufflePALIGNRImmediate(SVOp),
5572 DAG);
5573
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00005574 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
5575 SVOp->getSplatIndex() == 0 && V2IsUndef) {
5576 if (VT == MVT::v2f64)
5577 return getTargetShuffleNode(X86ISD::UNPCKLPD, dl, VT, V1, V1, DAG);
5578 if (VT == MVT::v2i64)
5579 return getTargetShuffleNode(X86ISD::PUNPCKLQDQ, dl, VT, V1, V1, DAG);
5580 }
5581
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00005582 if (isPSHUFHWMask(M, VT))
5583 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
5584 X86::getShufflePSHUFHWImmediate(SVOp),
5585 DAG);
5586
5587 if (isPSHUFLWMask(M, VT))
5588 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
5589 X86::getShufflePSHUFLWImmediate(SVOp),
5590 DAG);
5591
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00005592 if (isSHUFPMask(M, VT)) {
5593 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
5594 if (VT == MVT::v4f32 || VT == MVT::v4i32)
5595 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V1, V2,
5596 TargetMask, DAG);
5597 if (VT == MVT::v2f64 || VT == MVT::v2i64)
5598 return getTargetShuffleNode(X86ISD::SHUFPD, dl, VT, V1, V2,
5599 TargetMask, DAG);
5600 }
5601
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005602 if (X86::isUNPCKL_v_undef_Mask(SVOp))
5603 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5604 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V1, DAG);
5605 if (X86::isUNPCKH_v_undef_Mask(SVOp))
5606 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5607 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
5608
Evan Cheng14b32e12007-12-11 01:46:18 +00005609 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
Owen Anderson825b72b2009-08-11 20:47:22 +00005610 if (VT == MVT::v8i16) {
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005611 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00005612 if (NewOp.getNode())
Evan Cheng14b32e12007-12-11 01:46:18 +00005613 return NewOp;
5614 }
5615
Owen Anderson825b72b2009-08-11 20:47:22 +00005616 if (VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005617 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005618 if (NewOp.getNode())
5619 return NewOp;
5620 }
Eric Christopherfd179292009-08-27 18:07:15 +00005621
Dale Johannesen0488fb62010-09-30 23:57:10 +00005622 // Handle all 4 wide cases with a number of shuffles.
5623 if (NumElems == 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00005624 return LowerVECTOR_SHUFFLE_4wide(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005625
Dan Gohman475871a2008-07-27 21:46:04 +00005626 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005627}
5628
Dan Gohman475871a2008-07-27 21:46:04 +00005629SDValue
5630X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00005631 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005632 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005633 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005634 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005635 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005636 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00005637 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005638 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00005639 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005640 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00005641 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
5642 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
5643 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005644 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
5645 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005646 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005647 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00005648 Op.getOperand(0)),
5649 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00005650 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005651 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00005652 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005653 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00005654 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Owen Anderson825b72b2009-08-11 20:47:22 +00005655 } else if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00005656 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
5657 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00005658 // result has a single use which is a store or a bitcast to i32. And in
5659 // the case of a store, it's not worth it if the index is a constant 0,
5660 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00005661 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00005662 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00005663 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00005664 if ((User->getOpcode() != ISD::STORE ||
5665 (isa<ConstantSDNode>(Op.getOperand(1)) &&
5666 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005667 (User->getOpcode() != ISD::BITCAST ||
Owen Anderson825b72b2009-08-11 20:47:22 +00005668 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00005669 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00005670 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005671 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00005672 Op.getOperand(0)),
5673 Op.getOperand(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005674 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Extract);
Owen Anderson825b72b2009-08-11 20:47:22 +00005675 } else if (VT == MVT::i32) {
Mon P Wangf0fcdd82009-01-15 21:10:20 +00005676 // ExtractPS works with constant index.
5677 if (isa<ConstantSDNode>(Op.getOperand(1)))
5678 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00005679 }
Dan Gohman475871a2008-07-27 21:46:04 +00005680 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00005681}
5682
5683
Dan Gohman475871a2008-07-27 21:46:04 +00005684SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005685X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
5686 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005687 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00005688 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005689
Evan Cheng62a3f152008-03-24 21:52:23 +00005690 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005691 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00005692 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00005693 return Res;
5694 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00005695
Owen Andersone50ed302009-08-10 22:56:29 +00005696 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005697 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005698 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005699 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00005700 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005701 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00005702 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005703 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
5704 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005705 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005706 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00005707 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005708 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00005709 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00005710 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00005711 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00005712 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00005713 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00005714 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005715 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005716 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005717 if (Idx == 0)
5718 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00005719
Evan Cheng0db9fe62006-04-25 20:13:52 +00005720 // SHUFPS the element to the lowest double word, then movss.
Nate Begeman9008ca62009-04-27 18:41:29 +00005721 int Mask[4] = { Idx, -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00005722 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00005723 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00005724 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00005725 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00005726 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00005727 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00005728 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
5729 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
5730 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005731 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005732 if (Idx == 0)
5733 return Op;
5734
5735 // UNPCKHPD the element to the lowest double word, then movsd.
5736 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
5737 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00005738 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00005739 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00005740 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00005741 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00005742 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00005743 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005744 }
5745
Dan Gohman475871a2008-07-27 21:46:04 +00005746 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005747}
5748
Dan Gohman475871a2008-07-27 21:46:04 +00005749SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005750X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op,
5751 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005752 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00005753 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005754 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00005755
Dan Gohman475871a2008-07-27 21:46:04 +00005756 SDValue N0 = Op.getOperand(0);
5757 SDValue N1 = Op.getOperand(1);
5758 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00005759
Dan Gohman8a55ce42009-09-23 21:02:20 +00005760 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00005761 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00005762 unsigned Opc;
5763 if (VT == MVT::v8i16)
5764 Opc = X86ISD::PINSRW;
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00005765 else if (VT == MVT::v16i8)
5766 Opc = X86ISD::PINSRB;
5767 else
5768 Opc = X86ISD::PINSRB;
5769
Nate Begeman14d12ca2008-02-11 04:19:36 +00005770 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
5771 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00005772 if (N1.getValueType() != MVT::i32)
5773 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
5774 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005775 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00005776 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00005777 } else if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00005778 // Bits [7:6] of the constant are the source select. This will always be
5779 // zero here. The DAG Combiner may combine an extract_elt index into these
5780 // bits. For example (insert (extract, 3), 2) could be matched by putting
5781 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00005782 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00005783 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00005784 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00005785 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005786 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00005787 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00005788 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00005789 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00005790 } else if (EltVT == MVT::i32 && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00005791 // PINSR* works with constant index.
5792 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00005793 }
Dan Gohman475871a2008-07-27 21:46:04 +00005794 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00005795}
5796
Dan Gohman475871a2008-07-27 21:46:04 +00005797SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005798X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005799 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00005800 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00005801
5802 if (Subtarget->hasSSE41())
5803 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
5804
Dan Gohman8a55ce42009-09-23 21:02:20 +00005805 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00005806 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00005807
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005808 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00005809 SDValue N0 = Op.getOperand(0);
5810 SDValue N1 = Op.getOperand(1);
5811 SDValue N2 = Op.getOperand(2);
Evan Cheng794405e2007-12-12 07:55:34 +00005812
Dan Gohman8a55ce42009-09-23 21:02:20 +00005813 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00005814 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
5815 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00005816 if (N1.getValueType() != MVT::i32)
5817 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
5818 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005819 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesen0488fb62010-09-30 23:57:10 +00005820 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005821 }
Dan Gohman475871a2008-07-27 21:46:04 +00005822 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005823}
5824
Dan Gohman475871a2008-07-27 21:46:04 +00005825SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005826X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005827 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00005828
Chris Lattnerf172ecd2010-07-04 23:07:25 +00005829 if (Op.getValueType() == MVT::v1i64 &&
5830 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00005831 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00005832
Owen Anderson825b72b2009-08-11 20:47:22 +00005833 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Dale Johannesen0488fb62010-09-30 23:57:10 +00005834 assert(Op.getValueType().getSimpleVT().getSizeInBits() == 128 &&
5835 "Expected an SSE type!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005836 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(),
Dale Johannesen0488fb62010-09-30 23:57:10 +00005837 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005838}
5839
Bill Wendling056292f2008-09-16 21:48:12 +00005840// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
5841// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
5842// one of the above mentioned nodes. It has to be wrapped because otherwise
5843// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
5844// be used to form addressing mode. These wrapped nodes will be selected
5845// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00005846SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005847X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005848 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00005849
Chris Lattner41621a22009-06-26 19:22:52 +00005850 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
5851 // global base reg.
5852 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00005853 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005854 CodeModel::Model M = getTargetMachine().getCodeModel();
5855
Chris Lattner4f066492009-07-11 20:29:19 +00005856 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005857 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00005858 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00005859 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005860 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00005861 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005862 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00005863
Evan Cheng1606e8e2009-03-13 07:51:59 +00005864 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00005865 CP->getAlignment(),
5866 CP->getOffset(), OpFlag);
5867 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00005868 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00005869 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00005870 if (OpFlag) {
5871 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00005872 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00005873 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00005874 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005875 }
5876
5877 return Result;
5878}
5879
Dan Gohmand858e902010-04-17 15:26:15 +00005880SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00005881 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00005882
Chris Lattner18c59872009-06-27 04:16:01 +00005883 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
5884 // global base reg.
5885 unsigned char OpFlag = 0;
5886 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005887 CodeModel::Model M = getTargetMachine().getCodeModel();
5888
Chris Lattner4f066492009-07-11 20:29:19 +00005889 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005890 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00005891 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00005892 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005893 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00005894 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005895 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00005896
Chris Lattner18c59872009-06-27 04:16:01 +00005897 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
5898 OpFlag);
5899 DebugLoc DL = JT->getDebugLoc();
5900 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00005901
Chris Lattner18c59872009-06-27 04:16:01 +00005902 // With PIC, the address is actually $g + Offset.
Chris Lattner1e61e692010-11-15 02:46:57 +00005903 if (OpFlag)
Chris Lattner18c59872009-06-27 04:16:01 +00005904 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
5905 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00005906 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00005907 Result);
Eric Christopherfd179292009-08-27 18:07:15 +00005908
Chris Lattner18c59872009-06-27 04:16:01 +00005909 return Result;
5910}
5911
5912SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005913X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00005914 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00005915
Chris Lattner18c59872009-06-27 04:16:01 +00005916 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
5917 // global base reg.
5918 unsigned char OpFlag = 0;
5919 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005920 CodeModel::Model M = getTargetMachine().getCodeModel();
5921
Chris Lattner4f066492009-07-11 20:29:19 +00005922 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005923 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00005924 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00005925 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005926 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00005927 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005928 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00005929
Chris Lattner18c59872009-06-27 04:16:01 +00005930 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00005931
Chris Lattner18c59872009-06-27 04:16:01 +00005932 DebugLoc DL = Op.getDebugLoc();
5933 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00005934
5935
Chris Lattner18c59872009-06-27 04:16:01 +00005936 // With PIC, the address is actually $g + Offset.
5937 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00005938 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00005939 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
5940 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00005941 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00005942 Result);
5943 }
Eric Christopherfd179292009-08-27 18:07:15 +00005944
Chris Lattner18c59872009-06-27 04:16:01 +00005945 return Result;
5946}
5947
Dan Gohman475871a2008-07-27 21:46:04 +00005948SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005949X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00005950 // Create the TargetBlockAddressAddress node.
5951 unsigned char OpFlags =
5952 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00005953 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00005954 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Dan Gohman29cbade2009-11-20 23:18:13 +00005955 DebugLoc dl = Op.getDebugLoc();
5956 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(),
5957 /*isTarget=*/true, OpFlags);
5958
Dan Gohmanf705adb2009-10-30 01:28:02 +00005959 if (Subtarget->isPICStyleRIPRel() &&
5960 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00005961 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
5962 else
5963 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00005964
Dan Gohman29cbade2009-11-20 23:18:13 +00005965 // With PIC, the address is actually $g + Offset.
5966 if (isGlobalRelativeToPICBase(OpFlags)) {
5967 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
5968 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
5969 Result);
5970 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00005971
5972 return Result;
5973}
5974
5975SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00005976X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00005977 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00005978 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00005979 // Create the TargetGlobalAddress node, folding in the constant
5980 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00005981 unsigned char OpFlags =
5982 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005983 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00005984 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005985 if (OpFlags == X86II::MO_NO_FLAG &&
5986 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00005987 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00005988 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00005989 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00005990 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00005991 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00005992 }
Eric Christopherfd179292009-08-27 18:07:15 +00005993
Chris Lattner4f066492009-07-11 20:29:19 +00005994 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005995 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00005996 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
5997 else
5998 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00005999
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006000 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00006001 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00006002 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
6003 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006004 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006005 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006006
Chris Lattner36c25012009-07-10 07:34:39 +00006007 // For globals that require a load from a stub to get the address, emit the
6008 // load.
6009 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00006010 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00006011 MachinePointerInfo::getGOT(), false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006012
Dan Gohman6520e202008-10-18 02:06:02 +00006013 // If there was a non-zero offset that we didn't fold, create an explicit
6014 // addition for it.
6015 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00006016 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00006017 DAG.getConstant(Offset, getPointerTy()));
6018
Evan Cheng0db9fe62006-04-25 20:13:52 +00006019 return Result;
6020}
6021
Evan Chengda43bcf2008-09-24 00:05:32 +00006022SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006023X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00006024 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00006025 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006026 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00006027}
6028
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006029static SDValue
6030GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00006031 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Chris Lattnerb903bed2009-06-26 21:20:29 +00006032 unsigned char OperandFlags) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006033 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Owen Anderson825b72b2009-08-11 20:47:22 +00006034 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006035 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00006036 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006037 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00006038 GA->getOffset(),
6039 OperandFlags);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006040 if (InFlag) {
6041 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00006042 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006043 } else {
6044 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00006045 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006046 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006047
6048 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00006049 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006050
Rafael Espindola15f1b662009-04-24 12:59:40 +00006051 SDValue Flag = Chain.getValue(1);
6052 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006053}
6054
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006055// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00006056static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006057LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006058 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00006059 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00006060 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
6061 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006062 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006063 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006064 InFlag = Chain.getValue(1);
6065
Chris Lattnerb903bed2009-06-26 21:20:29 +00006066 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006067}
6068
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006069// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00006070static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006071LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006072 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00006073 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
6074 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006075}
6076
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006077// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
6078// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00006079static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006080 const EVT PtrVT, TLSModel::Model model,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00006081 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00006082 DebugLoc dl = GA->getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00006083
Chris Lattnerf93b90c2010-09-22 04:39:11 +00006084 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
6085 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
6086 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00006087
Michael J. Spencerec38de22010-10-10 22:04:20 +00006088 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Chris Lattnerf93b90c2010-09-22 04:39:11 +00006089 DAG.getIntPtrConstant(0),
6090 MachinePointerInfo(Ptr), false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00006091
Chris Lattnerb903bed2009-06-26 21:20:29 +00006092 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00006093 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
6094 // initialexec.
6095 unsigned WrapperKind = X86ISD::Wrapper;
6096 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00006097 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Chris Lattner18c59872009-06-27 04:16:01 +00006098 } else if (is64Bit) {
6099 assert(model == TLSModel::InitialExec);
6100 OperandFlags = X86II::MO_GOTTPOFF;
6101 WrapperKind = X86ISD::WrapperRIP;
6102 } else {
6103 assert(model == TLSModel::InitialExec);
6104 OperandFlags = X86II::MO_INDNTPOFF;
Chris Lattnerb903bed2009-06-26 21:20:29 +00006105 }
Eric Christopherfd179292009-08-27 18:07:15 +00006106
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006107 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
6108 // exec)
Michael J. Spencerec38de22010-10-10 22:04:20 +00006109 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Devang Patel0d881da2010-07-06 22:08:15 +00006110 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00006111 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00006112 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00006113
Rafael Espindola9a580232009-02-27 13:37:18 +00006114 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00006115 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00006116 MachinePointerInfo::getGOT(), false, false, 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00006117
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006118 // The address of the thread local variable is the add of the thread
6119 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00006120 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006121}
6122
Dan Gohman475871a2008-07-27 21:46:04 +00006123SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006124X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00006125
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006126 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00006127 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00006128
Eric Christopher30ef0e52010-06-03 04:07:48 +00006129 if (Subtarget->isTargetELF()) {
6130 // TODO: implement the "local dynamic" model
6131 // TODO: implement the "initial exec"model for pic executables
Michael J. Spencerec38de22010-10-10 22:04:20 +00006132
Eric Christopher30ef0e52010-06-03 04:07:48 +00006133 // If GV is an alias then use the aliasee for determining
6134 // thread-localness.
6135 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
6136 GV = GA->resolveAliasedGlobal(false);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006137
6138 TLSModel::Model model
Eric Christopher30ef0e52010-06-03 04:07:48 +00006139 = getTLSModel(GV, getTargetMachine().getRelocationModel());
Michael J. Spencerec38de22010-10-10 22:04:20 +00006140
Eric Christopher30ef0e52010-06-03 04:07:48 +00006141 switch (model) {
6142 case TLSModel::GeneralDynamic:
6143 case TLSModel::LocalDynamic: // not implemented
6144 if (Subtarget->is64Bit())
6145 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
6146 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Michael J. Spencerec38de22010-10-10 22:04:20 +00006147
Eric Christopher30ef0e52010-06-03 04:07:48 +00006148 case TLSModel::InitialExec:
6149 case TLSModel::LocalExec:
6150 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
6151 Subtarget->is64Bit());
6152 }
6153 } else if (Subtarget->isTargetDarwin()) {
6154 // Darwin only has one model of TLS. Lower to that.
6155 unsigned char OpFlag = 0;
6156 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
6157 X86ISD::WrapperRIP : X86ISD::Wrapper;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006158
Eric Christopher30ef0e52010-06-03 04:07:48 +00006159 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6160 // global base reg.
6161 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
6162 !Subtarget->is64Bit();
6163 if (PIC32)
6164 OpFlag = X86II::MO_TLVP_PIC_BASE;
6165 else
6166 OpFlag = X86II::MO_TLVP;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006167 DebugLoc DL = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00006168 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopherd8c05362010-12-09 06:25:53 +00006169 GA->getValueType(0),
Eric Christopher30ef0e52010-06-03 04:07:48 +00006170 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00006171 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006172
Eric Christopher30ef0e52010-06-03 04:07:48 +00006173 // With PIC32, the address is actually $g + Offset.
6174 if (PIC32)
6175 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
6176 DAG.getNode(X86ISD::GlobalBaseReg,
6177 DebugLoc(), getPointerTy()),
6178 Offset);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006179
Eric Christopher30ef0e52010-06-03 04:07:48 +00006180 // Lowering the machine isd will make sure everything is in the right
6181 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00006182 SDValue Chain = DAG.getEntryNode();
6183 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
6184 SDValue Args[] = { Chain, Offset };
6185 Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006186
Eric Christopher30ef0e52010-06-03 04:07:48 +00006187 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
6188 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
6189 MFI->setAdjustsStack(true);
Eric Christopher8bce7cc2010-12-09 00:27:58 +00006190
Eric Christopher30ef0e52010-06-03 04:07:48 +00006191 // And our return value (tls address) is in the standard call return value
6192 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00006193 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
6194 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy());
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006195 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00006196
Eric Christopher30ef0e52010-06-03 04:07:48 +00006197 assert(false &&
6198 "TLS not implemented for this target.");
Eric Christopherfd179292009-08-27 18:07:15 +00006199
Torok Edwinc23197a2009-07-14 16:55:14 +00006200 llvm_unreachable("Unreachable");
Chris Lattner5867de12009-04-01 22:14:45 +00006201 return SDValue();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006202}
6203
Evan Cheng0db9fe62006-04-25 20:13:52 +00006204
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006205/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michelfdc40a02009-02-17 22:15:04 +00006206/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00006207SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman4c1fa612008-03-03 22:22:09 +00006208 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00006209 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006210 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006211 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006212 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00006213 SDValue ShOpLo = Op.getOperand(0);
6214 SDValue ShOpHi = Op.getOperand(1);
6215 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00006216 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00006217 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00006218 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00006219
Dan Gohman475871a2008-07-27 21:46:04 +00006220 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006221 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00006222 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
6223 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006224 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00006225 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
6226 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006227 }
Evan Chenge3413162006-01-09 18:33:28 +00006228
Owen Anderson825b72b2009-08-11 20:47:22 +00006229 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
6230 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00006231 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00006232 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00006233
Dan Gohman475871a2008-07-27 21:46:04 +00006234 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00006235 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00006236 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
6237 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00006238
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006239 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00006240 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
6241 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006242 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00006243 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
6244 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006245 }
6246
Dan Gohman475871a2008-07-27 21:46:04 +00006247 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00006248 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006249}
Evan Chenga3195e82006-01-12 22:54:21 +00006250
Dan Gohmand858e902010-04-17 15:26:15 +00006251SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
6252 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006253 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00006254
Dale Johannesen0488fb62010-09-30 23:57:10 +00006255 if (SrcVT.isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00006256 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00006257
Owen Anderson825b72b2009-08-11 20:47:22 +00006258 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00006259 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00006260
Eli Friedman36df4992009-05-27 00:47:34 +00006261 // These are really Legal; return the operand so the caller accepts it as
6262 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00006263 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00006264 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00006265 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00006266 Subtarget->is64Bit()) {
6267 return Op;
6268 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006269
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006270 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006271 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006272 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00006273 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006274 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00006275 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00006276 StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006277 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006278 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00006279 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
6280}
Evan Cheng0db9fe62006-04-25 20:13:52 +00006281
Owen Andersone50ed302009-08-10 22:56:29 +00006282SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Michael J. Spencerec38de22010-10-10 22:04:20 +00006283 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00006284 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006285 // Build the FILD
Chris Lattner492a43e2010-09-22 01:28:21 +00006286 DebugLoc DL = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00006287 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00006288 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006289 if (useSSE)
Owen Anderson825b72b2009-08-11 20:47:22 +00006290 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
Chris Lattner5a88b832007-02-25 07:10:00 +00006291 else
Owen Anderson825b72b2009-08-11 20:47:22 +00006292 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006293
Chris Lattner492a43e2010-09-22 01:28:21 +00006294 unsigned ByteSize = SrcVT.getSizeInBits()/8;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006295
Chris Lattner492a43e2010-09-22 01:28:21 +00006296 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
6297 MachineMemOperand *MMO =
6298 DAG.getMachineFunction()
6299 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6300 MachineMemOperand::MOLoad, ByteSize, ByteSize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006301
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006302 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Chris Lattner492a43e2010-09-22 01:28:21 +00006303 SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
6304 X86ISD::FILD, DL,
6305 Tys, Ops, array_lengthof(Ops),
6306 SrcVT, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006307
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006308 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006309 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00006310 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006311
6312 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
6313 // shouldn't be necessary except that RFP cannot be live across
6314 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006315 MachineFunction &MF = DAG.getMachineFunction();
Bob Wilsoneafca4e2010-09-22 17:35:14 +00006316 unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
6317 int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006318 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00006319 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006320 SDValue Ops[] = {
6321 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
6322 };
Chris Lattner492a43e2010-09-22 01:28:21 +00006323 MachineMemOperand *MMO =
6324 DAG.getMachineFunction()
6325 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
Bob Wilsoneafca4e2010-09-22 17:35:14 +00006326 MachineMemOperand::MOStore, SSFISize, SSFISize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006327
Chris Lattner492a43e2010-09-22 01:28:21 +00006328 Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
6329 Ops, array_lengthof(Ops),
6330 Op.getValueType(), MMO);
6331 Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006332 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006333 false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006334 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006335
Evan Cheng0db9fe62006-04-25 20:13:52 +00006336 return Result;
6337}
6338
Bill Wendling8b8a6362009-01-17 03:56:04 +00006339// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00006340SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
6341 SelectionDAG &DAG) const {
Bill Wendling8b8a6362009-01-17 03:56:04 +00006342 // This algorithm is not obvious. Here it is in C code, more or less:
6343 /*
6344 double uint64_to_double( uint32_t hi, uint32_t lo ) {
6345 static const __m128i exp = { 0x4330000045300000ULL, 0 };
6346 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00006347
Bill Wendling8b8a6362009-01-17 03:56:04 +00006348 // Copy ints to xmm registers.
6349 __m128i xh = _mm_cvtsi32_si128( hi );
6350 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00006351
Bill Wendling8b8a6362009-01-17 03:56:04 +00006352 // Combine into low half of a single xmm register.
6353 __m128i x = _mm_unpacklo_epi32( xh, xl );
6354 __m128d d;
6355 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00006356
Bill Wendling8b8a6362009-01-17 03:56:04 +00006357 // Merge in appropriate exponents to give the integer bits the right
6358 // magnitude.
6359 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00006360
Bill Wendling8b8a6362009-01-17 03:56:04 +00006361 // Subtract away the biases to deal with the IEEE-754 double precision
6362 // implicit 1.
6363 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00006364
Bill Wendling8b8a6362009-01-17 03:56:04 +00006365 // All conversions up to here are exact. The correctly rounded result is
6366 // calculated using the current rounding mode using the following
6367 // horizontal add.
6368 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
6369 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
6370 // store doesn't really need to be here (except
6371 // maybe to zero the other double)
6372 return sd;
6373 }
6374 */
Dale Johannesen040225f2008-10-21 23:07:49 +00006375
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006376 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00006377 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00006378
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006379 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00006380 std::vector<Constant*> CV0;
Owen Andersoneed707b2009-07-24 23:12:02 +00006381 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x45300000)));
6382 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x43300000)));
6383 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
6384 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
Owen Andersonaf7ec972009-07-28 21:19:26 +00006385 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006386 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006387
Bill Wendling8b8a6362009-01-17 03:56:04 +00006388 std::vector<Constant*> CV1;
Owen Andersona90b3dc2009-07-15 21:51:10 +00006389 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006390 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
Owen Andersona90b3dc2009-07-15 21:51:10 +00006391 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006392 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Owen Andersonaf7ec972009-07-28 21:19:26 +00006393 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006394 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006395
Owen Anderson825b72b2009-08-11 20:47:22 +00006396 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6397 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00006398 Op.getOperand(0),
6399 DAG.getIntPtrConstant(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006400 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6401 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00006402 Op.getOperand(0),
6403 DAG.getIntPtrConstant(0)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006404 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
6405 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Chris Lattnere8639032010-09-21 06:22:23 +00006406 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006407 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00006408 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006409 SDValue XR2F = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Unpck2);
Owen Anderson825b72b2009-08-11 20:47:22 +00006410 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00006411 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006412 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00006413 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006414
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006415 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman9008ca62009-04-27 18:41:29 +00006416 int ShufMask[2] = { 1, -1 };
Owen Anderson825b72b2009-08-11 20:47:22 +00006417 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
6418 DAG.getUNDEF(MVT::v2f64), ShufMask);
6419 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
6420 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006421 DAG.getIntPtrConstant(0));
6422}
6423
Bill Wendling8b8a6362009-01-17 03:56:04 +00006424// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00006425SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
6426 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006427 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00006428 // FP constant to bias correct the final result.
6429 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00006430 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006431
6432 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00006433 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6434 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Bill Wendling8b8a6362009-01-17 03:56:04 +00006435 Op.getOperand(0),
6436 DAG.getIntPtrConstant(0)));
6437
Owen Anderson825b72b2009-08-11 20:47:22 +00006438 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006439 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00006440 DAG.getIntPtrConstant(0));
6441
6442 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00006443 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
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, Load)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006447 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00006448 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006449 MVT::v2f64, Bias)));
6450 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006451 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00006452 DAG.getIntPtrConstant(0));
6453
6454 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00006455 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006456
6457 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00006458 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00006459
Owen Anderson825b72b2009-08-11 20:47:22 +00006460 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006461 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00006462 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00006463 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006464 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00006465 }
6466
6467 // Handle final rounding.
6468 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00006469}
6470
Dan Gohmand858e902010-04-17 15:26:15 +00006471SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
6472 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00006473 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006474 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00006475
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006476 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00006477 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
6478 // the optimization here.
6479 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00006480 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00006481
Owen Andersone50ed302009-08-10 22:56:29 +00006482 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006483 EVT DstVT = Op.getValueType();
6484 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00006485 return LowerUINT_TO_FP_i64(Op, DAG);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006486 else if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00006487 return LowerUINT_TO_FP_i32(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00006488
6489 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00006490 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006491 if (SrcVT == MVT::i32) {
6492 SDValue WordOff = DAG.getConstant(4, getPointerTy());
6493 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
6494 getPointerTy(), StackSlot, WordOff);
6495 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006496 StackSlot, MachinePointerInfo(),
6497 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006498 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006499 OffsetSlot, MachinePointerInfo(),
6500 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006501 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
6502 return Fild;
6503 }
6504
6505 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
6506 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006507 StackSlot, MachinePointerInfo(),
6508 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006509 // For i64 source, we need to add the appropriate power of 2 if the input
6510 // was negative. This is the same as the optimization in
6511 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
6512 // we must be careful to do the computation in x87 extended precision, not
6513 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00006514 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
6515 MachineMemOperand *MMO =
6516 DAG.getMachineFunction()
6517 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6518 MachineMemOperand::MOLoad, 8, 8);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006519
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006520 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
6521 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Chris Lattner492a43e2010-09-22 01:28:21 +00006522 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, 3,
6523 MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006524
6525 APInt FF(32, 0x5F800000ULL);
6526
6527 // Check whether the sign bit is set.
6528 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
6529 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
6530 ISD::SETLT);
6531
6532 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
6533 SDValue FudgePtr = DAG.getConstantPool(
6534 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
6535 getPointerTy());
6536
6537 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
6538 SDValue Zero = DAG.getIntPtrConstant(0);
6539 SDValue Four = DAG.getIntPtrConstant(4);
6540 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
6541 Zero, Four);
6542 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
6543
6544 // Load the value out, extending it from f32 to f80.
6545 // FIXME: Avoid the extend by constructing the right constant pool?
Evan Chengbcc80172010-07-07 22:15:37 +00006546 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, MVT::f80, dl, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00006547 FudgePtr, MachinePointerInfo::getConstantPool(),
6548 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006549 // Extend everything to 80 bits to force it to be done on x87.
6550 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
6551 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00006552}
6553
Dan Gohman475871a2008-07-27 21:46:04 +00006554std::pair<SDValue,SDValue> X86TargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00006555FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) const {
Chris Lattner07290932010-09-22 01:05:16 +00006556 DebugLoc DL = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00006557
Owen Andersone50ed302009-08-10 22:56:29 +00006558 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00006559
6560 if (!IsSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006561 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
6562 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00006563 }
6564
Owen Anderson825b72b2009-08-11 20:47:22 +00006565 assert(DstTy.getSimpleVT() <= MVT::i64 &&
6566 DstTy.getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00006567 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00006568
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006569 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00006570 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00006571 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00006572 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00006573 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00006574 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00006575 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00006576 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006577
Evan Cheng87c89352007-10-15 20:11:21 +00006578 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
6579 // stack slot.
6580 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00006581 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00006582 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006583 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00006584
Michael J. Spencerec38de22010-10-10 22:04:20 +00006585
6586
Evan Cheng0db9fe62006-04-25 20:13:52 +00006587 unsigned Opc;
Owen Anderson825b72b2009-08-11 20:47:22 +00006588 switch (DstTy.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00006589 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
Owen Anderson825b72b2009-08-11 20:47:22 +00006590 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
6591 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
6592 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006593 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006594
Dan Gohman475871a2008-07-27 21:46:04 +00006595 SDValue Chain = DAG.getEntryNode();
6596 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00006597 EVT TheVT = Op.getOperand(0).getValueType();
6598 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006599 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00006600 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006601 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006602 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00006603 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00006604 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00006605 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00006606 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00006607
Chris Lattner492a43e2010-09-22 01:28:21 +00006608 MachineMemOperand *MMO =
6609 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6610 MachineMemOperand::MOLoad, MemSize, MemSize);
6611 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, 3,
6612 DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006613 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00006614 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006615 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
6616 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00006617
Chris Lattner07290932010-09-22 01:05:16 +00006618 MachineMemOperand *MMO =
6619 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6620 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006621
Evan Cheng0db9fe62006-04-25 20:13:52 +00006622 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00006623 SDValue Ops[] = { Chain, Value, StackSlot };
Chris Lattner07290932010-09-22 01:05:16 +00006624 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
6625 Ops, 3, DstTy, MMO);
Evan Chengd9558e02006-01-06 00:43:03 +00006626
Chris Lattner27a6c732007-11-24 07:07:01 +00006627 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006628}
6629
Dan Gohmand858e902010-04-17 15:26:15 +00006630SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
6631 SelectionDAG &DAG) const {
Dale Johannesen0488fb62010-09-30 23:57:10 +00006632 if (Op.getValueType().isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00006633 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00006634
Eli Friedman948e95a2009-05-23 09:59:16 +00006635 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00006636 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00006637 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
6638 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00006639
Chris Lattner27a6c732007-11-24 07:07:01 +00006640 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006641 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00006642 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00006643}
6644
Dan Gohmand858e902010-04-17 15:26:15 +00006645SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
6646 SelectionDAG &DAG) const {
Eli Friedman948e95a2009-05-23 09:59:16 +00006647 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
6648 SDValue FIST = Vals.first, StackSlot = Vals.second;
6649 assert(FIST.getNode() && "Unexpected failure");
6650
6651 // Load the result.
6652 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00006653 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00006654}
6655
Dan Gohmand858e902010-04-17 15:26:15 +00006656SDValue X86TargetLowering::LowerFABS(SDValue Op,
6657 SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00006658 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006659 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00006660 EVT VT = Op.getValueType();
6661 EVT EltVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00006662 if (VT.isVector())
6663 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006664 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00006665 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006666 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00006667 CV.push_back(C);
6668 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006669 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006670 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00006671 CV.push_back(C);
6672 CV.push_back(C);
6673 CV.push_back(C);
6674 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006675 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00006676 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006677 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006678 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00006679 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006680 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006681 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006682}
6683
Dan Gohmand858e902010-04-17 15:26:15 +00006684SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00006685 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006686 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00006687 EVT VT = Op.getValueType();
6688 EVT EltVT = VT;
Duncan Sandsda9ad382009-09-06 19:29:07 +00006689 if (VT.isVector())
Duncan Sands83ec4b62008-06-06 12:08:01 +00006690 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006691 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00006692 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006693 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00006694 CV.push_back(C);
6695 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006696 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006697 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00006698 CV.push_back(C);
6699 CV.push_back(C);
6700 CV.push_back(C);
6701 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006702 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00006703 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006704 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006705 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00006706 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006707 false, false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006708 if (VT.isVector()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006709 return DAG.getNode(ISD::BITCAST, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00006710 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006711 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00006712 Op.getOperand(0)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006713 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00006714 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00006715 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00006716 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006717}
6718
Dan Gohmand858e902010-04-17 15:26:15 +00006719SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00006720 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00006721 SDValue Op0 = Op.getOperand(0);
6722 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006723 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00006724 EVT VT = Op.getValueType();
6725 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00006726
6727 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00006728 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006729 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00006730 SrcVT = VT;
6731 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00006732 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00006733 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006734 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00006735 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00006736 }
6737
6738 // At this point the operands and the result should have the same
6739 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00006740
Evan Cheng68c47cb2007-01-05 07:55:56 +00006741 // First get the sign bit of second operand.
6742 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00006743 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006744 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
6745 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00006746 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006747 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
6748 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
6749 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
6750 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00006751 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00006752 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006753 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006754 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00006755 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006756 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006757 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00006758
6759 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00006760 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006761 // Op0 is MVT::f32, Op1 is MVT::f64.
6762 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
6763 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
6764 DAG.getConstant(32, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006765 SignBit = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, SignBit);
Owen Anderson825b72b2009-08-11 20:47:22 +00006766 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00006767 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00006768 }
6769
Evan Cheng73d6cf12007-01-05 21:37:56 +00006770 // Clear first operand sign bit.
6771 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00006772 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006773 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
6774 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00006775 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006776 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
6777 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
6778 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
6779 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00006780 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00006781 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006782 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006783 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00006784 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006785 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006786 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00006787
6788 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00006789 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00006790}
6791
Dan Gohman076aee32009-03-04 19:44:21 +00006792/// Emit nodes that will be selected as "test Op0,Op0", or something
6793/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00006794SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00006795 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00006796 DebugLoc dl = Op.getDebugLoc();
6797
Dan Gohman31125812009-03-07 01:58:32 +00006798 // CF and OF aren't always set the way we want. Determine which
6799 // of these we need.
6800 bool NeedCF = false;
6801 bool NeedOF = false;
6802 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006803 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00006804 case X86::COND_A: case X86::COND_AE:
6805 case X86::COND_B: case X86::COND_BE:
6806 NeedCF = true;
6807 break;
6808 case X86::COND_G: case X86::COND_GE:
6809 case X86::COND_L: case X86::COND_LE:
6810 case X86::COND_O: case X86::COND_NO:
6811 NeedOF = true;
6812 break;
Dan Gohman31125812009-03-07 01:58:32 +00006813 }
6814
Dan Gohman076aee32009-03-04 19:44:21 +00006815 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00006816 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
6817 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006818 if (Op.getResNo() != 0 || NeedOF || NeedCF)
6819 // Emit a CMP with 0, which is the TEST pattern.
6820 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
6821 DAG.getConstant(0, Op.getValueType()));
6822
6823 unsigned Opcode = 0;
6824 unsigned NumOperands = 0;
6825 switch (Op.getNode()->getOpcode()) {
6826 case ISD::ADD:
6827 // Due to an isel shortcoming, be conservative if this add is likely to be
6828 // selected as part of a load-modify-store instruction. When the root node
6829 // in a match is a store, isel doesn't know how to remap non-chain non-flag
6830 // uses of other nodes in the match, such as the ADD in this case. This
6831 // leads to the ADD being left around and reselected, with the result being
6832 // two adds in the output. Alas, even if none our users are stores, that
6833 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
6834 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
6835 // climbing the DAG back to the root, and it doesn't seem to be worth the
6836 // effort.
6837 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Dan Gohman076aee32009-03-04 19:44:21 +00006838 UE = Op.getNode()->use_end(); UI != UE; ++UI)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006839 if (UI->getOpcode() != ISD::CopyToReg && UI->getOpcode() != ISD::SETCC)
6840 goto default_case;
6841
6842 if (ConstantSDNode *C =
6843 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
6844 // An add of one will be selected as an INC.
6845 if (C->getAPIntValue() == 1) {
6846 Opcode = X86ISD::INC;
6847 NumOperands = 1;
6848 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00006849 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006850
6851 // An add of negative one (subtract of one) will be selected as a DEC.
6852 if (C->getAPIntValue().isAllOnesValue()) {
6853 Opcode = X86ISD::DEC;
6854 NumOperands = 1;
6855 break;
6856 }
Dan Gohman076aee32009-03-04 19:44:21 +00006857 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006858
6859 // Otherwise use a regular EFLAGS-setting add.
6860 Opcode = X86ISD::ADD;
6861 NumOperands = 2;
6862 break;
6863 case ISD::AND: {
6864 // If the primary and result isn't used, don't bother using X86ISD::AND,
6865 // because a TEST instruction will be better.
6866 bool NonFlagUse = false;
6867 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
6868 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
6869 SDNode *User = *UI;
6870 unsigned UOpNo = UI.getOperandNo();
6871 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
6872 // Look pass truncate.
6873 UOpNo = User->use_begin().getOperandNo();
6874 User = *User->use_begin();
6875 }
6876
6877 if (User->getOpcode() != ISD::BRCOND &&
6878 User->getOpcode() != ISD::SETCC &&
6879 (User->getOpcode() != ISD::SELECT || UOpNo != 0)) {
6880 NonFlagUse = true;
6881 break;
6882 }
Dan Gohman076aee32009-03-04 19:44:21 +00006883 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006884
6885 if (!NonFlagUse)
6886 break;
6887 }
6888 // FALL THROUGH
6889 case ISD::SUB:
6890 case ISD::OR:
6891 case ISD::XOR:
6892 // Due to the ISEL shortcoming noted above, be conservative if this op is
6893 // likely to be selected as part of a load-modify-store instruction.
6894 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
6895 UE = Op.getNode()->use_end(); UI != UE; ++UI)
6896 if (UI->getOpcode() == ISD::STORE)
6897 goto default_case;
6898
6899 // Otherwise use a regular EFLAGS-setting instruction.
6900 switch (Op.getNode()->getOpcode()) {
6901 default: llvm_unreachable("unexpected operator!");
6902 case ISD::SUB: Opcode = X86ISD::SUB; break;
6903 case ISD::OR: Opcode = X86ISD::OR; break;
6904 case ISD::XOR: Opcode = X86ISD::XOR; break;
6905 case ISD::AND: Opcode = X86ISD::AND; break;
6906 }
6907
6908 NumOperands = 2;
6909 break;
6910 case X86ISD::ADD:
6911 case X86ISD::SUB:
6912 case X86ISD::INC:
6913 case X86ISD::DEC:
6914 case X86ISD::OR:
6915 case X86ISD::XOR:
6916 case X86ISD::AND:
6917 return SDValue(Op.getNode(), 1);
6918 default:
6919 default_case:
6920 break;
Dan Gohman076aee32009-03-04 19:44:21 +00006921 }
6922
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006923 if (Opcode == 0)
6924 // Emit a CMP with 0, which is the TEST pattern.
6925 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
6926 DAG.getConstant(0, Op.getValueType()));
6927
6928 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
6929 SmallVector<SDValue, 4> Ops;
6930 for (unsigned i = 0; i != NumOperands; ++i)
6931 Ops.push_back(Op.getOperand(i));
6932
6933 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
6934 DAG.ReplaceAllUsesWith(Op, New);
6935 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00006936}
6937
6938/// Emit nodes that will be selected as "cmp Op0,Op1", or something
6939/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00006940SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00006941 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00006942 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
6943 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00006944 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00006945
6946 DebugLoc dl = Op0.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00006947 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00006948}
6949
Evan Chengd40d03e2010-01-06 19:38:29 +00006950/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
6951/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00006952SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
6953 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00006954 SDValue Op0 = And.getOperand(0);
6955 SDValue Op1 = And.getOperand(1);
6956 if (Op0.getOpcode() == ISD::TRUNCATE)
6957 Op0 = Op0.getOperand(0);
6958 if (Op1.getOpcode() == ISD::TRUNCATE)
6959 Op1 = Op1.getOperand(0);
6960
Evan Chengd40d03e2010-01-06 19:38:29 +00006961 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00006962 if (Op1.getOpcode() == ISD::SHL)
6963 std::swap(Op0, Op1);
6964 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00006965 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
6966 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00006967 // If we looked past a truncate, check that it's only truncating away
6968 // known zeros.
6969 unsigned BitWidth = Op0.getValueSizeInBits();
6970 unsigned AndBitWidth = And.getValueSizeInBits();
6971 if (BitWidth > AndBitWidth) {
6972 APInt Mask = APInt::getAllOnesValue(BitWidth), Zeros, Ones;
6973 DAG.ComputeMaskedBits(Op0, Mask, Zeros, Ones);
6974 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
6975 return SDValue();
6976 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00006977 LHS = Op1;
6978 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00006979 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00006980 } else if (Op1.getOpcode() == ISD::Constant) {
6981 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
6982 SDValue AndLHS = Op0;
Evan Chengd40d03e2010-01-06 19:38:29 +00006983 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
6984 LHS = AndLHS.getOperand(0);
6985 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00006986 }
Evan Chengd40d03e2010-01-06 19:38:29 +00006987 }
Evan Cheng0488db92007-09-25 01:57:46 +00006988
Evan Chengd40d03e2010-01-06 19:38:29 +00006989 if (LHS.getNode()) {
Evan Chenge5b51ac2010-04-17 06:13:15 +00006990 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00006991 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00006992 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00006993 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00006994 // Also promote i16 to i32 for performance / code size reason.
6995 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00006996 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00006997 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00006998
Evan Chengd40d03e2010-01-06 19:38:29 +00006999 // If the operand types disagree, extend the shift amount to match. Since
7000 // BT ignores high bits (like shifts) we can use anyextend.
7001 if (LHS.getValueType() != RHS.getValueType())
7002 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00007003
Evan Chengd40d03e2010-01-06 19:38:29 +00007004 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
7005 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
7006 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
7007 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00007008 }
7009
Evan Cheng54de3ea2010-01-05 06:52:31 +00007010 return SDValue();
7011}
7012
Dan Gohmand858e902010-04-17 15:26:15 +00007013SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng54de3ea2010-01-05 06:52:31 +00007014 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
7015 SDValue Op0 = Op.getOperand(0);
7016 SDValue Op1 = Op.getOperand(1);
7017 DebugLoc dl = Op.getDebugLoc();
7018 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
7019
7020 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00007021 // Lower (X & (1 << N)) == 0 to BT(X, N).
7022 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
7023 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
7024 if (Op0.getOpcode() == ISD::AND &&
7025 Op0.hasOneUse() &&
7026 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane368b462010-06-18 14:22:04 +00007027 cast<ConstantSDNode>(Op1)->isNullValue() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00007028 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
7029 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
7030 if (NewSetCC.getNode())
7031 return NewSetCC;
7032 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00007033
Chris Lattnerb20e0b12010-12-05 07:30:36 +00007034 // Look for "(setcc) == / != 1" to avoid unnecessary setcc.
Evan Cheng2c755ba2010-02-27 07:36:59 +00007035 if (Op0.getOpcode() == X86ISD::SETCC &&
7036 Op1.getOpcode() == ISD::Constant &&
7037 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
7038 cast<ConstantSDNode>(Op1)->isNullValue()) &&
7039 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
7040 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
7041 bool Invert = (CC == ISD::SETNE) ^
7042 cast<ConstantSDNode>(Op1)->isNullValue();
7043 if (Invert)
7044 CCode = X86::GetOppositeBranchCondition(CCode);
7045 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
7046 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
7047 }
7048
Evan Chenge5b51ac2010-04-17 06:13:15 +00007049 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00007050 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00007051 if (X86CC == X86::COND_INVALID)
7052 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00007053
Evan Cheng552f09a2010-04-26 19:06:11 +00007054 SDValue Cond = EmitCmp(Op0, Op1, X86CC, DAG);
Evan Chengad9c0a32009-12-15 00:53:42 +00007055
7056 // Use sbb x, x to materialize carry bit into a GPR.
Evan Cheng2e489c42009-12-16 00:53:11 +00007057 if (X86CC == X86::COND_B)
Evan Chengad9c0a32009-12-15 00:53:42 +00007058 return DAG.getNode(ISD::AND, dl, MVT::i8,
7059 DAG.getNode(X86ISD::SETCC_CARRY, dl, MVT::i8,
7060 DAG.getConstant(X86CC, MVT::i8), Cond),
7061 DAG.getConstant(1, MVT::i8));
Evan Chengad9c0a32009-12-15 00:53:42 +00007062
Owen Anderson825b72b2009-08-11 20:47:22 +00007063 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
7064 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00007065}
7066
Dan Gohmand858e902010-04-17 15:26:15 +00007067SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00007068 SDValue Cond;
7069 SDValue Op0 = Op.getOperand(0);
7070 SDValue Op1 = Op.getOperand(1);
7071 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00007072 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00007073 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
7074 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007075 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00007076
7077 if (isFP) {
7078 unsigned SSECC = 8;
Owen Andersone50ed302009-08-10 22:56:29 +00007079 EVT VT0 = Op0.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00007080 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
7081 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00007082 bool Swap = false;
7083
7084 switch (SetCCOpcode) {
7085 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00007086 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00007087 case ISD::SETEQ: SSECC = 0; break;
Scott Michelfdc40a02009-02-17 22:15:04 +00007088 case ISD::SETOGT:
Nate Begeman30a0de92008-07-17 16:51:19 +00007089 case ISD::SETGT: Swap = true; // Fallthrough
7090 case ISD::SETLT:
7091 case ISD::SETOLT: SSECC = 1; break;
7092 case ISD::SETOGE:
7093 case ISD::SETGE: Swap = true; // Fallthrough
7094 case ISD::SETLE:
7095 case ISD::SETOLE: SSECC = 2; break;
7096 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00007097 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00007098 case ISD::SETNE: SSECC = 4; break;
7099 case ISD::SETULE: Swap = true;
7100 case ISD::SETUGE: SSECC = 5; break;
7101 case ISD::SETULT: Swap = true;
7102 case ISD::SETUGT: SSECC = 6; break;
7103 case ISD::SETO: SSECC = 7; break;
7104 }
7105 if (Swap)
7106 std::swap(Op0, Op1);
7107
Nate Begemanfb8ead02008-07-25 19:05:58 +00007108 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00007109 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00007110 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00007111 SDValue UNORD, EQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00007112 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
7113 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00007114 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00007115 }
7116 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00007117 SDValue ORD, NEQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00007118 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
7119 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00007120 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00007121 }
Torok Edwinc23197a2009-07-14 16:55:14 +00007122 llvm_unreachable("Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00007123 }
7124 // Handle all other FP comparisons here.
Owen Anderson825b72b2009-08-11 20:47:22 +00007125 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00007126 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007127
Nate Begeman30a0de92008-07-17 16:51:19 +00007128 // We are handling one of the integer comparisons here. Since SSE only has
7129 // GT and EQ comparisons for integer, swapping operands and multiple
7130 // operations may be required for some comparisons.
7131 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
7132 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00007133
Owen Anderson825b72b2009-08-11 20:47:22 +00007134 switch (VT.getSimpleVT().SimpleTy) {
Nate Begeman30a0de92008-07-17 16:51:19 +00007135 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007136 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007137 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007138 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
7139 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00007140 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007141
Nate Begeman30a0de92008-07-17 16:51:19 +00007142 switch (SetCCOpcode) {
7143 default: break;
7144 case ISD::SETNE: Invert = true;
7145 case ISD::SETEQ: Opc = EQOpc; break;
7146 case ISD::SETLT: Swap = true;
7147 case ISD::SETGT: Opc = GTOpc; break;
7148 case ISD::SETGE: Swap = true;
7149 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
7150 case ISD::SETULT: Swap = true;
7151 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
7152 case ISD::SETUGE: Swap = true;
7153 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
7154 }
7155 if (Swap)
7156 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007157
Nate Begeman30a0de92008-07-17 16:51:19 +00007158 // Since SSE has no unsigned integer comparisons, we need to flip the sign
7159 // bits of the inputs before performing those operations.
7160 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00007161 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00007162 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
7163 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00007164 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00007165 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
7166 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00007167 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
7168 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00007169 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007170
Dale Johannesenace16102009-02-03 19:33:06 +00007171 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00007172
7173 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00007174 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00007175 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00007176
Nate Begeman30a0de92008-07-17 16:51:19 +00007177 return Result;
7178}
Evan Cheng0488db92007-09-25 01:57:46 +00007179
Evan Cheng370e5342008-12-03 08:38:43 +00007180// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00007181static bool isX86LogicalCmp(SDValue Op) {
7182 unsigned Opc = Op.getNode()->getOpcode();
7183 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
7184 return true;
7185 if (Op.getResNo() == 1 &&
7186 (Opc == X86ISD::ADD ||
7187 Opc == X86ISD::SUB ||
7188 Opc == X86ISD::SMUL ||
7189 Opc == X86ISD::UMUL ||
7190 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00007191 Opc == X86ISD::DEC ||
7192 Opc == X86ISD::OR ||
7193 Opc == X86ISD::XOR ||
7194 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00007195 return true;
7196
Chris Lattner9637d5b2010-12-05 07:49:54 +00007197 if (Op.getResNo() == 2 && Opc == X86ISD::UMUL)
7198 return true;
7199
Dan Gohman076aee32009-03-04 19:44:21 +00007200 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00007201}
7202
Chris Lattnera2b56002010-12-05 01:23:24 +00007203static bool isZero(SDValue V) {
7204 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
7205 return C && C->isNullValue();
7206}
7207
Chris Lattner96908b12010-12-05 02:00:51 +00007208static bool isAllOnes(SDValue V) {
7209 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
7210 return C && C->isAllOnesValue();
7211}
7212
Dan Gohmand858e902010-04-17 15:26:15 +00007213SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00007214 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00007215 SDValue Cond = Op.getOperand(0);
Chris Lattnera2b56002010-12-05 01:23:24 +00007216 SDValue Op1 = Op.getOperand(1);
7217 SDValue Op2 = Op.getOperand(2);
7218 DebugLoc DL = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00007219 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00007220
Dan Gohman1a492952009-10-20 16:22:37 +00007221 if (Cond.getOpcode() == ISD::SETCC) {
7222 SDValue NewCond = LowerSETCC(Cond, DAG);
7223 if (NewCond.getNode())
7224 Cond = NewCond;
7225 }
Evan Cheng734503b2006-09-11 02:19:56 +00007226
Chris Lattnera2b56002010-12-05 01:23:24 +00007227 // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00007228 // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
Chris Lattnera2b56002010-12-05 01:23:24 +00007229 // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00007230 // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007231 if (Cond.getOpcode() == X86ISD::SETCC &&
Chris Lattner96908b12010-12-05 02:00:51 +00007232 Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
7233 isZero(Cond.getOperand(1).getOperand(1))) {
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007234 SDValue Cmp = Cond.getOperand(1);
Chris Lattnera2b56002010-12-05 01:23:24 +00007235
7236 unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
7237
Chris Lattner96908b12010-12-05 02:00:51 +00007238 if ((isAllOnes(Op1) || isAllOnes(Op2)) &&
7239 (CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
7240 SDValue Y = isAllOnes(Op2) ? Op1 : Op2;
Chris Lattnera2b56002010-12-05 01:23:24 +00007241
7242 SDValue CmpOp0 = Cmp.getOperand(0);
7243 Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32,
7244 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
7245
Chris Lattner96908b12010-12-05 02:00:51 +00007246 SDValue Res = // Res = 0 or -1.
Chris Lattnera2b56002010-12-05 01:23:24 +00007247 DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
7248 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
Chris Lattner96908b12010-12-05 02:00:51 +00007249
7250 if (isAllOnes(Op1) != (CondCode == X86::COND_E))
7251 Res = DAG.getNOT(DL, Res, Res.getValueType());
7252
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007253 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
Chris Lattnera2b56002010-12-05 01:23:24 +00007254 if (N2C == 0 || !N2C->isNullValue())
7255 Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
7256 return Res;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007257 }
7258 }
7259
Chris Lattnera2b56002010-12-05 01:23:24 +00007260 // Look past (and (setcc_carry (cmp ...)), 1).
Evan Chengad9c0a32009-12-15 00:53:42 +00007261 if (Cond.getOpcode() == ISD::AND &&
7262 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
7263 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00007264 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00007265 Cond = Cond.getOperand(0);
7266 }
7267
Evan Cheng3f41d662007-10-08 22:16:29 +00007268 // If condition flag is set by a X86ISD::CMP, then use it as the condition
7269 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00007270 if (Cond.getOpcode() == X86ISD::SETCC ||
7271 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00007272 CC = Cond.getOperand(0);
7273
Dan Gohman475871a2008-07-27 21:46:04 +00007274 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00007275 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00007276 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00007277
Evan Cheng3f41d662007-10-08 22:16:29 +00007278 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007279 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00007280 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00007281 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00007282
Chris Lattnerd1980a52009-03-12 06:52:53 +00007283 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
7284 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00007285 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00007286 addTest = false;
7287 }
7288 }
7289
7290 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007291 // Look pass the truncate.
7292 if (Cond.getOpcode() == ISD::TRUNCATE)
7293 Cond = Cond.getOperand(0);
7294
7295 // We know the result of AND is compared against zero. Try to match
7296 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00007297 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Chris Lattnera2b56002010-12-05 01:23:24 +00007298 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG);
Evan Chengd40d03e2010-01-06 19:38:29 +00007299 if (NewSetCC.getNode()) {
7300 CC = NewSetCC.getOperand(0);
7301 Cond = NewSetCC.getOperand(1);
7302 addTest = false;
7303 }
7304 }
7305 }
7306
7307 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007308 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00007309 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00007310 }
7311
Evan Cheng0488db92007-09-25 01:57:46 +00007312 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
7313 // condition is true.
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007314 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Flag);
7315 SDValue Ops[] = { Op2, Op1, CC, Cond };
Chris Lattnera2b56002010-12-05 01:23:24 +00007316 return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00007317}
7318
Evan Cheng370e5342008-12-03 08:38:43 +00007319// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
7320// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
7321// from the AND / OR.
7322static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
7323 Opc = Op.getOpcode();
7324 if (Opc != ISD::OR && Opc != ISD::AND)
7325 return false;
7326 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
7327 Op.getOperand(0).hasOneUse() &&
7328 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
7329 Op.getOperand(1).hasOneUse());
7330}
7331
Evan Cheng961d6d42009-02-02 08:19:07 +00007332// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
7333// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00007334static bool isXor1OfSetCC(SDValue Op) {
7335 if (Op.getOpcode() != ISD::XOR)
7336 return false;
7337 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
7338 if (N1C && N1C->getAPIntValue() == 1) {
7339 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
7340 Op.getOperand(0).hasOneUse();
7341 }
7342 return false;
7343}
7344
Dan Gohmand858e902010-04-17 15:26:15 +00007345SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00007346 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00007347 SDValue Chain = Op.getOperand(0);
7348 SDValue Cond = Op.getOperand(1);
7349 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007350 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00007351 SDValue CC;
Evan Cheng734503b2006-09-11 02:19:56 +00007352
Dan Gohman1a492952009-10-20 16:22:37 +00007353 if (Cond.getOpcode() == ISD::SETCC) {
7354 SDValue NewCond = LowerSETCC(Cond, DAG);
7355 if (NewCond.getNode())
7356 Cond = NewCond;
7357 }
Chris Lattnere55484e2008-12-25 05:34:37 +00007358#if 0
7359 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00007360 else if (Cond.getOpcode() == X86ISD::ADD ||
7361 Cond.getOpcode() == X86ISD::SUB ||
7362 Cond.getOpcode() == X86ISD::SMUL ||
7363 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00007364 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00007365#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00007366
Evan Chengad9c0a32009-12-15 00:53:42 +00007367 // Look pass (and (setcc_carry (cmp ...)), 1).
7368 if (Cond.getOpcode() == ISD::AND &&
7369 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
7370 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00007371 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00007372 Cond = Cond.getOperand(0);
7373 }
7374
Evan Cheng3f41d662007-10-08 22:16:29 +00007375 // If condition flag is set by a X86ISD::CMP, then use it as the condition
7376 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00007377 if (Cond.getOpcode() == X86ISD::SETCC ||
7378 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00007379 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007380
Dan Gohman475871a2008-07-27 21:46:04 +00007381 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00007382 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00007383 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00007384 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00007385 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00007386 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00007387 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00007388 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00007389 default: break;
7390 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00007391 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00007392 // These can only come from an arithmetic instruction with overflow,
7393 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00007394 Cond = Cond.getNode()->getOperand(1);
7395 addTest = false;
7396 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00007397 }
Evan Cheng0488db92007-09-25 01:57:46 +00007398 }
Evan Cheng370e5342008-12-03 08:38:43 +00007399 } else {
7400 unsigned CondOpc;
7401 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
7402 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00007403 if (CondOpc == ISD::OR) {
7404 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
7405 // two branches instead of an explicit OR instruction with a
7406 // separate test.
7407 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00007408 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00007409 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007410 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00007411 Chain, Dest, CC, Cmp);
7412 CC = Cond.getOperand(1).getOperand(0);
7413 Cond = Cmp;
7414 addTest = false;
7415 }
7416 } else { // ISD::AND
7417 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
7418 // two branches instead of an explicit AND instruction with a
7419 // separate test. However, we only do this if this block doesn't
7420 // have a fall-through edge, because this requires an explicit
7421 // jmp when the condition is false.
7422 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00007423 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00007424 Op.getNode()->hasOneUse()) {
7425 X86::CondCode CCode =
7426 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
7427 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007428 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00007429 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00007430 // Look for an unconditional branch following this conditional branch.
7431 // We need this because we need to reverse the successors in order
7432 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00007433 if (User->getOpcode() == ISD::BR) {
7434 SDValue FalseBB = User->getOperand(1);
7435 SDNode *NewBR =
7436 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00007437 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00007438 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00007439 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00007440
Dale Johannesene4d209d2009-02-03 20:21:25 +00007441 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00007442 Chain, Dest, CC, Cmp);
7443 X86::CondCode CCode =
7444 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
7445 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007446 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00007447 Cond = Cmp;
7448 addTest = false;
7449 }
7450 }
Dan Gohman279c22e2008-10-21 03:29:32 +00007451 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00007452 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
7453 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
7454 // It should be transformed during dag combiner except when the condition
7455 // is set by a arithmetics with overflow node.
7456 X86::CondCode CCode =
7457 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
7458 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007459 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00007460 Cond = Cond.getOperand(0).getOperand(1);
7461 addTest = false;
Dan Gohman279c22e2008-10-21 03:29:32 +00007462 }
Evan Cheng0488db92007-09-25 01:57:46 +00007463 }
7464
7465 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007466 // Look pass the truncate.
7467 if (Cond.getOpcode() == ISD::TRUNCATE)
7468 Cond = Cond.getOperand(0);
7469
7470 // We know the result of AND is compared against zero. Try to match
7471 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00007472 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007473 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
7474 if (NewSetCC.getNode()) {
7475 CC = NewSetCC.getOperand(0);
7476 Cond = NewSetCC.getOperand(1);
7477 addTest = false;
7478 }
7479 }
7480 }
7481
7482 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007483 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00007484 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00007485 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00007486 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00007487 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00007488}
7489
Anton Korobeynikove060b532007-04-17 19:34:00 +00007490
7491// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
7492// Calls to _alloca is needed to probe the stack when allocating more than 4k
7493// bytes in one go. Touching the stack at 4K increments is necessary to ensure
7494// that the guard pages used by the OS virtual memory manager are allocated in
7495// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00007496SDValue
7497X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00007498 SelectionDAG &DAG) const {
Duncan Sands1e1ca0b2010-10-21 16:02:12 +00007499 assert((Subtarget->isTargetCygMing() || Subtarget->isTargetWindows()) &&
Michael J. Spencere9c253e2010-10-21 01:41:01 +00007500 "This should be used only on Windows targets");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007501 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007502
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007503 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00007504 SDValue Chain = Op.getOperand(0);
7505 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007506 // FIXME: Ensure alignment here
7507
Dan Gohman475871a2008-07-27 21:46:04 +00007508 SDValue Flag;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007509
Owen Anderson825b72b2009-08-11 20:47:22 +00007510 EVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007511
Dale Johannesendd64c412009-02-04 00:33:20 +00007512 Chain = DAG.getCopyToReg(Chain, dl, X86::EAX, Size, Flag);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00007513 Flag = Chain.getValue(1);
7514
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00007515 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00007516
Michael J. Spencere9c253e2010-10-21 01:41:01 +00007517 Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag);
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00007518 Flag = Chain.getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007519
Dale Johannesendd64c412009-02-04 00:33:20 +00007520 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007521
Dan Gohman475871a2008-07-27 21:46:04 +00007522 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Dale Johannesene4d209d2009-02-03 20:21:25 +00007523 return DAG.getMergeValues(Ops1, 2, dl);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007524}
7525
Dan Gohmand858e902010-04-17 15:26:15 +00007526SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00007527 MachineFunction &MF = DAG.getMachineFunction();
7528 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
7529
Dan Gohman69de1932008-02-06 22:27:42 +00007530 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00007531 DebugLoc DL = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00007532
Anton Korobeynikove7beda12010-10-03 22:52:07 +00007533 if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00007534 // vastart just stores the address of the VarArgsFrameIndex slot into the
7535 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +00007536 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
7537 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00007538 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
7539 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007540 }
7541
7542 // __va_list_tag:
7543 // gp_offset (0 - 6 * 8)
7544 // fp_offset (48 - 48 + 8 * 16)
7545 // overflow_arg_area (point to parameters coming in memory).
7546 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00007547 SmallVector<SDValue, 8> MemOps;
7548 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00007549 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00007550 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00007551 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
7552 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007553 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007554 MemOps.push_back(Store);
7555
7556 // Store fp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00007557 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007558 FIN, DAG.getIntPtrConstant(4));
Chris Lattner8026a9d2010-09-21 17:50:43 +00007559 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00007560 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
7561 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007562 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007563 MemOps.push_back(Store);
7564
7565 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +00007566 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007567 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +00007568 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
7569 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00007570 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
7571 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +00007572 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007573 MemOps.push_back(Store);
7574
7575 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +00007576 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007577 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +00007578 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
7579 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00007580 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
7581 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007582 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +00007583 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00007584 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00007585}
7586
Dan Gohmand858e902010-04-17 15:26:15 +00007587SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman320afb82010-10-12 18:00:49 +00007588 assert(Subtarget->is64Bit() &&
7589 "LowerVAARG only handles 64-bit va_arg!");
7590 assert((Subtarget->isTargetLinux() ||
7591 Subtarget->isTargetDarwin()) &&
7592 "Unhandled target in LowerVAARG");
7593 assert(Op.getNode()->getNumOperands() == 4);
7594 SDValue Chain = Op.getOperand(0);
7595 SDValue SrcPtr = Op.getOperand(1);
7596 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
7597 unsigned Align = Op.getConstantOperandVal(3);
7598 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9018e832008-05-10 01:26:14 +00007599
Dan Gohman320afb82010-10-12 18:00:49 +00007600 EVT ArgVT = Op.getNode()->getValueType(0);
7601 const Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
7602 uint32_t ArgSize = getTargetData()->getTypeAllocSize(ArgTy);
7603 uint8_t ArgMode;
7604
7605 // Decide which area this value should be read from.
7606 // TODO: Implement the AMD64 ABI in its entirety. This simple
7607 // selection mechanism works only for the basic types.
7608 if (ArgVT == MVT::f80) {
7609 llvm_unreachable("va_arg for f80 not yet implemented");
7610 } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) {
7611 ArgMode = 2; // Argument passed in XMM register. Use fp_offset.
7612 } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) {
7613 ArgMode = 1; // Argument passed in GPR64 register(s). Use gp_offset.
7614 } else {
7615 llvm_unreachable("Unhandled argument type in LowerVAARG");
7616 }
7617
7618 if (ArgMode == 2) {
7619 // Sanity Check: Make sure using fp_offset makes sense.
Michael J. Spencer87b86652010-10-19 07:32:42 +00007620 assert(!UseSoftFloat &&
Eric Christopher52b45052010-10-12 19:44:17 +00007621 !(DAG.getMachineFunction()
7622 .getFunction()->hasFnAttr(Attribute::NoImplicitFloat)) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00007623 Subtarget->hasXMM());
Dan Gohman320afb82010-10-12 18:00:49 +00007624 }
7625
7626 // Insert VAARG_64 node into the DAG
7627 // VAARG_64 returns two values: Variable Argument Address, Chain
7628 SmallVector<SDValue, 11> InstOps;
7629 InstOps.push_back(Chain);
7630 InstOps.push_back(SrcPtr);
7631 InstOps.push_back(DAG.getConstant(ArgSize, MVT::i32));
7632 InstOps.push_back(DAG.getConstant(ArgMode, MVT::i8));
7633 InstOps.push_back(DAG.getConstant(Align, MVT::i32));
7634 SDVTList VTs = DAG.getVTList(getPointerTy(), MVT::Other);
7635 SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl,
7636 VTs, &InstOps[0], InstOps.size(),
7637 MVT::i64,
7638 MachinePointerInfo(SV),
7639 /*Align=*/0,
7640 /*Volatile=*/false,
7641 /*ReadMem=*/true,
7642 /*WriteMem=*/true);
7643 Chain = VAARG.getValue(1);
7644
7645 // Load the next argument and return it
7646 return DAG.getLoad(ArgVT, dl,
7647 Chain,
7648 VAARG,
7649 MachinePointerInfo(),
7650 false, false, 0);
Dan Gohman9018e832008-05-10 01:26:14 +00007651}
7652
Dan Gohmand858e902010-04-17 15:26:15 +00007653SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) const {
Evan Chengae642192007-03-02 23:16:35 +00007654 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00007655 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00007656 SDValue Chain = Op.getOperand(0);
7657 SDValue DstPtr = Op.getOperand(1);
7658 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00007659 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
7660 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Chris Lattnere72f2022010-09-21 05:40:29 +00007661 DebugLoc DL = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00007662
Chris Lattnere72f2022010-09-21 05:40:29 +00007663 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +00007664 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Michael J. Spencerec38de22010-10-10 22:04:20 +00007665 false,
Chris Lattnere72f2022010-09-21 05:40:29 +00007666 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +00007667}
7668
Dan Gohman475871a2008-07-27 21:46:04 +00007669SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007670X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007671 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007672 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007673 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00007674 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00007675 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00007676 case Intrinsic::x86_sse_comieq_ss:
7677 case Intrinsic::x86_sse_comilt_ss:
7678 case Intrinsic::x86_sse_comile_ss:
7679 case Intrinsic::x86_sse_comigt_ss:
7680 case Intrinsic::x86_sse_comige_ss:
7681 case Intrinsic::x86_sse_comineq_ss:
7682 case Intrinsic::x86_sse_ucomieq_ss:
7683 case Intrinsic::x86_sse_ucomilt_ss:
7684 case Intrinsic::x86_sse_ucomile_ss:
7685 case Intrinsic::x86_sse_ucomigt_ss:
7686 case Intrinsic::x86_sse_ucomige_ss:
7687 case Intrinsic::x86_sse_ucomineq_ss:
7688 case Intrinsic::x86_sse2_comieq_sd:
7689 case Intrinsic::x86_sse2_comilt_sd:
7690 case Intrinsic::x86_sse2_comile_sd:
7691 case Intrinsic::x86_sse2_comigt_sd:
7692 case Intrinsic::x86_sse2_comige_sd:
7693 case Intrinsic::x86_sse2_comineq_sd:
7694 case Intrinsic::x86_sse2_ucomieq_sd:
7695 case Intrinsic::x86_sse2_ucomilt_sd:
7696 case Intrinsic::x86_sse2_ucomile_sd:
7697 case Intrinsic::x86_sse2_ucomigt_sd:
7698 case Intrinsic::x86_sse2_ucomige_sd:
7699 case Intrinsic::x86_sse2_ucomineq_sd: {
7700 unsigned Opc = 0;
7701 ISD::CondCode CC = ISD::SETCC_INVALID;
7702 switch (IntNo) {
7703 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007704 case Intrinsic::x86_sse_comieq_ss:
7705 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007706 Opc = X86ISD::COMI;
7707 CC = ISD::SETEQ;
7708 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00007709 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007710 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007711 Opc = X86ISD::COMI;
7712 CC = ISD::SETLT;
7713 break;
7714 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007715 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007716 Opc = X86ISD::COMI;
7717 CC = ISD::SETLE;
7718 break;
7719 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007720 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007721 Opc = X86ISD::COMI;
7722 CC = ISD::SETGT;
7723 break;
7724 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007725 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007726 Opc = X86ISD::COMI;
7727 CC = ISD::SETGE;
7728 break;
7729 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007730 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007731 Opc = X86ISD::COMI;
7732 CC = ISD::SETNE;
7733 break;
7734 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007735 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007736 Opc = X86ISD::UCOMI;
7737 CC = ISD::SETEQ;
7738 break;
7739 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007740 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007741 Opc = X86ISD::UCOMI;
7742 CC = ISD::SETLT;
7743 break;
7744 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007745 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007746 Opc = X86ISD::UCOMI;
7747 CC = ISD::SETLE;
7748 break;
7749 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007750 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007751 Opc = X86ISD::UCOMI;
7752 CC = ISD::SETGT;
7753 break;
7754 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007755 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007756 Opc = X86ISD::UCOMI;
7757 CC = ISD::SETGE;
7758 break;
7759 case Intrinsic::x86_sse_ucomineq_ss:
7760 case Intrinsic::x86_sse2_ucomineq_sd:
7761 Opc = X86ISD::UCOMI;
7762 CC = ISD::SETNE;
7763 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00007764 }
Evan Cheng734503b2006-09-11 02:19:56 +00007765
Dan Gohman475871a2008-07-27 21:46:04 +00007766 SDValue LHS = Op.getOperand(1);
7767 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00007768 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00007769 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00007770 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
7771 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
7772 DAG.getConstant(X86CC, MVT::i8), Cond);
7773 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00007774 }
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007775 // ptest and testp intrinsics. The intrinsic these come from are designed to
7776 // return an integer value, not just an instruction so lower it to the ptest
7777 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00007778 case Intrinsic::x86_sse41_ptestz:
7779 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007780 case Intrinsic::x86_sse41_ptestnzc:
7781 case Intrinsic::x86_avx_ptestz_256:
7782 case Intrinsic::x86_avx_ptestc_256:
7783 case Intrinsic::x86_avx_ptestnzc_256:
7784 case Intrinsic::x86_avx_vtestz_ps:
7785 case Intrinsic::x86_avx_vtestc_ps:
7786 case Intrinsic::x86_avx_vtestnzc_ps:
7787 case Intrinsic::x86_avx_vtestz_pd:
7788 case Intrinsic::x86_avx_vtestc_pd:
7789 case Intrinsic::x86_avx_vtestnzc_pd:
7790 case Intrinsic::x86_avx_vtestz_ps_256:
7791 case Intrinsic::x86_avx_vtestc_ps_256:
7792 case Intrinsic::x86_avx_vtestnzc_ps_256:
7793 case Intrinsic::x86_avx_vtestz_pd_256:
7794 case Intrinsic::x86_avx_vtestc_pd_256:
7795 case Intrinsic::x86_avx_vtestnzc_pd_256: {
7796 bool IsTestPacked = false;
Eric Christopher71c67532009-07-29 00:28:05 +00007797 unsigned X86CC = 0;
7798 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00007799 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007800 case Intrinsic::x86_avx_vtestz_ps:
7801 case Intrinsic::x86_avx_vtestz_pd:
7802 case Intrinsic::x86_avx_vtestz_ps_256:
7803 case Intrinsic::x86_avx_vtestz_pd_256:
7804 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00007805 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007806 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +00007807 // ZF = 1
7808 X86CC = X86::COND_E;
7809 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007810 case Intrinsic::x86_avx_vtestc_ps:
7811 case Intrinsic::x86_avx_vtestc_pd:
7812 case Intrinsic::x86_avx_vtestc_ps_256:
7813 case Intrinsic::x86_avx_vtestc_pd_256:
7814 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00007815 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007816 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00007817 // CF = 1
7818 X86CC = X86::COND_B;
7819 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007820 case Intrinsic::x86_avx_vtestnzc_ps:
7821 case Intrinsic::x86_avx_vtestnzc_pd:
7822 case Intrinsic::x86_avx_vtestnzc_ps_256:
7823 case Intrinsic::x86_avx_vtestnzc_pd_256:
7824 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +00007825 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007826 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00007827 // ZF and CF = 0
7828 X86CC = X86::COND_A;
7829 break;
7830 }
Eric Christopherfd179292009-08-27 18:07:15 +00007831
Eric Christopher71c67532009-07-29 00:28:05 +00007832 SDValue LHS = Op.getOperand(1);
7833 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007834 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
7835 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00007836 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
7837 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
7838 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +00007839 }
Evan Cheng5759f972008-05-04 09:15:50 +00007840
7841 // Fix vector shift instructions where the last operand is a non-immediate
7842 // i32 value.
7843 case Intrinsic::x86_sse2_pslli_w:
7844 case Intrinsic::x86_sse2_pslli_d:
7845 case Intrinsic::x86_sse2_pslli_q:
7846 case Intrinsic::x86_sse2_psrli_w:
7847 case Intrinsic::x86_sse2_psrli_d:
7848 case Intrinsic::x86_sse2_psrli_q:
7849 case Intrinsic::x86_sse2_psrai_w:
7850 case Intrinsic::x86_sse2_psrai_d:
7851 case Intrinsic::x86_mmx_pslli_w:
7852 case Intrinsic::x86_mmx_pslli_d:
7853 case Intrinsic::x86_mmx_pslli_q:
7854 case Intrinsic::x86_mmx_psrli_w:
7855 case Intrinsic::x86_mmx_psrli_d:
7856 case Intrinsic::x86_mmx_psrli_q:
7857 case Intrinsic::x86_mmx_psrai_w:
7858 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00007859 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00007860 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00007861 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00007862
7863 unsigned NewIntNo = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00007864 EVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00007865 switch (IntNo) {
7866 case Intrinsic::x86_sse2_pslli_w:
7867 NewIntNo = Intrinsic::x86_sse2_psll_w;
7868 break;
7869 case Intrinsic::x86_sse2_pslli_d:
7870 NewIntNo = Intrinsic::x86_sse2_psll_d;
7871 break;
7872 case Intrinsic::x86_sse2_pslli_q:
7873 NewIntNo = Intrinsic::x86_sse2_psll_q;
7874 break;
7875 case Intrinsic::x86_sse2_psrli_w:
7876 NewIntNo = Intrinsic::x86_sse2_psrl_w;
7877 break;
7878 case Intrinsic::x86_sse2_psrli_d:
7879 NewIntNo = Intrinsic::x86_sse2_psrl_d;
7880 break;
7881 case Intrinsic::x86_sse2_psrli_q:
7882 NewIntNo = Intrinsic::x86_sse2_psrl_q;
7883 break;
7884 case Intrinsic::x86_sse2_psrai_w:
7885 NewIntNo = Intrinsic::x86_sse2_psra_w;
7886 break;
7887 case Intrinsic::x86_sse2_psrai_d:
7888 NewIntNo = Intrinsic::x86_sse2_psra_d;
7889 break;
7890 default: {
Owen Anderson825b72b2009-08-11 20:47:22 +00007891 ShAmtVT = MVT::v2i32;
Evan Cheng5759f972008-05-04 09:15:50 +00007892 switch (IntNo) {
7893 case Intrinsic::x86_mmx_pslli_w:
7894 NewIntNo = Intrinsic::x86_mmx_psll_w;
7895 break;
7896 case Intrinsic::x86_mmx_pslli_d:
7897 NewIntNo = Intrinsic::x86_mmx_psll_d;
7898 break;
7899 case Intrinsic::x86_mmx_pslli_q:
7900 NewIntNo = Intrinsic::x86_mmx_psll_q;
7901 break;
7902 case Intrinsic::x86_mmx_psrli_w:
7903 NewIntNo = Intrinsic::x86_mmx_psrl_w;
7904 break;
7905 case Intrinsic::x86_mmx_psrli_d:
7906 NewIntNo = Intrinsic::x86_mmx_psrl_d;
7907 break;
7908 case Intrinsic::x86_mmx_psrli_q:
7909 NewIntNo = Intrinsic::x86_mmx_psrl_q;
7910 break;
7911 case Intrinsic::x86_mmx_psrai_w:
7912 NewIntNo = Intrinsic::x86_mmx_psra_w;
7913 break;
7914 case Intrinsic::x86_mmx_psrai_d:
7915 NewIntNo = Intrinsic::x86_mmx_psra_d;
7916 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00007917 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng5759f972008-05-04 09:15:50 +00007918 }
7919 break;
7920 }
7921 }
Mon P Wangefa42202009-09-03 19:56:25 +00007922
7923 // The vector shift intrinsics with scalars uses 32b shift amounts but
7924 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
7925 // to be zero.
7926 SDValue ShOps[4];
7927 ShOps[0] = ShAmt;
7928 ShOps[1] = DAG.getConstant(0, MVT::i32);
7929 if (ShAmtVT == MVT::v4i32) {
7930 ShOps[2] = DAG.getUNDEF(MVT::i32);
7931 ShOps[3] = DAG.getUNDEF(MVT::i32);
7932 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 4);
7933 } else {
7934 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 2);
Dale Johannesen0488fb62010-09-30 23:57:10 +00007935// FIXME this must be lowered to get rid of the invalid type.
Mon P Wangefa42202009-09-03 19:56:25 +00007936 }
7937
Owen Andersone50ed302009-08-10 22:56:29 +00007938 EVT VT = Op.getValueType();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007939 ShAmt = DAG.getNode(ISD::BITCAST, dl, VT, ShAmt);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007940 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007941 DAG.getConstant(NewIntNo, MVT::i32),
Evan Cheng5759f972008-05-04 09:15:50 +00007942 Op.getOperand(1), ShAmt);
7943 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00007944 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007945}
Evan Cheng72261582005-12-20 06:22:03 +00007946
Dan Gohmand858e902010-04-17 15:26:15 +00007947SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
7948 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +00007949 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7950 MFI->setReturnAddressIsTaken(true);
7951
Bill Wendling64e87322009-01-16 19:25:27 +00007952 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007953 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00007954
7955 if (Depth > 0) {
7956 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
7957 SDValue Offset =
7958 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +00007959 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007960 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00007961 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007962 FrameAddr, Offset),
Chris Lattner51abfe42010-09-21 06:02:19 +00007963 MachinePointerInfo(), false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +00007964 }
7965
7966 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00007967 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00007968 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Chris Lattner51abfe42010-09-21 06:02:19 +00007969 RetAddrFI, MachinePointerInfo(), false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00007970}
7971
Dan Gohmand858e902010-04-17 15:26:15 +00007972SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +00007973 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7974 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00007975
Owen Andersone50ed302009-08-10 22:56:29 +00007976 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007977 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00007978 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
7979 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00007980 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00007981 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +00007982 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
7983 MachinePointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +00007984 false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00007985 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00007986}
7987
Dan Gohman475871a2008-07-27 21:46:04 +00007988SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00007989 SelectionDAG &DAG) const {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00007990 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007991}
7992
Dan Gohmand858e902010-04-17 15:26:15 +00007993SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007994 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00007995 SDValue Chain = Op.getOperand(0);
7996 SDValue Offset = Op.getOperand(1);
7997 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007998 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007999
Dan Gohmand8816272010-08-11 18:14:00 +00008000 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
8001 Subtarget->is64Bit() ? X86::RBP : X86::EBP,
8002 getPointerTy());
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008003 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008004
Dan Gohmand8816272010-08-11 18:14:00 +00008005 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Frame,
8006 DAG.getIntPtrConstant(TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008007 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +00008008 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
8009 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00008010 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008011 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008012
Dale Johannesene4d209d2009-02-03 20:21:25 +00008013 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008014 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008015 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008016}
8017
Dan Gohman475871a2008-07-27 21:46:04 +00008018SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008019 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00008020 SDValue Root = Op.getOperand(0);
8021 SDValue Trmp = Op.getOperand(1); // trampoline
8022 SDValue FPtr = Op.getOperand(2); // nested function
8023 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008024 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008025
Dan Gohman69de1932008-02-06 22:27:42 +00008026 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008027
8028 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00008029 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00008030
8031 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +00008032 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
8033 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +00008034
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00008035 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
8036 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00008037
8038 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
8039
8040 // Load the pointer to the nested function into R11.
8041 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00008042 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00008043 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008044 Addr, MachinePointerInfo(TrmpAddr),
8045 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008046
Owen Anderson825b72b2009-08-11 20:47:22 +00008047 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8048 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008049 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
8050 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +00008051 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00008052
8053 // Load the 'nest' parameter value into R10.
8054 // R10 is specified in X86CallingConv.td
8055 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +00008056 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8057 DAG.getConstant(10, MVT::i64));
8058 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008059 Addr, MachinePointerInfo(TrmpAddr, 10),
8060 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008061
Owen Anderson825b72b2009-08-11 20:47:22 +00008062 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8063 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008064 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
8065 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +00008066 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00008067
8068 // Jump to the nested function.
8069 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +00008070 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8071 DAG.getConstant(20, MVT::i64));
8072 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008073 Addr, MachinePointerInfo(TrmpAddr, 20),
8074 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008075
8076 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +00008077 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8078 DAG.getConstant(22, MVT::i64));
8079 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00008080 MachinePointerInfo(TrmpAddr, 22),
8081 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008082
Dan Gohman475871a2008-07-27 21:46:04 +00008083 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00008084 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008085 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008086 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00008087 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00008088 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00008089 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00008090 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008091
8092 switch (CC) {
8093 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00008094 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +00008095 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00008096 case CallingConv::X86_StdCall: {
8097 // Pass 'nest' parameter in ECX.
8098 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00008099 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008100
8101 // Check that ECX wasn't needed by an 'inreg' parameter.
8102 const FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00008103 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008104
Chris Lattner58d74912008-03-12 17:45:29 +00008105 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00008106 unsigned InRegCount = 0;
8107 unsigned Idx = 1;
8108
8109 for (FunctionType::param_iterator I = FTy->param_begin(),
8110 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00008111 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00008112 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00008113 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008114
8115 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +00008116 report_fatal_error("Nest register in use - reduce number of inreg"
8117 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +00008118 }
8119 }
8120 break;
8121 }
8122 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +00008123 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00008124 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00008125 // Pass 'nest' parameter in EAX.
8126 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00008127 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008128 break;
8129 }
8130
Dan Gohman475871a2008-07-27 21:46:04 +00008131 SDValue OutChains[4];
8132 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008133
Owen Anderson825b72b2009-08-11 20:47:22 +00008134 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8135 DAG.getConstant(10, MVT::i32));
8136 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008137
Chris Lattnera62fe662010-02-05 19:20:30 +00008138 // This is storing the opcode for MOV32ri.
8139 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00008140 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00008141 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008142 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008143 Trmp, MachinePointerInfo(TrmpAddr),
8144 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008145
Owen Anderson825b72b2009-08-11 20:47:22 +00008146 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8147 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008148 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
8149 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +00008150 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008151
Chris Lattnera62fe662010-02-05 19:20:30 +00008152 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +00008153 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8154 DAG.getConstant(5, MVT::i32));
8155 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00008156 MachinePointerInfo(TrmpAddr, 5),
8157 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008158
Owen Anderson825b72b2009-08-11 20:47:22 +00008159 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8160 DAG.getConstant(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008161 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
8162 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +00008163 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008164
Dan Gohman475871a2008-07-27 21:46:04 +00008165 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00008166 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008167 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008168 }
8169}
8170
Dan Gohmand858e902010-04-17 15:26:15 +00008171SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
8172 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008173 /*
8174 The rounding mode is in bits 11:10 of FPSR, and has the following
8175 settings:
8176 00 Round to nearest
8177 01 Round to -inf
8178 10 Round to +inf
8179 11 Round to 0
8180
8181 FLT_ROUNDS, on the other hand, expects the following:
8182 -1 Undefined
8183 0 Round to 0
8184 1 Round to nearest
8185 2 Round to +inf
8186 3 Round to -inf
8187
8188 To perform the conversion, we do:
8189 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
8190 */
8191
8192 MachineFunction &MF = DAG.getMachineFunction();
8193 const TargetMachine &TM = MF.getTarget();
8194 const TargetFrameInfo &TFI = *TM.getFrameInfo();
8195 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00008196 EVT VT = Op.getValueType();
Chris Lattner2156b792010-09-22 01:11:26 +00008197 DebugLoc DL = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008198
8199 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +00008200 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +00008201 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008202
Michael J. Spencerec38de22010-10-10 22:04:20 +00008203
Chris Lattner2156b792010-09-22 01:11:26 +00008204 MachineMemOperand *MMO =
8205 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8206 MachineMemOperand::MOStore, 2, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008207
Chris Lattner2156b792010-09-22 01:11:26 +00008208 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
8209 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
8210 DAG.getVTList(MVT::Other),
8211 Ops, 2, MVT::i16, MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008212
8213 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +00008214 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Chris Lattner51abfe42010-09-21 06:02:19 +00008215 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008216
8217 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00008218 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +00008219 DAG.getNode(ISD::SRL, DL, MVT::i16,
8220 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00008221 CWD, DAG.getConstant(0x800, MVT::i16)),
8222 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00008223 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +00008224 DAG.getNode(ISD::SRL, DL, MVT::i16,
8225 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00008226 CWD, DAG.getConstant(0x400, MVT::i16)),
8227 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008228
Dan Gohman475871a2008-07-27 21:46:04 +00008229 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +00008230 DAG.getNode(ISD::AND, DL, MVT::i16,
8231 DAG.getNode(ISD::ADD, DL, MVT::i16,
8232 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +00008233 DAG.getConstant(1, MVT::i16)),
8234 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008235
8236
Duncan Sands83ec4b62008-06-06 12:08:01 +00008237 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +00008238 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008239}
8240
Dan Gohmand858e902010-04-17 15:26:15 +00008241SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008242 EVT VT = Op.getValueType();
8243 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008244 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008245 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00008246
8247 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008248 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00008249 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +00008250 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00008251 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008252 }
Evan Cheng18efe262007-12-14 02:13:44 +00008253
Evan Cheng152804e2007-12-14 08:30:15 +00008254 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008255 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008256 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00008257
8258 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00008259 SDValue Ops[] = {
8260 Op,
8261 DAG.getConstant(NumBits+NumBits-1, OpVT),
8262 DAG.getConstant(X86::COND_E, MVT::i8),
8263 Op.getValue(1)
8264 };
8265 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00008266
8267 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +00008268 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +00008269
Owen Anderson825b72b2009-08-11 20:47:22 +00008270 if (VT == MVT::i8)
8271 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008272 return Op;
8273}
8274
Dan Gohmand858e902010-04-17 15:26:15 +00008275SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008276 EVT VT = Op.getValueType();
8277 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008278 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008279 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00008280
8281 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008282 if (VT == MVT::i8) {
8283 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00008284 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008285 }
Evan Cheng152804e2007-12-14 08:30:15 +00008286
8287 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008288 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008289 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00008290
8291 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00008292 SDValue Ops[] = {
8293 Op,
8294 DAG.getConstant(NumBits, OpVT),
8295 DAG.getConstant(X86::COND_E, MVT::i8),
8296 Op.getValue(1)
8297 };
8298 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00008299
Owen Anderson825b72b2009-08-11 20:47:22 +00008300 if (VT == MVT::i8)
8301 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008302 return Op;
8303}
8304
Dan Gohmand858e902010-04-17 15:26:15 +00008305SDValue X86TargetLowering::LowerMUL_V2I64(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008306 EVT VT = Op.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00008307 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008308 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00008309
Mon P Wangaf9b9522008-12-18 21:42:19 +00008310 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
8311 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
8312 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
8313 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
8314 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
8315 //
8316 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
8317 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
8318 // return AloBlo + AloBhi + AhiBlo;
8319
8320 SDValue A = Op.getOperand(0);
8321 SDValue B = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008322
Dale Johannesene4d209d2009-02-03 20:21:25 +00008323 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008324 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
8325 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008326 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008327 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
8328 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008329 SDValue AloBlo = 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 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008332 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008333 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008334 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008335 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008336 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008337 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008338 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008339 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
8340 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008341 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008342 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
8343 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008344 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
8345 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +00008346 return Res;
8347}
8348
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008349SDValue X86TargetLowering::LowerSHL(SDValue Op, SelectionDAG &DAG) const {
8350 EVT VT = Op.getValueType();
8351 DebugLoc dl = Op.getDebugLoc();
8352 SDValue R = Op.getOperand(0);
8353
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008354 LLVMContext *Context = DAG.getContext();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008355
Nate Begeman51409212010-07-28 00:21:48 +00008356 assert(Subtarget->hasSSE41() && "Cannot lower SHL without SSE4.1 or later");
8357
8358 if (VT == MVT::v4i32) {
8359 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8360 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
8361 Op.getOperand(1), DAG.getConstant(23, MVT::i32));
8362
8363 ConstantInt *CI = ConstantInt::get(*Context, APInt(32, 0x3f800000U));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008364
Nate Begeman51409212010-07-28 00:21:48 +00008365 std::vector<Constant*> CV(4, CI);
8366 Constant *C = ConstantVector::get(CV);
8367 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8368 SDValue Addend = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008369 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +00008370 false, false, 16);
8371
8372 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Addend);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008373 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op);
Nate Begeman51409212010-07-28 00:21:48 +00008374 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
8375 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
8376 }
8377 if (VT == MVT::v16i8) {
8378 // a = a << 5;
8379 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8380 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
8381 Op.getOperand(1), DAG.getConstant(5, MVT::i32));
8382
8383 ConstantInt *CM1 = ConstantInt::get(*Context, APInt(8, 15));
8384 ConstantInt *CM2 = ConstantInt::get(*Context, APInt(8, 63));
8385
8386 std::vector<Constant*> CVM1(16, CM1);
8387 std::vector<Constant*> CVM2(16, CM2);
8388 Constant *C = ConstantVector::get(CVM1);
8389 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8390 SDValue M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008391 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +00008392 false, false, 16);
8393
8394 // r = pblendv(r, psllw(r & (char16)15, 4), a);
8395 M = DAG.getNode(ISD::AND, dl, VT, R, M);
8396 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8397 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
8398 DAG.getConstant(4, MVT::i32));
8399 R = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8400 DAG.getConstant(Intrinsic::x86_sse41_pblendvb, MVT::i32),
8401 R, M, Op);
8402 // a += a
8403 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008404
Nate Begeman51409212010-07-28 00:21:48 +00008405 C = ConstantVector::get(CVM2);
8406 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8407 M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008408 MachinePointerInfo::getConstantPool(),
Chris Lattner51abfe42010-09-21 06:02:19 +00008409 false, false, 16);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008410
Nate Begeman51409212010-07-28 00:21:48 +00008411 // r = pblendv(r, psllw(r & (char16)63, 2), a);
8412 M = DAG.getNode(ISD::AND, dl, VT, R, M);
8413 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8414 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
8415 DAG.getConstant(2, MVT::i32));
8416 R = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8417 DAG.getConstant(Intrinsic::x86_sse41_pblendvb, MVT::i32),
8418 R, M, Op);
8419 // a += a
8420 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008421
Nate Begeman51409212010-07-28 00:21:48 +00008422 // return pblendv(r, r+r, a);
8423 R = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8424 DAG.getConstant(Intrinsic::x86_sse41_pblendvb, MVT::i32),
8425 R, DAG.getNode(ISD::ADD, dl, VT, R, R), Op);
8426 return R;
8427 }
8428 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008429}
Mon P Wangaf9b9522008-12-18 21:42:19 +00008430
Dan Gohmand858e902010-04-17 15:26:15 +00008431SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
Bill Wendling74c37652008-12-09 22:08:41 +00008432 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
8433 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +00008434 // looks for this combo and may remove the "setcc" instruction if the "setcc"
8435 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +00008436 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +00008437 SDValue LHS = N->getOperand(0);
8438 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +00008439 unsigned BaseOp = 0;
8440 unsigned Cond = 0;
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008441 DebugLoc DL = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +00008442 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008443 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +00008444 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +00008445 // A subtract of one will be selected as a INC. Note that INC doesn't
8446 // set CF, so we can't do this for UADDO.
8447 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
8448 if (C->getAPIntValue() == 1) {
8449 BaseOp = X86ISD::INC;
8450 Cond = X86::COND_O;
8451 break;
8452 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008453 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +00008454 Cond = X86::COND_O;
8455 break;
8456 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008457 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +00008458 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00008459 break;
8460 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +00008461 // A subtract of one will be selected as a DEC. Note that DEC doesn't
8462 // set CF, so we can't do this for USUBO.
8463 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
8464 if (C->getAPIntValue() == 1) {
8465 BaseOp = X86ISD::DEC;
8466 Cond = X86::COND_O;
8467 break;
8468 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008469 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +00008470 Cond = X86::COND_O;
8471 break;
8472 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008473 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +00008474 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00008475 break;
8476 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00008477 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +00008478 Cond = X86::COND_O;
8479 break;
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008480 case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs
8481 SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0),
8482 MVT::i32);
8483 SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS);
8484
8485 SDValue SetCC =
8486 DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
8487 DAG.getConstant(X86::COND_O, MVT::i32),
8488 SDValue(Sum.getNode(), 2));
8489
8490 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
8491 return Sum;
8492 }
Bill Wendling74c37652008-12-09 22:08:41 +00008493 }
Bill Wendling3fafd932008-11-26 22:37:40 +00008494
Bill Wendling61edeb52008-12-02 01:06:39 +00008495 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008496 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008497 SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +00008498
Bill Wendling61edeb52008-12-02 01:06:39 +00008499 SDValue SetCC =
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008500 DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1),
8501 DAG.getConstant(Cond, MVT::i32),
8502 SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +00008503
Bill Wendling61edeb52008-12-02 01:06:39 +00008504 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
8505 return Sum;
Bill Wendling41ea7e72008-11-24 19:21:46 +00008506}
8507
Eric Christopher9a9d2752010-07-22 02:48:34 +00008508SDValue X86TargetLowering::LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG) const{
8509 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00008510
Eric Christopherb6729dc2010-08-04 23:03:04 +00008511 if (!Subtarget->hasSSE2()) {
Eric Christopherc0b2a202010-08-14 21:51:50 +00008512 SDValue Chain = Op.getOperand(0);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008513 SDValue Zero = DAG.getConstant(0,
Eric Christopherb6729dc2010-08-04 23:03:04 +00008514 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Eric Christopherc0b2a202010-08-14 21:51:50 +00008515 SDValue Ops[] = {
8516 DAG.getRegister(X86::ESP, MVT::i32), // Base
8517 DAG.getTargetConstant(1, MVT::i8), // Scale
8518 DAG.getRegister(0, MVT::i32), // Index
8519 DAG.getTargetConstant(0, MVT::i32), // Disp
8520 DAG.getRegister(0, MVT::i32), // Segment.
8521 Zero,
8522 Chain
8523 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00008524 SDNode *Res =
Eric Christopherc0b2a202010-08-14 21:51:50 +00008525 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
8526 array_lengthof(Ops));
8527 return SDValue(Res, 0);
Eric Christopherb6729dc2010-08-04 23:03:04 +00008528 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00008529
Eric Christopher9a9d2752010-07-22 02:48:34 +00008530 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
Chris Lattner132929a2010-08-14 17:26:09 +00008531 if (!isDev)
Eric Christopher9a9d2752010-07-22 02:48:34 +00008532 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008533
Chris Lattner132929a2010-08-14 17:26:09 +00008534 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
8535 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
8536 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
8537 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
Michael J. Spencerec38de22010-10-10 22:04:20 +00008538
Chris Lattner132929a2010-08-14 17:26:09 +00008539 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
8540 if (!Op1 && !Op2 && !Op3 && Op4)
8541 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008542
Chris Lattner132929a2010-08-14 17:26:09 +00008543 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
8544 if (Op1 && !Op2 && !Op3 && !Op4)
8545 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008546
8547 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
Chris Lattner132929a2010-08-14 17:26:09 +00008548 // (MFENCE)>;
8549 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
Eric Christopher9a9d2752010-07-22 02:48:34 +00008550}
8551
Dan Gohmand858e902010-04-17 15:26:15 +00008552SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008553 EVT T = Op.getValueType();
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008554 DebugLoc DL = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +00008555 unsigned Reg = 0;
8556 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00008557 switch(T.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00008558 default:
8559 assert(false && "Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +00008560 case MVT::i8: Reg = X86::AL; size = 1; break;
8561 case MVT::i16: Reg = X86::AX; size = 2; break;
8562 case MVT::i32: Reg = X86::EAX; size = 4; break;
8563 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +00008564 assert(Subtarget->is64Bit() && "Node not type legal!");
8565 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +00008566 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00008567 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008568 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +00008569 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +00008570 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +00008571 Op.getOperand(1),
8572 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +00008573 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +00008574 cpIn.getValue(1) };
Owen Anderson825b72b2009-08-11 20:47:22 +00008575 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008576 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
8577 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
8578 Ops, 5, T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +00008579 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008580 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +00008581 return cpOut;
8582}
8583
Duncan Sands1607f052008-12-01 11:39:25 +00008584SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008585 SelectionDAG &DAG) const {
Duncan Sands1607f052008-12-01 11:39:25 +00008586 assert(Subtarget->is64Bit() && "Result not type legalized?");
Owen Anderson825b72b2009-08-11 20:47:22 +00008587 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Duncan Sands1607f052008-12-01 11:39:25 +00008588 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008589 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +00008590 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00008591 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
8592 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +00008593 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +00008594 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
8595 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +00008596 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +00008597 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +00008598 rdx.getValue(1)
8599 };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008600 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008601}
8602
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008603SDValue X86TargetLowering::LowerBITCAST(SDValue Op,
Dale Johannesen7d07b482010-05-21 00:52:33 +00008604 SelectionDAG &DAG) const {
8605 EVT SrcVT = Op.getOperand(0).getValueType();
8606 EVT DstVT = Op.getValueType();
Michael J. Spencerec38de22010-10-10 22:04:20 +00008607 assert((Subtarget->is64Bit() && !Subtarget->hasSSE2() &&
Dale Johannesen7d07b482010-05-21 00:52:33 +00008608 Subtarget->hasMMX() && !DisableMMX) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008609 "Unexpected custom BITCAST");
Michael J. Spencerec38de22010-10-10 22:04:20 +00008610 assert((DstVT == MVT::i64 ||
Dale Johannesen7d07b482010-05-21 00:52:33 +00008611 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008612 "Unexpected custom BITCAST");
Dale Johannesen7d07b482010-05-21 00:52:33 +00008613 // i64 <=> MMX conversions are Legal.
8614 if (SrcVT==MVT::i64 && DstVT.isVector())
8615 return Op;
8616 if (DstVT==MVT::i64 && SrcVT.isVector())
8617 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +00008618 // MMX <=> MMX conversions are Legal.
8619 if (SrcVT.isVector() && DstVT.isVector())
8620 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +00008621 // All other conversions need to be expanded.
8622 return SDValue();
8623}
Dan Gohmand858e902010-04-17 15:26:15 +00008624SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen71d1bf52008-09-29 22:25:26 +00008625 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +00008626 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008627 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008628 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +00008629 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008630 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008631 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +00008632 Node->getOperand(0),
8633 Node->getOperand(1), negOp,
8634 cast<AtomicSDNode>(Node)->getSrcValue(),
8635 cast<AtomicSDNode>(Node)->getAlignment());
Mon P Wang63307c32008-05-05 19:05:59 +00008636}
8637
Evan Cheng0db9fe62006-04-25 20:13:52 +00008638/// LowerOperation - Provide custom lowering hooks for some operations.
8639///
Dan Gohmand858e902010-04-17 15:26:15 +00008640SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00008641 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008642 default: llvm_unreachable("Should not custom lower this!");
Eric Christopher9a9d2752010-07-22 02:48:34 +00008643 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op,DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008644 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
8645 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008646 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00008647 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008648 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
8649 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
8650 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
8651 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
8652 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
8653 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00008654 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +00008655 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +00008656 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008657 case ISD::SHL_PARTS:
8658 case ISD::SRA_PARTS:
8659 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
8660 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00008661 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008662 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00008663 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008664 case ISD::FABS: return LowerFABS(Op, DAG);
8665 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008666 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00008667 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman30a0de92008-07-17 16:51:19 +00008668 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00008669 case ISD::SELECT: return LowerSELECT(Op, DAG);
8670 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008671 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008672 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +00008673 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +00008674 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008675 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00008676 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
8677 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008678 case ISD::FRAME_TO_ARGS_OFFSET:
8679 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008680 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008681 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008682 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +00008683 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +00008684 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
8685 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Mon P Wangaf9b9522008-12-18 21:42:19 +00008686 case ISD::MUL: return LowerMUL_V2I64(Op, DAG);
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008687 case ISD::SHL: return LowerSHL(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +00008688 case ISD::SADDO:
8689 case ISD::UADDO:
8690 case ISD::SSUBO:
8691 case ISD::USUBO:
8692 case ISD::SMULO:
8693 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +00008694 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008695 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008696 }
Chris Lattner27a6c732007-11-24 07:07:01 +00008697}
8698
Duncan Sands1607f052008-12-01 11:39:25 +00008699void X86TargetLowering::
8700ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00008701 SelectionDAG &DAG, unsigned NewOp) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008702 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008703 DebugLoc dl = Node->getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00008704 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +00008705
8706 SDValue Chain = Node->getOperand(0);
8707 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00008708 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00008709 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00008710 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00008711 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +00008712 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +00008713 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +00008714 SDValue Result =
8715 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
8716 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +00008717 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +00008718 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00008719 Results.push_back(Result.getValue(2));
8720}
8721
Duncan Sands126d9072008-07-04 11:47:58 +00008722/// ReplaceNodeResults - Replace a node with an illegal result type
8723/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00008724void X86TargetLowering::ReplaceNodeResults(SDNode *N,
8725 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00008726 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +00008727 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +00008728 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +00008729 default:
Duncan Sands1607f052008-12-01 11:39:25 +00008730 assert(false && "Do not know how to custom type legalize this operation!");
8731 return;
8732 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +00008733 std::pair<SDValue,SDValue> Vals =
8734 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +00008735 SDValue FIST = Vals.first, StackSlot = Vals.second;
8736 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +00008737 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +00008738 // Return a load from the stack slot.
Chris Lattner51abfe42010-09-21 06:02:19 +00008739 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
8740 MachinePointerInfo(), false, false, 0));
Duncan Sands1607f052008-12-01 11:39:25 +00008741 }
8742 return;
8743 }
8744 case ISD::READCYCLECOUNTER: {
Owen Anderson825b72b2009-08-11 20:47:22 +00008745 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Duncan Sands1607f052008-12-01 11:39:25 +00008746 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008747 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00008748 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +00008749 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00008750 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00008751 eax.getValue(2));
8752 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
8753 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +00008754 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00008755 Results.push_back(edx.getValue(1));
8756 return;
8757 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008758 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +00008759 EVT T = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008760 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
Duncan Sands1607f052008-12-01 11:39:25 +00008761 SDValue cpInL, cpInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00008762 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
8763 DAG.getConstant(0, MVT::i32));
8764 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
8765 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00008766 cpInL = DAG.getCopyToReg(N->getOperand(0), dl, X86::EAX, cpInL, SDValue());
8767 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl, X86::EDX, cpInH,
Duncan Sands1607f052008-12-01 11:39:25 +00008768 cpInL.getValue(1));
8769 SDValue swapInL, swapInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00008770 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
8771 DAG.getConstant(0, MVT::i32));
8772 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
8773 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00008774 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl, X86::EBX, swapInL,
Duncan Sands1607f052008-12-01 11:39:25 +00008775 cpInH.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00008776 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl, X86::ECX, swapInH,
Duncan Sands1607f052008-12-01 11:39:25 +00008777 swapInL.getValue(1));
8778 SDValue Ops[] = { swapInH.getValue(0),
8779 N->getOperand(1),
8780 swapInH.getValue(1) };
Owen Anderson825b72b2009-08-11 20:47:22 +00008781 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Andrew Trick1a2cf3b2010-10-11 19:02:04 +00008782 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
8783 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG8_DAG, dl, Tys,
8784 Ops, 3, T, MMO);
Dale Johannesendd64c412009-02-04 00:33:20 +00008785 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl, X86::EAX,
Owen Anderson825b72b2009-08-11 20:47:22 +00008786 MVT::i32, Result.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00008787 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl, X86::EDX,
Owen Anderson825b72b2009-08-11 20:47:22 +00008788 MVT::i32, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +00008789 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Owen Anderson825b72b2009-08-11 20:47:22 +00008790 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00008791 Results.push_back(cpOutH.getValue(1));
8792 return;
8793 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008794 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +00008795 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
8796 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008797 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +00008798 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
8799 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008800 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +00008801 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
8802 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008803 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +00008804 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
8805 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008806 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +00008807 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
8808 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008809 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +00008810 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
8811 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008812 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +00008813 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
8814 return;
Chris Lattner27a6c732007-11-24 07:07:01 +00008815 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00008816}
8817
Evan Cheng72261582005-12-20 06:22:03 +00008818const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
8819 switch (Opcode) {
8820 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +00008821 case X86ISD::BSF: return "X86ISD::BSF";
8822 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +00008823 case X86ISD::SHLD: return "X86ISD::SHLD";
8824 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00008825 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +00008826 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +00008827 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +00008828 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +00008829 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00008830 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00008831 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
8832 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
8833 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00008834 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00008835 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +00008836 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +00008837 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +00008838 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +00008839 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +00008840 case X86ISD::COMI: return "X86ISD::COMI";
8841 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +00008842 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +00008843 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Evan Cheng72261582005-12-20 06:22:03 +00008844 case X86ISD::CMOV: return "X86ISD::CMOV";
8845 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00008846 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00008847 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
8848 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +00008849 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00008850 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +00008851 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +00008852 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +00008853 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +00008854 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
8855 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +00008856 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +00008857 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Nate Begemanb65c1752010-12-17 22:55:37 +00008858 case X86ISD::PANDN: return "X86ISD::PANDN";
8859 case X86ISD::PSIGNB: return "X86ISD::PSIGNB";
8860 case X86ISD::PSIGNW: return "X86ISD::PSIGNW";
8861 case X86ISD::PSIGND: return "X86ISD::PSIGND";
Evan Cheng8ca29322006-11-10 21:43:37 +00008862 case X86ISD::FMAX: return "X86ISD::FMAX";
8863 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +00008864 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
8865 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00008866 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +00008867 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008868 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00008869 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008870 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +00008871 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
8872 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008873 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
8874 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
8875 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
8876 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
8877 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
8878 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +00008879 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
8880 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +00008881 case X86ISD::VSHL: return "X86ISD::VSHL";
8882 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +00008883 case X86ISD::CMPPD: return "X86ISD::CMPPD";
8884 case X86ISD::CMPPS: return "X86ISD::CMPPS";
8885 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
8886 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
8887 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
8888 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
8889 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
8890 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
8891 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
8892 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008893 case X86ISD::ADD: return "X86ISD::ADD";
8894 case X86ISD::SUB: return "X86ISD::SUB";
Bill Wendlingd350e022008-12-12 21:15:41 +00008895 case X86ISD::SMUL: return "X86ISD::SMUL";
8896 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +00008897 case X86ISD::INC: return "X86ISD::INC";
8898 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +00008899 case X86ISD::OR: return "X86ISD::OR";
8900 case X86ISD::XOR: return "X86ISD::XOR";
8901 case X86ISD::AND: return "X86ISD::AND";
Evan Cheng73f24c92009-03-30 21:36:47 +00008902 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +00008903 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008904 case X86ISD::TESTP: return "X86ISD::TESTP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00008905 case X86ISD::PALIGN: return "X86ISD::PALIGN";
8906 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
8907 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
8908 case X86ISD::PSHUFHW_LD: return "X86ISD::PSHUFHW_LD";
8909 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
8910 case X86ISD::PSHUFLW_LD: return "X86ISD::PSHUFLW_LD";
8911 case X86ISD::SHUFPS: return "X86ISD::SHUFPS";
8912 case X86ISD::SHUFPD: return "X86ISD::SHUFPD";
8913 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00008914 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00008915 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00008916 case X86ISD::MOVHLPD: return "X86ISD::MOVHLPD";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00008917 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
8918 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00008919 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
8920 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
8921 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
8922 case X86ISD::MOVSHDUP_LD: return "X86ISD::MOVSHDUP_LD";
8923 case X86ISD::MOVSLDUP_LD: return "X86ISD::MOVSLDUP_LD";
8924 case X86ISD::MOVSD: return "X86ISD::MOVSD";
8925 case X86ISD::MOVSS: return "X86ISD::MOVSS";
8926 case X86ISD::UNPCKLPS: return "X86ISD::UNPCKLPS";
8927 case X86ISD::UNPCKLPD: return "X86ISD::UNPCKLPD";
8928 case X86ISD::UNPCKHPS: return "X86ISD::UNPCKHPS";
8929 case X86ISD::UNPCKHPD: return "X86ISD::UNPCKHPD";
8930 case X86ISD::PUNPCKLBW: return "X86ISD::PUNPCKLBW";
8931 case X86ISD::PUNPCKLWD: return "X86ISD::PUNPCKLWD";
8932 case X86ISD::PUNPCKLDQ: return "X86ISD::PUNPCKLDQ";
8933 case X86ISD::PUNPCKLQDQ: return "X86ISD::PUNPCKLQDQ";
8934 case X86ISD::PUNPCKHBW: return "X86ISD::PUNPCKHBW";
8935 case X86ISD::PUNPCKHWD: return "X86ISD::PUNPCKHWD";
8936 case X86ISD::PUNPCKHDQ: return "X86ISD::PUNPCKHDQ";
8937 case X86ISD::PUNPCKHQDQ: return "X86ISD::PUNPCKHQDQ";
Dan Gohmand6708ea2009-08-15 01:38:56 +00008938 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Dan Gohman320afb82010-10-12 18:00:49 +00008939 case X86ISD::VAARG_64: return "X86ISD::VAARG_64";
Michael J. Spencere9c253e2010-10-21 01:41:01 +00008940 case X86ISD::WIN_ALLOCA: return "X86ISD::WIN_ALLOCA";
Evan Cheng72261582005-12-20 06:22:03 +00008941 }
8942}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008943
Chris Lattnerc9addb72007-03-30 23:15:24 +00008944// isLegalAddressingMode - Return true if the addressing mode represented
8945// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +00008946bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerc9addb72007-03-30 23:15:24 +00008947 const Type *Ty) const {
8948 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00008949 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +00008950 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +00008951
Chris Lattnerc9addb72007-03-30 23:15:24 +00008952 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00008953 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +00008954 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00008955
Chris Lattnerc9addb72007-03-30 23:15:24 +00008956 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +00008957 unsigned GVFlags =
8958 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00008959
Chris Lattnerdfed4132009-07-10 07:38:24 +00008960 // If a reference to this global requires an extra load, we can't fold it.
8961 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +00008962 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00008963
Chris Lattnerdfed4132009-07-10 07:38:24 +00008964 // If BaseGV requires a register for the PIC base, we cannot also have a
8965 // BaseReg specified.
8966 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +00008967 return false;
Evan Cheng52787842007-08-01 23:46:47 +00008968
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00008969 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +00008970 if ((M != CodeModel::Small || R != Reloc::Static) &&
8971 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00008972 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +00008973 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008974
Chris Lattnerc9addb72007-03-30 23:15:24 +00008975 switch (AM.Scale) {
8976 case 0:
8977 case 1:
8978 case 2:
8979 case 4:
8980 case 8:
8981 // These scales always work.
8982 break;
8983 case 3:
8984 case 5:
8985 case 9:
8986 // These scales are formed with basereg+scalereg. Only accept if there is
8987 // no basereg yet.
8988 if (AM.HasBaseReg)
8989 return false;
8990 break;
8991 default: // Other stuff never works.
8992 return false;
8993 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008994
Chris Lattnerc9addb72007-03-30 23:15:24 +00008995 return true;
8996}
8997
8998
Evan Cheng2bd122c2007-10-26 01:56:11 +00008999bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00009000 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +00009001 return false;
Evan Chenge127a732007-10-29 07:57:50 +00009002 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
9003 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00009004 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +00009005 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00009006 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +00009007}
9008
Owen Andersone50ed302009-08-10 22:56:29 +00009009bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +00009010 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009011 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009012 unsigned NumBits1 = VT1.getSizeInBits();
9013 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00009014 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009015 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00009016 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009017}
Evan Cheng2bd122c2007-10-26 01:56:11 +00009018
Dan Gohman97121ba2009-04-08 00:15:30 +00009019bool X86TargetLowering::isZExtFree(const Type *Ty1, const Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00009020 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00009021 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00009022}
9023
Owen Andersone50ed302009-08-10 22:56:29 +00009024bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00009025 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00009026 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00009027}
9028
Owen Andersone50ed302009-08-10 22:56:29 +00009029bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +00009030 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +00009031 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +00009032}
9033
Evan Cheng60c07e12006-07-05 22:17:51 +00009034/// isShuffleMaskLegal - Targets can use this to indicate that they only
9035/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
9036/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
9037/// are assumed to be legal.
9038bool
Eric Christopherfd179292009-08-27 18:07:15 +00009039X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +00009040 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +00009041 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +00009042 if (VT.getSizeInBits() == 64)
Eric Christophercff6f852010-04-15 01:40:20 +00009043 return isPALIGNRMask(M, VT, Subtarget->hasSSSE3());
Nate Begeman9008ca62009-04-27 18:41:29 +00009044
Nate Begemana09008b2009-10-19 02:17:23 +00009045 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +00009046 return (VT.getVectorNumElements() == 2 ||
9047 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
9048 isMOVLMask(M, VT) ||
9049 isSHUFPMask(M, VT) ||
9050 isPSHUFDMask(M, VT) ||
9051 isPSHUFHWMask(M, VT) ||
9052 isPSHUFLWMask(M, VT) ||
Nate Begemana09008b2009-10-19 02:17:23 +00009053 isPALIGNRMask(M, VT, Subtarget->hasSSSE3()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +00009054 isUNPCKLMask(M, VT) ||
9055 isUNPCKHMask(M, VT) ||
9056 isUNPCKL_v_undef_Mask(M, VT) ||
9057 isUNPCKH_v_undef_Mask(M, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00009058}
9059
Dan Gohman7d8143f2008-04-09 20:09:42 +00009060bool
Nate Begeman5a5ca152009-04-29 05:20:52 +00009061X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +00009062 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00009063 unsigned NumElts = VT.getVectorNumElements();
9064 // FIXME: This collection of masks seems suspect.
9065 if (NumElts == 2)
9066 return true;
9067 if (NumElts == 4 && VT.getSizeInBits() == 128) {
9068 return (isMOVLMask(Mask, VT) ||
9069 isCommutedMOVLMask(Mask, VT, true) ||
9070 isSHUFPMask(Mask, VT) ||
9071 isCommutedSHUFPMask(Mask, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00009072 }
9073 return false;
9074}
9075
9076//===----------------------------------------------------------------------===//
9077// X86 Scheduler Hooks
9078//===----------------------------------------------------------------------===//
9079
Mon P Wang63307c32008-05-05 19:05:59 +00009080// private utility function
9081MachineBasicBlock *
9082X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
9083 MachineBasicBlock *MBB,
9084 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009085 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009086 unsigned LoadOpc,
9087 unsigned CXchgOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009088 unsigned notOpc,
9089 unsigned EAXreg,
9090 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009091 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00009092 // For the atomic bitwise operator, we generate
9093 // thisMBB:
9094 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00009095 // ld t1 = [bitinstr.addr]
9096 // op t2 = t1, [bitinstr.val]
9097 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00009098 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
9099 // bz newMBB
9100 // fallthrough -->nextMBB
9101 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9102 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009103 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00009104 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009105
Mon P Wang63307c32008-05-05 19:05:59 +00009106 /// First build the CFG
9107 MachineFunction *F = MBB->getParent();
9108 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009109 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9110 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9111 F->insert(MBBIter, newMBB);
9112 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009113
Dan Gohman14152b42010-07-06 20:24:04 +00009114 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9115 nextMBB->splice(nextMBB->begin(), thisMBB,
9116 llvm::next(MachineBasicBlock::iterator(bInstr)),
9117 thisMBB->end());
9118 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009119
Mon P Wang63307c32008-05-05 19:05:59 +00009120 // Update thisMBB to fall through to newMBB
9121 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009122
Mon P Wang63307c32008-05-05 19:05:59 +00009123 // newMBB jumps to itself and fall through to nextMBB
9124 newMBB->addSuccessor(nextMBB);
9125 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009126
Mon P Wang63307c32008-05-05 19:05:59 +00009127 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009128 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009129 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +00009130 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00009131 MachineOperand& destOper = bInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009132 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00009133 int numArgs = bInstr->getNumOperands() - 1;
9134 for (int i=0; i < numArgs; ++i)
9135 argOpers[i] = &bInstr->getOperand(i+1);
9136
9137 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009138 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009139 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00009140
Dale Johannesen140be2d2008-08-19 18:47:28 +00009141 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009142 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00009143 for (int i=0; i <= lastAddrIndx; ++i)
9144 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009145
Dale Johannesen140be2d2008-08-19 18:47:28 +00009146 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009147 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00009148 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009149 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009150 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009151 tt = t1;
9152
Dale Johannesen140be2d2008-08-19 18:47:28 +00009153 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +00009154 assert((argOpers[valArgIndx]->isReg() ||
9155 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00009156 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +00009157 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009158 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00009159 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009160 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009161 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +00009162 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009163
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009164 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +00009165 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009166
Dale Johannesene4d209d2009-02-03 20:21:25 +00009167 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +00009168 for (int i=0; i <= lastAddrIndx; ++i)
9169 (*MIB).addOperand(*argOpers[i]);
9170 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +00009171 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00009172 (*MIB).setMemRefs(bInstr->memoperands_begin(),
9173 bInstr->memoperands_end());
Mon P Wangf5952662008-07-17 04:54:06 +00009174
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009175 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +00009176 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +00009177
Mon P Wang63307c32008-05-05 19:05:59 +00009178 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009179 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00009180
Dan Gohman14152b42010-07-06 20:24:04 +00009181 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00009182 return nextMBB;
9183}
9184
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00009185// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +00009186MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009187X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
9188 MachineBasicBlock *MBB,
9189 unsigned regOpcL,
9190 unsigned regOpcH,
9191 unsigned immOpcL,
9192 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009193 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009194 // For the atomic bitwise operator, we generate
9195 // thisMBB (instructions are in pairs, except cmpxchg8b)
9196 // ld t1,t2 = [bitinstr.addr]
9197 // newMBB:
9198 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
9199 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +00009200 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009201 // mov ECX, EBX <- t5, t6
9202 // mov EAX, EDX <- t1, t2
9203 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
9204 // mov t3, t4 <- EAX, EDX
9205 // bz newMBB
9206 // result in out1, out2
9207 // fallthrough -->nextMBB
9208
9209 const TargetRegisterClass *RC = X86::GR32RegisterClass;
9210 const unsigned LoadOpc = X86::MOV32rm;
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009211 const unsigned NotOpc = X86::NOT32r;
9212 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9213 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
9214 MachineFunction::iterator MBBIter = MBB;
9215 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009216
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009217 /// First build the CFG
9218 MachineFunction *F = MBB->getParent();
9219 MachineBasicBlock *thisMBB = MBB;
9220 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9221 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9222 F->insert(MBBIter, newMBB);
9223 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009224
Dan Gohman14152b42010-07-06 20:24:04 +00009225 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9226 nextMBB->splice(nextMBB->begin(), thisMBB,
9227 llvm::next(MachineBasicBlock::iterator(bInstr)),
9228 thisMBB->end());
9229 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009230
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009231 // Update thisMBB to fall through to newMBB
9232 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009233
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009234 // newMBB jumps to itself and fall through to nextMBB
9235 newMBB->addSuccessor(nextMBB);
9236 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009237
Dale Johannesene4d209d2009-02-03 20:21:25 +00009238 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009239 // Insert instructions into newMBB based on incoming instruction
9240 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009241 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009242 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009243 MachineOperand& dest1Oper = bInstr->getOperand(0);
9244 MachineOperand& dest2Oper = bInstr->getOperand(1);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009245 MachineOperand* argOpers[2 + X86::AddrNumOperands];
9246 for (int i=0; i < 2 + X86::AddrNumOperands; ++i) {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009247 argOpers[i] = &bInstr->getOperand(i+2);
9248
Dan Gohman71ea4e52010-05-14 21:01:44 +00009249 // We use some of the operands multiple times, so conservatively just
9250 // clear any kill flags that might be present.
9251 if (argOpers[i]->isReg() && argOpers[i]->isUse())
9252 argOpers[i]->setIsKill(false);
9253 }
9254
Evan Chengad5b52f2010-01-08 19:14:57 +00009255 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009256 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +00009257
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009258 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009259 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009260 for (int i=0; i <= lastAddrIndx; ++i)
9261 (*MIB).addOperand(*argOpers[i]);
9262 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009263 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +00009264 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +00009265 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009266 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +00009267 MachineOperand newOp3 = *(argOpers[3]);
9268 if (newOp3.isImm())
9269 newOp3.setImm(newOp3.getImm()+4);
9270 else
9271 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009272 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +00009273 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009274
9275 // t3/4 are defined later, at the bottom of the loop
9276 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
9277 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009278 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009279 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009280 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009281 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
9282
Evan Cheng306b4ca2010-01-08 23:41:50 +00009283 // The subsequent operations should be using the destination registers of
9284 //the PHI instructions.
Scott Michelfdc40a02009-02-17 22:15:04 +00009285 if (invSrc) {
Evan Cheng306b4ca2010-01-08 23:41:50 +00009286 t1 = F->getRegInfo().createVirtualRegister(RC);
9287 t2 = F->getRegInfo().createVirtualRegister(RC);
9288 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t1).addReg(dest1Oper.getReg());
9289 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t2).addReg(dest2Oper.getReg());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009290 } else {
Evan Cheng306b4ca2010-01-08 23:41:50 +00009291 t1 = dest1Oper.getReg();
9292 t2 = dest2Oper.getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009293 }
9294
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009295 int valArgIndx = lastAddrIndx + 1;
9296 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +00009297 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009298 "invalid operand");
9299 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
9300 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009301 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009302 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009303 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009304 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +00009305 if (regOpcL != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00009306 MIB.addReg(t1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009307 (*MIB).addOperand(*argOpers[valArgIndx]);
9308 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00009309 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009310 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00009311 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009312 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009313 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009314 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009315 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +00009316 if (regOpcH != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00009317 MIB.addReg(t2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009318 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009319
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009320 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009321 MIB.addReg(t1);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009322 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009323 MIB.addReg(t2);
9324
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009325 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009326 MIB.addReg(t5);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009327 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009328 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +00009329
Dale Johannesene4d209d2009-02-03 20:21:25 +00009330 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009331 for (int i=0; i <= lastAddrIndx; ++i)
9332 (*MIB).addOperand(*argOpers[i]);
9333
9334 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00009335 (*MIB).setMemRefs(bInstr->memoperands_begin(),
9336 bInstr->memoperands_end());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009337
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009338 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009339 MIB.addReg(X86::EAX);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009340 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009341 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +00009342
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009343 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009344 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009345
Dan Gohman14152b42010-07-06 20:24:04 +00009346 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009347 return nextMBB;
9348}
9349
9350// private utility function
9351MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +00009352X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
9353 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009354 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00009355 // For the atomic min/max operator, we generate
9356 // thisMBB:
9357 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00009358 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +00009359 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +00009360 // cmp t1, t2
9361 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +00009362 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00009363 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
9364 // bz newMBB
9365 // fallthrough -->nextMBB
9366 //
9367 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9368 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009369 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00009370 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009371
Mon P Wang63307c32008-05-05 19:05:59 +00009372 /// First build the CFG
9373 MachineFunction *F = MBB->getParent();
9374 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009375 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9376 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9377 F->insert(MBBIter, newMBB);
9378 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009379
Dan Gohman14152b42010-07-06 20:24:04 +00009380 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9381 nextMBB->splice(nextMBB->begin(), thisMBB,
9382 llvm::next(MachineBasicBlock::iterator(mInstr)),
9383 thisMBB->end());
9384 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009385
Mon P Wang63307c32008-05-05 19:05:59 +00009386 // Update thisMBB to fall through to newMBB
9387 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009388
Mon P Wang63307c32008-05-05 19:05:59 +00009389 // newMBB jumps to newMBB and fall through to nextMBB
9390 newMBB->addSuccessor(nextMBB);
9391 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009392
Dale Johannesene4d209d2009-02-03 20:21:25 +00009393 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00009394 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009395 assert(mInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009396 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +00009397 MachineOperand& destOper = mInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009398 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00009399 int numArgs = mInstr->getNumOperands() - 1;
9400 for (int i=0; i < numArgs; ++i)
9401 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009402
Mon P Wang63307c32008-05-05 19:05:59 +00009403 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009404 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009405 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00009406
Mon P Wangab3e7472008-05-05 22:56:23 +00009407 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009408 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00009409 for (int i=0; i <= lastAddrIndx; ++i)
9410 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +00009411
Mon P Wang63307c32008-05-05 19:05:59 +00009412 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +00009413 assert((argOpers[valArgIndx]->isReg() ||
9414 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00009415 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +00009416
9417 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +00009418 if (argOpers[valArgIndx]->isReg())
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009419 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +00009420 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009421 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00009422 (*MIB).addOperand(*argOpers[valArgIndx]);
9423
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009424 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +00009425 MIB.addReg(t1);
9426
Dale Johannesene4d209d2009-02-03 20:21:25 +00009427 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +00009428 MIB.addReg(t1);
9429 MIB.addReg(t2);
9430
9431 // Generate movc
9432 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009433 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +00009434 MIB.addReg(t2);
9435 MIB.addReg(t1);
9436
9437 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +00009438 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +00009439 for (int i=0; i <= lastAddrIndx; ++i)
9440 (*MIB).addOperand(*argOpers[i]);
9441 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +00009442 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00009443 (*MIB).setMemRefs(mInstr->memoperands_begin(),
9444 mInstr->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +00009445
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009446 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +00009447 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +00009448
Mon P Wang63307c32008-05-05 19:05:59 +00009449 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009450 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00009451
Dan Gohman14152b42010-07-06 20:24:04 +00009452 mInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00009453 return nextMBB;
9454}
9455
Eric Christopherf83a5de2009-08-27 18:08:16 +00009456// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00009457// or XMM0_V32I8 in AVX all of this code can be replaced with that
9458// in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +00009459MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +00009460X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +00009461 unsigned numArgs, bool memArg) const {
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00009462 assert((Subtarget->hasSSE42() || Subtarget->hasAVX()) &&
9463 "Target must have SSE4.2 or AVX features enabled");
9464
Eric Christopherb120ab42009-08-18 22:50:32 +00009465 DebugLoc dl = MI->getDebugLoc();
9466 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Eric Christopherb120ab42009-08-18 22:50:32 +00009467 unsigned Opc;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00009468 if (!Subtarget->hasAVX()) {
9469 if (memArg)
9470 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
9471 else
9472 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
9473 } else {
9474 if (memArg)
9475 Opc = numArgs == 3 ? X86::VPCMPISTRM128rm : X86::VPCMPESTRM128rm;
9476 else
9477 Opc = numArgs == 3 ? X86::VPCMPISTRM128rr : X86::VPCMPESTRM128rr;
9478 }
Eric Christopherb120ab42009-08-18 22:50:32 +00009479
Eric Christopher41c902f2010-11-30 08:20:21 +00009480 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Eric Christopherb120ab42009-08-18 22:50:32 +00009481 for (unsigned i = 0; i < numArgs; ++i) {
9482 MachineOperand &Op = MI->getOperand(i+1);
Eric Christopherb120ab42009-08-18 22:50:32 +00009483 if (!(Op.isReg() && Op.isImplicit()))
9484 MIB.addOperand(Op);
9485 }
Eric Christopher41c902f2010-11-30 08:20:21 +00009486 BuildMI(*BB, MI, dl, TII->get(X86::MOVAPSrr), MI->getOperand(0).getReg())
Eric Christopherb120ab42009-08-18 22:50:32 +00009487 .addReg(X86::XMM0);
9488
Dan Gohman14152b42010-07-06 20:24:04 +00009489 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +00009490 return BB;
9491}
9492
9493MachineBasicBlock *
Eric Christopher228232b2010-11-30 07:20:12 +00009494X86TargetLowering::EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +00009495 DebugLoc dl = MI->getDebugLoc();
9496 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9497
9498 // Address into RAX/EAX, other two args into ECX, EDX.
9499 unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
9500 unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
9501 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg);
9502 for (int i = 0; i < X86::AddrNumOperands; ++i)
Eric Christopher82be2202010-11-30 08:10:28 +00009503 MIB.addOperand(MI->getOperand(i));
Eric Christopher228232b2010-11-30 07:20:12 +00009504
9505 unsigned ValOps = X86::AddrNumOperands;
9506 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
9507 .addReg(MI->getOperand(ValOps).getReg());
9508 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX)
9509 .addReg(MI->getOperand(ValOps+1).getReg());
9510
9511 // The instruction doesn't actually take any operands though.
9512 BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr));
9513
9514 MI->eraseFromParent(); // The pseudo is gone now.
9515 return BB;
9516}
9517
9518MachineBasicBlock *
9519X86TargetLowering::EmitMwait(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +00009520 DebugLoc dl = MI->getDebugLoc();
9521 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9522
9523 // First arg in ECX, the second in EAX.
9524 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
9525 .addReg(MI->getOperand(0).getReg());
9526 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EAX)
9527 .addReg(MI->getOperand(1).getReg());
9528
9529 // The instruction doesn't actually take any operands though.
9530 BuildMI(*BB, MI, dl, TII->get(X86::MWAITrr));
9531
9532 MI->eraseFromParent(); // The pseudo is gone now.
9533 return BB;
9534}
9535
9536MachineBasicBlock *
Dan Gohman320afb82010-10-12 18:00:49 +00009537X86TargetLowering::EmitVAARG64WithCustomInserter(
9538 MachineInstr *MI,
9539 MachineBasicBlock *MBB) const {
9540 // Emit va_arg instruction on X86-64.
9541
9542 // Operands to this pseudo-instruction:
9543 // 0 ) Output : destination address (reg)
9544 // 1-5) Input : va_list address (addr, i64mem)
9545 // 6 ) ArgSize : Size (in bytes) of vararg type
9546 // 7 ) ArgMode : 0=overflow only, 1=use gp_offset, 2=use fp_offset
9547 // 8 ) Align : Alignment of type
9548 // 9 ) EFLAGS (implicit-def)
9549
9550 assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
9551 assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands");
9552
9553 unsigned DestReg = MI->getOperand(0).getReg();
9554 MachineOperand &Base = MI->getOperand(1);
9555 MachineOperand &Scale = MI->getOperand(2);
9556 MachineOperand &Index = MI->getOperand(3);
9557 MachineOperand &Disp = MI->getOperand(4);
9558 MachineOperand &Segment = MI->getOperand(5);
9559 unsigned ArgSize = MI->getOperand(6).getImm();
9560 unsigned ArgMode = MI->getOperand(7).getImm();
9561 unsigned Align = MI->getOperand(8).getImm();
9562
9563 // Memory Reference
9564 assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand");
9565 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
9566 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
9567
9568 // Machine Information
9569 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9570 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
9571 const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64);
9572 const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32);
9573 DebugLoc DL = MI->getDebugLoc();
9574
9575 // struct va_list {
9576 // i32 gp_offset
9577 // i32 fp_offset
9578 // i64 overflow_area (address)
9579 // i64 reg_save_area (address)
9580 // }
9581 // sizeof(va_list) = 24
9582 // alignment(va_list) = 8
9583
9584 unsigned TotalNumIntRegs = 6;
9585 unsigned TotalNumXMMRegs = 8;
9586 bool UseGPOffset = (ArgMode == 1);
9587 bool UseFPOffset = (ArgMode == 2);
9588 unsigned MaxOffset = TotalNumIntRegs * 8 +
9589 (UseFPOffset ? TotalNumXMMRegs * 16 : 0);
9590
9591 /* Align ArgSize to a multiple of 8 */
9592 unsigned ArgSizeA8 = (ArgSize + 7) & ~7;
9593 bool NeedsAlign = (Align > 8);
9594
9595 MachineBasicBlock *thisMBB = MBB;
9596 MachineBasicBlock *overflowMBB;
9597 MachineBasicBlock *offsetMBB;
9598 MachineBasicBlock *endMBB;
9599
9600 unsigned OffsetDestReg = 0; // Argument address computed by offsetMBB
9601 unsigned OverflowDestReg = 0; // Argument address computed by overflowMBB
9602 unsigned OffsetReg = 0;
9603
9604 if (!UseGPOffset && !UseFPOffset) {
9605 // If we only pull from the overflow region, we don't create a branch.
9606 // We don't need to alter control flow.
9607 OffsetDestReg = 0; // unused
9608 OverflowDestReg = DestReg;
9609
9610 offsetMBB = NULL;
9611 overflowMBB = thisMBB;
9612 endMBB = thisMBB;
9613 } else {
9614 // First emit code to check if gp_offset (or fp_offset) is below the bound.
9615 // If so, pull the argument from reg_save_area. (branch to offsetMBB)
9616 // If not, pull from overflow_area. (branch to overflowMBB)
9617 //
9618 // thisMBB
9619 // | .
9620 // | .
9621 // offsetMBB overflowMBB
9622 // | .
9623 // | .
9624 // endMBB
9625
9626 // Registers for the PHI in endMBB
9627 OffsetDestReg = MRI.createVirtualRegister(AddrRegClass);
9628 OverflowDestReg = MRI.createVirtualRegister(AddrRegClass);
9629
9630 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
9631 MachineFunction *MF = MBB->getParent();
9632 overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB);
9633 offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB);
9634 endMBB = MF->CreateMachineBasicBlock(LLVM_BB);
9635
9636 MachineFunction::iterator MBBIter = MBB;
9637 ++MBBIter;
9638
9639 // Insert the new basic blocks
9640 MF->insert(MBBIter, offsetMBB);
9641 MF->insert(MBBIter, overflowMBB);
9642 MF->insert(MBBIter, endMBB);
9643
9644 // Transfer the remainder of MBB and its successor edges to endMBB.
9645 endMBB->splice(endMBB->begin(), thisMBB,
9646 llvm::next(MachineBasicBlock::iterator(MI)),
9647 thisMBB->end());
9648 endMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
9649
9650 // Make offsetMBB and overflowMBB successors of thisMBB
9651 thisMBB->addSuccessor(offsetMBB);
9652 thisMBB->addSuccessor(overflowMBB);
9653
9654 // endMBB is a successor of both offsetMBB and overflowMBB
9655 offsetMBB->addSuccessor(endMBB);
9656 overflowMBB->addSuccessor(endMBB);
9657
9658 // Load the offset value into a register
9659 OffsetReg = MRI.createVirtualRegister(OffsetRegClass);
9660 BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg)
9661 .addOperand(Base)
9662 .addOperand(Scale)
9663 .addOperand(Index)
9664 .addDisp(Disp, UseFPOffset ? 4 : 0)
9665 .addOperand(Segment)
9666 .setMemRefs(MMOBegin, MMOEnd);
9667
9668 // Check if there is enough room left to pull this argument.
9669 BuildMI(thisMBB, DL, TII->get(X86::CMP32ri))
9670 .addReg(OffsetReg)
9671 .addImm(MaxOffset + 8 - ArgSizeA8);
9672
9673 // Branch to "overflowMBB" if offset >= max
9674 // Fall through to "offsetMBB" otherwise
9675 BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE)))
9676 .addMBB(overflowMBB);
9677 }
9678
9679 // In offsetMBB, emit code to use the reg_save_area.
9680 if (offsetMBB) {
9681 assert(OffsetReg != 0);
9682
9683 // Read the reg_save_area address.
9684 unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass);
9685 BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg)
9686 .addOperand(Base)
9687 .addOperand(Scale)
9688 .addOperand(Index)
9689 .addDisp(Disp, 16)
9690 .addOperand(Segment)
9691 .setMemRefs(MMOBegin, MMOEnd);
9692
9693 // Zero-extend the offset
9694 unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass);
9695 BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64)
9696 .addImm(0)
9697 .addReg(OffsetReg)
9698 .addImm(X86::sub_32bit);
9699
9700 // Add the offset to the reg_save_area to get the final address.
9701 BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg)
9702 .addReg(OffsetReg64)
9703 .addReg(RegSaveReg);
9704
9705 // Compute the offset for the next argument
9706 unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass);
9707 BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg)
9708 .addReg(OffsetReg)
9709 .addImm(UseFPOffset ? 16 : 8);
9710
9711 // Store it back into the va_list.
9712 BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr))
9713 .addOperand(Base)
9714 .addOperand(Scale)
9715 .addOperand(Index)
9716 .addDisp(Disp, UseFPOffset ? 4 : 0)
9717 .addOperand(Segment)
9718 .addReg(NextOffsetReg)
9719 .setMemRefs(MMOBegin, MMOEnd);
9720
9721 // Jump to endMBB
9722 BuildMI(offsetMBB, DL, TII->get(X86::JMP_4))
9723 .addMBB(endMBB);
9724 }
9725
9726 //
9727 // Emit code to use overflow area
9728 //
9729
9730 // Load the overflow_area address into a register.
9731 unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass);
9732 BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg)
9733 .addOperand(Base)
9734 .addOperand(Scale)
9735 .addOperand(Index)
9736 .addDisp(Disp, 8)
9737 .addOperand(Segment)
9738 .setMemRefs(MMOBegin, MMOEnd);
9739
9740 // If we need to align it, do so. Otherwise, just copy the address
9741 // to OverflowDestReg.
9742 if (NeedsAlign) {
9743 // Align the overflow address
9744 assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2");
9745 unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass);
9746
9747 // aligned_addr = (addr + (align-1)) & ~(align-1)
9748 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg)
9749 .addReg(OverflowAddrReg)
9750 .addImm(Align-1);
9751
9752 BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg)
9753 .addReg(TmpReg)
9754 .addImm(~(uint64_t)(Align-1));
9755 } else {
9756 BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg)
9757 .addReg(OverflowAddrReg);
9758 }
9759
9760 // Compute the next overflow address after this argument.
9761 // (the overflow address should be kept 8-byte aligned)
9762 unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass);
9763 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg)
9764 .addReg(OverflowDestReg)
9765 .addImm(ArgSizeA8);
9766
9767 // Store the new overflow address.
9768 BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr))
9769 .addOperand(Base)
9770 .addOperand(Scale)
9771 .addOperand(Index)
9772 .addDisp(Disp, 8)
9773 .addOperand(Segment)
9774 .addReg(NextAddrReg)
9775 .setMemRefs(MMOBegin, MMOEnd);
9776
9777 // If we branched, emit the PHI to the front of endMBB.
9778 if (offsetMBB) {
9779 BuildMI(*endMBB, endMBB->begin(), DL,
9780 TII->get(X86::PHI), DestReg)
9781 .addReg(OffsetDestReg).addMBB(offsetMBB)
9782 .addReg(OverflowDestReg).addMBB(overflowMBB);
9783 }
9784
9785 // Erase the pseudo instruction
9786 MI->eraseFromParent();
9787
9788 return endMBB;
9789}
9790
9791MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +00009792X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
9793 MachineInstr *MI,
9794 MachineBasicBlock *MBB) const {
9795 // Emit code to save XMM registers to the stack. The ABI says that the
9796 // number of registers to save is given in %al, so it's theoretically
9797 // possible to do an indirect jump trick to avoid saving all of them,
9798 // however this code takes a simpler approach and just executes all
9799 // of the stores if %al is non-zero. It's less code, and it's probably
9800 // easier on the hardware branch predictor, and stores aren't all that
9801 // expensive anyway.
9802
9803 // Create the new basic blocks. One block contains all the XMM stores,
9804 // and one block is the final destination regardless of whether any
9805 // stores were performed.
9806 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
9807 MachineFunction *F = MBB->getParent();
9808 MachineFunction::iterator MBBIter = MBB;
9809 ++MBBIter;
9810 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
9811 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
9812 F->insert(MBBIter, XMMSaveMBB);
9813 F->insert(MBBIter, EndMBB);
9814
Dan Gohman14152b42010-07-06 20:24:04 +00009815 // Transfer the remainder of MBB and its successor edges to EndMBB.
9816 EndMBB->splice(EndMBB->begin(), MBB,
9817 llvm::next(MachineBasicBlock::iterator(MI)),
9818 MBB->end());
9819 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
9820
Dan Gohmand6708ea2009-08-15 01:38:56 +00009821 // The original block will now fall through to the XMM save block.
9822 MBB->addSuccessor(XMMSaveMBB);
9823 // The XMMSaveMBB will fall through to the end block.
9824 XMMSaveMBB->addSuccessor(EndMBB);
9825
9826 // Now add the instructions.
9827 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9828 DebugLoc DL = MI->getDebugLoc();
9829
9830 unsigned CountReg = MI->getOperand(0).getReg();
9831 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
9832 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
9833
9834 if (!Subtarget->isTargetWin64()) {
9835 // If %al is 0, branch around the XMM save block.
9836 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009837 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +00009838 MBB->addSuccessor(EndMBB);
9839 }
9840
9841 // In the XMM save block, save all the XMM argument registers.
9842 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
9843 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +00009844 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +00009845 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +00009846 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +00009847 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +00009848 /*Size=*/16, /*Align=*/16);
Dan Gohmand6708ea2009-08-15 01:38:56 +00009849 BuildMI(XMMSaveMBB, DL, TII->get(X86::MOVAPSmr))
9850 .addFrameIndex(RegSaveFrameIndex)
9851 .addImm(/*Scale=*/1)
9852 .addReg(/*IndexReg=*/0)
9853 .addImm(/*Disp=*/Offset)
9854 .addReg(/*Segment=*/0)
9855 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +00009856 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +00009857 }
9858
Dan Gohman14152b42010-07-06 20:24:04 +00009859 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +00009860
9861 return EndMBB;
9862}
Mon P Wang63307c32008-05-05 19:05:59 +00009863
Evan Cheng60c07e12006-07-05 22:17:51 +00009864MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +00009865X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00009866 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +00009867 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9868 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +00009869
Chris Lattner52600972009-09-02 05:57:00 +00009870 // To "insert" a SELECT_CC instruction, we actually have to insert the
9871 // diamond control-flow pattern. The incoming instruction knows the
9872 // destination vreg to set, the condition code register to branch on, the
9873 // true/false values to select between, and a branch opcode to use.
9874 const BasicBlock *LLVM_BB = BB->getBasicBlock();
9875 MachineFunction::iterator It = BB;
9876 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +00009877
Chris Lattner52600972009-09-02 05:57:00 +00009878 // thisMBB:
9879 // ...
9880 // TrueVal = ...
9881 // cmpTY ccX, r1, r2
9882 // bCC copy1MBB
9883 // fallthrough --> copy0MBB
9884 MachineBasicBlock *thisMBB = BB;
9885 MachineFunction *F = BB->getParent();
9886 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
9887 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +00009888 F->insert(It, copy0MBB);
9889 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +00009890
Bill Wendling730c07e2010-06-25 20:48:10 +00009891 // If the EFLAGS register isn't dead in the terminator, then claim that it's
9892 // live into the sink and copy blocks.
9893 const MachineFunction *MF = BB->getParent();
9894 const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo();
9895 BitVector ReservedRegs = TRI->getReservedRegs(*MF);
Bill Wendling730c07e2010-06-25 20:48:10 +00009896
Dan Gohman14152b42010-07-06 20:24:04 +00009897 for (unsigned I = 0, E = MI->getNumOperands(); I != E; ++I) {
9898 const MachineOperand &MO = MI->getOperand(I);
9899 if (!MO.isReg() || !MO.isUse() || MO.isKill()) continue;
Bill Wendling730c07e2010-06-25 20:48:10 +00009900 unsigned Reg = MO.getReg();
9901 if (Reg != X86::EFLAGS) continue;
9902 copy0MBB->addLiveIn(Reg);
9903 sinkMBB->addLiveIn(Reg);
9904 }
9905
Dan Gohman14152b42010-07-06 20:24:04 +00009906 // Transfer the remainder of BB and its successor edges to sinkMBB.
9907 sinkMBB->splice(sinkMBB->begin(), BB,
9908 llvm::next(MachineBasicBlock::iterator(MI)),
9909 BB->end());
9910 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
9911
9912 // Add the true and fallthrough blocks as its successors.
9913 BB->addSuccessor(copy0MBB);
9914 BB->addSuccessor(sinkMBB);
9915
9916 // Create the conditional branch instruction.
9917 unsigned Opc =
9918 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
9919 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
9920
Chris Lattner52600972009-09-02 05:57:00 +00009921 // copy0MBB:
9922 // %FalseValue = ...
9923 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +00009924 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +00009925
Chris Lattner52600972009-09-02 05:57:00 +00009926 // sinkMBB:
9927 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
9928 // ...
Dan Gohman14152b42010-07-06 20:24:04 +00009929 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
9930 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +00009931 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
9932 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
9933
Dan Gohman14152b42010-07-06 20:24:04 +00009934 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +00009935 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +00009936}
9937
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00009938MachineBasicBlock *
Michael J. Spencere9c253e2010-10-21 01:41:01 +00009939X86TargetLowering::EmitLoweredWinAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00009940 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00009941 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9942 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00009943
9944 // The lowering is pretty easy: we're just emitting the call to _alloca. The
9945 // non-trivial part is impdef of ESP.
9946 // FIXME: The code should be tweaked as soon as we'll try to do codegen for
9947 // mingw-w64.
9948
Michael J. Spencere9c253e2010-10-21 01:41:01 +00009949 const char *StackProbeSymbol =
9950 Subtarget->isTargetWindows() ? "_chkstk" : "_alloca";
9951
Dan Gohman14152b42010-07-06 20:24:04 +00009952 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
Michael J. Spencere9c253e2010-10-21 01:41:01 +00009953 .addExternalSymbol(StackProbeSymbol)
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00009954 .addReg(X86::EAX, RegState::Implicit)
9955 .addReg(X86::ESP, RegState::Implicit)
9956 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
Anton Korobeynikov9f7f83b2010-08-25 07:50:11 +00009957 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
9958 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00009959
Dan Gohman14152b42010-07-06 20:24:04 +00009960 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00009961 return BB;
9962}
Chris Lattner52600972009-09-02 05:57:00 +00009963
9964MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +00009965X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
9966 MachineBasicBlock *BB) const {
9967 // This is pretty easy. We're taking the value that we received from
9968 // our load from the relocation, sticking it in either RDI (x86-64)
9969 // or EAX and doing an indirect call. The return value will then
9970 // be in the normal return register.
Michael J. Spencerec38de22010-10-10 22:04:20 +00009971 const X86InstrInfo *TII
Eric Christopher54415362010-06-08 22:04:25 +00009972 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +00009973 DebugLoc DL = MI->getDebugLoc();
9974 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +00009975
9976 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +00009977 assert(MI->getOperand(3).isGlobal() && "This should be a global");
Michael J. Spencerec38de22010-10-10 22:04:20 +00009978
Eric Christopher30ef0e52010-06-03 04:07:48 +00009979 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +00009980 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
9981 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +00009982 .addReg(X86::RIP)
9983 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +00009984 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +00009985 MI->getOperand(3).getTargetFlags())
9986 .addReg(0);
Eric Christopher722d3152010-09-27 06:01:51 +00009987 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +00009988 addDirectMem(MIB, X86::RDI);
Eric Christopher61025492010-06-15 23:08:42 +00009989 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +00009990 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
9991 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +00009992 .addReg(0)
9993 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +00009994 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher61025492010-06-15 23:08:42 +00009995 MI->getOperand(3).getTargetFlags())
9996 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +00009997 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +00009998 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +00009999 } else {
Dan Gohman14152b42010-07-06 20:24:04 +000010000 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
10001 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +000010002 .addReg(TII->getGlobalBaseReg(F))
10003 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000010004 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000010005 MI->getOperand(3).getTargetFlags())
10006 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000010007 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000010008 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010009 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000010010
Dan Gohman14152b42010-07-06 20:24:04 +000010011 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +000010012 return BB;
10013}
10014
10015MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +000010016X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010017 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +000010018 switch (MI->getOpcode()) {
10019 default: assert(false && "Unexpected instr type to insert");
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010020 case X86::WIN_ALLOCA:
10021 return EmitLoweredWinAlloca(MI, BB);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010022 case X86::TLSCall_32:
10023 case X86::TLSCall_64:
10024 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +000010025 case X86::CMOV_GR8:
Evan Cheng60c07e12006-07-05 22:17:51 +000010026 case X86::CMOV_FR32:
10027 case X86::CMOV_FR64:
10028 case X86::CMOV_V4F32:
10029 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +000010030 case X86::CMOV_V2I64:
Chris Lattner314a1132010-03-14 18:31:44 +000010031 case X86::CMOV_GR16:
10032 case X86::CMOV_GR32:
10033 case X86::CMOV_RFP32:
10034 case X86::CMOV_RFP64:
10035 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010036 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000010037
Dale Johannesen849f2142007-07-03 00:53:03 +000010038 case X86::FP32_TO_INT16_IN_MEM:
10039 case X86::FP32_TO_INT32_IN_MEM:
10040 case X86::FP32_TO_INT64_IN_MEM:
10041 case X86::FP64_TO_INT16_IN_MEM:
10042 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +000010043 case X86::FP64_TO_INT64_IN_MEM:
10044 case X86::FP80_TO_INT16_IN_MEM:
10045 case X86::FP80_TO_INT32_IN_MEM:
10046 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +000010047 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10048 DebugLoc DL = MI->getDebugLoc();
10049
Evan Cheng60c07e12006-07-05 22:17:51 +000010050 // Change the floating point control register to use "round towards zero"
10051 // mode when truncating to an integer value.
10052 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +000010053 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +000010054 addFrameReference(BuildMI(*BB, MI, DL,
10055 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010056
10057 // Load the old value of the high byte of the control word...
10058 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +000010059 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohman14152b42010-07-06 20:24:04 +000010060 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +000010061 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010062
10063 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +000010064 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000010065 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +000010066
10067 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +000010068 addFrameReference(BuildMI(*BB, MI, DL,
10069 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010070
10071 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +000010072 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000010073 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +000010074
10075 // Get the X86 opcode to use.
10076 unsigned Opc;
10077 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000010078 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +000010079 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
10080 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
10081 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
10082 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
10083 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
10084 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +000010085 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
10086 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
10087 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +000010088 }
10089
10090 X86AddressMode AM;
10091 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +000010092 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000010093 AM.BaseType = X86AddressMode::RegBase;
10094 AM.Base.Reg = Op.getReg();
10095 } else {
10096 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +000010097 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +000010098 }
10099 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +000010100 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000010101 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010102 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +000010103 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000010104 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010105 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +000010106 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000010107 AM.GV = Op.getGlobal();
10108 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +000010109 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010110 }
Dan Gohman14152b42010-07-06 20:24:04 +000010111 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010112 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +000010113
10114 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +000010115 addFrameReference(BuildMI(*BB, MI, DL,
10116 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010117
Dan Gohman14152b42010-07-06 20:24:04 +000010118 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +000010119 return BB;
10120 }
Eric Christopherb120ab42009-08-18 22:50:32 +000010121 // String/text processing lowering.
10122 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010123 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000010124 return EmitPCMP(MI, BB, 3, false /* in-mem */);
10125 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010126 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000010127 return EmitPCMP(MI, BB, 3, true /* in-mem */);
10128 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010129 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000010130 return EmitPCMP(MI, BB, 5, false /* in mem */);
10131 case X86::PCMPESTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010132 case X86::VPCMPESTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000010133 return EmitPCMP(MI, BB, 5, true /* in mem */);
10134
Eric Christopher228232b2010-11-30 07:20:12 +000010135 // Thread synchronization.
10136 case X86::MONITOR:
10137 return EmitMonitor(MI, BB);
10138 case X86::MWAIT:
10139 return EmitMwait(MI, BB);
10140
Eric Christopherb120ab42009-08-18 22:50:32 +000010141 // Atomic Lowering.
Mon P Wang63307c32008-05-05 19:05:59 +000010142 case X86::ATOMAND32:
10143 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010144 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010145 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010146 X86::NOT32r, X86::EAX,
10147 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000010148 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +000010149 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
10150 X86::OR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010151 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010152 X86::NOT32r, X86::EAX,
10153 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000010154 case X86::ATOMXOR32:
10155 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010156 X86::XOR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010157 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010158 X86::NOT32r, X86::EAX,
10159 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010160 case X86::ATOMNAND32:
10161 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010162 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010163 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010164 X86::NOT32r, X86::EAX,
10165 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +000010166 case X86::ATOMMIN32:
10167 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
10168 case X86::ATOMMAX32:
10169 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
10170 case X86::ATOMUMIN32:
10171 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
10172 case X86::ATOMUMAX32:
10173 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +000010174
10175 case X86::ATOMAND16:
10176 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
10177 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010178 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010179 X86::NOT16r, X86::AX,
10180 X86::GR16RegisterClass);
10181 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +000010182 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010183 X86::OR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010184 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010185 X86::NOT16r, X86::AX,
10186 X86::GR16RegisterClass);
10187 case X86::ATOMXOR16:
10188 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
10189 X86::XOR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010190 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010191 X86::NOT16r, X86::AX,
10192 X86::GR16RegisterClass);
10193 case X86::ATOMNAND16:
10194 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
10195 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010196 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010197 X86::NOT16r, X86::AX,
10198 X86::GR16RegisterClass, true);
10199 case X86::ATOMMIN16:
10200 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
10201 case X86::ATOMMAX16:
10202 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
10203 case X86::ATOMUMIN16:
10204 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
10205 case X86::ATOMUMAX16:
10206 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
10207
10208 case X86::ATOMAND8:
10209 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
10210 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010211 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010212 X86::NOT8r, X86::AL,
10213 X86::GR8RegisterClass);
10214 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +000010215 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010216 X86::OR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010217 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010218 X86::NOT8r, X86::AL,
10219 X86::GR8RegisterClass);
10220 case X86::ATOMXOR8:
10221 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
10222 X86::XOR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010223 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010224 X86::NOT8r, X86::AL,
10225 X86::GR8RegisterClass);
10226 case X86::ATOMNAND8:
10227 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
10228 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010229 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010230 X86::NOT8r, X86::AL,
10231 X86::GR8RegisterClass, true);
10232 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010233 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +000010234 case X86::ATOMAND64:
10235 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010236 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010237 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010238 X86::NOT64r, X86::RAX,
10239 X86::GR64RegisterClass);
10240 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +000010241 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
10242 X86::OR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010243 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010244 X86::NOT64r, X86::RAX,
10245 X86::GR64RegisterClass);
10246 case X86::ATOMXOR64:
10247 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010248 X86::XOR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010249 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010250 X86::NOT64r, X86::RAX,
10251 X86::GR64RegisterClass);
10252 case X86::ATOMNAND64:
10253 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
10254 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010255 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010256 X86::NOT64r, X86::RAX,
10257 X86::GR64RegisterClass, true);
10258 case X86::ATOMMIN64:
10259 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
10260 case X86::ATOMMAX64:
10261 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
10262 case X86::ATOMUMIN64:
10263 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
10264 case X86::ATOMUMAX64:
10265 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010266
10267 // This group does 64-bit operations on a 32-bit host.
10268 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010269 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010270 X86::AND32rr, X86::AND32rr,
10271 X86::AND32ri, X86::AND32ri,
10272 false);
10273 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010274 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010275 X86::OR32rr, X86::OR32rr,
10276 X86::OR32ri, X86::OR32ri,
10277 false);
10278 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010279 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010280 X86::XOR32rr, X86::XOR32rr,
10281 X86::XOR32ri, X86::XOR32ri,
10282 false);
10283 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010284 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010285 X86::AND32rr, X86::AND32rr,
10286 X86::AND32ri, X86::AND32ri,
10287 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010288 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010289 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010290 X86::ADD32rr, X86::ADC32rr,
10291 X86::ADD32ri, X86::ADC32ri,
10292 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010293 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010294 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010295 X86::SUB32rr, X86::SBB32rr,
10296 X86::SUB32ri, X86::SBB32ri,
10297 false);
Dale Johannesen880ae362008-10-03 22:25:52 +000010298 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010299 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +000010300 X86::MOV32rr, X86::MOV32rr,
10301 X86::MOV32ri, X86::MOV32ri,
10302 false);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010303 case X86::VASTART_SAVE_XMM_REGS:
10304 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Dan Gohman320afb82010-10-12 18:00:49 +000010305
10306 case X86::VAARG_64:
10307 return EmitVAARG64WithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000010308 }
10309}
10310
10311//===----------------------------------------------------------------------===//
10312// X86 Optimization Hooks
10313//===----------------------------------------------------------------------===//
10314
Dan Gohman475871a2008-07-27 21:46:04 +000010315void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +000010316 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000010317 APInt &KnownZero,
10318 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +000010319 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +000010320 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010321 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +000010322 assert((Opc >= ISD::BUILTIN_OP_END ||
10323 Opc == ISD::INTRINSIC_WO_CHAIN ||
10324 Opc == ISD::INTRINSIC_W_CHAIN ||
10325 Opc == ISD::INTRINSIC_VOID) &&
10326 "Should use MaskedValueIsZero if you don't know whether Op"
10327 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010328
Dan Gohmanf4f92f52008-02-13 23:07:24 +000010329 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010330 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +000010331 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +000010332 case X86ISD::ADD:
10333 case X86ISD::SUB:
10334 case X86ISD::SMUL:
10335 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +000010336 case X86ISD::INC:
10337 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +000010338 case X86ISD::OR:
10339 case X86ISD::XOR:
10340 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000010341 // These nodes' second result is a boolean.
10342 if (Op.getResNo() == 0)
10343 break;
10344 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010345 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000010346 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
10347 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +000010348 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010349 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010350}
Chris Lattner259e97c2006-01-31 19:43:35 +000010351
Owen Andersonbc146b02010-09-21 20:42:50 +000010352unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
10353 unsigned Depth) const {
10354 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
10355 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
10356 return Op.getValueType().getScalarType().getSizeInBits();
Michael J. Spencerec38de22010-10-10 22:04:20 +000010357
Owen Andersonbc146b02010-09-21 20:42:50 +000010358 // Fallback case.
10359 return 1;
10360}
10361
Evan Cheng206ee9d2006-07-07 08:33:52 +000010362/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +000010363/// node is a GlobalAddress + offset.
10364bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +000010365 const GlobalValue* &GA,
10366 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +000010367 if (N->getOpcode() == X86ISD::Wrapper) {
10368 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +000010369 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +000010370 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +000010371 return true;
10372 }
Evan Cheng206ee9d2006-07-07 08:33:52 +000010373 }
Evan Chengad4196b2008-05-12 19:56:52 +000010374 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +000010375}
10376
Evan Cheng206ee9d2006-07-07 08:33:52 +000010377/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
10378/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
10379/// if the load addresses are consecutive, non-overlapping, and in the right
Nate Begemanfdea31a2010-03-24 20:49:50 +000010380/// order.
Dan Gohman475871a2008-07-27 21:46:04 +000010381static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Nate Begeman9008ca62009-04-27 18:41:29 +000010382 const TargetLowering &TLI) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000010383 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000010384 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +000010385
Eli Friedman7a5e5552009-06-07 06:52:44 +000010386 if (VT.getSizeInBits() != 128)
10387 return SDValue();
10388
Nate Begemanfdea31a2010-03-24 20:49:50 +000010389 SmallVector<SDValue, 16> Elts;
10390 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000010391 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +000010392
Nate Begemanfdea31a2010-03-24 20:49:50 +000010393 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000010394}
Evan Chengd880b972008-05-09 21:53:03 +000010395
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +000010396/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
10397/// generation and convert it from being a bunch of shuffles and extracts
10398/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010399static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
10400 const TargetLowering &TLI) {
10401 SDValue InputVector = N->getOperand(0);
10402
10403 // Only operate on vectors of 4 elements, where the alternative shuffling
10404 // gets to be more expensive.
10405 if (InputVector.getValueType() != MVT::v4i32)
10406 return SDValue();
10407
10408 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
10409 // single use which is a sign-extend or zero-extend, and all elements are
10410 // used.
10411 SmallVector<SDNode *, 4> Uses;
10412 unsigned ExtractedElements = 0;
10413 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
10414 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
10415 if (UI.getUse().getResNo() != InputVector.getResNo())
10416 return SDValue();
10417
10418 SDNode *Extract = *UI;
10419 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
10420 return SDValue();
10421
10422 if (Extract->getValueType(0) != MVT::i32)
10423 return SDValue();
10424 if (!Extract->hasOneUse())
10425 return SDValue();
10426 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
10427 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
10428 return SDValue();
10429 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
10430 return SDValue();
10431
10432 // Record which element was extracted.
10433 ExtractedElements |=
10434 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
10435
10436 Uses.push_back(Extract);
10437 }
10438
10439 // If not all the elements were used, this may not be worthwhile.
10440 if (ExtractedElements != 15)
10441 return SDValue();
10442
10443 // Ok, we've now decided to do the transformation.
10444 DebugLoc dl = InputVector.getDebugLoc();
10445
10446 // Store the value to a temporary stack slot.
10447 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000010448 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
10449 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010450
10451 // Replace each use (extract) with a load of the appropriate element.
10452 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
10453 UE = Uses.end(); UI != UE; ++UI) {
10454 SDNode *Extract = *UI;
10455
10456 // Compute the element's address.
10457 SDValue Idx = Extract->getOperand(1);
10458 unsigned EltSize =
10459 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
10460 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
10461 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
10462
Eric Christopher90eb4022010-07-22 00:26:08 +000010463 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, Idx.getValueType(),
Chris Lattner51abfe42010-09-21 06:02:19 +000010464 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010465
10466 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000010467 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000010468 ScalarAddr, MachinePointerInfo(),
10469 false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010470
10471 // Replace the exact with the load.
10472 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
10473 }
10474
10475 // The replacement was made in place; don't return anything.
10476 return SDValue();
10477}
10478
Chris Lattner83e6c992006-10-04 06:57:07 +000010479/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000010480static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner47b4ce82009-03-11 05:48:52 +000010481 const X86Subtarget *Subtarget) {
10482 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +000010483 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000010484 // Get the LHS/RHS of the select.
10485 SDValue LHS = N->getOperand(1);
10486 SDValue RHS = N->getOperand(2);
Eric Christopherfd179292009-08-27 18:07:15 +000010487
Dan Gohman670e5392009-09-21 18:03:22 +000010488 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000010489 // instructions match the semantics of the common C idiom x<y?x:y but not
10490 // x<=y?x:y, because of how they handle negative zero (which can be
10491 // ignored in unsafe-math mode).
Chris Lattner83e6c992006-10-04 06:57:07 +000010492 if (Subtarget->hasSSE2() &&
Owen Anderson825b72b2009-08-11 20:47:22 +000010493 (LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64) &&
Chris Lattner47b4ce82009-03-11 05:48:52 +000010494 Cond.getOpcode() == ISD::SETCC) {
10495 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010496
Chris Lattner47b4ce82009-03-11 05:48:52 +000010497 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000010498 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000010499 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
10500 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000010501 switch (CC) {
10502 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000010503 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000010504 // Converting this to a min would handle NaNs incorrectly, and swapping
10505 // the operands would cause it to handle comparisons between positive
10506 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000010507 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000010508 if (!UnsafeFPMath &&
10509 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
10510 break;
10511 std::swap(LHS, RHS);
10512 }
Dan Gohman670e5392009-09-21 18:03:22 +000010513 Opcode = X86ISD::FMIN;
10514 break;
10515 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000010516 // Converting this to a min would handle comparisons between positive
10517 // and negative zero incorrectly.
10518 if (!UnsafeFPMath &&
10519 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
10520 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010521 Opcode = X86ISD::FMIN;
10522 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000010523 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000010524 // Converting this to a min would handle both negative zeros and NaNs
10525 // incorrectly, but we can swap the operands to fix both.
10526 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000010527 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010528 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000010529 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010530 Opcode = X86ISD::FMIN;
10531 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010532
Dan Gohman670e5392009-09-21 18:03:22 +000010533 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000010534 // Converting this to a max would handle comparisons between positive
10535 // and negative zero incorrectly.
10536 if (!UnsafeFPMath &&
10537 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(LHS))
10538 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010539 Opcode = X86ISD::FMAX;
10540 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000010541 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000010542 // Converting this to a max would handle NaNs incorrectly, and swapping
10543 // the operands would cause it to handle comparisons between positive
10544 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000010545 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000010546 if (!UnsafeFPMath &&
10547 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
10548 break;
10549 std::swap(LHS, RHS);
10550 }
Dan Gohman670e5392009-09-21 18:03:22 +000010551 Opcode = X86ISD::FMAX;
10552 break;
10553 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000010554 // Converting this to a max would handle both negative zeros and NaNs
10555 // incorrectly, but we can swap the operands to fix both.
10556 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000010557 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010558 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010559 case ISD::SETGE:
10560 Opcode = X86ISD::FMAX;
10561 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000010562 }
Dan Gohman670e5392009-09-21 18:03:22 +000010563 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000010564 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
10565 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000010566 switch (CC) {
10567 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000010568 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000010569 // Converting this to a min would handle comparisons between positive
10570 // and negative zero incorrectly, and swapping the operands would
10571 // cause it to handle NaNs incorrectly.
10572 if (!UnsafeFPMath &&
10573 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000010574 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000010575 break;
10576 std::swap(LHS, RHS);
10577 }
Dan Gohman670e5392009-09-21 18:03:22 +000010578 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000010579 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010580 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000010581 // Converting this to a min would handle NaNs incorrectly.
10582 if (!UnsafeFPMath &&
10583 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
10584 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010585 Opcode = X86ISD::FMIN;
10586 break;
10587 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000010588 // Converting this to a min would handle both negative zeros and NaNs
10589 // incorrectly, but we can swap the operands to fix both.
10590 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000010591 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010592 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010593 case ISD::SETGE:
10594 Opcode = X86ISD::FMIN;
10595 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010596
Dan Gohman670e5392009-09-21 18:03:22 +000010597 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000010598 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000010599 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000010600 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010601 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000010602 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010603 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000010604 // Converting this to a max would handle comparisons between positive
10605 // and negative zero incorrectly, and swapping the operands would
10606 // cause it to handle NaNs incorrectly.
10607 if (!UnsafeFPMath &&
10608 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000010609 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000010610 break;
10611 std::swap(LHS, RHS);
10612 }
Dan Gohman670e5392009-09-21 18:03:22 +000010613 Opcode = X86ISD::FMAX;
10614 break;
10615 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000010616 // Converting this to a max would handle both negative zeros and NaNs
10617 // incorrectly, but we can swap the operands to fix both.
10618 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000010619 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010620 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000010621 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010622 Opcode = X86ISD::FMAX;
10623 break;
10624 }
Chris Lattner83e6c992006-10-04 06:57:07 +000010625 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010626
Chris Lattner47b4ce82009-03-11 05:48:52 +000010627 if (Opcode)
10628 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000010629 }
Eric Christopherfd179292009-08-27 18:07:15 +000010630
Chris Lattnerd1980a52009-03-12 06:52:53 +000010631 // If this is a select between two integer constants, try to do some
10632 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000010633 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
10634 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000010635 // Don't do this for crazy integer types.
10636 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
10637 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000010638 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000010639 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000010640
Chris Lattnercee56e72009-03-13 05:53:31 +000010641 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000010642 // Efficiently invertible.
10643 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
10644 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
10645 isa<ConstantSDNode>(Cond.getOperand(1))))) {
10646 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000010647 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000010648 }
Eric Christopherfd179292009-08-27 18:07:15 +000010649
Chris Lattnerd1980a52009-03-12 06:52:53 +000010650 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000010651 if (FalseC->getAPIntValue() == 0 &&
10652 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000010653 if (NeedsCondInvert) // Invert the condition if needed.
10654 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
10655 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000010656
Chris Lattnerd1980a52009-03-12 06:52:53 +000010657 // Zero extend the condition if needed.
10658 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000010659
Chris Lattnercee56e72009-03-13 05:53:31 +000010660 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000010661 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000010662 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000010663 }
Eric Christopherfd179292009-08-27 18:07:15 +000010664
Chris Lattner97a29a52009-03-13 05:22:11 +000010665 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000010666 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +000010667 if (NeedsCondInvert) // Invert the condition if needed.
10668 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
10669 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000010670
Chris Lattner97a29a52009-03-13 05:22:11 +000010671 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000010672 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
10673 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000010674 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000010675 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000010676 }
Eric Christopherfd179292009-08-27 18:07:15 +000010677
Chris Lattnercee56e72009-03-13 05:53:31 +000010678 // Optimize cases that will turn into an LEA instruction. This requires
10679 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000010680 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000010681 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000010682 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000010683
Chris Lattnercee56e72009-03-13 05:53:31 +000010684 bool isFastMultiplier = false;
10685 if (Diff < 10) {
10686 switch ((unsigned char)Diff) {
10687 default: break;
10688 case 1: // result = add base, cond
10689 case 2: // result = lea base( , cond*2)
10690 case 3: // result = lea base(cond, cond*2)
10691 case 4: // result = lea base( , cond*4)
10692 case 5: // result = lea base(cond, cond*4)
10693 case 8: // result = lea base( , cond*8)
10694 case 9: // result = lea base(cond, cond*8)
10695 isFastMultiplier = true;
10696 break;
10697 }
10698 }
Eric Christopherfd179292009-08-27 18:07:15 +000010699
Chris Lattnercee56e72009-03-13 05:53:31 +000010700 if (isFastMultiplier) {
10701 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
10702 if (NeedsCondInvert) // Invert the condition if needed.
10703 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
10704 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000010705
Chris Lattnercee56e72009-03-13 05:53:31 +000010706 // Zero extend the condition if needed.
10707 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
10708 Cond);
10709 // Scale the condition by the difference.
10710 if (Diff != 1)
10711 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
10712 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000010713
Chris Lattnercee56e72009-03-13 05:53:31 +000010714 // Add the base if non-zero.
10715 if (FalseC->getAPIntValue() != 0)
10716 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
10717 SDValue(FalseC, 0));
10718 return Cond;
10719 }
Eric Christopherfd179292009-08-27 18:07:15 +000010720 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000010721 }
10722 }
Eric Christopherfd179292009-08-27 18:07:15 +000010723
Dan Gohman475871a2008-07-27 21:46:04 +000010724 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000010725}
10726
Chris Lattnerd1980a52009-03-12 06:52:53 +000010727/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
10728static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
10729 TargetLowering::DAGCombinerInfo &DCI) {
10730 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +000010731
Chris Lattnerd1980a52009-03-12 06:52:53 +000010732 // If the flag operand isn't dead, don't touch this CMOV.
10733 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
10734 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +000010735
Chris Lattnerd1980a52009-03-12 06:52:53 +000010736 // If this is a select between two integer constants, try to do some
10737 // optimizations. Note that the operands are ordered the opposite of SELECT
10738 // operands.
10739 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
10740 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
10741 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
10742 // larger than FalseC (the false value).
10743 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
Eric Christopherfd179292009-08-27 18:07:15 +000010744
Chris Lattnerd1980a52009-03-12 06:52:53 +000010745 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
10746 CC = X86::GetOppositeBranchCondition(CC);
10747 std::swap(TrueC, FalseC);
10748 }
Eric Christopherfd179292009-08-27 18:07:15 +000010749
Chris Lattnerd1980a52009-03-12 06:52:53 +000010750 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000010751 // This is efficient for any integer data type (including i8/i16) and
10752 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000010753 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
10754 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +000010755 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
10756 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000010757
Chris Lattnerd1980a52009-03-12 06:52:53 +000010758 // Zero extend the condition if needed.
10759 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000010760
Chris Lattnerd1980a52009-03-12 06:52:53 +000010761 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
10762 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000010763 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000010764 if (N->getNumValues() == 2) // Dead flag value?
10765 return DCI.CombineTo(N, Cond, SDValue());
10766 return Cond;
10767 }
Eric Christopherfd179292009-08-27 18:07:15 +000010768
Chris Lattnercee56e72009-03-13 05:53:31 +000010769 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
10770 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000010771 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
10772 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +000010773 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
10774 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000010775
Chris Lattner97a29a52009-03-13 05:22:11 +000010776 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000010777 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
10778 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000010779 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
10780 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000010781
Chris Lattner97a29a52009-03-13 05:22:11 +000010782 if (N->getNumValues() == 2) // Dead flag value?
10783 return DCI.CombineTo(N, Cond, SDValue());
10784 return Cond;
10785 }
Eric Christopherfd179292009-08-27 18:07:15 +000010786
Chris Lattnercee56e72009-03-13 05:53:31 +000010787 // Optimize cases that will turn into an LEA instruction. This requires
10788 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000010789 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000010790 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000010791 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000010792
Chris Lattnercee56e72009-03-13 05:53:31 +000010793 bool isFastMultiplier = false;
10794 if (Diff < 10) {
10795 switch ((unsigned char)Diff) {
10796 default: break;
10797 case 1: // result = add base, cond
10798 case 2: // result = lea base( , cond*2)
10799 case 3: // result = lea base(cond, cond*2)
10800 case 4: // result = lea base( , cond*4)
10801 case 5: // result = lea base(cond, cond*4)
10802 case 8: // result = lea base( , cond*8)
10803 case 9: // result = lea base(cond, cond*8)
10804 isFastMultiplier = true;
10805 break;
10806 }
10807 }
Eric Christopherfd179292009-08-27 18:07:15 +000010808
Chris Lattnercee56e72009-03-13 05:53:31 +000010809 if (isFastMultiplier) {
10810 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
10811 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +000010812 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
10813 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000010814 // Zero extend the condition if needed.
10815 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
10816 Cond);
10817 // Scale the condition by the difference.
10818 if (Diff != 1)
10819 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
10820 DAG.getConstant(Diff, Cond.getValueType()));
10821
10822 // Add the base if non-zero.
10823 if (FalseC->getAPIntValue() != 0)
10824 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
10825 SDValue(FalseC, 0));
10826 if (N->getNumValues() == 2) // Dead flag value?
10827 return DCI.CombineTo(N, Cond, SDValue());
10828 return Cond;
10829 }
Eric Christopherfd179292009-08-27 18:07:15 +000010830 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000010831 }
10832 }
10833 return SDValue();
10834}
10835
10836
Evan Cheng0b0cd912009-03-28 05:57:29 +000010837/// PerformMulCombine - Optimize a single multiply with constant into two
10838/// in order to implement it with two cheaper instructions, e.g.
10839/// LEA + SHL, LEA + LEA.
10840static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
10841 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000010842 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
10843 return SDValue();
10844
Owen Andersone50ed302009-08-10 22:56:29 +000010845 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010846 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000010847 return SDValue();
10848
10849 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
10850 if (!C)
10851 return SDValue();
10852 uint64_t MulAmt = C->getZExtValue();
10853 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
10854 return SDValue();
10855
10856 uint64_t MulAmt1 = 0;
10857 uint64_t MulAmt2 = 0;
10858 if ((MulAmt % 9) == 0) {
10859 MulAmt1 = 9;
10860 MulAmt2 = MulAmt / 9;
10861 } else if ((MulAmt % 5) == 0) {
10862 MulAmt1 = 5;
10863 MulAmt2 = MulAmt / 5;
10864 } else if ((MulAmt % 3) == 0) {
10865 MulAmt1 = 3;
10866 MulAmt2 = MulAmt / 3;
10867 }
10868 if (MulAmt2 &&
10869 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
10870 DebugLoc DL = N->getDebugLoc();
10871
10872 if (isPowerOf2_64(MulAmt2) &&
10873 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
10874 // If second multiplifer is pow2, issue it first. We want the multiply by
10875 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
10876 // is an add.
10877 std::swap(MulAmt1, MulAmt2);
10878
10879 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000010880 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000010881 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000010882 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000010883 else
Evan Cheng73f24c92009-03-30 21:36:47 +000010884 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000010885 DAG.getConstant(MulAmt1, VT));
10886
Eric Christopherfd179292009-08-27 18:07:15 +000010887 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000010888 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000010889 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000010890 else
Evan Cheng73f24c92009-03-30 21:36:47 +000010891 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000010892 DAG.getConstant(MulAmt2, VT));
10893
10894 // Do not add new nodes to DAG combiner worklist.
10895 DCI.CombineTo(N, NewMul, false);
10896 }
10897 return SDValue();
10898}
10899
Evan Chengad9c0a32009-12-15 00:53:42 +000010900static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
10901 SDValue N0 = N->getOperand(0);
10902 SDValue N1 = N->getOperand(1);
10903 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
10904 EVT VT = N0.getValueType();
10905
10906 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
10907 // since the result of setcc_c is all zero's or all ones.
10908 if (N1C && N0.getOpcode() == ISD::AND &&
10909 N0.getOperand(1).getOpcode() == ISD::Constant) {
10910 SDValue N00 = N0.getOperand(0);
10911 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
10912 ((N00.getOpcode() == ISD::ANY_EXTEND ||
10913 N00.getOpcode() == ISD::ZERO_EXTEND) &&
10914 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
10915 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
10916 APInt ShAmt = N1C->getAPIntValue();
10917 Mask = Mask.shl(ShAmt);
10918 if (Mask != 0)
10919 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
10920 N00, DAG.getConstant(Mask, VT));
10921 }
10922 }
10923
10924 return SDValue();
10925}
Evan Cheng0b0cd912009-03-28 05:57:29 +000010926
Nate Begeman740ab032009-01-26 00:52:55 +000010927/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
10928/// when possible.
10929static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
10930 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +000010931 EVT VT = N->getValueType(0);
10932 if (!VT.isVector() && VT.isInteger() &&
10933 N->getOpcode() == ISD::SHL)
10934 return PerformSHLCombine(N, DAG);
10935
Nate Begeman740ab032009-01-26 00:52:55 +000010936 // On X86 with SSE2 support, we can transform this to a vector shift if
10937 // all elements are shifted by the same amount. We can't do this in legalize
10938 // because the a constant vector is typically transformed to a constant pool
10939 // so we have no knowledge of the shift amount.
Nate Begemanc2fd67f2009-01-26 03:15:31 +000010940 if (!Subtarget->hasSSE2())
10941 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000010942
Owen Anderson825b72b2009-08-11 20:47:22 +000010943 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
Nate Begemanc2fd67f2009-01-26 03:15:31 +000010944 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000010945
Mon P Wang3becd092009-01-28 08:12:05 +000010946 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +000010947 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +000010948 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +000010949 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +000010950 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
10951 unsigned NumElts = VT.getVectorNumElements();
10952 unsigned i = 0;
10953 for (; i != NumElts; ++i) {
10954 SDValue Arg = ShAmtOp.getOperand(i);
10955 if (Arg.getOpcode() == ISD::UNDEF) continue;
10956 BaseShAmt = Arg;
10957 break;
10958 }
10959 for (; i != NumElts; ++i) {
10960 SDValue Arg = ShAmtOp.getOperand(i);
10961 if (Arg.getOpcode() == ISD::UNDEF) continue;
10962 if (Arg != BaseShAmt) {
10963 return SDValue();
10964 }
10965 }
10966 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +000010967 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +000010968 SDValue InVec = ShAmtOp.getOperand(0);
10969 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
10970 unsigned NumElts = InVec.getValueType().getVectorNumElements();
10971 unsigned i = 0;
10972 for (; i != NumElts; ++i) {
10973 SDValue Arg = InVec.getOperand(i);
10974 if (Arg.getOpcode() == ISD::UNDEF) continue;
10975 BaseShAmt = Arg;
10976 break;
10977 }
10978 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
10979 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +000010980 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +000010981 if (C->getZExtValue() == SplatIdx)
10982 BaseShAmt = InVec.getOperand(1);
10983 }
10984 }
10985 if (BaseShAmt.getNode() == 0)
10986 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
10987 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +000010988 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +000010989 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000010990
Mon P Wangefa42202009-09-03 19:56:25 +000010991 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +000010992 if (EltVT.bitsGT(MVT::i32))
10993 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
10994 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +000010995 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +000010996
Nate Begemanc2fd67f2009-01-26 03:15:31 +000010997 // The shift amount is identical so we can do a vector shift.
10998 SDValue ValOp = N->getOperand(0);
10999 switch (N->getOpcode()) {
11000 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000011001 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011002 break;
11003 case ISD::SHL:
Owen Anderson825b72b2009-08-11 20:47:22 +000011004 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011005 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011006 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011007 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011008 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011009 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011010 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011011 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011012 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011013 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011014 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011015 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011016 break;
11017 case ISD::SRA:
Owen Anderson825b72b2009-08-11 20:47:22 +000011018 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011019 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011020 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011021 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011022 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011023 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011024 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011025 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011026 break;
11027 case ISD::SRL:
Owen Anderson825b72b2009-08-11 20:47:22 +000011028 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011029 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011030 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011031 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011032 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011033 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011034 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011035 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011036 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011037 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011038 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011039 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011040 break;
Nate Begeman740ab032009-01-26 00:52:55 +000011041 }
11042 return SDValue();
11043}
11044
Nate Begemanb65c1752010-12-17 22:55:37 +000011045
11046static SDValue PerformAndCombine(SDNode *N, SelectionDAG &DAG,
11047 TargetLowering::DAGCombinerInfo &DCI,
11048 const X86Subtarget *Subtarget) {
11049 if (DCI.isBeforeLegalizeOps())
11050 return SDValue();
11051
11052 // Want to form PANDN nodes, in the hopes of then easily combining them with
11053 // OR and AND nodes to form PBLEND/PSIGN.
11054 EVT VT = N->getValueType(0);
11055 if (VT != MVT::v2i64)
11056 return SDValue();
11057
11058 SDValue N0 = N->getOperand(0);
11059 SDValue N1 = N->getOperand(1);
11060 DebugLoc DL = N->getDebugLoc();
11061
11062 // Check LHS for vnot
11063 if (N0.getOpcode() == ISD::XOR &&
11064 ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode()))
11065 return DAG.getNode(X86ISD::PANDN, DL, VT, N0.getOperand(0), N1);
11066
11067 // Check RHS for vnot
11068 if (N1.getOpcode() == ISD::XOR &&
11069 ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode()))
11070 return DAG.getNode(X86ISD::PANDN, DL, VT, N1.getOperand(0), N0);
11071
11072 return SDValue();
11073}
11074
Evan Cheng760d1942010-01-04 21:22:48 +000011075static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000011076 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000011077 const X86Subtarget *Subtarget) {
Evan Cheng39cfeec2010-04-28 02:25:18 +000011078 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000011079 return SDValue();
11080
Evan Cheng760d1942010-01-04 21:22:48 +000011081 EVT VT = N->getValueType(0);
Nate Begemanb65c1752010-12-17 22:55:37 +000011082 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64 && VT != MVT::v2i64)
Evan Cheng760d1942010-01-04 21:22:48 +000011083 return SDValue();
11084
Evan Cheng760d1942010-01-04 21:22:48 +000011085 SDValue N0 = N->getOperand(0);
11086 SDValue N1 = N->getOperand(1);
Nate Begemanb65c1752010-12-17 22:55:37 +000011087
11088 // look for psign/blend
11089 if (Subtarget->hasSSSE3()) {
11090 if (VT == MVT::v2i64) {
11091 // Canonicalize pandn to RHS
11092 if (N0.getOpcode() == X86ISD::PANDN)
11093 std::swap(N0, N1);
11094 // or (and (m, x), (pandn m, y))
11095 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::PANDN) {
11096 SDValue Mask = N1.getOperand(0);
11097 SDValue X = N1.getOperand(1);
11098 SDValue Y;
11099 if (N0.getOperand(0) == Mask)
11100 Y = N0.getOperand(1);
11101 if (N0.getOperand(1) == Mask)
11102 Y = N0.getOperand(0);
11103
11104 // Check to see if the mask appeared in both the AND and PANDN and
11105 if (!Y.getNode())
11106 return SDValue();
11107
11108 // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them.
11109 if (Mask.getOpcode() != ISD::BITCAST ||
11110 X.getOpcode() != ISD::BITCAST ||
11111 Y.getOpcode() != ISD::BITCAST)
11112 return SDValue();
11113
11114 // Look through mask bitcast.
11115 Mask = Mask.getOperand(0);
11116 EVT MaskVT = Mask.getValueType();
11117
11118 // Validate that the Mask operand is a vector sra node. The sra node
11119 // will be an intrinsic.
11120 if (Mask.getOpcode() != ISD::INTRINSIC_WO_CHAIN)
11121 return SDValue();
11122
11123 // FIXME: what to do for bytes, since there is a psignb/pblendvb, but
11124 // there is no psrai.b
11125 switch (cast<ConstantSDNode>(Mask.getOperand(0))->getZExtValue()) {
11126 case Intrinsic::x86_sse2_psrai_w:
11127 case Intrinsic::x86_sse2_psrai_d:
11128 break;
11129 default: return SDValue();
11130 }
11131
11132 // Check that the SRA is all signbits.
11133 SDValue SraC = Mask.getOperand(2);
11134 unsigned SraAmt = cast<ConstantSDNode>(SraC)->getZExtValue();
11135 unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits();
11136 if ((SraAmt + 1) != EltBits)
11137 return SDValue();
11138
11139 DebugLoc DL = N->getDebugLoc();
11140
11141 // Now we know we at least have a plendvb with the mask val. See if
11142 // we can form a psignb/w/d.
11143 // psign = x.type == y.type == mask.type && y = sub(0, x);
11144 X = X.getOperand(0);
11145 Y = Y.getOperand(0);
11146 if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X &&
11147 ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) &&
11148 X.getValueType() == MaskVT && X.getValueType() == Y.getValueType()){
11149 unsigned Opc = 0;
11150 switch (EltBits) {
11151 case 8: Opc = X86ISD::PSIGNB; break;
11152 case 16: Opc = X86ISD::PSIGNW; break;
11153 case 32: Opc = X86ISD::PSIGND; break;
11154 default: break;
11155 }
11156 if (Opc) {
11157 SDValue Sign = DAG.getNode(Opc, DL, MaskVT, X, Mask.getOperand(1));
11158 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Sign);
11159 }
11160 }
11161 // PBLENDVB only available on SSE 4.1
11162 if (!Subtarget->hasSSE41())
11163 return SDValue();
11164
11165 unsigned IID = Intrinsic::x86_sse41_pblendvb;
11166 X = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, X);
11167 Y = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Y);
11168 Mask = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Mask);
11169 Mask = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, MVT::v16i8,
11170 DAG.getConstant(IID, MVT::i32), X, Y, Mask);
11171 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Mask);
11172 }
11173 }
11174 }
11175
11176 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
Evan Cheng760d1942010-01-04 21:22:48 +000011177 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
11178 std::swap(N0, N1);
11179 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
11180 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000011181 if (!N0.hasOneUse() || !N1.hasOneUse())
11182 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000011183
11184 SDValue ShAmt0 = N0.getOperand(1);
11185 if (ShAmt0.getValueType() != MVT::i8)
11186 return SDValue();
11187 SDValue ShAmt1 = N1.getOperand(1);
11188 if (ShAmt1.getValueType() != MVT::i8)
11189 return SDValue();
11190 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
11191 ShAmt0 = ShAmt0.getOperand(0);
11192 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
11193 ShAmt1 = ShAmt1.getOperand(0);
11194
11195 DebugLoc DL = N->getDebugLoc();
11196 unsigned Opc = X86ISD::SHLD;
11197 SDValue Op0 = N0.getOperand(0);
11198 SDValue Op1 = N1.getOperand(0);
11199 if (ShAmt0.getOpcode() == ISD::SUB) {
11200 Opc = X86ISD::SHRD;
11201 std::swap(Op0, Op1);
11202 std::swap(ShAmt0, ShAmt1);
11203 }
11204
Evan Cheng8b1190a2010-04-28 01:18:01 +000011205 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000011206 if (ShAmt1.getOpcode() == ISD::SUB) {
11207 SDValue Sum = ShAmt1.getOperand(0);
11208 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000011209 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
11210 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
11211 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
11212 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000011213 return DAG.getNode(Opc, DL, VT,
11214 Op0, Op1,
11215 DAG.getNode(ISD::TRUNCATE, DL,
11216 MVT::i8, ShAmt0));
11217 }
11218 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
11219 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
11220 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000011221 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000011222 return DAG.getNode(Opc, DL, VT,
11223 N0.getOperand(0), N1.getOperand(0),
11224 DAG.getNode(ISD::TRUNCATE, DL,
11225 MVT::i8, ShAmt0));
11226 }
Nate Begemanb65c1752010-12-17 22:55:37 +000011227
Evan Cheng760d1942010-01-04 21:22:48 +000011228 return SDValue();
11229}
11230
Chris Lattner149a4e52008-02-22 02:09:43 +000011231/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000011232static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000011233 const X86Subtarget *Subtarget) {
Chris Lattner149a4e52008-02-22 02:09:43 +000011234 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
11235 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000011236 // A preferable solution to the general problem is to figure out the right
11237 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000011238
11239 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng7e2ff772008-05-08 00:57:18 +000011240 StoreSDNode *St = cast<StoreSDNode>(N);
Owen Andersone50ed302009-08-10 22:56:29 +000011241 EVT VT = St->getValue().getValueType();
Evan Cheng536e6672009-03-12 05:59:15 +000011242 if (VT.getSizeInBits() != 64)
11243 return SDValue();
11244
Devang Patel578efa92009-06-05 21:57:13 +000011245 const Function *F = DAG.getMachineFunction().getFunction();
11246 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Eric Christopherfd179292009-08-27 18:07:15 +000011247 bool F64IsLegal = !UseSoftFloat && !NoImplicitFloatOps
Devang Patel578efa92009-06-05 21:57:13 +000011248 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +000011249 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000011250 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000011251 isa<LoadSDNode>(St->getValue()) &&
11252 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
11253 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000011254 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000011255 LoadSDNode *Ld = 0;
11256 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000011257 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000011258 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000011259 // Must be a store of a load. We currently handle two cases: the load
11260 // is a direct child, and it's under an intervening TokenFactor. It is
11261 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000011262 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000011263 Ld = cast<LoadSDNode>(St->getChain());
11264 else if (St->getValue().hasOneUse() &&
11265 ChainVal->getOpcode() == ISD::TokenFactor) {
11266 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000011267 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000011268 TokenFactorIndex = i;
11269 Ld = cast<LoadSDNode>(St->getValue());
11270 } else
11271 Ops.push_back(ChainVal->getOperand(i));
11272 }
11273 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000011274
Evan Cheng536e6672009-03-12 05:59:15 +000011275 if (!Ld || !ISD::isNormalLoad(Ld))
11276 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000011277
Evan Cheng536e6672009-03-12 05:59:15 +000011278 // If this is not the MMX case, i.e. we are just turning i64 load/store
11279 // into f64 load/store, avoid the transformation if there are multiple
11280 // uses of the loaded value.
11281 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
11282 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000011283
Evan Cheng536e6672009-03-12 05:59:15 +000011284 DebugLoc LdDL = Ld->getDebugLoc();
11285 DebugLoc StDL = N->getDebugLoc();
11286 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
11287 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
11288 // pair instead.
11289 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000011290 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000011291 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
11292 Ld->getPointerInfo(), Ld->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000011293 Ld->isNonTemporal(), Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000011294 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000011295 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000011296 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000011297 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000011298 Ops.size());
11299 }
Evan Cheng536e6672009-03-12 05:59:15 +000011300 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000011301 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000011302 St->isVolatile(), St->isNonTemporal(),
11303 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000011304 }
Evan Cheng536e6672009-03-12 05:59:15 +000011305
11306 // Otherwise, lower to two pairs of 32-bit loads / stores.
11307 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000011308 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
11309 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000011310
Owen Anderson825b72b2009-08-11 20:47:22 +000011311 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000011312 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000011313 Ld->isVolatile(), Ld->isNonTemporal(),
11314 Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000011315 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000011316 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000011317 Ld->isVolatile(), Ld->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000011318 MinAlign(Ld->getAlignment(), 4));
11319
11320 SDValue NewChain = LoLd.getValue(1);
11321 if (TokenFactorIndex != -1) {
11322 Ops.push_back(LoLd);
11323 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000011324 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000011325 Ops.size());
11326 }
11327
11328 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000011329 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
11330 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000011331
11332 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000011333 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000011334 St->isVolatile(), St->isNonTemporal(),
11335 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000011336 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000011337 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000011338 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000011339 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000011340 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000011341 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000011342 }
Dan Gohman475871a2008-07-27 21:46:04 +000011343 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000011344}
11345
Chris Lattner6cf73262008-01-25 06:14:17 +000011346/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
11347/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000011348static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000011349 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
11350 // F[X]OR(0.0, x) -> x
11351 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000011352 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
11353 if (C->getValueAPF().isPosZero())
11354 return N->getOperand(1);
11355 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
11356 if (C->getValueAPF().isPosZero())
11357 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000011358 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000011359}
11360
11361/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000011362static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000011363 // FAND(0.0, x) -> 0.0
11364 // FAND(x, 0.0) -> 0.0
11365 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
11366 if (C->getValueAPF().isPosZero())
11367 return N->getOperand(0);
11368 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
11369 if (C->getValueAPF().isPosZero())
11370 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000011371 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000011372}
11373
Dan Gohmane5af2d32009-01-29 01:59:02 +000011374static SDValue PerformBTCombine(SDNode *N,
11375 SelectionDAG &DAG,
11376 TargetLowering::DAGCombinerInfo &DCI) {
11377 // BT ignores high bits in the bit index operand.
11378 SDValue Op1 = N->getOperand(1);
11379 if (Op1.hasOneUse()) {
11380 unsigned BitWidth = Op1.getValueSizeInBits();
11381 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
11382 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000011383 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
11384 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000011385 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000011386 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
11387 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
11388 DCI.CommitTargetLoweringOpt(TLO);
11389 }
11390 return SDValue();
11391}
Chris Lattner83e6c992006-10-04 06:57:07 +000011392
Eli Friedman7a5e5552009-06-07 06:52:44 +000011393static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
11394 SDValue Op = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011395 if (Op.getOpcode() == ISD::BITCAST)
Eli Friedman7a5e5552009-06-07 06:52:44 +000011396 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000011397 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000011398 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000011399 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000011400 OpVT.getVectorElementType().getSizeInBits()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011401 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Eli Friedman7a5e5552009-06-07 06:52:44 +000011402 }
11403 return SDValue();
11404}
11405
Evan Cheng2e489c42009-12-16 00:53:11 +000011406static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG) {
11407 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
11408 // (and (i32 x86isd::setcc_carry), 1)
11409 // This eliminates the zext. This transformation is necessary because
11410 // ISD::SETCC is always legalized to i8.
11411 DebugLoc dl = N->getDebugLoc();
11412 SDValue N0 = N->getOperand(0);
11413 EVT VT = N->getValueType(0);
11414 if (N0.getOpcode() == ISD::AND &&
11415 N0.hasOneUse() &&
11416 N0.getOperand(0).hasOneUse()) {
11417 SDValue N00 = N0.getOperand(0);
11418 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
11419 return SDValue();
11420 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
11421 if (!C || C->getZExtValue() != 1)
11422 return SDValue();
11423 return DAG.getNode(ISD::AND, dl, VT,
11424 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
11425 N00.getOperand(0), N00.getOperand(1)),
11426 DAG.getConstant(1, VT));
11427 }
11428
11429 return SDValue();
11430}
11431
Dan Gohman475871a2008-07-27 21:46:04 +000011432SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000011433 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000011434 SelectionDAG &DAG = DCI.DAG;
11435 switch (N->getOpcode()) {
11436 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000011437 case ISD::EXTRACT_VECTOR_ELT:
11438 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, *this);
Chris Lattneraf723b92008-01-25 05:46:26 +000011439 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +000011440 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000011441 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000011442 case ISD::SHL:
11443 case ISD::SRA:
11444 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Nate Begemanb65c1752010-12-17 22:55:37 +000011445 case ISD::AND: return PerformAndCombine(N, DAG, DCI, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000011446 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000011447 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +000011448 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000011449 case X86ISD::FOR: return PerformFORCombine(N, DAG);
11450 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000011451 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000011452 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Evan Cheng2e489c42009-12-16 00:53:11 +000011453 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000011454 case X86ISD::SHUFPS: // Handle all target specific shuffles
11455 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +000011456 case X86ISD::PALIGN:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000011457 case X86ISD::PUNPCKHBW:
11458 case X86ISD::PUNPCKHWD:
11459 case X86ISD::PUNPCKHDQ:
11460 case X86ISD::PUNPCKHQDQ:
11461 case X86ISD::UNPCKHPS:
11462 case X86ISD::UNPCKHPD:
11463 case X86ISD::PUNPCKLBW:
11464 case X86ISD::PUNPCKLWD:
11465 case X86ISD::PUNPCKLDQ:
11466 case X86ISD::PUNPCKLQDQ:
11467 case X86ISD::UNPCKLPS:
11468 case X86ISD::UNPCKLPD:
11469 case X86ISD::MOVHLPS:
11470 case X86ISD::MOVLHPS:
11471 case X86ISD::PSHUFD:
11472 case X86ISD::PSHUFHW:
11473 case X86ISD::PSHUFLW:
11474 case X86ISD::MOVSS:
11475 case X86ISD::MOVSD:
11476 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, *this);
Evan Cheng206ee9d2006-07-07 08:33:52 +000011477 }
11478
Dan Gohman475871a2008-07-27 21:46:04 +000011479 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000011480}
11481
Evan Chenge5b51ac2010-04-17 06:13:15 +000011482/// isTypeDesirableForOp - Return true if the target has native support for
11483/// the specified value type and it is 'desirable' to use the type for the
11484/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
11485/// instruction encodings are longer and some i16 instructions are slow.
11486bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
11487 if (!isTypeLegal(VT))
11488 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000011489 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000011490 return true;
11491
11492 switch (Opc) {
11493 default:
11494 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000011495 case ISD::LOAD:
11496 case ISD::SIGN_EXTEND:
11497 case ISD::ZERO_EXTEND:
11498 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000011499 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000011500 case ISD::SRL:
11501 case ISD::SUB:
11502 case ISD::ADD:
11503 case ISD::MUL:
11504 case ISD::AND:
11505 case ISD::OR:
11506 case ISD::XOR:
11507 return false;
11508 }
11509}
11510
11511/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000011512/// beneficial for dag combiner to promote the specified node. If true, it
11513/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000011514bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000011515 EVT VT = Op.getValueType();
11516 if (VT != MVT::i16)
11517 return false;
11518
Evan Cheng4c26e932010-04-19 19:29:22 +000011519 bool Promote = false;
11520 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000011521 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000011522 default: break;
11523 case ISD::LOAD: {
11524 LoadSDNode *LD = cast<LoadSDNode>(Op);
11525 // If the non-extending load has a single use and it's not live out, then it
11526 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000011527 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
11528 Op.hasOneUse()*/) {
11529 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
11530 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
11531 // The only case where we'd want to promote LOAD (rather then it being
11532 // promoted as an operand is when it's only use is liveout.
11533 if (UI->getOpcode() != ISD::CopyToReg)
11534 return false;
11535 }
11536 }
Evan Cheng4c26e932010-04-19 19:29:22 +000011537 Promote = true;
11538 break;
11539 }
11540 case ISD::SIGN_EXTEND:
11541 case ISD::ZERO_EXTEND:
11542 case ISD::ANY_EXTEND:
11543 Promote = true;
11544 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000011545 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000011546 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000011547 SDValue N0 = Op.getOperand(0);
11548 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000011549 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000011550 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000011551 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000011552 break;
11553 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000011554 case ISD::ADD:
11555 case ISD::MUL:
11556 case ISD::AND:
11557 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000011558 case ISD::XOR:
11559 Commute = true;
11560 // fallthrough
11561 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000011562 SDValue N0 = Op.getOperand(0);
11563 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000011564 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000011565 return false;
11566 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000011567 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000011568 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000011569 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000011570 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000011571 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000011572 }
11573 }
11574
11575 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000011576 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000011577}
11578
Evan Cheng60c07e12006-07-05 22:17:51 +000011579//===----------------------------------------------------------------------===//
11580// X86 Inline Assembly Support
11581//===----------------------------------------------------------------------===//
11582
Chris Lattnerb8105652009-07-20 17:51:36 +000011583static bool LowerToBSwap(CallInst *CI) {
11584 // FIXME: this should verify that we are targetting a 486 or better. If not,
11585 // we will turn this bswap into something that will be lowered to logical ops
11586 // instead of emitting the bswap asm. For now, we don't support 486 or lower
11587 // so don't worry about this.
Eric Christopherfd179292009-08-27 18:07:15 +000011588
Chris Lattnerb8105652009-07-20 17:51:36 +000011589 // Verify this is a simple bswap.
Gabor Greife1c2b9c2010-06-30 13:03:37 +000011590 if (CI->getNumArgOperands() != 1 ||
Gabor Greif1cfe44a2010-06-26 11:51:52 +000011591 CI->getType() != CI->getArgOperand(0)->getType() ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000011592 !CI->getType()->isIntegerTy())
Chris Lattnerb8105652009-07-20 17:51:36 +000011593 return false;
Eric Christopherfd179292009-08-27 18:07:15 +000011594
Chris Lattnerb8105652009-07-20 17:51:36 +000011595 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
11596 if (!Ty || Ty->getBitWidth() % 16 != 0)
11597 return false;
Eric Christopherfd179292009-08-27 18:07:15 +000011598
Chris Lattnerb8105652009-07-20 17:51:36 +000011599 // Okay, we can do this xform, do so now.
11600 const Type *Tys[] = { Ty };
11601 Module *M = CI->getParent()->getParent()->getParent();
11602 Constant *Int = Intrinsic::getDeclaration(M, Intrinsic::bswap, Tys, 1);
Eric Christopherfd179292009-08-27 18:07:15 +000011603
Gabor Greif1cfe44a2010-06-26 11:51:52 +000011604 Value *Op = CI->getArgOperand(0);
Chris Lattnerb8105652009-07-20 17:51:36 +000011605 Op = CallInst::Create(Int, Op, CI->getName(), CI);
Eric Christopherfd179292009-08-27 18:07:15 +000011606
Chris Lattnerb8105652009-07-20 17:51:36 +000011607 CI->replaceAllUsesWith(Op);
11608 CI->eraseFromParent();
11609 return true;
11610}
11611
11612bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
11613 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
John Thompson44ab89e2010-10-29 17:29:13 +000011614 InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
Chris Lattnerb8105652009-07-20 17:51:36 +000011615
11616 std::string AsmStr = IA->getAsmString();
11617
11618 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000011619 SmallVector<StringRef, 4> AsmPieces;
Peter Collingbourne98361182010-11-13 19:54:23 +000011620 SplitString(AsmStr, AsmPieces, ";\n");
Chris Lattnerb8105652009-07-20 17:51:36 +000011621
11622 switch (AsmPieces.size()) {
11623 default: return false;
11624 case 1:
11625 AsmStr = AsmPieces[0];
11626 AsmPieces.clear();
11627 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
11628
11629 // bswap $0
11630 if (AsmPieces.size() == 2 &&
11631 (AsmPieces[0] == "bswap" ||
11632 AsmPieces[0] == "bswapq" ||
11633 AsmPieces[0] == "bswapl") &&
11634 (AsmPieces[1] == "$0" ||
11635 AsmPieces[1] == "${0:q}")) {
11636 // No need to check constraints, nothing other than the equivalent of
11637 // "=r,0" would be valid here.
11638 return LowerToBSwap(CI);
11639 }
11640 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000011641 if (CI->getType()->isIntegerTy(16) &&
Chris Lattnerb8105652009-07-20 17:51:36 +000011642 AsmPieces.size() == 3 &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000011643 (AsmPieces[0] == "rorw" || AsmPieces[0] == "rolw") &&
Chris Lattnerb8105652009-07-20 17:51:36 +000011644 AsmPieces[1] == "$$8," &&
11645 AsmPieces[2] == "${0:w}" &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000011646 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
11647 AsmPieces.clear();
Benjamin Kramer018cbd52010-03-12 13:54:59 +000011648 const std::string &Constraints = IA->getConstraintString();
11649 SplitString(StringRef(Constraints).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000011650 std::sort(AsmPieces.begin(), AsmPieces.end());
11651 if (AsmPieces.size() == 4 &&
11652 AsmPieces[0] == "~{cc}" &&
11653 AsmPieces[1] == "~{dirflag}" &&
11654 AsmPieces[2] == "~{flags}" &&
11655 AsmPieces[3] == "~{fpsr}") {
11656 return LowerToBSwap(CI);
11657 }
Chris Lattnerb8105652009-07-20 17:51:36 +000011658 }
11659 break;
11660 case 3:
Peter Collingbourne948cf022010-11-13 19:54:30 +000011661 if (CI->getType()->isIntegerTy(32) &&
11662 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
11663 SmallVector<StringRef, 4> Words;
11664 SplitString(AsmPieces[0], Words, " \t,");
11665 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
11666 Words[2] == "${0:w}") {
11667 Words.clear();
11668 SplitString(AsmPieces[1], Words, " \t,");
11669 if (Words.size() == 3 && Words[0] == "rorl" && Words[1] == "$$16" &&
11670 Words[2] == "$0") {
11671 Words.clear();
11672 SplitString(AsmPieces[2], Words, " \t,");
11673 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
11674 Words[2] == "${0:w}") {
11675 AsmPieces.clear();
11676 const std::string &Constraints = IA->getConstraintString();
11677 SplitString(StringRef(Constraints).substr(5), AsmPieces, ",");
11678 std::sort(AsmPieces.begin(), AsmPieces.end());
11679 if (AsmPieces.size() == 4 &&
11680 AsmPieces[0] == "~{cc}" &&
11681 AsmPieces[1] == "~{dirflag}" &&
11682 AsmPieces[2] == "~{flags}" &&
11683 AsmPieces[3] == "~{fpsr}") {
11684 return LowerToBSwap(CI);
11685 }
11686 }
11687 }
11688 }
11689 }
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000011690 if (CI->getType()->isIntegerTy(64) &&
Owen Anderson1d0be152009-08-13 21:58:54 +000011691 Constraints.size() >= 2 &&
Chris Lattnerb8105652009-07-20 17:51:36 +000011692 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
11693 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
11694 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
Benjamin Kramerd4f19592010-01-11 18:03:24 +000011695 SmallVector<StringRef, 4> Words;
Chris Lattnerb8105652009-07-20 17:51:36 +000011696 SplitString(AsmPieces[0], Words, " \t");
11697 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
11698 Words.clear();
11699 SplitString(AsmPieces[1], Words, " \t");
11700 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
11701 Words.clear();
11702 SplitString(AsmPieces[2], Words, " \t,");
11703 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
11704 Words[2] == "%edx") {
11705 return LowerToBSwap(CI);
11706 }
11707 }
11708 }
11709 }
11710 break;
11711 }
11712 return false;
11713}
11714
11715
11716
Chris Lattnerf4dff842006-07-11 02:54:03 +000011717/// getConstraintType - Given a constraint letter, return the type of
11718/// constraint it is for this target.
11719X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000011720X86TargetLowering::getConstraintType(const std::string &Constraint) const {
11721 if (Constraint.size() == 1) {
11722 switch (Constraint[0]) {
Chris Lattner4234f572007-03-25 02:14:49 +000011723 case 'R':
Chris Lattner4234f572007-03-25 02:14:49 +000011724 case 'q':
11725 case 'Q':
John Thompson44ab89e2010-10-29 17:29:13 +000011726 case 'f':
11727 case 't':
11728 case 'u':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000011729 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +000011730 case 'x':
Chris Lattner4234f572007-03-25 02:14:49 +000011731 case 'Y':
11732 return C_RegisterClass;
John Thompson44ab89e2010-10-29 17:29:13 +000011733 case 'a':
11734 case 'b':
11735 case 'c':
11736 case 'd':
11737 case 'S':
11738 case 'D':
11739 case 'A':
11740 return C_Register;
11741 case 'I':
11742 case 'J':
11743 case 'K':
11744 case 'L':
11745 case 'M':
11746 case 'N':
11747 case 'G':
11748 case 'C':
Dale Johannesen78e3e522009-02-12 20:58:09 +000011749 case 'e':
11750 case 'Z':
11751 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000011752 default:
11753 break;
11754 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000011755 }
Chris Lattner4234f572007-03-25 02:14:49 +000011756 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000011757}
11758
John Thompson44ab89e2010-10-29 17:29:13 +000011759/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +000011760/// This object must already have been set up with the operand type
11761/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +000011762TargetLowering::ConstraintWeight
11763 X86TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +000011764 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +000011765 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +000011766 Value *CallOperandVal = info.CallOperandVal;
11767 // If we don't have a value, we can't do a match,
11768 // but allow it at the lowest weight.
11769 if (CallOperandVal == NULL)
John Thompson44ab89e2010-10-29 17:29:13 +000011770 return CW_Default;
11771 const Type *type = CallOperandVal->getType();
John Thompsoneac6e1d2010-09-13 18:15:37 +000011772 // Look at the constraint type.
11773 switch (*constraint) {
11774 default:
John Thompson44ab89e2010-10-29 17:29:13 +000011775 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
11776 case 'R':
11777 case 'q':
11778 case 'Q':
11779 case 'a':
11780 case 'b':
11781 case 'c':
11782 case 'd':
11783 case 'S':
11784 case 'D':
11785 case 'A':
11786 if (CallOperandVal->getType()->isIntegerTy())
11787 weight = CW_SpecificReg;
11788 break;
11789 case 'f':
11790 case 't':
11791 case 'u':
11792 if (type->isFloatingPointTy())
11793 weight = CW_SpecificReg;
11794 break;
11795 case 'y':
11796 if (type->isX86_MMXTy() && !DisableMMX && Subtarget->hasMMX())
11797 weight = CW_SpecificReg;
11798 break;
11799 case 'x':
11800 case 'Y':
Nate Begeman2ea8ee72010-12-10 00:26:57 +000011801 if ((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasXMM())
John Thompson44ab89e2010-10-29 17:29:13 +000011802 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +000011803 break;
11804 case 'I':
11805 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
11806 if (C->getZExtValue() <= 31)
John Thompson44ab89e2010-10-29 17:29:13 +000011807 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +000011808 }
11809 break;
John Thompson44ab89e2010-10-29 17:29:13 +000011810 case 'J':
11811 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
11812 if (C->getZExtValue() <= 63)
11813 weight = CW_Constant;
11814 }
11815 break;
11816 case 'K':
11817 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
11818 if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f))
11819 weight = CW_Constant;
11820 }
11821 break;
11822 case 'L':
11823 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
11824 if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff))
11825 weight = CW_Constant;
11826 }
11827 break;
11828 case 'M':
11829 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
11830 if (C->getZExtValue() <= 3)
11831 weight = CW_Constant;
11832 }
11833 break;
11834 case 'N':
11835 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
11836 if (C->getZExtValue() <= 0xff)
11837 weight = CW_Constant;
11838 }
11839 break;
11840 case 'G':
11841 case 'C':
11842 if (dyn_cast<ConstantFP>(CallOperandVal)) {
11843 weight = CW_Constant;
11844 }
11845 break;
11846 case 'e':
11847 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
11848 if ((C->getSExtValue() >= -0x80000000LL) &&
11849 (C->getSExtValue() <= 0x7fffffffLL))
11850 weight = CW_Constant;
11851 }
11852 break;
11853 case 'Z':
11854 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
11855 if (C->getZExtValue() <= 0xffffffff)
11856 weight = CW_Constant;
11857 }
11858 break;
John Thompsoneac6e1d2010-09-13 18:15:37 +000011859 }
11860 return weight;
11861}
11862
Dale Johannesenba2a0b92008-01-29 02:21:21 +000011863/// LowerXConstraint - try to replace an X constraint, which matches anything,
11864/// with another that has more specific requirements based on the type of the
11865/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000011866const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000011867LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000011868 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
11869 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000011870 if (ConstraintVT.isFloatingPoint()) {
Nate Begeman2ea8ee72010-12-10 00:26:57 +000011871 if (Subtarget->hasXMMInt())
Chris Lattner5e764232008-04-26 23:02:14 +000011872 return "Y";
Nate Begeman2ea8ee72010-12-10 00:26:57 +000011873 if (Subtarget->hasXMM())
Chris Lattner5e764232008-04-26 23:02:14 +000011874 return "x";
11875 }
Scott Michelfdc40a02009-02-17 22:15:04 +000011876
Chris Lattner5e764232008-04-26 23:02:14 +000011877 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000011878}
11879
Chris Lattner48884cd2007-08-25 00:47:38 +000011880/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
11881/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000011882void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattner48884cd2007-08-25 00:47:38 +000011883 char Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000011884 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000011885 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000011886 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000011887
Chris Lattner22aaf1d2006-10-31 20:13:11 +000011888 switch (Constraint) {
11889 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000011890 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000011891 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000011892 if (C->getZExtValue() <= 31) {
11893 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000011894 break;
11895 }
Devang Patel84f7fd22007-03-17 00:13:28 +000011896 }
Chris Lattner48884cd2007-08-25 00:47:38 +000011897 return;
Evan Cheng364091e2008-09-22 23:57:37 +000011898 case 'J':
11899 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000011900 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000011901 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
11902 break;
11903 }
11904 }
11905 return;
11906 case 'K':
11907 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000011908 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +000011909 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
11910 break;
11911 }
11912 }
11913 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000011914 case 'N':
11915 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000011916 if (C->getZExtValue() <= 255) {
11917 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000011918 break;
11919 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000011920 }
Chris Lattner48884cd2007-08-25 00:47:38 +000011921 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000011922 case 'e': {
11923 // 32-bit signed value
11924 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000011925 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
11926 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000011927 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000011928 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000011929 break;
11930 }
11931 // FIXME gcc accepts some relocatable values here too, but only in certain
11932 // memory models; it's complicated.
11933 }
11934 return;
11935 }
11936 case 'Z': {
11937 // 32-bit unsigned value
11938 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000011939 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
11940 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000011941 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
11942 break;
11943 }
11944 }
11945 // FIXME gcc accepts some relocatable values here too, but only in certain
11946 // memory models; it's complicated.
11947 return;
11948 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000011949 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000011950 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000011951 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000011952 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000011953 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000011954 break;
11955 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011956
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000011957 // In any sort of PIC mode addresses need to be computed at runtime by
11958 // adding in a register or some sort of table lookup. These can't
11959 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000011960 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000011961 return;
11962
Chris Lattnerdc43a882007-05-03 16:52:29 +000011963 // If we are in non-pic codegen mode, we allow the address of a global (with
11964 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000011965 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000011966 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000011967
Chris Lattner49921962009-05-08 18:23:14 +000011968 // Match either (GA), (GA+C), (GA+C1+C2), etc.
11969 while (1) {
11970 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
11971 Offset += GA->getOffset();
11972 break;
11973 } else if (Op.getOpcode() == ISD::ADD) {
11974 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
11975 Offset += C->getZExtValue();
11976 Op = Op.getOperand(0);
11977 continue;
11978 }
11979 } else if (Op.getOpcode() == ISD::SUB) {
11980 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
11981 Offset += -C->getZExtValue();
11982 Op = Op.getOperand(0);
11983 continue;
11984 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000011985 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000011986
Chris Lattner49921962009-05-08 18:23:14 +000011987 // Otherwise, this isn't something we can handle, reject it.
11988 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000011989 }
Eric Christopherfd179292009-08-27 18:07:15 +000011990
Dan Gohman46510a72010-04-15 01:51:59 +000011991 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000011992 // If we require an extra load to get this address, as in PIC mode, we
11993 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000011994 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
11995 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000011996 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000011997
Devang Patel0d881da2010-07-06 22:08:15 +000011998 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
11999 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000012000 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012001 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000012002 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012003
Gabor Greifba36cb52008-08-28 21:40:38 +000012004 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000012005 Ops.push_back(Result);
12006 return;
12007 }
Dale Johannesen1784d162010-06-25 21:55:36 +000012008 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012009}
12010
Chris Lattner259e97c2006-01-31 19:43:35 +000012011std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +000012012getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000012013 EVT VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +000012014 if (Constraint.size() == 1) {
12015 // FIXME: not handling fp-stack yet!
Chris Lattner259e97c2006-01-31 19:43:35 +000012016 switch (Constraint[0]) { // GCC X86 Constraint Letters
Chris Lattnerf4dff842006-07-11 02:54:03 +000012017 default: break; // Unknown constraint letter
Evan Cheng47e9fab2009-07-17 22:13:25 +000012018 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
12019 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000012020 if (VT == MVT::i32)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012021 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX,
12022 X86::ESI, X86::EDI, X86::R8D, X86::R9D,
12023 X86::R10D,X86::R11D,X86::R12D,
12024 X86::R13D,X86::R14D,X86::R15D,
12025 X86::EBP, X86::ESP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012026 else if (VT == MVT::i16)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012027 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX,
12028 X86::SI, X86::DI, X86::R8W,X86::R9W,
12029 X86::R10W,X86::R11W,X86::R12W,
12030 X86::R13W,X86::R14W,X86::R15W,
12031 X86::BP, X86::SP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012032 else if (VT == MVT::i8)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012033 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL,
12034 X86::SIL, X86::DIL, X86::R8B,X86::R9B,
12035 X86::R10B,X86::R11B,X86::R12B,
12036 X86::R13B,X86::R14B,X86::R15B,
12037 X86::BPL, X86::SPL, 0);
12038
Owen Anderson825b72b2009-08-11 20:47:22 +000012039 else if (VT == MVT::i64)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012040 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX,
12041 X86::RSI, X86::RDI, X86::R8, X86::R9,
12042 X86::R10, X86::R11, X86::R12,
12043 X86::R13, X86::R14, X86::R15,
12044 X86::RBP, X86::RSP, 0);
12045
12046 break;
12047 }
Eric Christopherfd179292009-08-27 18:07:15 +000012048 // 32-bit fallthrough
Chris Lattner259e97c2006-01-31 19:43:35 +000012049 case 'Q': // Q_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +000012050 if (VT == MVT::i32)
Chris Lattner80a7ecc2006-05-06 00:29:37 +000012051 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012052 else if (VT == MVT::i16)
Chris Lattner80a7ecc2006-05-06 00:29:37 +000012053 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012054 else if (VT == MVT::i8)
Evan Cheng12914382007-08-13 23:27:11 +000012055 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012056 else if (VT == MVT::i64)
Chris Lattner03e6c702007-11-04 06:51:12 +000012057 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
12058 break;
Chris Lattner259e97c2006-01-31 19:43:35 +000012059 }
12060 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012061
Chris Lattner1efa40f2006-02-22 00:56:39 +000012062 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +000012063}
Chris Lattnerf76d1802006-07-31 23:26:50 +000012064
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012065std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000012066X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000012067 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000012068 // First, see if this is a constraint that directly corresponds to an LLVM
12069 // register class.
12070 if (Constraint.size() == 1) {
12071 // GCC Constraint Letters
12072 switch (Constraint[0]) {
12073 default: break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000012074 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000012075 case 'l': // INDEX_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +000012076 if (VT == MVT::i8)
Chris Lattner0f65cad2007-04-09 05:49:22 +000012077 return std::make_pair(0U, X86::GR8RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012078 if (VT == MVT::i16)
Chris Lattner1fa71982008-10-17 18:15:05 +000012079 return std::make_pair(0U, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012080 if (VT == MVT::i32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +000012081 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +000012082 return std::make_pair(0U, X86::GR64RegisterClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000012083 case 'R': // LEGACY_REGS
12084 if (VT == MVT::i8)
12085 return std::make_pair(0U, X86::GR8_NOREXRegisterClass);
12086 if (VT == MVT::i16)
12087 return std::make_pair(0U, X86::GR16_NOREXRegisterClass);
12088 if (VT == MVT::i32 || !Subtarget->is64Bit())
12089 return std::make_pair(0U, X86::GR32_NOREXRegisterClass);
12090 return std::make_pair(0U, X86::GR64_NOREXRegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000012091 case 'f': // FP Stack registers.
12092 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
12093 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000012094 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000012095 return std::make_pair(0U, X86::RFP32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012096 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000012097 return std::make_pair(0U, X86::RFP64RegisterClass);
12098 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000012099 case 'y': // MMX_REGS if MMX allowed.
12100 if (!Subtarget->hasMMX()) break;
12101 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000012102 case 'Y': // SSE_REGS if SSE2 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012103 if (!Subtarget->hasXMMInt()) break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000012104 // FALL THROUGH.
12105 case 'x': // SSE_REGS if SSE1 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012106 if (!Subtarget->hasXMM()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000012107
Owen Anderson825b72b2009-08-11 20:47:22 +000012108 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000012109 default: break;
12110 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000012111 case MVT::f32:
12112 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +000012113 return std::make_pair(0U, X86::FR32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012114 case MVT::f64:
12115 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +000012116 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000012117 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000012118 case MVT::v16i8:
12119 case MVT::v8i16:
12120 case MVT::v4i32:
12121 case MVT::v2i64:
12122 case MVT::v4f32:
12123 case MVT::v2f64:
Chris Lattner0f65cad2007-04-09 05:49:22 +000012124 return std::make_pair(0U, X86::VR128RegisterClass);
12125 }
Chris Lattnerad043e82007-04-09 05:11:28 +000012126 break;
12127 }
12128 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012129
Chris Lattnerf76d1802006-07-31 23:26:50 +000012130 // Use the default implementation in TargetLowering to convert the register
12131 // constraint into a member of a register class.
12132 std::pair<unsigned, const TargetRegisterClass*> Res;
12133 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000012134
12135 // Not found as a standard register?
12136 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000012137 // Map st(0) -> st(7) -> ST0
12138 if (Constraint.size() == 7 && Constraint[0] == '{' &&
12139 tolower(Constraint[1]) == 's' &&
12140 tolower(Constraint[2]) == 't' &&
12141 Constraint[3] == '(' &&
12142 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
12143 Constraint[5] == ')' &&
12144 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000012145
Chris Lattner56d77c72009-09-13 22:41:48 +000012146 Res.first = X86::ST0+Constraint[4]-'0';
12147 Res.second = X86::RFP80RegisterClass;
12148 return Res;
12149 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000012150
Chris Lattner56d77c72009-09-13 22:41:48 +000012151 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000012152 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000012153 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +000012154 Res.second = X86::RFP80RegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000012155 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000012156 }
Chris Lattner56d77c72009-09-13 22:41:48 +000012157
12158 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000012159 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000012160 Res.first = X86::EFLAGS;
12161 Res.second = X86::CCRRegisterClass;
12162 return Res;
12163 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000012164
Dale Johannesen330169f2008-11-13 21:52:36 +000012165 // 'A' means EAX + EDX.
12166 if (Constraint == "A") {
12167 Res.first = X86::EAX;
Dan Gohman68a31c22009-07-30 17:02:08 +000012168 Res.second = X86::GR32_ADRegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000012169 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000012170 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000012171 return Res;
12172 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012173
Chris Lattnerf76d1802006-07-31 23:26:50 +000012174 // Otherwise, check to see if this is a register class of the wrong value
12175 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
12176 // turn into {ax},{dx}.
12177 if (Res.second->hasType(VT))
12178 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012179
Chris Lattnerf76d1802006-07-31 23:26:50 +000012180 // All of the single-register GCC register classes map their values onto
12181 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
12182 // really want an 8-bit or 32-bit register, map to the appropriate register
12183 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +000012184 if (Res.second == X86::GR16RegisterClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000012185 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000012186 unsigned DestReg = 0;
12187 switch (Res.first) {
12188 default: break;
12189 case X86::AX: DestReg = X86::AL; break;
12190 case X86::DX: DestReg = X86::DL; break;
12191 case X86::CX: DestReg = X86::CL; break;
12192 case X86::BX: DestReg = X86::BL; break;
12193 }
12194 if (DestReg) {
12195 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000012196 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000012197 }
Owen Anderson825b72b2009-08-11 20:47:22 +000012198 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000012199 unsigned DestReg = 0;
12200 switch (Res.first) {
12201 default: break;
12202 case X86::AX: DestReg = X86::EAX; break;
12203 case X86::DX: DestReg = X86::EDX; break;
12204 case X86::CX: DestReg = X86::ECX; break;
12205 case X86::BX: DestReg = X86::EBX; break;
12206 case X86::SI: DestReg = X86::ESI; break;
12207 case X86::DI: DestReg = X86::EDI; break;
12208 case X86::BP: DestReg = X86::EBP; break;
12209 case X86::SP: DestReg = X86::ESP; break;
12210 }
12211 if (DestReg) {
12212 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000012213 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000012214 }
Owen Anderson825b72b2009-08-11 20:47:22 +000012215 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000012216 unsigned DestReg = 0;
12217 switch (Res.first) {
12218 default: break;
12219 case X86::AX: DestReg = X86::RAX; break;
12220 case X86::DX: DestReg = X86::RDX; break;
12221 case X86::CX: DestReg = X86::RCX; break;
12222 case X86::BX: DestReg = X86::RBX; break;
12223 case X86::SI: DestReg = X86::RSI; break;
12224 case X86::DI: DestReg = X86::RDI; break;
12225 case X86::BP: DestReg = X86::RBP; break;
12226 case X86::SP: DestReg = X86::RSP; break;
12227 }
12228 if (DestReg) {
12229 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000012230 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000012231 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000012232 }
Chris Lattner6ba50a92008-08-26 06:19:02 +000012233 } else if (Res.second == X86::FR32RegisterClass ||
12234 Res.second == X86::FR64RegisterClass ||
12235 Res.second == X86::VR128RegisterClass) {
12236 // Handle references to XMM physical registers that got mapped into the
12237 // wrong class. This can happen with constraints like {xmm0} where the
12238 // target independent register mapper will just pick the first match it can
12239 // find, ignoring the required type.
Owen Anderson825b72b2009-08-11 20:47:22 +000012240 if (VT == MVT::f32)
Chris Lattner6ba50a92008-08-26 06:19:02 +000012241 Res.second = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +000012242 else if (VT == MVT::f64)
Chris Lattner6ba50a92008-08-26 06:19:02 +000012243 Res.second = X86::FR64RegisterClass;
12244 else if (X86::VR128RegisterClass->hasType(VT))
12245 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000012246 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012247
Chris Lattnerf76d1802006-07-31 23:26:50 +000012248 return Res;
12249}