blob: 1e8b27b8d10419b27160a503192abb323bc6bf40 [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()) {
70 if (is64Bit) return new X8664_MachoTargetObjectFile();
Anton Korobeynikov293d5922010-02-21 20:28:15 +000071 return new TargetLoweringObjectFileMachO();
Eric Christopher62f35a22010-07-05 19:26:33 +000072 } else if (TM.getSubtarget<X86Subtarget>().isTargetELF() ){
73 if (is64Bit) return new X8664_ELFTargetObjectFile(TM);
Anton Korobeynikov9184b252010-02-15 22:35:59 +000074 return new X8632_ELFTargetObjectFile(TM);
Eric Christopher62f35a22010-07-05 19:26:33 +000075 } else if (TM.getSubtarget<X86Subtarget>().isTargetCOFF()) {
Chris Lattnerf0144122009-07-28 03:13:23 +000076 return new TargetLoweringObjectFileCOFF();
Michael J. Spencerec38de22010-10-10 22:04:20 +000077 }
Eric Christopher62f35a22010-07-05 19:26:33 +000078 llvm_unreachable("unknown subtarget type");
Chris Lattnerf0144122009-07-28 03:13:23 +000079}
80
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +000081X86TargetLowering::X86TargetLowering(X86TargetMachine &TM)
Chris Lattnerf0144122009-07-28 03:13:23 +000082 : TargetLowering(TM, createTLOF(TM)) {
Evan Cheng559806f2006-01-27 08:10:46 +000083 Subtarget = &TM.getSubtarget<X86Subtarget>();
Nate Begeman2ea8ee72010-12-10 00:26:57 +000084 X86ScalarSSEf64 = Subtarget->hasXMMInt();
85 X86ScalarSSEf32 = Subtarget->hasXMM();
Evan Cheng25ab6902006-09-08 06:48:29 +000086 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000087
Anton Korobeynikov2365f512007-07-14 14:06:15 +000088 RegInfo = TM.getRegisterInfo();
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000089 TD = getTargetData();
Anton Korobeynikov2365f512007-07-14 14:06:15 +000090
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000091 // Set up the TargetLowering object.
92
93 // X86 is weird, it always uses i8 for shift amounts and setcc results.
Owen Anderson825b72b2009-08-11 20:47:22 +000094 setShiftAmountType(MVT::i8);
Duncan Sands03228082008-11-23 15:47:28 +000095 setBooleanContents(ZeroOrOneBooleanContent);
Evan Cheng211ffa12010-05-19 20:19:50 +000096 setSchedulingPreference(Sched::RegPressure);
Evan Cheng25ab6902006-09-08 06:48:29 +000097 setStackPointerRegisterToSaveRestore(X86StackPtr);
Evan Cheng714554d2006-03-16 21:47:42 +000098
Michael J. Spencer92bf38c2010-10-10 23:11:06 +000099 if (Subtarget->isTargetWindows() && !Subtarget->isTargetCygMing()) {
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000100 // Setup Windows compiler runtime calls.
101 setLibcallName(RTLIB::SDIV_I64, "_alldiv");
Michael J. Spencer335b8062010-10-11 05:29:15 +0000102 setLibcallName(RTLIB::UDIV_I64, "_aulldiv");
103 setLibcallName(RTLIB::FPTOUINT_F64_I64, "_ftol2");
Michael J. Spencer94f7eeb2010-10-19 07:32:52 +0000104 setLibcallName(RTLIB::FPTOUINT_F32_I64, "_ftol2");
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000105 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::X86_StdCall);
Michael J. Spencer335b8062010-10-11 05:29:15 +0000106 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::X86_StdCall);
Michael J. Spencer6dad10e2010-10-27 18:52:38 +0000107 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I64, CallingConv::C);
108 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I64, CallingConv::C);
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000109 }
110
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000111 if (Subtarget->isTargetDarwin()) {
Evan Chengdf57fa02006-03-17 20:31:41 +0000112 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000113 setUseUnderscoreSetJmp(false);
114 setUseUnderscoreLongJmp(false);
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000115 } else if (Subtarget->isTargetMingw()) {
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000116 // MS runtime is weird: it exports _setjmp, but longjmp!
117 setUseUnderscoreSetJmp(true);
118 setUseUnderscoreLongJmp(false);
119 } else {
120 setUseUnderscoreSetJmp(true);
121 setUseUnderscoreLongJmp(true);
122 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000123
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000124 // Set up the register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000125 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
Dan Gohman71edb242010-04-30 18:30:26 +0000126 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000127 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
Evan Cheng25ab6902006-09-08 06:48:29 +0000128 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000129 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000130
Owen Anderson825b72b2009-08-11 20:47:22 +0000131 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Evan Chengc5484282006-10-04 00:56:09 +0000132
Scott Michelfdc40a02009-02-17 22:15:04 +0000133 // We don't accept any truncstore of integer registers.
Owen Anderson825b72b2009-08-11 20:47:22 +0000134 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000135 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000136 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000137 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000138 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
139 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
Evan Cheng7f042682008-10-15 02:05:31 +0000140
141 // SETOEQ and SETUNE require checking two conditions.
Owen Anderson825b72b2009-08-11 20:47:22 +0000142 setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand);
143 setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand);
144 setCondCodeAction(ISD::SETOEQ, MVT::f80, Expand);
145 setCondCodeAction(ISD::SETUNE, MVT::f32, Expand);
146 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
147 setCondCodeAction(ISD::SETUNE, MVT::f80, Expand);
Chris Lattnerddf89562008-01-17 19:59:44 +0000148
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000149 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
150 // operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000151 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
152 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
153 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +0000154
Evan Cheng25ab6902006-09-08 06:48:29 +0000155 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000156 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
157 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
Eli Friedman948e95a2009-05-23 09:59:16 +0000158 } else if (!UseSoftFloat) {
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000159 // We have an algorithm for SSE2->double, and we turn this into a
160 // 64-bit FILD followed by conditional FADD for other targets.
161 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Eli Friedman948e95a2009-05-23 09:59:16 +0000162 // We have an algorithm for SSE2, and we turn this into a 64-bit
163 // FILD for other targets.
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000164 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000165 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000166
167 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
168 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000169 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
170 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000171
Devang Patel6a784892009-06-05 18:48:29 +0000172 if (!UseSoftFloat) {
Bill Wendling105be5a2009-03-13 08:41:47 +0000173 // SSE has no i16 to fp conversion, only i32
174 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000175 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000176 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000177 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000178 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000179 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
180 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000181 }
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000182 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000183 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
184 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Promote);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000185 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000186
Dale Johannesen73328d12007-09-19 23:55:34 +0000187 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
188 // are Legal, f80 is custom lowered.
Owen Anderson825b72b2009-08-11 20:47:22 +0000189 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
190 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Evan Cheng6dab0532006-01-30 08:02:57 +0000191
Evan Cheng02568ff2006-01-30 22:13:22 +0000192 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
193 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000194 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
195 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
Evan Cheng02568ff2006-01-30 22:13:22 +0000196
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000197 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000198 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000199 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000200 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000201 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000202 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
203 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000204 }
205
206 // Handle FP_TO_UINT by promoting the destination to a larger signed
207 // conversion.
Owen Anderson825b72b2009-08-11 20:47:22 +0000208 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
209 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
210 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000211
Evan Cheng25ab6902006-09-08 06:48:29 +0000212 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000213 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
214 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Eli Friedman948e95a2009-05-23 09:59:16 +0000215 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000216 if (X86ScalarSSEf32 && !Subtarget->hasSSE3())
Evan Cheng25ab6902006-09-08 06:48:29 +0000217 // Expand FP_TO_UINT into a select.
218 // FIXME: We would like to use a Custom expander here eventually to do
219 // the optimal thing for SSE vs. the default expansion in the legalizer.
Owen Anderson825b72b2009-08-11 20:47:22 +0000220 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000221 else
Eli Friedman948e95a2009-05-23 09:59:16 +0000222 // With SSE3 we can use fisttpll to convert to a signed i64; without
223 // SSE, we're stuck with a fistpll.
Owen Anderson825b72b2009-08-11 20:47:22 +0000224 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000225 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000226
Chris Lattner399610a2006-12-05 18:22:22 +0000227 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Michael J. Spencerec38de22010-10-10 22:04:20 +0000228 if (!X86ScalarSSEf64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000229 setOperationAction(ISD::BITCAST , MVT::f32 , Expand);
230 setOperationAction(ISD::BITCAST , MVT::i32 , Expand);
Dale Johannesene39859a2010-05-21 18:40:15 +0000231 if (Subtarget->is64Bit()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000232 setOperationAction(ISD::BITCAST , MVT::f64 , Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000233 // Without SSE, i64->f64 goes through memory.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000234 setOperationAction(ISD::BITCAST , MVT::i64 , Expand);
Dale Johannesen7d07b482010-05-21 00:52:33 +0000235 }
Chris Lattnerf3597a12006-12-05 18:45:06 +0000236 }
Chris Lattner21f66852005-12-23 05:15:23 +0000237
Dan Gohmanb00ee212008-02-18 19:34:53 +0000238 // Scalar integer divide and remainder are lowered to use operations that
239 // produce two results, to match the available instructions. This exposes
240 // the two-result form to trivial CSE, which is able to combine x/y and x%y
241 // into a single instruction.
242 //
243 // Scalar integer multiply-high is also lowered to use two-result
244 // operations, to match the available instructions. However, plain multiply
245 // (low) operations are left as Legal, as there are single-result
246 // instructions for this in x86. Using the two-result multiply instructions
247 // when both high and low results are needed must be arranged by dagcombine.
Owen Anderson825b72b2009-08-11 20:47:22 +0000248 setOperationAction(ISD::MULHS , MVT::i8 , Expand);
249 setOperationAction(ISD::MULHU , MVT::i8 , Expand);
250 setOperationAction(ISD::SDIV , MVT::i8 , Expand);
251 setOperationAction(ISD::UDIV , MVT::i8 , Expand);
252 setOperationAction(ISD::SREM , MVT::i8 , Expand);
253 setOperationAction(ISD::UREM , MVT::i8 , Expand);
254 setOperationAction(ISD::MULHS , MVT::i16 , Expand);
255 setOperationAction(ISD::MULHU , MVT::i16 , Expand);
256 setOperationAction(ISD::SDIV , MVT::i16 , Expand);
257 setOperationAction(ISD::UDIV , MVT::i16 , Expand);
258 setOperationAction(ISD::SREM , MVT::i16 , Expand);
259 setOperationAction(ISD::UREM , MVT::i16 , Expand);
260 setOperationAction(ISD::MULHS , MVT::i32 , Expand);
261 setOperationAction(ISD::MULHU , MVT::i32 , Expand);
262 setOperationAction(ISD::SDIV , MVT::i32 , Expand);
263 setOperationAction(ISD::UDIV , MVT::i32 , Expand);
264 setOperationAction(ISD::SREM , MVT::i32 , Expand);
265 setOperationAction(ISD::UREM , MVT::i32 , Expand);
266 setOperationAction(ISD::MULHS , MVT::i64 , Expand);
267 setOperationAction(ISD::MULHU , MVT::i64 , Expand);
268 setOperationAction(ISD::SDIV , MVT::i64 , Expand);
269 setOperationAction(ISD::UDIV , MVT::i64 , Expand);
270 setOperationAction(ISD::SREM , MVT::i64 , Expand);
271 setOperationAction(ISD::UREM , MVT::i64 , Expand);
Dan Gohmana37c9f72007-09-25 18:23:27 +0000272
Owen Anderson825b72b2009-08-11 20:47:22 +0000273 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
274 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
275 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
276 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000277 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000278 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
279 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
280 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
281 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
282 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
283 setOperationAction(ISD::FREM , MVT::f32 , Expand);
284 setOperationAction(ISD::FREM , MVT::f64 , Expand);
285 setOperationAction(ISD::FREM , MVT::f80 , Expand);
286 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000287
Owen Anderson825b72b2009-08-11 20:47:22 +0000288 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
289 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000290 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
291 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000292 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
293 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000294 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000295 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
296 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000297 }
298
Benjamin Kramer1292c222010-12-04 20:32:23 +0000299 if (Subtarget->hasPOPCNT()) {
300 setOperationAction(ISD::CTPOP , MVT::i8 , Promote);
301 } else {
302 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
303 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
304 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
305 if (Subtarget->is64Bit())
306 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
307 }
308
Owen Anderson825b72b2009-08-11 20:47:22 +0000309 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
310 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000311
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000312 // These should be promoted to a larger select which is supported.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000313 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000314 // X86 wants to expand cmov itself.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000315 setOperationAction(ISD::SELECT , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000316 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000317 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
318 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
319 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
320 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
321 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000322 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000323 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
324 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
325 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
326 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000327 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000328 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
329 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000330 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000331 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000332
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000333 // Darwin ABI issue.
Owen Anderson825b72b2009-08-11 20:47:22 +0000334 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
335 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
336 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
337 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000338 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000339 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
340 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000341 setOperationAction(ISD::BlockAddress , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000342 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000343 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
344 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
345 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
346 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000347 setOperationAction(ISD::BlockAddress , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000348 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000349 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Owen Anderson825b72b2009-08-11 20:47:22 +0000350 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
351 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
352 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000353 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000354 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
355 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
356 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000357 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000358
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000359 if (Subtarget->hasXMM())
Owen Anderson825b72b2009-08-11 20:47:22 +0000360 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Cheng27b7db52008-03-08 00:58:38 +0000361
Eric Christopher9a9d2752010-07-22 02:48:34 +0000362 // We may not have a libcall for MEMBARRIER so we should lower this.
363 setOperationAction(ISD::MEMBARRIER , MVT::Other, Custom);
Michael J. Spencerec38de22010-10-10 22:04:20 +0000364
Jim Grosbachf1ab49e2010-06-23 16:25:07 +0000365 // On X86 and X86-64, atomic operations are lowered to locked instructions.
366 // Locked instructions, in turn, have implicit fence semantics (all memory
367 // operations are flushed before issuing the locked instruction, and they
368 // are not buffered), so we can fold away the common pattern of
369 // fence-atomic-fence.
370 setShouldFoldAtomicFences(true);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000371
Mon P Wang63307c32008-05-05 19:05:59 +0000372 // Expand certain atomics
Owen Anderson825b72b2009-08-11 20:47:22 +0000373 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i8, Custom);
374 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i16, Custom);
375 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Custom);
376 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Custom);
Bill Wendling5bf1b4e2008-08-20 00:28:16 +0000377
Owen Anderson825b72b2009-08-11 20:47:22 +0000378 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i8, Custom);
379 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i16, Custom);
380 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Custom);
381 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000382
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000383 if (!Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000384 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
385 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
386 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
387 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
388 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
389 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
390 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000391 }
392
Evan Cheng3c992d22006-03-07 02:02:57 +0000393 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000394 if (!Subtarget->isTargetDarwin() &&
395 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000396 !Subtarget->isTargetCygMing()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000397 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Dan Gohman44066042008-07-01 00:05:16 +0000398 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000399
Owen Anderson825b72b2009-08-11 20:47:22 +0000400 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
401 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
402 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
403 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000404 if (Subtarget->is64Bit()) {
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000405 setExceptionPointerRegister(X86::RAX);
406 setExceptionSelectorRegister(X86::RDX);
407 } else {
408 setExceptionPointerRegister(X86::EAX);
409 setExceptionSelectorRegister(X86::EDX);
410 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000411 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
412 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000413
Owen Anderson825b72b2009-08-11 20:47:22 +0000414 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000415
Owen Anderson825b72b2009-08-11 20:47:22 +0000416 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000417
Nate Begemanacc398c2006-01-25 18:21:52 +0000418 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson825b72b2009-08-11 20:47:22 +0000419 setOperationAction(ISD::VASTART , MVT::Other, Custom);
420 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000421 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000422 setOperationAction(ISD::VAARG , MVT::Other, Custom);
423 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000424 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000425 setOperationAction(ISD::VAARG , MVT::Other, Expand);
426 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000427 }
Evan Chengae642192007-03-02 23:16:35 +0000428
Owen Anderson825b72b2009-08-11 20:47:22 +0000429 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
430 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000431 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000432 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Michael J. Spencere9c253e2010-10-21 01:41:01 +0000433 if (Subtarget->isTargetCygMing() || Subtarget->isTargetWindows())
Owen Anderson825b72b2009-08-11 20:47:22 +0000434 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +0000435 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000436 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000437
Evan Chengc7ce29b2009-02-13 22:36:38 +0000438 if (!UseSoftFloat && X86ScalarSSEf64) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000439 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000440 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000441 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
442 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000443
Evan Cheng223547a2006-01-31 22:28:30 +0000444 // Use ANDPD to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000445 setOperationAction(ISD::FABS , MVT::f64, Custom);
446 setOperationAction(ISD::FABS , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000447
448 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000449 setOperationAction(ISD::FNEG , MVT::f64, Custom);
450 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000451
Evan Cheng68c47cb2007-01-05 07:55:56 +0000452 // Use ANDPD and ORPD to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000453 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
454 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000455
Evan Chengd25e9e82006-02-02 00:28:23 +0000456 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000457 setOperationAction(ISD::FSIN , MVT::f64, Expand);
458 setOperationAction(ISD::FCOS , MVT::f64, Expand);
459 setOperationAction(ISD::FSIN , MVT::f32, Expand);
460 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000461
Chris Lattnera54aa942006-01-29 06:26:08 +0000462 // Expand FP immediates into loads from the stack, except for the special
463 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000464 addLegalFPImmediate(APFloat(+0.0)); // xorpd
465 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Evan Chengc7ce29b2009-02-13 22:36:38 +0000466 } else if (!UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000467 // Use SSE for f32, x87 for f64.
468 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000469 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
470 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000471
472 // Use ANDPS to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000473 setOperationAction(ISD::FABS , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000474
475 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000476 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000477
Owen Anderson825b72b2009-08-11 20:47:22 +0000478 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000479
480 // Use ANDPS and ORPS to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000481 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
482 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000483
484 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000485 setOperationAction(ISD::FSIN , MVT::f32, Expand);
486 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000487
Nate Begemane1795842008-02-14 08:57:00 +0000488 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000489 addLegalFPImmediate(APFloat(+0.0f)); // xorps
490 addLegalFPImmediate(APFloat(+0.0)); // FLD0
491 addLegalFPImmediate(APFloat(+1.0)); // FLD1
492 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
493 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
494
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000495 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000496 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
497 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000498 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000499 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000500 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000501 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000502 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
503 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000504
Owen Anderson825b72b2009-08-11 20:47:22 +0000505 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
506 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
507 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
508 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000509
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000510 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000511 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
512 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000513 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000514 addLegalFPImmediate(APFloat(+0.0)); // FLD0
515 addLegalFPImmediate(APFloat(+1.0)); // FLD1
516 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
517 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000518 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
519 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
520 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
521 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000522 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000523
Dale Johannesen59a58732007-08-05 18:49:15 +0000524 // Long double always uses X87.
Evan Cheng92722532009-03-26 23:06:32 +0000525 if (!UseSoftFloat) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000526 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
527 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
528 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000529 {
Benjamin Kramer98383962010-12-04 14:22:24 +0000530 APFloat TmpFlt = APFloat::getZero(APFloat::x87DoubleExtended);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000531 addLegalFPImmediate(TmpFlt); // FLD0
532 TmpFlt.changeSign();
533 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
Benjamin Kramer98383962010-12-04 14:22:24 +0000534
535 bool ignored;
Evan Chengc7ce29b2009-02-13 22:36:38 +0000536 APFloat TmpFlt2(+1.0);
537 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
538 &ignored);
539 addLegalFPImmediate(TmpFlt2); // FLD1
540 TmpFlt2.changeSign();
541 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
542 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000543
Evan Chengc7ce29b2009-02-13 22:36:38 +0000544 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000545 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
546 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000547 }
Dale Johannesen2f429012007-09-26 21:10:55 +0000548 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000549
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000550 // Always use a library call for pow.
Owen Anderson825b72b2009-08-11 20:47:22 +0000551 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
552 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
553 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000554
Owen Anderson825b72b2009-08-11 20:47:22 +0000555 setOperationAction(ISD::FLOG, MVT::f80, Expand);
556 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
557 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
558 setOperationAction(ISD::FEXP, MVT::f80, Expand);
559 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000560
Mon P Wangf007a8b2008-11-06 05:31:54 +0000561 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000562 // (for widening) or expand (for scalarization). Then we will selectively
563 // turn on ones that can be effectively codegen'd.
Owen Anderson825b72b2009-08-11 20:47:22 +0000564 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
565 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
566 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
567 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
568 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
569 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
570 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
571 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
572 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
573 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
574 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
575 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
576 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
577 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
578 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
579 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
580 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
581 setOperationAction(ISD::EXTRACT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
582 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
583 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
584 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
585 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
586 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
587 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
588 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
589 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
590 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
591 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
592 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
593 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
594 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
595 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
596 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
597 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
598 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
599 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
600 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
601 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
602 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
603 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
604 setOperationAction(ISD::VSETCC, (MVT::SimpleValueType)VT, Expand);
605 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
606 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
607 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
608 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
609 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
610 setOperationAction(ISD::FP_TO_UINT, (MVT::SimpleValueType)VT, Expand);
611 setOperationAction(ISD::FP_TO_SINT, (MVT::SimpleValueType)VT, Expand);
612 setOperationAction(ISD::UINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
613 setOperationAction(ISD::SINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
Dan Gohman87862e72009-12-11 21:31:27 +0000614 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,Expand);
Dan Gohman2e141d72009-12-14 23:40:38 +0000615 setOperationAction(ISD::TRUNCATE, (MVT::SimpleValueType)VT, Expand);
616 setOperationAction(ISD::SIGN_EXTEND, (MVT::SimpleValueType)VT, Expand);
617 setOperationAction(ISD::ZERO_EXTEND, (MVT::SimpleValueType)VT, Expand);
618 setOperationAction(ISD::ANY_EXTEND, (MVT::SimpleValueType)VT, Expand);
619 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
620 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
621 setTruncStoreAction((MVT::SimpleValueType)VT,
622 (MVT::SimpleValueType)InnerVT, Expand);
623 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
624 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
625 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000626 }
627
Evan Chengc7ce29b2009-02-13 22:36:38 +0000628 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
629 // with -msoft-float, disable use of MMX as well.
Evan Cheng92722532009-03-26 23:06:32 +0000630 if (!UseSoftFloat && !DisableMMX && Subtarget->hasMMX()) {
Dale Johannesene93d99c2010-10-20 21:32:10 +0000631 addRegisterClass(MVT::x86mmx, X86::VR64RegisterClass);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000632 // No operations on x86mmx supported, everything uses intrinsics.
Evan Cheng470a6ad2006-02-22 02:26:30 +0000633 }
634
Dale Johannesen0488fb62010-09-30 23:57:10 +0000635 // MMX-sized vectors (other than x86mmx) are expected to be expanded
636 // into smaller operations.
637 setOperationAction(ISD::MULHS, MVT::v8i8, Expand);
638 setOperationAction(ISD::MULHS, MVT::v4i16, Expand);
639 setOperationAction(ISD::MULHS, MVT::v2i32, Expand);
640 setOperationAction(ISD::MULHS, MVT::v1i64, Expand);
641 setOperationAction(ISD::AND, MVT::v8i8, Expand);
642 setOperationAction(ISD::AND, MVT::v4i16, Expand);
643 setOperationAction(ISD::AND, MVT::v2i32, Expand);
644 setOperationAction(ISD::AND, MVT::v1i64, Expand);
645 setOperationAction(ISD::OR, MVT::v8i8, Expand);
646 setOperationAction(ISD::OR, MVT::v4i16, Expand);
647 setOperationAction(ISD::OR, MVT::v2i32, Expand);
648 setOperationAction(ISD::OR, MVT::v1i64, Expand);
649 setOperationAction(ISD::XOR, MVT::v8i8, Expand);
650 setOperationAction(ISD::XOR, MVT::v4i16, Expand);
651 setOperationAction(ISD::XOR, MVT::v2i32, Expand);
652 setOperationAction(ISD::XOR, MVT::v1i64, Expand);
653 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Expand);
654 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Expand);
655 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Expand);
656 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Expand);
657 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v1i64, Expand);
658 setOperationAction(ISD::SELECT, MVT::v8i8, Expand);
659 setOperationAction(ISD::SELECT, MVT::v4i16, Expand);
660 setOperationAction(ISD::SELECT, MVT::v2i32, Expand);
661 setOperationAction(ISD::SELECT, MVT::v1i64, Expand);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000662 setOperationAction(ISD::BITCAST, MVT::v8i8, Expand);
663 setOperationAction(ISD::BITCAST, MVT::v4i16, Expand);
664 setOperationAction(ISD::BITCAST, MVT::v2i32, Expand);
665 setOperationAction(ISD::BITCAST, MVT::v1i64, Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000666
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000667 if (!UseSoftFloat && Subtarget->hasXMM()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000668 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000669
Owen Anderson825b72b2009-08-11 20:47:22 +0000670 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
671 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
672 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
673 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
674 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
675 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
676 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
677 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
678 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
679 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
680 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
681 setOperationAction(ISD::VSETCC, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000682 }
683
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000684 if (!UseSoftFloat && Subtarget->hasXMMInt()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000685 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000686
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000687 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
688 // registers cannot be used even for integer operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000689 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
690 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
691 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
692 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000693
Owen Anderson825b72b2009-08-11 20:47:22 +0000694 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
695 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
696 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
697 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
698 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
699 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
700 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
701 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
702 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
703 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
704 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
705 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
706 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
707 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
708 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
709 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000710
Owen Anderson825b72b2009-08-11 20:47:22 +0000711 setOperationAction(ISD::VSETCC, MVT::v2f64, Custom);
712 setOperationAction(ISD::VSETCC, MVT::v16i8, Custom);
713 setOperationAction(ISD::VSETCC, MVT::v8i16, Custom);
714 setOperationAction(ISD::VSETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000715
Owen Anderson825b72b2009-08-11 20:47:22 +0000716 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
717 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
718 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
719 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
720 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000721
Mon P Wangeb38ebf2010-01-24 00:05:03 +0000722 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2f64, Custom);
723 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2i64, Custom);
724 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i8, Custom);
725 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i16, Custom);
726 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i32, Custom);
727
Evan Cheng2c3ae372006-04-12 21:21:57 +0000728 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Owen Anderson825b72b2009-08-11 20:47:22 +0000729 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
730 EVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000731 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000732 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000733 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000734 // Do not attempt to custom lower non-128-bit vectors
735 if (!VT.is128BitVector())
736 continue;
Owen Anderson825b72b2009-08-11 20:47:22 +0000737 setOperationAction(ISD::BUILD_VECTOR,
738 VT.getSimpleVT().SimpleTy, Custom);
739 setOperationAction(ISD::VECTOR_SHUFFLE,
740 VT.getSimpleVT().SimpleTy, Custom);
741 setOperationAction(ISD::EXTRACT_VECTOR_ELT,
742 VT.getSimpleVT().SimpleTy, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000743 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000744
Owen Anderson825b72b2009-08-11 20:47:22 +0000745 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
746 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
747 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
748 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
749 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
750 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000751
Nate Begemancdd1eec2008-02-12 22:51:28 +0000752 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000753 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
754 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000755 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000756
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000757 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Owen Anderson825b72b2009-08-11 20:47:22 +0000758 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; i++) {
759 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
Owen Andersone50ed302009-08-10 22:56:29 +0000760 EVT VT = SVT;
David Greene9b9838d2009-06-29 16:47:10 +0000761
762 // Do not attempt to promote non-128-bit vectors
Chris Lattner32b4b5a2010-07-05 05:53:14 +0000763 if (!VT.is128BitVector())
David Greene9b9838d2009-06-29 16:47:10 +0000764 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +0000765
Owen Andersond6662ad2009-08-10 20:46:15 +0000766 setOperationAction(ISD::AND, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000767 AddPromotedToType (ISD::AND, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000768 setOperationAction(ISD::OR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000769 AddPromotedToType (ISD::OR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000770 setOperationAction(ISD::XOR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000771 AddPromotedToType (ISD::XOR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000772 setOperationAction(ISD::LOAD, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000773 AddPromotedToType (ISD::LOAD, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000774 setOperationAction(ISD::SELECT, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000775 AddPromotedToType (ISD::SELECT, SVT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000776 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000777
Owen Anderson825b72b2009-08-11 20:47:22 +0000778 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000779
Evan Cheng2c3ae372006-04-12 21:21:57 +0000780 // Custom lower v2i64 and v2f64 selects.
Owen Anderson825b72b2009-08-11 20:47:22 +0000781 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
782 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
783 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
784 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000785
Owen Anderson825b72b2009-08-11 20:47:22 +0000786 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
787 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000788 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000789
Nate Begeman14d12ca2008-02-11 04:19:36 +0000790 if (Subtarget->hasSSE41()) {
Dale Johannesen54feef22010-05-27 20:12:41 +0000791 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
792 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
793 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
794 setOperationAction(ISD::FRINT, MVT::f32, Legal);
795 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
796 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
797 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
798 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
799 setOperationAction(ISD::FRINT, MVT::f64, Legal);
800 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
801
Nate Begeman14d12ca2008-02-11 04:19:36 +0000802 // FIXME: Do we need to handle scalar-to-vector here?
Owen Anderson825b72b2009-08-11 20:47:22 +0000803 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000804
Nate Begemanbdcb5af2010-07-27 22:37:06 +0000805 // Can turn SHL into an integer multiply.
806 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
Nate Begeman51409212010-07-28 00:21:48 +0000807 setOperationAction(ISD::SHL, MVT::v16i8, Custom);
Nate Begemanbdcb5af2010-07-27 22:37:06 +0000808
Nate Begeman14d12ca2008-02-11 04:19:36 +0000809 // i8 and i16 vectors are custom , because the source register and source
810 // source memory operand types are not the same width. f32 vectors are
811 // custom since the immediate controlling the insert encodes additional
812 // information.
Owen Anderson825b72b2009-08-11 20:47:22 +0000813 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
814 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
815 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
816 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000817
Owen Anderson825b72b2009-08-11 20:47:22 +0000818 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
819 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
820 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
821 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000822
823 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000824 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
825 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000826 }
827 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000828
Nate Begeman30a0de92008-07-17 16:51:19 +0000829 if (Subtarget->hasSSE42()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000830 setOperationAction(ISD::VSETCC, MVT::v2i64, Custom);
Nate Begeman30a0de92008-07-17 16:51:19 +0000831 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000832
David Greene9b9838d2009-06-29 16:47:10 +0000833 if (!UseSoftFloat && Subtarget->hasAVX()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000834 addRegisterClass(MVT::v8f32, X86::VR256RegisterClass);
835 addRegisterClass(MVT::v4f64, X86::VR256RegisterClass);
836 addRegisterClass(MVT::v8i32, X86::VR256RegisterClass);
837 addRegisterClass(MVT::v4i64, X86::VR256RegisterClass);
Bruno Cardoso Lopes405f11b2010-08-10 01:43:16 +0000838 addRegisterClass(MVT::v32i8, X86::VR256RegisterClass);
David Greened94c1012009-06-29 22:50:51 +0000839
Owen Anderson825b72b2009-08-11 20:47:22 +0000840 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
841 setOperationAction(ISD::LOAD, MVT::v8i32, Legal);
842 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
843 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
844 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
845 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
846 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
847 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
848 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
849 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +0000850 setOperationAction(ISD::BUILD_VECTOR, MVT::v8f32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000851 //setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8f32, Custom);
852 //setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8f32, Custom);
853 //setOperationAction(ISD::SELECT, MVT::v8f32, Custom);
854 //setOperationAction(ISD::VSETCC, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000855
856 // Operations to consider commented out -v16i16 v32i8
Owen Anderson825b72b2009-08-11 20:47:22 +0000857 //setOperationAction(ISD::ADD, MVT::v16i16, Legal);
858 setOperationAction(ISD::ADD, MVT::v8i32, Custom);
859 setOperationAction(ISD::ADD, MVT::v4i64, Custom);
860 //setOperationAction(ISD::SUB, MVT::v32i8, Legal);
861 //setOperationAction(ISD::SUB, MVT::v16i16, Legal);
862 setOperationAction(ISD::SUB, MVT::v8i32, Custom);
863 setOperationAction(ISD::SUB, MVT::v4i64, Custom);
864 //setOperationAction(ISD::MUL, MVT::v16i16, Legal);
865 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
866 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
867 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
868 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
869 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
870 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000871
Owen Anderson825b72b2009-08-11 20:47:22 +0000872 setOperationAction(ISD::VSETCC, MVT::v4f64, Custom);
873 // setOperationAction(ISD::VSETCC, MVT::v32i8, Custom);
874 // setOperationAction(ISD::VSETCC, MVT::v16i16, Custom);
875 setOperationAction(ISD::VSETCC, MVT::v8i32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000876
Owen Anderson825b72b2009-08-11 20:47:22 +0000877 // setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v32i8, Custom);
878 // setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i16, Custom);
879 // setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i16, Custom);
880 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i32, Custom);
881 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000882
Owen Anderson825b72b2009-08-11 20:47:22 +0000883 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f64, Custom);
884 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i64, Custom);
885 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f64, Custom);
886 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i64, Custom);
887 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f64, Custom);
888 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000889
890#if 0
891 // Not sure we want to do this since there are no 256-bit integer
892 // operations in AVX
893
894 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
895 // This includes 256-bit vectors
Owen Anderson825b72b2009-08-11 20:47:22 +0000896 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v4i64; ++i) {
897 EVT VT = (MVT::SimpleValueType)i;
David Greene9b9838d2009-06-29 16:47:10 +0000898
899 // Do not attempt to custom lower non-power-of-2 vectors
900 if (!isPowerOf2_32(VT.getVectorNumElements()))
901 continue;
902
903 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
904 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
905 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
906 }
907
908 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000909 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i64, Custom);
910 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i64, Custom);
Eric Christopherfd179292009-08-27 18:07:15 +0000911 }
David Greene9b9838d2009-06-29 16:47:10 +0000912#endif
913
914#if 0
915 // Not sure we want to do this since there are no 256-bit integer
916 // operations in AVX
917
918 // Promote v32i8, v16i16, v8i32 load, select, and, or, xor to v4i64.
919 // Including 256-bit vectors
Owen Anderson825b72b2009-08-11 20:47:22 +0000920 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v4i64; i++) {
921 EVT VT = (MVT::SimpleValueType)i;
David Greene9b9838d2009-06-29 16:47:10 +0000922
923 if (!VT.is256BitVector()) {
924 continue;
925 }
926 setOperationAction(ISD::AND, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000927 AddPromotedToType (ISD::AND, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000928 setOperationAction(ISD::OR, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000929 AddPromotedToType (ISD::OR, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000930 setOperationAction(ISD::XOR, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000931 AddPromotedToType (ISD::XOR, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000932 setOperationAction(ISD::LOAD, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000933 AddPromotedToType (ISD::LOAD, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000934 setOperationAction(ISD::SELECT, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000935 AddPromotedToType (ISD::SELECT, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000936 }
937
Owen Anderson825b72b2009-08-11 20:47:22 +0000938 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
David Greene9b9838d2009-06-29 16:47:10 +0000939#endif
940 }
941
Evan Cheng6be2c582006-04-05 23:38:46 +0000942 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000943 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +0000944
Bill Wendling74c37652008-12-09 22:08:41 +0000945 // Add/Sub/Mul with overflow operations are custom lowered.
Owen Anderson825b72b2009-08-11 20:47:22 +0000946 setOperationAction(ISD::SADDO, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000947 setOperationAction(ISD::UADDO, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000948 setOperationAction(ISD::SSUBO, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000949 setOperationAction(ISD::USUBO, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000950 setOperationAction(ISD::SMULO, MVT::i32, Custom);
Chris Lattnerb20e0b12010-12-05 07:30:36 +0000951 setOperationAction(ISD::UMULO, MVT::i32, Custom);
Dan Gohman71c62a22010-06-02 19:13:40 +0000952
Eli Friedman962f5492010-06-02 19:35:46 +0000953 // Only custom-lower 64-bit SADDO and friends on 64-bit because we don't
954 // handle type legalization for these operations here.
Dan Gohman71c62a22010-06-02 19:13:40 +0000955 //
Eli Friedman962f5492010-06-02 19:35:46 +0000956 // FIXME: We really should do custom legalization for addition and
957 // subtraction on x86-32 once PR3203 is fixed. We really can't do much better
958 // than generic legalization for 64-bit multiplication-with-overflow, though.
Eli Friedmana993f0a2010-06-02 00:27:18 +0000959 if (Subtarget->is64Bit()) {
960 setOperationAction(ISD::SADDO, MVT::i64, Custom);
961 setOperationAction(ISD::UADDO, MVT::i64, Custom);
962 setOperationAction(ISD::SSUBO, MVT::i64, Custom);
963 setOperationAction(ISD::USUBO, MVT::i64, Custom);
964 setOperationAction(ISD::SMULO, MVT::i64, Custom);
Chris Lattnerb20e0b12010-12-05 07:30:36 +0000965 setOperationAction(ISD::UMULO, MVT::i64, Custom);
Eli Friedmana993f0a2010-06-02 00:27:18 +0000966 }
Bill Wendling41ea7e72008-11-24 19:21:46 +0000967
Evan Chengd54f2d52009-03-31 19:38:51 +0000968 if (!Subtarget->is64Bit()) {
969 // These libcalls are not available in 32-bit.
970 setLibcallName(RTLIB::SHL_I128, 0);
971 setLibcallName(RTLIB::SRL_I128, 0);
972 setLibcallName(RTLIB::SRA_I128, 0);
973 }
974
Evan Cheng206ee9d2006-07-07 08:33:52 +0000975 // We have target-specific dag combine patterns for the following nodes:
976 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Dan Gohman1bbf72b2010-03-15 23:23:03 +0000977 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Evan Chengd880b972008-05-09 21:53:03 +0000978 setTargetDAGCombine(ISD::BUILD_VECTOR);
Chris Lattner83e6c992006-10-04 06:57:07 +0000979 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +0000980 setTargetDAGCombine(ISD::SHL);
981 setTargetDAGCombine(ISD::SRA);
982 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +0000983 setTargetDAGCombine(ISD::OR);
Nate Begemanb65c1752010-12-17 22:55:37 +0000984 setTargetDAGCombine(ISD::AND);
Chris Lattner149a4e52008-02-22 02:09:43 +0000985 setTargetDAGCombine(ISD::STORE);
Evan Cheng2e489c42009-12-16 00:53:11 +0000986 setTargetDAGCombine(ISD::ZERO_EXTEND);
Evan Cheng0b0cd912009-03-28 05:57:29 +0000987 if (Subtarget->is64Bit())
988 setTargetDAGCombine(ISD::MUL);
Evan Cheng206ee9d2006-07-07 08:33:52 +0000989
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000990 computeRegisterProperties();
991
Evan Cheng87ed7162006-02-14 08:25:08 +0000992 // FIXME: These should be based on subtarget info. Plus, the values should
993 // be smaller when we are in optimizing for size mode.
Dan Gohman87060f52008-06-30 21:00:56 +0000994 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
Evan Cheng255f20f2010-04-01 06:04:33 +0000995 maxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
Dan Gohman87060f52008-06-30 21:00:56 +0000996 maxStoresPerMemmove = 3; // For @llvm.memmove -> sequence of stores
Evan Chengfb8075d2008-02-28 00:43:03 +0000997 setPrefLoopAlignment(16);
Evan Cheng6ebf7bc2009-05-13 21:42:09 +0000998 benefitFromCodePlacementOpt = true;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000999}
1000
Scott Michel5b8f82e2008-03-10 15:42:14 +00001001
Owen Anderson825b72b2009-08-11 20:47:22 +00001002MVT::SimpleValueType X86TargetLowering::getSetCCResultType(EVT VT) const {
1003 return MVT::i8;
Scott Michel5b8f82e2008-03-10 15:42:14 +00001004}
1005
1006
Evan Cheng29286502008-01-23 23:17:41 +00001007/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1008/// the desired ByVal argument alignment.
1009static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
1010 if (MaxAlign == 16)
1011 return;
1012 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
1013 if (VTy->getBitWidth() == 128)
1014 MaxAlign = 16;
Evan Cheng29286502008-01-23 23:17:41 +00001015 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
1016 unsigned EltAlign = 0;
1017 getMaxByValAlign(ATy->getElementType(), EltAlign);
1018 if (EltAlign > MaxAlign)
1019 MaxAlign = EltAlign;
1020 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
1021 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1022 unsigned EltAlign = 0;
1023 getMaxByValAlign(STy->getElementType(i), EltAlign);
1024 if (EltAlign > MaxAlign)
1025 MaxAlign = EltAlign;
1026 if (MaxAlign == 16)
1027 break;
1028 }
1029 }
1030 return;
1031}
1032
1033/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1034/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001035/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1036/// are at 4-byte boundaries.
Evan Cheng29286502008-01-23 23:17:41 +00001037unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001038 if (Subtarget->is64Bit()) {
1039 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001040 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001041 if (TyAlign > 8)
1042 return TyAlign;
1043 return 8;
1044 }
1045
Evan Cheng29286502008-01-23 23:17:41 +00001046 unsigned Align = 4;
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001047 if (Subtarget->hasXMM())
Dale Johannesen0c191872008-02-08 19:48:20 +00001048 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001049 return Align;
1050}
Chris Lattner2b02a442007-02-25 08:29:00 +00001051
Evan Chengf0df0312008-05-15 08:39:06 +00001052/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengc3b0c342010-04-08 07:37:57 +00001053/// and store operations as a result of memset, memcpy, and memmove
1054/// lowering. If DstAlign is zero that means it's safe to destination
1055/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1056/// means there isn't a need to check it against alignment requirement,
1057/// probably because the source does not need to be loaded. If
1058/// 'NonScalarIntSafe' is true, that means it's safe to return a
1059/// non-scalar-integer type, e.g. empty string source, constant, or loaded
1060/// from memory. 'MemcpyStrSrc' indicates whether the memcpy source is
1061/// constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001062/// It returns EVT::Other if the type should be determined using generic
1063/// target-independent logic.
Owen Andersone50ed302009-08-10 22:56:29 +00001064EVT
Evan Cheng255f20f2010-04-01 06:04:33 +00001065X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1066 unsigned DstAlign, unsigned SrcAlign,
Evan Chengf28f8bc2010-04-02 19:36:14 +00001067 bool NonScalarIntSafe,
Evan Chengc3b0c342010-04-08 07:37:57 +00001068 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00001069 MachineFunction &MF) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001070 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
1071 // linux. This is because the stack realignment code can't handle certain
1072 // cases like PR2962. This should be removed when PR2962 is fixed.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001073 const Function *F = MF.getFunction();
Evan Chengf28f8bc2010-04-02 19:36:14 +00001074 if (NonScalarIntSafe &&
1075 !F->hasFnAttr(Attribute::NoImplicitFloat)) {
Evan Cheng255f20f2010-04-01 06:04:33 +00001076 if (Size >= 16 &&
1077 (Subtarget->isUnalignedMemAccessFast() ||
Chandler Carruthae1d41c2010-04-02 01:31:24 +00001078 ((DstAlign == 0 || DstAlign >= 16) &&
1079 (SrcAlign == 0 || SrcAlign >= 16))) &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001080 Subtarget->getStackAlignment() >= 16) {
1081 if (Subtarget->hasSSE2())
1082 return MVT::v4i32;
Evan Chengf28f8bc2010-04-02 19:36:14 +00001083 if (Subtarget->hasSSE1())
Evan Cheng255f20f2010-04-01 06:04:33 +00001084 return MVT::v4f32;
Evan Chengc3b0c342010-04-08 07:37:57 +00001085 } else if (!MemcpyStrSrc && Size >= 8 &&
Evan Cheng3ea97552010-04-01 20:27:45 +00001086 !Subtarget->is64Bit() &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001087 Subtarget->getStackAlignment() >= 8 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001088 Subtarget->hasXMMInt()) {
Evan Chengc3b0c342010-04-08 07:37:57 +00001089 // Do not use f64 to lower memcpy if source is string constant. It's
1090 // better to use i32 to avoid the loads.
Evan Cheng255f20f2010-04-01 06:04:33 +00001091 return MVT::f64;
Evan Chengc3b0c342010-04-08 07:37:57 +00001092 }
Chris Lattner4002a1b2008-10-28 05:49:35 +00001093 }
Evan Chengf0df0312008-05-15 08:39:06 +00001094 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001095 return MVT::i64;
1096 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001097}
1098
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001099/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1100/// current function. The returned value is a member of the
1101/// MachineJumpTableInfo::JTEntryKind enum.
1102unsigned X86TargetLowering::getJumpTableEncoding() const {
1103 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1104 // symbol.
1105 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1106 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001107 return MachineJumpTableInfo::EK_Custom32;
Michael J. Spencerec38de22010-10-10 22:04:20 +00001108
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001109 // Otherwise, use the normal jump table encoding heuristics.
1110 return TargetLowering::getJumpTableEncoding();
1111}
1112
Chris Lattnerc64daab2010-01-26 05:02:42 +00001113const MCExpr *
1114X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1115 const MachineBasicBlock *MBB,
1116 unsigned uid,MCContext &Ctx) const{
1117 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1118 Subtarget->isPICStyleGOT());
1119 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1120 // entries.
Daniel Dunbar4e815f82010-03-15 23:51:06 +00001121 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1122 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001123}
1124
Evan Chengcc415862007-11-09 01:32:10 +00001125/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1126/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001127SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001128 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001129 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001130 // This doesn't have DebugLoc associated with it, but is not really the
1131 // same as a Register.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001132 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001133 return Table;
1134}
1135
Chris Lattner589c6f62010-01-26 06:28:43 +00001136/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1137/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1138/// MCExpr.
1139const MCExpr *X86TargetLowering::
1140getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1141 MCContext &Ctx) const {
1142 // X86-64 uses RIP relative addressing based on the jump table label.
1143 if (Subtarget->isPICStyleRIPRel())
1144 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1145
1146 // Otherwise, the reference is relative to the PIC base.
Chris Lattner142b5312010-11-14 22:48:15 +00001147 return MCSymbolRefExpr::Create(MF->getPICBaseSymbol(), Ctx);
Chris Lattner589c6f62010-01-26 06:28:43 +00001148}
1149
Bill Wendlingb4202b82009-07-01 18:50:55 +00001150/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +00001151unsigned X86TargetLowering::getFunctionAlignment(const Function *F) const {
Dan Gohman25103a22009-08-18 00:20:06 +00001152 return F->hasFnAttr(Attribute::OptimizeForSize) ? 0 : 4;
Bill Wendling20c568f2009-06-30 22:38:32 +00001153}
1154
Evan Chengdee81012010-07-26 21:50:05 +00001155std::pair<const TargetRegisterClass*, uint8_t>
1156X86TargetLowering::findRepresentativeClass(EVT VT) const{
1157 const TargetRegisterClass *RRC = 0;
1158 uint8_t Cost = 1;
1159 switch (VT.getSimpleVT().SimpleTy) {
1160 default:
1161 return TargetLowering::findRepresentativeClass(VT);
1162 case MVT::i8: case MVT::i16: case MVT::i32: case MVT::i64:
1163 RRC = (Subtarget->is64Bit()
1164 ? X86::GR64RegisterClass : X86::GR32RegisterClass);
1165 break;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001166 case MVT::x86mmx:
Evan Chengdee81012010-07-26 21:50:05 +00001167 RRC = X86::VR64RegisterClass;
1168 break;
1169 case MVT::f32: case MVT::f64:
1170 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1171 case MVT::v4f32: case MVT::v2f64:
1172 case MVT::v32i8: case MVT::v8i32: case MVT::v4i64: case MVT::v8f32:
1173 case MVT::v4f64:
1174 RRC = X86::VR128RegisterClass;
1175 break;
1176 }
1177 return std::make_pair(RRC, Cost);
1178}
1179
Evan Cheng70017e42010-07-24 00:39:05 +00001180unsigned
1181X86TargetLowering::getRegPressureLimit(const TargetRegisterClass *RC,
1182 MachineFunction &MF) const {
Anton Korobeynikovd0c38172010-11-18 21:19:35 +00001183 const TargetFrameInfo *TFI = MF.getTarget().getFrameInfo();
1184
1185 unsigned FPDiff = TFI->hasFP(MF) ? 1 : 0;
Evan Cheng70017e42010-07-24 00:39:05 +00001186 switch (RC->getID()) {
1187 default:
1188 return 0;
1189 case X86::GR32RegClassID:
1190 return 4 - FPDiff;
1191 case X86::GR64RegClassID:
1192 return 8 - FPDiff;
1193 case X86::VR128RegClassID:
1194 return Subtarget->is64Bit() ? 10 : 4;
1195 case X86::VR64RegClassID:
1196 return 4;
1197 }
1198}
1199
Eric Christopherf7a0c7b2010-07-06 05:18:56 +00001200bool X86TargetLowering::getStackCookieLocation(unsigned &AddressSpace,
1201 unsigned &Offset) const {
1202 if (!Subtarget->isTargetLinux())
1203 return false;
1204
1205 if (Subtarget->is64Bit()) {
1206 // %fs:0x28, unless we're using a Kernel code model, in which case it's %gs:
1207 Offset = 0x28;
1208 if (getTargetMachine().getCodeModel() == CodeModel::Kernel)
1209 AddressSpace = 256;
1210 else
1211 AddressSpace = 257;
1212 } else {
1213 // %gs:0x14 on i386
1214 Offset = 0x14;
1215 AddressSpace = 256;
1216 }
1217 return true;
1218}
1219
1220
Chris Lattner2b02a442007-02-25 08:29:00 +00001221//===----------------------------------------------------------------------===//
1222// Return Value Calling Convention Implementation
1223//===----------------------------------------------------------------------===//
1224
Chris Lattner59ed56b2007-02-28 04:55:35 +00001225#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001226
Michael J. Spencerec38de22010-10-10 22:04:20 +00001227bool
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001228X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv, bool isVarArg,
Dan Gohman84023e02010-07-10 09:00:22 +00001229 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001230 LLVMContext &Context) const {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001231 SmallVector<CCValAssign, 16> RVLocs;
1232 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001233 RVLocs, Context);
Dan Gohman84023e02010-07-10 09:00:22 +00001234 return CCInfo.CheckReturn(Outs, RetCC_X86);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001235}
1236
Dan Gohman98ca4f22009-08-05 01:29:28 +00001237SDValue
1238X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001239 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001240 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001241 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001242 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001243 MachineFunction &MF = DAG.getMachineFunction();
1244 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001245
Chris Lattner9774c912007-02-27 05:28:59 +00001246 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001247 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1248 RVLocs, *DAG.getContext());
1249 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001250
Evan Chengdcea1632010-02-04 02:40:39 +00001251 // Add the regs to the liveout set for the function.
1252 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1253 for (unsigned i = 0; i != RVLocs.size(); ++i)
1254 if (RVLocs[i].isRegLoc() && !MRI.isLiveOut(RVLocs[i].getLocReg()))
1255 MRI.addLiveOut(RVLocs[i].getLocReg());
Scott Michelfdc40a02009-02-17 22:15:04 +00001256
Dan Gohman475871a2008-07-27 21:46:04 +00001257 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001258
Dan Gohman475871a2008-07-27 21:46:04 +00001259 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001260 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1261 // Operand #1 = Bytes To Pop
Dan Gohman1e93df62010-04-17 14:41:14 +00001262 RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1263 MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001264
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001265 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001266 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1267 CCValAssign &VA = RVLocs[i];
1268 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohmanc9403652010-07-07 15:54:55 +00001269 SDValue ValToCopy = OutVals[i];
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001270 EVT ValVT = ValToCopy.getValueType();
1271
Dale Johannesenc4510512010-09-24 19:05:48 +00001272 // If this is x86-64, and we disabled SSE, we can't return FP values,
1273 // or SSE or MMX vectors.
1274 if ((ValVT == MVT::f32 || ValVT == MVT::f64 ||
1275 VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001276 (Subtarget->is64Bit() && !Subtarget->hasXMM())) {
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001277 report_fatal_error("SSE register return with SSE disabled");
1278 }
1279 // Likewise we can't return F64 values with SSE1 only. gcc does so, but
1280 // llvm-gcc has never done it right and no one has noticed, so this
1281 // should be OK for now.
1282 if (ValVT == MVT::f64 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001283 (Subtarget->is64Bit() && !Subtarget->hasXMMInt()))
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001284 report_fatal_error("SSE2 register return with SSE2 disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001285
Chris Lattner447ff682008-03-11 03:23:40 +00001286 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1287 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001288 if (VA.getLocReg() == X86::ST0 ||
1289 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001290 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1291 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001292 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001293 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001294 RetOps.push_back(ValToCopy);
1295 // Don't emit a copytoreg.
1296 continue;
1297 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001298
Evan Cheng242b38b2009-02-23 09:03:22 +00001299 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1300 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001301 if (Subtarget->is64Bit()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001302 if (ValVT == MVT::x86mmx) {
Chris Lattner97a2a562010-08-26 05:24:29 +00001303 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001304 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ValToCopy);
Eric Christopher90eb4022010-07-22 00:26:08 +00001305 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
1306 ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001307 // If we don't have SSE2 available, convert to v4f32 so the generated
1308 // register is legal.
1309 if (!Subtarget->hasSSE2())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001310 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32,ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001311 }
Evan Cheng242b38b2009-02-23 09:03:22 +00001312 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001313 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00001314
Dale Johannesendd64c412009-02-04 00:33:20 +00001315 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001316 Flag = Chain.getValue(1);
1317 }
Dan Gohman61a92132008-04-21 23:59:07 +00001318
1319 // The x86-64 ABI for returning structs by value requires that we copy
1320 // the sret argument into %rax for the return. We saved the argument into
1321 // a virtual register in the entry block, so now we copy the value out
1322 // and into %rax.
1323 if (Subtarget->is64Bit() &&
1324 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1325 MachineFunction &MF = DAG.getMachineFunction();
1326 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1327 unsigned Reg = FuncInfo->getSRetReturnReg();
Michael J. Spencerec38de22010-10-10 22:04:20 +00001328 assert(Reg &&
Zhongxing Xuc2798a12010-05-26 08:10:02 +00001329 "SRetReturnReg should have been set in LowerFormalArguments().");
Dale Johannesendd64c412009-02-04 00:33:20 +00001330 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001331
Dale Johannesendd64c412009-02-04 00:33:20 +00001332 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001333 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001334
1335 // RAX now acts like a return value.
Evan Chengdcea1632010-02-04 02:40:39 +00001336 MRI.addLiveOut(X86::RAX);
Dan Gohman61a92132008-04-21 23:59:07 +00001337 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001338
Chris Lattner447ff682008-03-11 03:23:40 +00001339 RetOps[0] = Chain; // Update chain.
1340
1341 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001342 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001343 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001344
1345 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001346 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001347}
1348
Evan Cheng3d2125c2010-11-30 23:55:39 +00001349bool X86TargetLowering::isUsedByReturnOnly(SDNode *N) const {
1350 if (N->getNumValues() != 1)
1351 return false;
1352 if (!N->hasNUsesOfValue(1, 0))
1353 return false;
1354
1355 SDNode *Copy = *N->use_begin();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001356 if (Copy->getOpcode() != ISD::CopyToReg &&
1357 Copy->getOpcode() != ISD::FP_EXTEND)
Evan Cheng3d2125c2010-11-30 23:55:39 +00001358 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001359
1360 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001361 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001362 UI != UE; ++UI) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001363 if (UI->getOpcode() != X86ISD::RET_FLAG)
1364 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001365 HasRet = true;
1366 }
Evan Cheng3d2125c2010-11-30 23:55:39 +00001367
Evan Cheng1bf891a2010-12-01 22:59:46 +00001368 return HasRet;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001369}
1370
Dan Gohman98ca4f22009-08-05 01:29:28 +00001371/// LowerCallResult - Lower the result values of a call into the
1372/// appropriate copies out of appropriate physical registers.
1373///
1374SDValue
1375X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001376 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001377 const SmallVectorImpl<ISD::InputArg> &Ins,
1378 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001379 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001380
Chris Lattnere32bbf62007-02-28 07:09:55 +00001381 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001382 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001383 bool Is64Bit = Subtarget->is64Bit();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001384 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +00001385 RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001386 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001387
Chris Lattner3085e152007-02-25 08:59:22 +00001388 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001389 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001390 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001391 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001392
Torok Edwin3f142c32009-02-01 18:15:56 +00001393 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001394 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001395 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasXMM())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001396 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001397 }
1398
Evan Cheng79fb3b42009-02-20 20:43:02 +00001399 SDValue Val;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001400
1401 // If this is a call to a function that returns an fp value on the floating
1402 // point stack, we must guarantee the the value is popped from the stack, so
1403 // a CopyFromReg is not good enough - the copy instruction may be eliminated
1404 // if the return value is not used. We use the FpGET_ST0 instructions
1405 // instead.
1406 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1) {
1407 // If we prefer to use the value in xmm registers, copy it out as f80 and
1408 // use a truncate to move it from fp stack reg to xmm reg.
1409 if (isScalarFPTypeInSSEReg(VA.getValVT())) CopyVT = MVT::f80;
1410 bool isST0 = VA.getLocReg() == X86::ST0;
1411 unsigned Opc = 0;
1412 if (CopyVT == MVT::f32) Opc = isST0 ? X86::FpGET_ST0_32:X86::FpGET_ST1_32;
1413 if (CopyVT == MVT::f64) Opc = isST0 ? X86::FpGET_ST0_64:X86::FpGET_ST1_64;
1414 if (CopyVT == MVT::f80) Opc = isST0 ? X86::FpGET_ST0_80:X86::FpGET_ST1_80;
1415 SDValue Ops[] = { Chain, InFlag };
1416 Chain = SDValue(DAG.getMachineNode(Opc, dl, CopyVT, MVT::Other, MVT::Flag,
1417 Ops, 2), 1);
1418 Val = Chain.getValue(0);
1419
1420 // Round the f80 to the right size, which also moves it to the appropriate
1421 // xmm register.
1422 if (CopyVT != VA.getValVT())
1423 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
1424 // This truncation won't change the value.
1425 DAG.getIntPtrConstant(1));
1426 } else if (Is64Bit && CopyVT.isVector() && CopyVT.getSizeInBits() == 64) {
Evan Cheng242b38b2009-02-23 09:03:22 +00001427 // For x86-64, MMX values are returned in XMM0 / XMM1 except for v1i64.
1428 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
1429 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001430 MVT::v2i64, InFlag).getValue(1);
Evan Cheng242b38b2009-02-23 09:03:22 +00001431 Val = Chain.getValue(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00001432 Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64,
1433 Val, DAG.getConstant(0, MVT::i64));
Evan Cheng242b38b2009-02-23 09:03:22 +00001434 } else {
1435 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001436 MVT::i64, InFlag).getValue(1);
Evan Cheng242b38b2009-02-23 09:03:22 +00001437 Val = Chain.getValue(0);
1438 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001439 Val = DAG.getNode(ISD::BITCAST, dl, CopyVT, Val);
Evan Cheng79fb3b42009-02-20 20:43:02 +00001440 } else {
1441 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1442 CopyVT, InFlag).getValue(1);
1443 Val = Chain.getValue(0);
1444 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001445 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001446 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001447 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001448
Dan Gohman98ca4f22009-08-05 01:29:28 +00001449 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001450}
1451
1452
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001453//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001454// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001455//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001456// StdCall calling convention seems to be standard for many Windows' API
1457// routines and around. It differs from C calling convention just a little:
1458// callee should clean up the stack, not caller. Symbols should be also
1459// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001460// For info on fast calling convention see Fast Calling Convention (tail call)
1461// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001462
Dan Gohman98ca4f22009-08-05 01:29:28 +00001463/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001464/// semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001465static bool CallIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
1466 if (Outs.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001467 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001468
Dan Gohman98ca4f22009-08-05 01:29:28 +00001469 return Outs[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001470}
1471
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001472/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001473/// return semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001474static bool
1475ArgsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
1476 if (Ins.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001477 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001478
Dan Gohman98ca4f22009-08-05 01:29:28 +00001479 return Ins[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001480}
1481
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001482/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1483/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001484/// the specific parameter attribute. The copy will be passed as a byval
1485/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001486static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001487CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001488 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1489 DebugLoc dl) {
Chris Lattnere72f2022010-09-21 05:40:29 +00001490 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Michael J. Spencerec38de22010-10-10 22:04:20 +00001491
Dale Johannesendd64c412009-02-04 00:33:20 +00001492 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Mon P Wang20adc9d2010-04-04 03:10:48 +00001493 /*isVolatile*/false, /*AlwaysInline=*/true,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001494 MachinePointerInfo(), MachinePointerInfo());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001495}
1496
Chris Lattner29689432010-03-11 00:22:57 +00001497/// IsTailCallConvention - Return true if the calling convention is one that
1498/// supports tail call optimization.
1499static bool IsTailCallConvention(CallingConv::ID CC) {
1500 return (CC == CallingConv::Fast || CC == CallingConv::GHC);
1501}
1502
Evan Cheng0c439eb2010-01-27 00:07:07 +00001503/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1504/// a tailcall target by changing its ABI.
1505static bool FuncIsMadeTailCallSafe(CallingConv::ID CC) {
Chris Lattner29689432010-03-11 00:22:57 +00001506 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001507}
1508
Dan Gohman98ca4f22009-08-05 01:29:28 +00001509SDValue
1510X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001511 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001512 const SmallVectorImpl<ISD::InputArg> &Ins,
1513 DebugLoc dl, SelectionDAG &DAG,
1514 const CCValAssign &VA,
1515 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00001516 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00001517 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001518 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Evan Cheng0c439eb2010-01-27 00:07:07 +00001519 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001520 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001521 EVT ValVT;
1522
1523 // If value is passed by pointer we have address passed instead of the value
1524 // itself.
1525 if (VA.getLocInfo() == CCValAssign::Indirect)
1526 ValVT = VA.getLocVT();
1527 else
1528 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001529
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001530 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001531 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001532 // In case of tail call optimization mark all arguments mutable. Since they
1533 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001534 if (Flags.isByVal()) {
1535 int FI = MFI->CreateFixedObject(Flags.getByValSize(),
Evan Chenged2ae132010-07-03 00:40:23 +00001536 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001537 return DAG.getFrameIndex(FI, getPointerTy());
1538 } else {
1539 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00001540 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001541 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1542 return DAG.getLoad(ValVT, dl, Chain, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001543 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00001544 false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001545 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001546}
1547
Dan Gohman475871a2008-07-27 21:46:04 +00001548SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001549X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001550 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001551 bool isVarArg,
1552 const SmallVectorImpl<ISD::InputArg> &Ins,
1553 DebugLoc dl,
1554 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001555 SmallVectorImpl<SDValue> &InVals)
1556 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00001557 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001558 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001559
Gordon Henriksen86737662008-01-05 16:56:59 +00001560 const Function* Fn = MF.getFunction();
1561 if (Fn->hasExternalLinkage() &&
1562 Subtarget->isTargetCygMing() &&
1563 Fn->getName() == "main")
1564 FuncInfo->setForceFramePointer(true);
1565
Evan Cheng1bc78042006-04-26 01:20:17 +00001566 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001567 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001568 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001569
Chris Lattner29689432010-03-11 00:22:57 +00001570 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1571 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001572
Chris Lattner638402b2007-02-28 07:00:42 +00001573 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001574 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001575 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1576 ArgLocs, *DAG.getContext());
Duncan Sands45907662010-10-31 13:21:44 +00001577 CCInfo.AnalyzeFormalArguments(Ins, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001578
Chris Lattnerf39f7712007-02-28 05:46:49 +00001579 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001580 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001581 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1582 CCValAssign &VA = ArgLocs[i];
1583 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1584 // places.
1585 assert(VA.getValNo() != LastVal &&
1586 "Don't support value assigned to multiple locs yet");
1587 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001588
Chris Lattnerf39f7712007-02-28 05:46:49 +00001589 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001590 EVT RegVT = VA.getLocVT();
Devang Patel8a84e442009-01-05 17:31:22 +00001591 TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001592 if (RegVT == MVT::i32)
Chris Lattnerf39f7712007-02-28 05:46:49 +00001593 RC = X86::GR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001594 else if (Is64Bit && RegVT == MVT::i64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001595 RC = X86::GR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001596 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001597 RC = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001598 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001599 RC = X86::FR64RegisterClass;
Bruno Cardoso Lopesac098352010-08-05 23:35:51 +00001600 else if (RegVT.isVector() && RegVT.getSizeInBits() == 256)
1601 RC = X86::VR256RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001602 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001603 RC = X86::VR128RegisterClass;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001604 else if (RegVT == MVT::x86mmx)
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001605 RC = X86::VR64RegisterClass;
1606 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001607 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001608
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001609 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001610 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001611
Chris Lattnerf39f7712007-02-28 05:46:49 +00001612 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1613 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1614 // right size.
1615 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001616 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001617 DAG.getValueType(VA.getValVT()));
1618 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001619 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001620 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001621 else if (VA.getLocInfo() == CCValAssign::BCvt)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001622 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001623
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001624 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001625 // Handle MMX values passed in XMM regs.
1626 if (RegVT.isVector()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001627 ArgValue = DAG.getNode(X86ISD::MOVDQ2Q, dl, VA.getValVT(),
1628 ArgValue);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001629 } else
1630 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001631 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001632 } else {
1633 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001634 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001635 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001636
1637 // If value is passed via pointer - do a load.
1638 if (VA.getLocInfo() == CCValAssign::Indirect)
Chris Lattner51abfe42010-09-21 06:02:19 +00001639 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue,
1640 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001641
Dan Gohman98ca4f22009-08-05 01:29:28 +00001642 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001643 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001644
Dan Gohman61a92132008-04-21 23:59:07 +00001645 // The x86-64 ABI for returning structs by value requires that we copy
1646 // the sret argument into %rax for the return. Save the argument into
1647 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001648 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001649 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1650 unsigned Reg = FuncInfo->getSRetReturnReg();
1651 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001652 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001653 FuncInfo->setSRetReturnReg(Reg);
1654 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001655 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001656 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001657 }
1658
Chris Lattnerf39f7712007-02-28 05:46:49 +00001659 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00001660 // Align stack specially for tail calls.
1661 if (FuncIsMadeTailCallSafe(CallConv))
Gordon Henriksenae636f82008-01-03 16:47:34 +00001662 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001663
Evan Cheng1bc78042006-04-26 01:20:17 +00001664 // If the function takes variable number of arguments, make a frame index for
1665 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001666 if (isVarArg) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001667 if (!IsWin64 && (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
1668 CallConv != CallingConv::X86_ThisCall))) {
Jakob Stoklund Olesenb2eeed72010-07-29 17:42:27 +00001669 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,true));
Gordon Henriksen86737662008-01-05 16:56:59 +00001670 }
1671 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001672 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1673
1674 // FIXME: We should really autogenerate these arrays
1675 static const unsigned GPR64ArgRegsWin64[] = {
1676 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001677 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001678 static const unsigned GPR64ArgRegs64Bit[] = {
1679 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1680 };
1681 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001682 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1683 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1684 };
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001685 const unsigned *GPR64ArgRegs;
1686 unsigned NumXMMRegs = 0;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001687
1688 if (IsWin64) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001689 // The XMM registers which might contain var arg parameters are shadowed
1690 // in their paired GPR. So we only need to save the GPR to their home
1691 // slots.
1692 TotalNumIntRegs = 4;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001693 GPR64ArgRegs = GPR64ArgRegsWin64;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001694 } else {
1695 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1696 GPR64ArgRegs = GPR64ArgRegs64Bit;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001697
1698 NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs64Bit, TotalNumXMMRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001699 }
1700 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1701 TotalNumIntRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001702
Devang Patel578efa92009-06-05 21:57:13 +00001703 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001704 assert(!(NumXMMRegs && !Subtarget->hasXMM()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001705 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001706 assert(!(NumXMMRegs && UseSoftFloat && NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00001707 "SSE register cannot be used when SSE is disabled!");
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001708 if (UseSoftFloat || NoImplicitFloatOps || !Subtarget->hasXMM())
Torok Edwin3f142c32009-02-01 18:15:56 +00001709 // Kernel mode asks for SSE to be disabled, so don't push them
1710 // on the stack.
1711 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001712
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001713 if (IsWin64) {
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001714 const TargetFrameInfo &TFI = *getTargetMachine().getFrameInfo();
1715 // Get to the caller-allocated home save location. Add 8 to account
1716 // for the return address.
1717 int HomeOffset = TFI.getOffsetOfLocalArea() + 8;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001718 FuncInfo->setRegSaveFrameIndex(
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001719 MFI->CreateFixedObject(1, NumIntRegs * 8 + HomeOffset, false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001720 FuncInfo->setVarArgsFrameIndex(FuncInfo->getRegSaveFrameIndex());
1721 } else {
1722 // For X86-64, if there are vararg parameters that are passed via
1723 // registers, then we must store them to their spots on the stack so they
1724 // may be loaded by deferencing the result of va_next.
1725 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
1726 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
1727 FuncInfo->setRegSaveFrameIndex(
1728 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
Dan Gohman1e93df62010-04-17 14:41:14 +00001729 false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001730 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001731
Gordon Henriksen86737662008-01-05 16:56:59 +00001732 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001733 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00001734 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
1735 getPointerTy());
1736 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001737 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00001738 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
1739 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00001740 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
1741 X86::GR64RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00001742 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00001743 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001744 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001745 MachinePointerInfo::getFixedStack(
1746 FuncInfo->getRegSaveFrameIndex(), Offset),
1747 false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001748 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001749 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001750 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001751
Dan Gohmanface41a2009-08-16 21:24:25 +00001752 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
1753 // Now store the XMM (fp + vector) parameter registers.
1754 SmallVector<SDValue, 11> SaveXMMOps;
1755 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001756
Dan Gohmanface41a2009-08-16 21:24:25 +00001757 unsigned AL = MF.addLiveIn(X86::AL, X86::GR8RegisterClass);
1758 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
1759 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001760
Dan Gohman1e93df62010-04-17 14:41:14 +00001761 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1762 FuncInfo->getRegSaveFrameIndex()));
1763 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1764 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00001765
Dan Gohmanface41a2009-08-16 21:24:25 +00001766 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001767 unsigned VReg = MF.addLiveIn(XMMArgRegs64Bit[NumXMMRegs],
Dan Gohmanface41a2009-08-16 21:24:25 +00001768 X86::VR128RegisterClass);
1769 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
1770 SaveXMMOps.push_back(Val);
1771 }
1772 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
1773 MVT::Other,
1774 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00001775 }
Dan Gohmanface41a2009-08-16 21:24:25 +00001776
1777 if (!MemOps.empty())
1778 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1779 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001780 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001781 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001782
Gordon Henriksen86737662008-01-05 16:56:59 +00001783 // Some CCs need callee pop.
Dan Gohman4d3d6e12010-05-27 18:43:40 +00001784 if (Subtarget->IsCalleePop(isVarArg, CallConv)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001785 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001786 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00001787 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001788 // If this is an sret function, the return should pop the hidden pointer.
Chris Lattner29689432010-03-11 00:22:57 +00001789 if (!Is64Bit && !IsTailCallConvention(CallConv) && ArgsAreStructReturn(Ins))
Dan Gohman1e93df62010-04-17 14:41:14 +00001790 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001791 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001792
Gordon Henriksen86737662008-01-05 16:56:59 +00001793 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001794 // RegSaveFrameIndex is X86-64 only.
1795 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Anton Korobeynikovded05e32010-05-16 09:08:45 +00001796 if (CallConv == CallingConv::X86_FastCall ||
1797 CallConv == CallingConv::X86_ThisCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00001798 // fastcc functions can't have varargs.
1799 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00001800 }
Evan Cheng25caf632006-05-23 21:06:34 +00001801
Dan Gohman98ca4f22009-08-05 01:29:28 +00001802 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001803}
1804
Dan Gohman475871a2008-07-27 21:46:04 +00001805SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001806X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
1807 SDValue StackPtr, SDValue Arg,
1808 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00001809 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001810 ISD::ArgFlagsTy Flags) const {
Anton Korobeynikovc7c62bb2010-09-02 22:31:32 +00001811 const unsigned FirstStackArgOffset = (Subtarget->isTargetWin64() ? 32 : 0);
1812 unsigned LocMemOffset = FirstStackArgOffset + VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00001813 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00001814 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001815 if (Flags.isByVal())
Dale Johannesendd64c412009-02-04 00:33:20 +00001816 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001817
1818 return DAG.getStore(Chain, dl, Arg, PtrOff,
1819 MachinePointerInfo::getStack(LocMemOffset),
David Greene67c9d422010-02-15 16:53:33 +00001820 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00001821}
1822
Bill Wendling64e87322009-01-16 19:25:27 +00001823/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001824/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00001825SDValue
1826X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00001827 SDValue &OutRetAddr, SDValue Chain,
1828 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00001829 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001830 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00001831 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001832 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00001833
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001834 // Load the "old" Return address.
Chris Lattner51abfe42010-09-21 06:02:19 +00001835 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, MachinePointerInfo(),
1836 false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001837 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001838}
1839
1840/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1841/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00001842static SDValue
1843EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00001844 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00001845 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001846 // Store the return address to the appropriate stack slot.
1847 if (!FPDiff) return Chain;
1848 // Calculate the new stack slot for the return address.
1849 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00001850 int NewReturnAddrFI =
Evan Chenged2ae132010-07-03 00:40:23 +00001851 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false);
Owen Anderson825b72b2009-08-11 20:47:22 +00001852 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00001853 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001854 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00001855 MachinePointerInfo::getFixedStack(NewReturnAddrFI),
David Greene67c9d422010-02-15 16:53:33 +00001856 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001857 return Chain;
1858}
1859
Dan Gohman98ca4f22009-08-05 01:29:28 +00001860SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001861X86TargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001862 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001863 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001864 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001865 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001866 const SmallVectorImpl<ISD::InputArg> &Ins,
1867 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001868 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001869 MachineFunction &MF = DAG.getMachineFunction();
1870 bool Is64Bit = Subtarget->is64Bit();
1871 bool IsStructRet = CallIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00001872 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001873
Evan Cheng5f941932010-02-05 02:21:12 +00001874 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00001875 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00001876 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1877 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001878 Outs, OutVals, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00001879
1880 // Sibcalls are automatically detected tailcalls which do not require
1881 // ABI changes.
Dan Gohman1797ed52010-02-08 20:27:50 +00001882 if (!GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00001883 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00001884
1885 if (isTailCall)
1886 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00001887 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00001888
Chris Lattner29689432010-03-11 00:22:57 +00001889 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1890 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001891
Chris Lattner638402b2007-02-28 07:00:42 +00001892 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00001893 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001894 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1895 ArgLocs, *DAG.getContext());
Duncan Sands45907662010-10-31 13:21:44 +00001896 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001897
Chris Lattner423c5f42007-02-28 05:31:48 +00001898 // Get a count of how many bytes are to be pushed on the stack.
1899 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00001900 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00001901 // This is a sibcall. The memory operands are available in caller's
1902 // own caller's stack.
1903 NumBytes = 0;
Chris Lattner29689432010-03-11 00:22:57 +00001904 else if (GuaranteedTailCallOpt && IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00001905 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001906
Gordon Henriksen86737662008-01-05 16:56:59 +00001907 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00001908 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001909 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00001910 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00001911 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1912 FPDiff = NumBytesCallerPushed - NumBytes;
1913
1914 // Set the delta of movement of the returnaddr stackslot.
1915 // But only set if delta is greater than previous delta.
1916 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1917 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1918 }
1919
Evan Chengf22f9b32010-02-06 03:28:46 +00001920 if (!IsSibcall)
1921 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001922
Dan Gohman475871a2008-07-27 21:46:04 +00001923 SDValue RetAddrFrIdx;
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001924 // Load return adress for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00001925 if (isTailCall && FPDiff)
1926 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
1927 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00001928
Dan Gohman475871a2008-07-27 21:46:04 +00001929 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
1930 SmallVector<SDValue, 8> MemOpChains;
1931 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00001932
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001933 // Walk the register/memloc assignments, inserting copies/loads. In the case
1934 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00001935 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1936 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001937 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00001938 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00001939 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00001940 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00001941
Chris Lattner423c5f42007-02-28 05:31:48 +00001942 // Promote the value if needed.
1943 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001944 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00001945 case CCValAssign::Full: break;
1946 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001947 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001948 break;
1949 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001950 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001951 break;
1952 case CCValAssign::AExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001953 if (RegVT.isVector() && RegVT.getSizeInBits() == 128) {
1954 // Special case: passing MMX values in XMM registers.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001955 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00001956 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
1957 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001958 } else
1959 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
1960 break;
1961 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001962 Arg = DAG.getNode(ISD::BITCAST, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001963 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001964 case CCValAssign::Indirect: {
1965 // Store the argument.
1966 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00001967 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001968 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00001969 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00001970 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001971 Arg = SpillSlot;
1972 break;
1973 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00001974 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001975
Chris Lattner423c5f42007-02-28 05:31:48 +00001976 if (VA.isRegLoc()) {
1977 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00001978 if (isVarArg && Subtarget->isTargetWin64()) {
1979 // Win64 ABI requires argument XMM reg to be copied to the corresponding
1980 // shadow reg if callee is a varargs function.
1981 unsigned ShadowReg = 0;
1982 switch (VA.getLocReg()) {
1983 case X86::XMM0: ShadowReg = X86::RCX; break;
1984 case X86::XMM1: ShadowReg = X86::RDX; break;
1985 case X86::XMM2: ShadowReg = X86::R8; break;
1986 case X86::XMM3: ShadowReg = X86::R9; break;
1987 }
1988 if (ShadowReg)
1989 RegsToPass.push_back(std::make_pair(ShadowReg, Arg));
1990 }
Evan Chengf22f9b32010-02-06 03:28:46 +00001991 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00001992 assert(VA.isMemLoc());
1993 if (StackPtr.getNode() == 0)
1994 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
1995 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1996 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001997 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001998 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001999
Evan Cheng32fe1032006-05-25 00:59:30 +00002000 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002001 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002002 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002003
Evan Cheng347d5f72006-04-28 21:29:37 +00002004 // Build a sequence of copy-to-reg nodes chained together with token chain
2005 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00002006 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002007 // Tail call byval lowering might overwrite argument registers so in case of
2008 // tail call optimization the copies to registers are lowered later.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002009 if (!isTailCall)
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002010 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002011 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002012 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002013 InFlag = Chain.getValue(1);
2014 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002015
Chris Lattner88e1fd52009-07-09 04:24:46 +00002016 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002017 // ELF / PIC requires GOT in the EBX register before function calls via PLT
2018 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002019 if (!isTailCall) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002020 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
2021 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00002022 DebugLoc(), getPointerTy()),
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002023 InFlag);
2024 InFlag = Chain.getValue(1);
2025 } else {
2026 // If we are tail calling and generating PIC/GOT style code load the
2027 // address of the callee into ECX. The value in ecx is used as target of
2028 // the tail jump. This is done to circumvent the ebx/callee-saved problem
2029 // for tail calls on PIC/GOT architectures. Normally we would just put the
2030 // address of GOT into ebx and then call target@PLT. But for tail calls
2031 // ebx would be restored (since ebx is callee saved) before jumping to the
2032 // target@PLT.
2033
2034 // Note: The actual moving to ECX is done further down.
2035 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
2036 if (G && !G->getGlobal()->hasHiddenVisibility() &&
2037 !G->getGlobal()->hasProtectedVisibility())
2038 Callee = LowerGlobalAddress(Callee, DAG);
2039 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00002040 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002041 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002042 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002043
Nate Begemanc8ea6732010-07-21 20:49:52 +00002044 if (Is64Bit && isVarArg && !Subtarget->isTargetWin64()) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002045 // From AMD64 ABI document:
2046 // For calls that may call functions that use varargs or stdargs
2047 // (prototype-less calls or calls to functions containing ellipsis (...) in
2048 // the declaration) %al is used as hidden argument to specify the number
2049 // of SSE registers used. The contents of %al do not need to match exactly
2050 // the number of registers, but must be an ubound on the number of SSE
2051 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002052
Gordon Henriksen86737662008-01-05 16:56:59 +00002053 // Count the number of XMM registers allocated.
2054 static const unsigned XMMArgRegs[] = {
2055 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2056 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2057 };
2058 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00002059 assert((Subtarget->hasXMM() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00002060 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00002061
Dale Johannesendd64c412009-02-04 00:33:20 +00002062 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Owen Anderson825b72b2009-08-11 20:47:22 +00002063 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002064 InFlag = Chain.getValue(1);
2065 }
2066
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002067
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002068 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002069 if (isTailCall) {
2070 // Force all the incoming stack arguments to be loaded from the stack
2071 // before any new outgoing arguments are stored to the stack, because the
2072 // outgoing stack slots may alias the incoming argument stack slots, and
2073 // the alias isn't otherwise explicit. This is slightly more conservative
2074 // than necessary, because it means that each store effectively depends
2075 // on every argument instead of just those arguments it would clobber.
2076 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2077
Dan Gohman475871a2008-07-27 21:46:04 +00002078 SmallVector<SDValue, 8> MemOpChains2;
2079 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002080 int FI = 0;
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002081 // Do not flag preceeding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00002082 InFlag = SDValue();
Dan Gohman1797ed52010-02-08 20:27:50 +00002083 if (GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002084 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2085 CCValAssign &VA = ArgLocs[i];
2086 if (VA.isRegLoc())
2087 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002088 assert(VA.isMemLoc());
Dan Gohmanc9403652010-07-07 15:54:55 +00002089 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002090 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002091 // Create frame index.
2092 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002093 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002094 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002095 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002096
Duncan Sands276dcbd2008-03-21 09:14:45 +00002097 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002098 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002099 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002100 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00002101 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00002102 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002103 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002104
Dan Gohman98ca4f22009-08-05 01:29:28 +00002105 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2106 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002107 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002108 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002109 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002110 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002111 DAG.getStore(ArgChain, dl, Arg, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002112 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002113 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002114 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002115 }
2116 }
2117
2118 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002119 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002120 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002121
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002122 // Copy arguments to their registers.
2123 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002124 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002125 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002126 InFlag = Chain.getValue(1);
2127 }
Dan Gohman475871a2008-07-27 21:46:04 +00002128 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002129
Gordon Henriksen86737662008-01-05 16:56:59 +00002130 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002131 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00002132 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002133 }
2134
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002135 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2136 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2137 // In the 64-bit large code model, we have to make all calls
2138 // through a register, since the call instruction's 32-bit
2139 // pc-relative offset may not be large enough to hold the whole
2140 // address.
2141 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002142 // If the callee is a GlobalAddress node (quite common, every direct call
2143 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2144 // it.
2145
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002146 // We should use extra load for direct calls to dllimported functions in
2147 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002148 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002149 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002150 unsigned char OpFlags = 0;
Eric Christopherfd179292009-08-27 18:07:15 +00002151
Chris Lattner48a7d022009-07-09 05:02:21 +00002152 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2153 // external symbols most go through the PLT in PIC mode. If the symbol
2154 // has hidden or protected visibility, or if it is static or local, then
2155 // we don't need to use the PLT - we can directly call it.
2156 if (Subtarget->isTargetELF() &&
2157 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002158 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002159 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002160 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner80945782010-09-27 06:34:01 +00002161 (GV->isDeclaration() || GV->isWeakForLinker()) &&
2162 Subtarget->getDarwinVers() < 9) {
Chris Lattner74e726e2009-07-09 05:27:35 +00002163 // PC-relative references to external symbols should go through $stub,
2164 // unless we're building with the leopard linker or later, which
2165 // automatically synthesizes these stubs.
2166 OpFlags = X86II::MO_DARWIN_STUB;
2167 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002168
Devang Patel0d881da2010-07-06 22:08:15 +00002169 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002170 G->getOffset(), OpFlags);
2171 }
Bill Wendling056292f2008-09-16 21:48:12 +00002172 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002173 unsigned char OpFlags = 0;
2174
Evan Cheng1bf891a2010-12-01 22:59:46 +00002175 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to
2176 // external symbols should go through the PLT.
2177 if (Subtarget->isTargetELF() &&
2178 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
2179 OpFlags = X86II::MO_PLT;
2180 } else if (Subtarget->isPICStyleStubAny() &&
2181 Subtarget->getDarwinVers() < 9) {
2182 // PC-relative references to external symbols should go through $stub,
2183 // unless we're building with the leopard linker or later, which
2184 // automatically synthesizes these stubs.
2185 OpFlags = X86II::MO_DARWIN_STUB;
Chris Lattner74e726e2009-07-09 05:27:35 +00002186 }
Eric Christopherfd179292009-08-27 18:07:15 +00002187
Chris Lattner48a7d022009-07-09 05:02:21 +00002188 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2189 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002190 }
2191
Chris Lattnerd96d0722007-02-25 06:40:16 +00002192 // Returns a chain & a flag for retval copy to use.
Owen Anderson825b72b2009-08-11 20:47:22 +00002193 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00002194 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002195
Evan Chengf22f9b32010-02-06 03:28:46 +00002196 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002197 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2198 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002199 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002200 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002201
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002202 Ops.push_back(Chain);
2203 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002204
Dan Gohman98ca4f22009-08-05 01:29:28 +00002205 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002206 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002207
Gordon Henriksen86737662008-01-05 16:56:59 +00002208 // Add argument registers to the end of the list so that they are known live
2209 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002210 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2211 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2212 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002213
Evan Cheng586ccac2008-03-18 23:36:35 +00002214 // Add an implicit use GOT pointer in EBX.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002215 if (!isTailCall && Subtarget->isPICStyleGOT())
Evan Cheng586ccac2008-03-18 23:36:35 +00002216 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
2217
Anton Korobeynikov3a1e54a2010-08-17 21:06:07 +00002218 // Add an implicit use of AL for non-Windows x86 64-bit vararg functions.
2219 if (Is64Bit && isVarArg && !Subtarget->isTargetWin64())
Owen Anderson825b72b2009-08-11 20:47:22 +00002220 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
Evan Cheng586ccac2008-03-18 23:36:35 +00002221
Gabor Greifba36cb52008-08-28 21:40:38 +00002222 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002223 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002224
Dan Gohman98ca4f22009-08-05 01:29:28 +00002225 if (isTailCall) {
Dale Johannesen88004c22010-06-05 00:30:45 +00002226 // We used to do:
2227 //// If this is the first return lowered for this function, add the regs
2228 //// to the liveout set for the function.
2229 // This isn't right, although it's probably harmless on x86; liveouts
2230 // should be computed from returns not tail calls. Consider a void
2231 // function making a tail call to a function returning int.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002232 return DAG.getNode(X86ISD::TC_RETURN, dl,
2233 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002234 }
2235
Dale Johannesenace16102009-02-03 19:33:06 +00002236 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002237 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002238
Chris Lattner2d297092006-05-23 18:50:38 +00002239 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002240 unsigned NumBytesForCalleeToPush;
Dan Gohman4d3d6e12010-05-27 18:43:40 +00002241 if (Subtarget->IsCalleePop(isVarArg, CallConv))
Gordon Henriksen86737662008-01-05 16:56:59 +00002242 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Chris Lattner29689432010-03-11 00:22:57 +00002243 else if (!Is64Bit && !IsTailCallConvention(CallConv) && IsStructRet)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002244 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002245 // pops the hidden struct pointer, so we have to push it back.
2246 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002247 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002248 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002249 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002250
Gordon Henriksenae636f82008-01-03 16:47:34 +00002251 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002252 if (!IsSibcall) {
2253 Chain = DAG.getCALLSEQ_END(Chain,
2254 DAG.getIntPtrConstant(NumBytes, true),
2255 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2256 true),
2257 InFlag);
2258 InFlag = Chain.getValue(1);
2259 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002260
Chris Lattner3085e152007-02-25 08:59:22 +00002261 // Handle result values, copying them out of physregs into vregs that we
2262 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002263 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2264 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002265}
2266
Evan Cheng25ab6902006-09-08 06:48:29 +00002267
2268//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002269// Fast Calling Convention (tail call) implementation
2270//===----------------------------------------------------------------------===//
2271
2272// Like std call, callee cleans arguments, convention except that ECX is
2273// reserved for storing the tail called function address. Only 2 registers are
2274// free for argument passing (inreg). Tail call optimization is performed
2275// provided:
2276// * tailcallopt is enabled
2277// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002278// On X86_64 architecture with GOT-style position independent code only local
2279// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002280// To keep the stack aligned according to platform abi the function
2281// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2282// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002283// If a tail called function callee has more arguments than the caller the
2284// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002285// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002286// original REtADDR, but before the saved framepointer or the spilled registers
2287// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2288// stack layout:
2289// arg1
2290// arg2
2291// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002292// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002293// move area ]
2294// (possible EBP)
2295// ESI
2296// EDI
2297// local1 ..
2298
2299/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2300/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002301unsigned
2302X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2303 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002304 MachineFunction &MF = DAG.getMachineFunction();
2305 const TargetMachine &TM = MF.getTarget();
2306 const TargetFrameInfo &TFI = *TM.getFrameInfo();
2307 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002308 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002309 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002310 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002311 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2312 // Number smaller than 12 so just add the difference.
2313 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2314 } else {
2315 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002316 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002317 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002318 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002319 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002320}
2321
Evan Cheng5f941932010-02-05 02:21:12 +00002322/// MatchingStackOffset - Return true if the given stack call argument is
2323/// already available in the same position (relatively) of the caller's
2324/// incoming argument stack.
2325static
2326bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2327 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2328 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002329 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2330 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002331 if (Arg.getOpcode() == ISD::CopyFromReg) {
2332 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
2333 if (!VR || TargetRegisterInfo::isPhysicalRegister(VR))
2334 return false;
2335 MachineInstr *Def = MRI->getVRegDef(VR);
2336 if (!Def)
2337 return false;
2338 if (!Flags.isByVal()) {
2339 if (!TII->isLoadFromStackSlot(Def, FI))
2340 return false;
2341 } else {
2342 unsigned Opcode = Def->getOpcode();
2343 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2344 Def->getOperand(1).isFI()) {
2345 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002346 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002347 } else
2348 return false;
2349 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002350 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2351 if (Flags.isByVal())
2352 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002353 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002354 // define @foo(%struct.X* %A) {
2355 // tail call @bar(%struct.X* byval %A)
2356 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002357 return false;
2358 SDValue Ptr = Ld->getBasePtr();
2359 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2360 if (!FINode)
2361 return false;
2362 FI = FINode->getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002363 } else
2364 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002365
Evan Cheng4cae1332010-03-05 08:38:04 +00002366 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002367 if (!MFI->isFixedObjectIndex(FI))
2368 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002369 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002370}
2371
Dan Gohman98ca4f22009-08-05 01:29:28 +00002372/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2373/// for tail call optimization. Targets which want to do tail call
2374/// optimization should implement this function.
2375bool
2376X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002377 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002378 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002379 bool isCalleeStructRet,
2380 bool isCallerStructRet,
Evan Chengb1712452010-01-27 06:25:16 +00002381 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002382 const SmallVectorImpl<SDValue> &OutVals,
Evan Chengb1712452010-01-27 06:25:16 +00002383 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002384 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002385 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002386 CalleeCC != CallingConv::C)
2387 return false;
2388
Evan Cheng7096ae42010-01-29 06:45:59 +00002389 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002390 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002391 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Cheng13617962010-04-30 01:12:32 +00002392 CallingConv::ID CallerCC = CallerF->getCallingConv();
2393 bool CCMatch = CallerCC == CalleeCC;
2394
Dan Gohman1797ed52010-02-08 20:27:50 +00002395 if (GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002396 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002397 return true;
2398 return false;
2399 }
2400
Dale Johannesen2f05cc02010-05-28 23:24:28 +00002401 // Look for obvious safe cases to perform tail call optimization that do not
2402 // require ABI changes. This is what gcc calls sibcall.
Evan Chengb2c92902010-02-02 02:22:50 +00002403
Evan Cheng2c12cb42010-03-26 16:26:03 +00002404 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2405 // emit a special epilogue.
2406 if (RegInfo->needsStackRealignment(MF))
2407 return false;
2408
Eric Christopher90eb4022010-07-22 00:26:08 +00002409 // Do not sibcall optimize vararg calls unless the call site is not passing
2410 // any arguments.
Evan Cheng3c262ee2010-03-26 02:13:13 +00002411 if (isVarArg && !Outs.empty())
Evan Cheng843bd692010-01-31 06:44:49 +00002412 return false;
2413
Evan Chenga375d472010-03-15 18:54:48 +00002414 // Also avoid sibcall optimization if either caller or callee uses struct
2415 // return semantics.
2416 if (isCalleeStructRet || isCallerStructRet)
2417 return false;
2418
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002419 // If the call result is in ST0 / ST1, it needs to be popped off the x87 stack.
2420 // Therefore if it's not used by the call it is not safe to optimize this into
2421 // a sibcall.
2422 bool Unused = false;
2423 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2424 if (!Ins[i].Used) {
2425 Unused = true;
2426 break;
2427 }
2428 }
2429 if (Unused) {
2430 SmallVector<CCValAssign, 16> RVLocs;
2431 CCState CCInfo(CalleeCC, false, getTargetMachine(),
2432 RVLocs, *DAG.getContext());
2433 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002434 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002435 CCValAssign &VA = RVLocs[i];
2436 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2437 return false;
2438 }
2439 }
2440
Evan Cheng13617962010-04-30 01:12:32 +00002441 // If the calling conventions do not match, then we'd better make sure the
2442 // results are returned in the same way as what the caller expects.
2443 if (!CCMatch) {
2444 SmallVector<CCValAssign, 16> RVLocs1;
2445 CCState CCInfo1(CalleeCC, false, getTargetMachine(),
2446 RVLocs1, *DAG.getContext());
2447 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2448
2449 SmallVector<CCValAssign, 16> RVLocs2;
2450 CCState CCInfo2(CallerCC, false, getTargetMachine(),
2451 RVLocs2, *DAG.getContext());
2452 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2453
2454 if (RVLocs1.size() != RVLocs2.size())
2455 return false;
2456 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2457 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2458 return false;
2459 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2460 return false;
2461 if (RVLocs1[i].isRegLoc()) {
2462 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2463 return false;
2464 } else {
2465 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2466 return false;
2467 }
2468 }
2469 }
2470
Evan Chenga6bff982010-01-30 01:22:00 +00002471 // If the callee takes no arguments then go on to check the results of the
2472 // call.
2473 if (!Outs.empty()) {
2474 // Check if stack adjustment is needed. For now, do not do this if any
2475 // argument is passed on the stack.
2476 SmallVector<CCValAssign, 16> ArgLocs;
2477 CCState CCInfo(CalleeCC, isVarArg, getTargetMachine(),
2478 ArgLocs, *DAG.getContext());
Duncan Sands45907662010-10-31 13:21:44 +00002479 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Evan Chengb2c92902010-02-02 02:22:50 +00002480 if (CCInfo.getNextStackOffset()) {
2481 MachineFunction &MF = DAG.getMachineFunction();
2482 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2483 return false;
2484 if (Subtarget->isTargetWin64())
2485 // Win64 ABI has additional complications.
2486 return false;
2487
2488 // Check if the arguments are already laid out in the right way as
2489 // the caller's fixed stack objects.
2490 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002491 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2492 const X86InstrInfo *TII =
2493 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002494 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2495 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00002496 SDValue Arg = OutVals[i];
Evan Chengb2c92902010-02-02 02:22:50 +00002497 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002498 if (VA.getLocInfo() == CCValAssign::Indirect)
2499 return false;
2500 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002501 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2502 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002503 return false;
2504 }
2505 }
2506 }
Evan Cheng9c044672010-05-29 01:35:22 +00002507
2508 // If the tailcall address may be in a register, then make sure it's
2509 // possible to register allocate for it. In 32-bit, the call address can
2510 // only target EAX, EDX, or ECX since the tail call must be scheduled after
Evan Chengdedd9742010-07-14 06:44:01 +00002511 // callee-saved registers are restored. These happen to be the same
2512 // registers used to pass 'inreg' arguments so watch out for those.
2513 if (!Subtarget->is64Bit() &&
2514 !isa<GlobalAddressSDNode>(Callee) &&
Evan Cheng9c044672010-05-29 01:35:22 +00002515 !isa<ExternalSymbolSDNode>(Callee)) {
Evan Cheng9c044672010-05-29 01:35:22 +00002516 unsigned NumInRegs = 0;
2517 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2518 CCValAssign &VA = ArgLocs[i];
Evan Chengdedd9742010-07-14 06:44:01 +00002519 if (!VA.isRegLoc())
2520 continue;
2521 unsigned Reg = VA.getLocReg();
2522 switch (Reg) {
2523 default: break;
2524 case X86::EAX: case X86::EDX: case X86::ECX:
2525 if (++NumInRegs == 3)
Evan Cheng9c044672010-05-29 01:35:22 +00002526 return false;
Evan Chengdedd9742010-07-14 06:44:01 +00002527 break;
Evan Cheng9c044672010-05-29 01:35:22 +00002528 }
2529 }
2530 }
Evan Chenga6bff982010-01-30 01:22:00 +00002531 }
Evan Chengb1712452010-01-27 06:25:16 +00002532
Dale Johannesend155d7e2010-10-25 22:17:05 +00002533 // An stdcall caller is expected to clean up its arguments; the callee
Dale Johannesen0e034562010-11-12 00:43:18 +00002534 // isn't going to do that.
Dale Johannesend155d7e2010-10-25 22:17:05 +00002535 if (!CCMatch && CallerCC==CallingConv::X86_StdCall)
2536 return false;
2537
Evan Cheng86809cc2010-02-03 03:28:02 +00002538 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002539}
2540
Dan Gohman3df24e62008-09-03 23:12:08 +00002541FastISel *
Dan Gohmana4160c32010-07-07 16:29:44 +00002542X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
2543 return X86::createFastISel(funcInfo);
Dan Gohmand9f3c482008-08-19 21:32:53 +00002544}
2545
2546
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002547//===----------------------------------------------------------------------===//
2548// Other Lowering Hooks
2549//===----------------------------------------------------------------------===//
2550
Bruno Cardoso Lopese654b562010-09-01 00:51:36 +00002551static bool MayFoldLoad(SDValue Op) {
2552 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
2553}
2554
2555static bool MayFoldIntoStore(SDValue Op) {
2556 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
2557}
2558
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002559static bool isTargetShuffle(unsigned Opcode) {
2560 switch(Opcode) {
2561 default: return false;
2562 case X86ISD::PSHUFD:
2563 case X86ISD::PSHUFHW:
2564 case X86ISD::PSHUFLW:
2565 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002566 case X86ISD::PALIGN:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002567 case X86ISD::SHUFPS:
2568 case X86ISD::MOVLHPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002569 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002570 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002571 case X86ISD::MOVLPS:
2572 case X86ISD::MOVLPD:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002573 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002574 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002575 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002576 case X86ISD::MOVSS:
2577 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002578 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002579 case X86ISD::UNPCKLPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002580 case X86ISD::PUNPCKLWD:
2581 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002582 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002583 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002584 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002585 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002586 case X86ISD::PUNPCKHWD:
2587 case X86ISD::PUNPCKHBW:
2588 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002589 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002590 return true;
2591 }
2592 return false;
2593}
2594
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002595static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002596 SDValue V1, SelectionDAG &DAG) {
2597 switch(Opc) {
2598 default: llvm_unreachable("Unknown x86 shuffle node");
2599 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002600 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002601 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002602 return DAG.getNode(Opc, dl, VT, V1);
2603 }
2604
2605 return SDValue();
2606}
2607
2608static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00002609 SDValue V1, unsigned TargetMask, SelectionDAG &DAG) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002610 switch(Opc) {
2611 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002612 case X86ISD::PSHUFD:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002613 case X86ISD::PSHUFHW:
2614 case X86ISD::PSHUFLW:
2615 return DAG.getNode(Opc, dl, VT, V1, DAG.getConstant(TargetMask, MVT::i8));
2616 }
2617
2618 return SDValue();
2619}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002620
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002621static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2622 SDValue V1, SDValue V2, unsigned TargetMask, SelectionDAG &DAG) {
2623 switch(Opc) {
2624 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002625 case X86ISD::PALIGN:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002626 case X86ISD::SHUFPD:
2627 case X86ISD::SHUFPS:
2628 return DAG.getNode(Opc, dl, VT, V1, V2,
2629 DAG.getConstant(TargetMask, MVT::i8));
2630 }
2631 return SDValue();
2632}
2633
2634static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2635 SDValue V1, SDValue V2, SelectionDAG &DAG) {
2636 switch(Opc) {
2637 default: llvm_unreachable("Unknown x86 shuffle node");
2638 case X86ISD::MOVLHPS:
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00002639 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002640 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002641 case X86ISD::MOVLPS:
2642 case X86ISD::MOVLPD:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002643 case X86ISD::MOVSS:
2644 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002645 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002646 case X86ISD::UNPCKLPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002647 case X86ISD::PUNPCKLWD:
2648 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002649 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002650 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002651 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002652 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002653 case X86ISD::PUNPCKHWD:
2654 case X86ISD::PUNPCKHBW:
2655 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002656 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002657 return DAG.getNode(Opc, dl, VT, V1, V2);
2658 }
2659 return SDValue();
2660}
2661
Dan Gohmand858e902010-04-17 15:26:15 +00002662SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002663 MachineFunction &MF = DAG.getMachineFunction();
2664 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2665 int ReturnAddrIndex = FuncInfo->getRAIndex();
2666
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002667 if (ReturnAddrIndex == 0) {
2668 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002669 uint64_t SlotSize = TD->getPointerSize();
David Greene3f2bf852009-11-12 20:49:22 +00002670 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00002671 false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002672 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002673 }
2674
Evan Cheng25ab6902006-09-08 06:48:29 +00002675 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002676}
2677
2678
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002679bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
2680 bool hasSymbolicDisplacement) {
2681 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00002682 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002683 return false;
2684
2685 // If we don't have a symbolic displacement - we don't have any extra
2686 // restrictions.
2687 if (!hasSymbolicDisplacement)
2688 return true;
2689
2690 // FIXME: Some tweaks might be needed for medium code model.
2691 if (M != CodeModel::Small && M != CodeModel::Kernel)
2692 return false;
2693
2694 // For small code model we assume that latest object is 16MB before end of 31
2695 // bits boundary. We may also accept pretty large negative constants knowing
2696 // that all objects are in the positive half of address space.
2697 if (M == CodeModel::Small && Offset < 16*1024*1024)
2698 return true;
2699
2700 // For kernel code model we know that all object resist in the negative half
2701 // of 32bits address space. We may not accept negative offsets, since they may
2702 // be just off and we may accept pretty large positive ones.
2703 if (M == CodeModel::Kernel && Offset > 0)
2704 return true;
2705
2706 return false;
2707}
2708
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002709/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
2710/// specific condition code, returning the condition code and the LHS/RHS of the
2711/// comparison to make.
2712static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2713 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00002714 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002715 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2716 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2717 // X > -1 -> X == 0, jump !sign.
2718 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002719 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002720 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2721 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002722 return X86::COND_S;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002723 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00002724 // X < 1 -> X <= 0
2725 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002726 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002727 }
Chris Lattnerf9570512006-09-13 03:22:10 +00002728 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002729
Evan Chengd9558e02006-01-06 00:43:03 +00002730 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002731 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002732 case ISD::SETEQ: return X86::COND_E;
2733 case ISD::SETGT: return X86::COND_G;
2734 case ISD::SETGE: return X86::COND_GE;
2735 case ISD::SETLT: return X86::COND_L;
2736 case ISD::SETLE: return X86::COND_LE;
2737 case ISD::SETNE: return X86::COND_NE;
2738 case ISD::SETULT: return X86::COND_B;
2739 case ISD::SETUGT: return X86::COND_A;
2740 case ISD::SETULE: return X86::COND_BE;
2741 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00002742 }
Chris Lattner4c78e022008-12-23 23:42:27 +00002743 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002744
Chris Lattner4c78e022008-12-23 23:42:27 +00002745 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00002746
Chris Lattner4c78e022008-12-23 23:42:27 +00002747 // If LHS is a foldable load, but RHS is not, flip the condition.
2748 if ((ISD::isNON_EXTLoad(LHS.getNode()) && LHS.hasOneUse()) &&
2749 !(ISD::isNON_EXTLoad(RHS.getNode()) && RHS.hasOneUse())) {
2750 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
2751 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00002752 }
2753
Chris Lattner4c78e022008-12-23 23:42:27 +00002754 switch (SetCCOpcode) {
2755 default: break;
2756 case ISD::SETOLT:
2757 case ISD::SETOLE:
2758 case ISD::SETUGT:
2759 case ISD::SETUGE:
2760 std::swap(LHS, RHS);
2761 break;
2762 }
2763
2764 // On a floating point condition, the flags are set as follows:
2765 // ZF PF CF op
2766 // 0 | 0 | 0 | X > Y
2767 // 0 | 0 | 1 | X < Y
2768 // 1 | 0 | 0 | X == Y
2769 // 1 | 1 | 1 | unordered
2770 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002771 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00002772 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002773 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00002774 case ISD::SETOLT: // flipped
2775 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002776 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00002777 case ISD::SETOLE: // flipped
2778 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002779 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002780 case ISD::SETUGT: // flipped
2781 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002782 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00002783 case ISD::SETUGE: // flipped
2784 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002785 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002786 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002787 case ISD::SETNE: return X86::COND_NE;
2788 case ISD::SETUO: return X86::COND_P;
2789 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00002790 case ISD::SETOEQ:
2791 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00002792 }
Evan Chengd9558e02006-01-06 00:43:03 +00002793}
2794
Evan Cheng4a460802006-01-11 00:33:36 +00002795/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2796/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00002797/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00002798static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00002799 switch (X86CC) {
2800 default:
2801 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00002802 case X86::COND_B:
2803 case X86::COND_BE:
2804 case X86::COND_E:
2805 case X86::COND_P:
2806 case X86::COND_A:
2807 case X86::COND_AE:
2808 case X86::COND_NE:
2809 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00002810 return true;
2811 }
2812}
2813
Evan Chengeb2f9692009-10-27 19:56:55 +00002814/// isFPImmLegal - Returns true if the target can instruction select the
2815/// specified FP immediate natively. If false, the legalizer will
2816/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00002817bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00002818 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
2819 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
2820 return true;
2821 }
2822 return false;
2823}
2824
Nate Begeman9008ca62009-04-27 18:41:29 +00002825/// isUndefOrInRange - Return true if Val is undef or if its value falls within
2826/// the specified range (L, H].
2827static bool isUndefOrInRange(int Val, int Low, int Hi) {
2828 return (Val < 0) || (Val >= Low && Val < Hi);
2829}
2830
2831/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
2832/// specified value.
2833static bool isUndefOrEqual(int Val, int CmpVal) {
2834 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00002835 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00002836 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00002837}
2838
Nate Begeman9008ca62009-04-27 18:41:29 +00002839/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
2840/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
2841/// the second operand.
Owen Andersone50ed302009-08-10 22:56:29 +00002842static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00002843 if (VT == MVT::v4f32 || VT == MVT::v4i32 )
Nate Begeman9008ca62009-04-27 18:41:29 +00002844 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002845 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00002846 return (Mask[0] < 2 && Mask[1] < 2);
2847 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002848}
2849
Nate Begeman9008ca62009-04-27 18:41:29 +00002850bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002851 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002852 N->getMask(M);
2853 return ::isPSHUFDMask(M, N->getValueType(0));
2854}
Evan Cheng0188ecb2006-03-22 18:59:22 +00002855
Nate Begeman9008ca62009-04-27 18:41:29 +00002856/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
2857/// is suitable for input to PSHUFHW.
Owen Andersone50ed302009-08-10 22:56:29 +00002858static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002859 if (VT != MVT::v8i16)
Evan Cheng0188ecb2006-03-22 18:59:22 +00002860 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002861
Nate Begeman9008ca62009-04-27 18:41:29 +00002862 // Lower quadword copied in order or undef.
2863 for (int i = 0; i != 4; ++i)
2864 if (Mask[i] >= 0 && Mask[i] != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00002865 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002866
Evan Cheng506d3df2006-03-29 23:07:14 +00002867 // Upper quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002868 for (int i = 4; i != 8; ++i)
2869 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Evan Cheng506d3df2006-03-29 23:07:14 +00002870 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002871
Evan Cheng506d3df2006-03-29 23:07:14 +00002872 return true;
2873}
2874
Nate Begeman9008ca62009-04-27 18:41:29 +00002875bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002876 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002877 N->getMask(M);
2878 return ::isPSHUFHWMask(M, N->getValueType(0));
2879}
Evan Cheng506d3df2006-03-29 23:07:14 +00002880
Nate Begeman9008ca62009-04-27 18:41:29 +00002881/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
2882/// is suitable for input to PSHUFLW.
Owen Andersone50ed302009-08-10 22:56:29 +00002883static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002884 if (VT != MVT::v8i16)
Evan Cheng506d3df2006-03-29 23:07:14 +00002885 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002886
Rafael Espindola15684b22009-04-24 12:40:33 +00002887 // Upper quadword copied in order.
Nate Begeman9008ca62009-04-27 18:41:29 +00002888 for (int i = 4; i != 8; ++i)
2889 if (Mask[i] >= 0 && Mask[i] != i)
Rafael Espindola15684b22009-04-24 12:40:33 +00002890 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002891
Rafael Espindola15684b22009-04-24 12:40:33 +00002892 // Lower quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002893 for (int i = 0; i != 4; ++i)
2894 if (Mask[i] >= 4)
Rafael Espindola15684b22009-04-24 12:40:33 +00002895 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002896
Rafael Espindola15684b22009-04-24 12:40:33 +00002897 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002898}
2899
Nate Begeman9008ca62009-04-27 18:41:29 +00002900bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002901 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002902 N->getMask(M);
2903 return ::isPSHUFLWMask(M, N->getValueType(0));
2904}
2905
Nate Begemana09008b2009-10-19 02:17:23 +00002906/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
2907/// is suitable for input to PALIGNR.
2908static bool isPALIGNRMask(const SmallVectorImpl<int> &Mask, EVT VT,
2909 bool hasSSSE3) {
2910 int i, e = VT.getVectorNumElements();
Michael J. Spencerec38de22010-10-10 22:04:20 +00002911
Nate Begemana09008b2009-10-19 02:17:23 +00002912 // Do not handle v2i64 / v2f64 shuffles with palignr.
2913 if (e < 4 || !hasSSSE3)
2914 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00002915
Nate Begemana09008b2009-10-19 02:17:23 +00002916 for (i = 0; i != e; ++i)
2917 if (Mask[i] >= 0)
2918 break;
Michael J. Spencerec38de22010-10-10 22:04:20 +00002919
Nate Begemana09008b2009-10-19 02:17:23 +00002920 // All undef, not a palignr.
2921 if (i == e)
2922 return false;
2923
2924 // Determine if it's ok to perform a palignr with only the LHS, since we
2925 // don't have access to the actual shuffle elements to see if RHS is undef.
2926 bool Unary = Mask[i] < (int)e;
2927 bool NeedsUnary = false;
2928
2929 int s = Mask[i] - i;
Michael J. Spencerec38de22010-10-10 22:04:20 +00002930
Nate Begemana09008b2009-10-19 02:17:23 +00002931 // Check the rest of the elements to see if they are consecutive.
2932 for (++i; i != e; ++i) {
2933 int m = Mask[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00002934 if (m < 0)
Nate Begemana09008b2009-10-19 02:17:23 +00002935 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00002936
Nate Begemana09008b2009-10-19 02:17:23 +00002937 Unary = Unary && (m < (int)e);
2938 NeedsUnary = NeedsUnary || (m < s);
2939
2940 if (NeedsUnary && !Unary)
2941 return false;
2942 if (Unary && m != ((s+i) & (e-1)))
2943 return false;
2944 if (!Unary && m != (s+i))
2945 return false;
2946 }
2947 return true;
2948}
2949
2950bool X86::isPALIGNRMask(ShuffleVectorSDNode *N) {
2951 SmallVector<int, 8> M;
2952 N->getMask(M);
2953 return ::isPALIGNRMask(M, N->getValueType(0), true);
2954}
2955
Evan Cheng14aed5e2006-03-24 01:18:28 +00002956/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2957/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Owen Andersone50ed302009-08-10 22:56:29 +00002958static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002959 int NumElems = VT.getVectorNumElements();
2960 if (NumElems != 2 && NumElems != 4)
2961 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002962
Nate Begeman9008ca62009-04-27 18:41:29 +00002963 int Half = NumElems / 2;
2964 for (int i = 0; i < Half; ++i)
2965 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002966 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002967 for (int i = Half; i < NumElems; ++i)
2968 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002969 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002970
Evan Cheng14aed5e2006-03-24 01:18:28 +00002971 return true;
2972}
2973
Nate Begeman9008ca62009-04-27 18:41:29 +00002974bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
2975 SmallVector<int, 8> M;
2976 N->getMask(M);
2977 return ::isSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002978}
2979
Evan Cheng213d2cf2007-05-17 18:45:50 +00002980/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00002981/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2982/// half elements to come from vector 1 (which would equal the dest.) and
2983/// the upper half to come from vector 2.
Owen Andersone50ed302009-08-10 22:56:29 +00002984static bool isCommutedSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002985 int NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00002986
2987 if (NumElems != 2 && NumElems != 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00002988 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002989
Nate Begeman9008ca62009-04-27 18:41:29 +00002990 int Half = NumElems / 2;
2991 for (int i = 0; i < Half; ++i)
2992 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002993 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002994 for (int i = Half; i < NumElems; ++i)
2995 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002996 return false;
2997 return true;
2998}
2999
Nate Begeman9008ca62009-04-27 18:41:29 +00003000static bool isCommutedSHUFP(ShuffleVectorSDNode *N) {
3001 SmallVector<int, 8> M;
3002 N->getMask(M);
3003 return isCommutedSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003004}
3005
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003006/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
3007/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00003008bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
3009 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003010 return false;
3011
Evan Cheng2064a2b2006-03-28 06:50:32 +00003012 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman9008ca62009-04-27 18:41:29 +00003013 return isUndefOrEqual(N->getMaskElt(0), 6) &&
3014 isUndefOrEqual(N->getMaskElt(1), 7) &&
3015 isUndefOrEqual(N->getMaskElt(2), 2) &&
3016 isUndefOrEqual(N->getMaskElt(3), 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00003017}
3018
Nate Begeman0b10b912009-11-07 23:17:15 +00003019/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
3020/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
3021/// <2, 3, 2, 3>
3022bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
3023 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Michael J. Spencerec38de22010-10-10 22:04:20 +00003024
Nate Begeman0b10b912009-11-07 23:17:15 +00003025 if (NumElems != 4)
3026 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003027
Nate Begeman0b10b912009-11-07 23:17:15 +00003028 return isUndefOrEqual(N->getMaskElt(0), 2) &&
3029 isUndefOrEqual(N->getMaskElt(1), 3) &&
3030 isUndefOrEqual(N->getMaskElt(2), 2) &&
3031 isUndefOrEqual(N->getMaskElt(3), 3);
3032}
3033
Evan Cheng5ced1d82006-04-06 23:23:56 +00003034/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
3035/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman9008ca62009-04-27 18:41:29 +00003036bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
3037 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003038
Evan Cheng5ced1d82006-04-06 23:23:56 +00003039 if (NumElems != 2 && NumElems != 4)
3040 return false;
3041
Evan Chengc5cdff22006-04-07 21:53:05 +00003042 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003043 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003044 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003045
Evan Chengc5cdff22006-04-07 21:53:05 +00003046 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003047 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003048 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003049
3050 return true;
3051}
3052
Nate Begeman0b10b912009-11-07 23:17:15 +00003053/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
3054/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
3055bool X86::isMOVLHPSMask(ShuffleVectorSDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003056 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003057
Evan Cheng5ced1d82006-04-06 23:23:56 +00003058 if (NumElems != 2 && NumElems != 4)
3059 return false;
3060
Evan Chengc5cdff22006-04-07 21:53:05 +00003061 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003062 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003063 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003064
Nate Begeman9008ca62009-04-27 18:41:29 +00003065 for (unsigned i = 0; i < NumElems/2; ++i)
3066 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003067 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003068
3069 return true;
3070}
3071
Evan Cheng0038e592006-03-28 00:39:58 +00003072/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
3073/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Owen Andersone50ed302009-08-10 22:56:29 +00003074static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003075 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003076 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003077 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng0038e592006-03-28 00:39:58 +00003078 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003079
Nate Begeman9008ca62009-04-27 18:41:29 +00003080 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
3081 int BitI = Mask[i];
3082 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00003083 if (!isUndefOrEqual(BitI, j))
3084 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00003085 if (V2IsSplat) {
Mon P Wang7bcaefa2009-02-04 01:16:59 +00003086 if (!isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003087 return false;
3088 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00003089 if (!isUndefOrEqual(BitI1, j + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003090 return false;
3091 }
Evan Cheng0038e592006-03-28 00:39:58 +00003092 }
Evan Cheng0038e592006-03-28 00:39:58 +00003093 return true;
3094}
3095
Nate Begeman9008ca62009-04-27 18:41:29 +00003096bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3097 SmallVector<int, 8> M;
3098 N->getMask(M);
3099 return ::isUNPCKLMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003100}
3101
Evan Cheng4fcb9222006-03-28 02:43:26 +00003102/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3103/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Eric Christopherfd179292009-08-27 18:07:15 +00003104static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003105 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003106 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003107 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng4fcb9222006-03-28 02:43:26 +00003108 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003109
Nate Begeman9008ca62009-04-27 18:41:29 +00003110 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
3111 int BitI = Mask[i];
3112 int BitI1 = Mask[i+1];
Chris Lattner5a88b832007-02-25 07:10:00 +00003113 if (!isUndefOrEqual(BitI, j + NumElts/2))
Evan Chengc5cdff22006-04-07 21:53:05 +00003114 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00003115 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00003116 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003117 return false;
3118 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00003119 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003120 return false;
3121 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003122 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003123 return true;
3124}
3125
Nate Begeman9008ca62009-04-27 18:41:29 +00003126bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3127 SmallVector<int, 8> M;
3128 N->getMask(M);
3129 return ::isUNPCKHMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003130}
3131
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003132/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3133/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3134/// <0, 0, 1, 1>
Owen Andersone50ed302009-08-10 22:56:29 +00003135static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003136 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003137 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003138 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003139
Nate Begeman9008ca62009-04-27 18:41:29 +00003140 for (int i = 0, j = 0; i != NumElems; i += 2, ++j) {
3141 int BitI = Mask[i];
3142 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00003143 if (!isUndefOrEqual(BitI, j))
3144 return false;
3145 if (!isUndefOrEqual(BitI1, j))
3146 return false;
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003147 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003148 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003149}
3150
Nate Begeman9008ca62009-04-27 18:41:29 +00003151bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
3152 SmallVector<int, 8> M;
3153 N->getMask(M);
3154 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
3155}
3156
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003157/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3158/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3159/// <2, 2, 3, 3>
Owen Andersone50ed302009-08-10 22:56:29 +00003160static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003161 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003162 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
3163 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003164
Nate Begeman9008ca62009-04-27 18:41:29 +00003165 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
3166 int BitI = Mask[i];
3167 int BitI1 = Mask[i+1];
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003168 if (!isUndefOrEqual(BitI, j))
3169 return false;
3170 if (!isUndefOrEqual(BitI1, j))
3171 return false;
3172 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003173 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003174}
3175
Nate Begeman9008ca62009-04-27 18:41:29 +00003176bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
3177 SmallVector<int, 8> M;
3178 N->getMask(M);
3179 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
3180}
3181
Evan Cheng017dcc62006-04-21 01:05:10 +00003182/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3183/// specifies a shuffle of elements that is suitable for input to MOVSS,
3184/// MOVSD, and MOVD, i.e. setting the lowest element.
Owen Andersone50ed302009-08-10 22:56:29 +00003185static bool isMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003186 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003187 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003188
3189 int NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003190
Nate Begeman9008ca62009-04-27 18:41:29 +00003191 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003192 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003193
Nate Begeman9008ca62009-04-27 18:41:29 +00003194 for (int i = 1; i < NumElts; ++i)
3195 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003196 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003197
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003198 return true;
3199}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003200
Nate Begeman9008ca62009-04-27 18:41:29 +00003201bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
3202 SmallVector<int, 8> M;
3203 N->getMask(M);
3204 return ::isMOVLMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003205}
3206
Evan Cheng017dcc62006-04-21 01:05:10 +00003207/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
3208/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003209/// element of vector 2 and the other elements to come from vector 1 in order.
Owen Andersone50ed302009-08-10 22:56:29 +00003210static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003211 bool V2IsSplat = false, bool V2IsUndef = false) {
3212 int NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003213 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003214 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003215
Nate Begeman9008ca62009-04-27 18:41:29 +00003216 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003217 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003218
Nate Begeman9008ca62009-04-27 18:41:29 +00003219 for (int i = 1; i < NumOps; ++i)
3220 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3221 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3222 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003223 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003224
Evan Cheng39623da2006-04-20 08:58:49 +00003225 return true;
3226}
3227
Nate Begeman9008ca62009-04-27 18:41:29 +00003228static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00003229 bool V2IsUndef = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003230 SmallVector<int, 8> M;
3231 N->getMask(M);
3232 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00003233}
3234
Evan Chengd9539472006-04-14 21:59:03 +00003235/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3236/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003237bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N) {
3238 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003239 return false;
3240
3241 // Expect 1, 1, 3, 3
Rafael Espindola15684b22009-04-24 12:40:33 +00003242 for (unsigned i = 0; i < 2; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003243 int Elt = N->getMaskElt(i);
3244 if (Elt >= 0 && Elt != 1)
3245 return false;
Rafael Espindola15684b22009-04-24 12:40:33 +00003246 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003247
3248 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003249 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003250 int Elt = N->getMaskElt(i);
3251 if (Elt >= 0 && Elt != 3)
3252 return false;
3253 if (Elt == 3)
3254 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003255 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003256 // Don't use movshdup if it can be done with a shufps.
Nate Begeman9008ca62009-04-27 18:41:29 +00003257 // FIXME: verify that matching u, u, 3, 3 is what we want.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003258 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003259}
3260
3261/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3262/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003263bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N) {
3264 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003265 return false;
3266
3267 // Expect 0, 0, 2, 2
Nate Begeman9008ca62009-04-27 18:41:29 +00003268 for (unsigned i = 0; i < 2; ++i)
3269 if (N->getMaskElt(i) > 0)
3270 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003271
3272 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003273 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003274 int Elt = N->getMaskElt(i);
3275 if (Elt >= 0 && Elt != 2)
3276 return false;
3277 if (Elt == 2)
3278 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003279 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003280 // Don't use movsldup if it can be done with a shufps.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003281 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003282}
3283
Evan Cheng0b457f02008-09-25 20:50:48 +00003284/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3285/// specifies a shuffle of elements that is suitable for input to MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003286bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
3287 int e = N->getValueType(0).getVectorNumElements() / 2;
Eric Christopherfd179292009-08-27 18:07:15 +00003288
Nate Begeman9008ca62009-04-27 18:41:29 +00003289 for (int i = 0; i < e; ++i)
3290 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003291 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003292 for (int i = 0; i < e; ++i)
3293 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003294 return false;
3295 return true;
3296}
3297
Evan Cheng63d33002006-03-22 08:01:21 +00003298/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003299/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Evan Cheng63d33002006-03-22 08:01:21 +00003300unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003301 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3302 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
3303
Evan Chengb9df0ca2006-03-22 02:53:00 +00003304 unsigned Shift = (NumOperands == 4) ? 2 : 1;
3305 unsigned Mask = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00003306 for (int i = 0; i < NumOperands; ++i) {
3307 int Val = SVOp->getMaskElt(NumOperands-i-1);
3308 if (Val < 0) Val = 0;
Evan Cheng14aed5e2006-03-24 01:18:28 +00003309 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00003310 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00003311 if (i != NumOperands - 1)
3312 Mask <<= Shift;
3313 }
Evan Cheng63d33002006-03-22 08:01:21 +00003314 return Mask;
3315}
3316
Evan Cheng506d3df2006-03-29 23:07:14 +00003317/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003318/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003319unsigned X86::getShufflePSHUFHWImmediate(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 last 4.
3323 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003324 int Val = SVOp->getMaskElt(i);
3325 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00003326 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00003327 if (i != 4)
3328 Mask <<= 2;
3329 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003330 return Mask;
3331}
3332
3333/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003334/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003335unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003336 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003337 unsigned Mask = 0;
3338 // 8 nodes, but we only care about the first 4.
3339 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003340 int Val = SVOp->getMaskElt(i);
3341 if (Val >= 0)
3342 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00003343 if (i != 0)
3344 Mask <<= 2;
3345 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003346 return Mask;
3347}
3348
Nate Begemana09008b2009-10-19 02:17:23 +00003349/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
3350/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
3351unsigned X86::getShufflePALIGNRImmediate(SDNode *N) {
3352 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3353 EVT VVT = N->getValueType(0);
3354 unsigned EltSize = VVT.getVectorElementType().getSizeInBits() >> 3;
3355 int Val = 0;
3356
3357 unsigned i, e;
3358 for (i = 0, e = VVT.getVectorNumElements(); i != e; ++i) {
3359 Val = SVOp->getMaskElt(i);
3360 if (Val >= 0)
3361 break;
3362 }
3363 return (Val - i) * EltSize;
3364}
3365
Evan Cheng37b73872009-07-30 08:33:02 +00003366/// isZeroNode - Returns true if Elt is a constant zero or a floating point
3367/// constant +0.0.
3368bool X86::isZeroNode(SDValue Elt) {
3369 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmane368b462010-06-18 14:22:04 +00003370 cast<ConstantSDNode>(Elt)->isNullValue()) ||
Evan Cheng37b73872009-07-30 08:33:02 +00003371 (isa<ConstantFPSDNode>(Elt) &&
3372 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
3373}
3374
Nate Begeman9008ca62009-04-27 18:41:29 +00003375/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
3376/// their permute mask.
3377static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
3378 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003379 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003380 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00003381 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00003382
Nate Begeman5a5ca152009-04-29 05:20:52 +00003383 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003384 int idx = SVOp->getMaskElt(i);
3385 if (idx < 0)
3386 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003387 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003388 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003389 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003390 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003391 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003392 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
3393 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003394}
3395
Evan Cheng779ccea2007-12-07 21:30:01 +00003396/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3397/// the two vector operands have swapped position.
Owen Andersone50ed302009-08-10 22:56:29 +00003398static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00003399 unsigned NumElems = VT.getVectorNumElements();
3400 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003401 int idx = Mask[i];
3402 if (idx < 0)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003403 continue;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003404 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003405 Mask[i] = idx + NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003406 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003407 Mask[i] = idx - NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003408 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003409}
3410
Evan Cheng533a0aa2006-04-19 20:35:22 +00003411/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
3412/// match movhlps. The lower half elements should come from upper half of
3413/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003414/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00003415static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
3416 if (Op->getValueType(0).getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00003417 return false;
3418 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003419 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003420 return false;
3421 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003422 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003423 return false;
3424 return true;
3425}
3426
Evan Cheng5ced1d82006-04-06 23:23:56 +00003427/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00003428/// is promoted to a vector. It also returns the LoadSDNode by reference if
3429/// required.
3430static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00003431 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
3432 return false;
3433 N = N->getOperand(0).getNode();
3434 if (!ISD::isNON_EXTLoad(N))
3435 return false;
3436 if (LD)
3437 *LD = cast<LoadSDNode>(N);
3438 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003439}
3440
Evan Cheng533a0aa2006-04-19 20:35:22 +00003441/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
3442/// match movlp{s|d}. The lower half elements should come from lower half of
3443/// V1 (and in order), and the upper half elements should come from the upper
3444/// half of V2 (and in order). And since V1 will become the source of the
3445/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003446static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
3447 ShuffleVectorSDNode *Op) {
Evan Cheng466685d2006-10-09 20:57:25 +00003448 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003449 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00003450 // Is V2 is a vector load, don't do this transformation. We will try to use
3451 // load folding shufps op.
3452 if (ISD::isNON_EXTLoad(V2))
3453 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003454
Nate Begeman5a5ca152009-04-29 05:20:52 +00003455 unsigned NumElems = Op->getValueType(0).getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003456
Evan Cheng533a0aa2006-04-19 20:35:22 +00003457 if (NumElems != 2 && NumElems != 4)
3458 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003459 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003460 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003461 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003462 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003463 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003464 return false;
3465 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003466}
3467
Evan Cheng39623da2006-04-20 08:58:49 +00003468/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
3469/// all the same.
3470static bool isSplatVector(SDNode *N) {
3471 if (N->getOpcode() != ISD::BUILD_VECTOR)
3472 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003473
Dan Gohman475871a2008-07-27 21:46:04 +00003474 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00003475 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
3476 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003477 return false;
3478 return true;
3479}
3480
Evan Cheng213d2cf2007-05-17 18:45:50 +00003481/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00003482/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00003483/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00003484static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00003485 SDValue V1 = N->getOperand(0);
3486 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003487 unsigned NumElems = N->getValueType(0).getVectorNumElements();
3488 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003489 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003490 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003491 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00003492 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
3493 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003494 if (Opc != ISD::BUILD_VECTOR ||
3495 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00003496 return false;
3497 } else if (Idx >= 0) {
3498 unsigned Opc = V1.getOpcode();
3499 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
3500 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003501 if (Opc != ISD::BUILD_VECTOR ||
3502 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00003503 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00003504 }
3505 }
3506 return true;
3507}
3508
3509/// getZeroVector - Returns a vector of specified type with all zero elements.
3510///
Owen Andersone50ed302009-08-10 22:56:29 +00003511static SDValue getZeroVector(EVT VT, bool HasSSE2, SelectionDAG &DAG,
Dale Johannesenace16102009-02-03 19:33:06 +00003512 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003513 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003514
Dale Johannesen0488fb62010-09-30 23:57:10 +00003515 // Always build SSE zero vectors as <4 x i32> bitcasted
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003516 // to their dest type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00003517 SDValue Vec;
Dale Johannesen0488fb62010-09-30 23:57:10 +00003518 if (VT.getSizeInBits() == 128) { // SSE
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003519 if (HasSSE2) { // SSE2
3520 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3521 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
3522 } else { // SSE1
3523 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
3524 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
3525 }
3526 } else if (VT.getSizeInBits() == 256) { // AVX
3527 // 256-bit logic and arithmetic instructions in AVX are
3528 // all floating-point, no support for integer ops. Default
3529 // to emitting fp zeroed vectors then.
Owen Anderson825b72b2009-08-11 20:47:22 +00003530 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003531 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
3532 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops, 8);
Evan Chengf0df0312008-05-15 08:39:06 +00003533 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003534 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00003535}
3536
Chris Lattner8a594482007-11-25 00:24:49 +00003537/// getOnesVector - Returns a vector of specified type with all bits set.
3538///
Owen Andersone50ed302009-08-10 22:56:29 +00003539static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003540 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003541
Chris Lattner8a594482007-11-25 00:24:49 +00003542 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
3543 // type. This ensures they get CSE'd.
Owen Anderson825b72b2009-08-11 20:47:22 +00003544 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00003545 SDValue Vec;
Dale Johannesen0488fb62010-09-30 23:57:10 +00003546 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003547 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00003548}
3549
3550
Evan Cheng39623da2006-04-20 08:58:49 +00003551/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
3552/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00003553static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003554 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003555 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003556
Evan Cheng39623da2006-04-20 08:58:49 +00003557 bool Changed = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003558 SmallVector<int, 8> MaskVec;
3559 SVOp->getMask(MaskVec);
Eric Christopherfd179292009-08-27 18:07:15 +00003560
Nate Begeman5a5ca152009-04-29 05:20:52 +00003561 for (unsigned i = 0; i != NumElems; ++i) {
3562 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003563 MaskVec[i] = NumElems;
3564 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00003565 }
Evan Cheng39623da2006-04-20 08:58:49 +00003566 }
Evan Cheng39623da2006-04-20 08:58:49 +00003567 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00003568 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
3569 SVOp->getOperand(1), &MaskVec[0]);
3570 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00003571}
3572
Evan Cheng017dcc62006-04-21 01:05:10 +00003573/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
3574/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00003575static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003576 SDValue V2) {
3577 unsigned NumElems = VT.getVectorNumElements();
3578 SmallVector<int, 8> Mask;
3579 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00003580 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003581 Mask.push_back(i);
3582 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00003583}
3584
Nate Begeman9008ca62009-04-27 18:41:29 +00003585/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003586static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003587 SDValue V2) {
3588 unsigned NumElems = VT.getVectorNumElements();
3589 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00003590 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003591 Mask.push_back(i);
3592 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00003593 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003594 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00003595}
3596
Nate Begeman9008ca62009-04-27 18:41:29 +00003597/// getUnpackhMask - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003598static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003599 SDValue V2) {
3600 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00003601 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00003602 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00003603 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003604 Mask.push_back(i + Half);
3605 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00003606 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003607 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00003608}
3609
Bruno Cardoso Lopesbb0a9482010-08-13 17:50:47 +00003610/// PromoteSplat - Promote a splat of v4i32, v8i16 or v16i8 to v4f32.
3611static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003612 EVT PVT = MVT::v4f32;
Owen Andersone50ed302009-08-10 22:56:29 +00003613 EVT VT = SV->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00003614 DebugLoc dl = SV->getDebugLoc();
3615 SDValue V1 = SV->getOperand(0);
3616 int NumElems = VT.getVectorNumElements();
3617 int EltNo = SV->getSplatIndex();
Rafael Espindola15684b22009-04-24 12:40:33 +00003618
Nate Begeman9008ca62009-04-27 18:41:29 +00003619 // unpack elements to the correct location
3620 while (NumElems > 4) {
3621 if (EltNo < NumElems/2) {
3622 V1 = getUnpackl(DAG, dl, VT, V1, V1);
3623 } else {
3624 V1 = getUnpackh(DAG, dl, VT, V1, V1);
3625 EltNo -= NumElems/2;
3626 }
3627 NumElems >>= 1;
3628 }
Eric Christopherfd179292009-08-27 18:07:15 +00003629
Nate Begeman9008ca62009-04-27 18:41:29 +00003630 // Perform the splat.
3631 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003632 V1 = DAG.getNode(ISD::BITCAST, dl, PVT, V1);
Nate Begeman9008ca62009-04-27 18:41:29 +00003633 V1 = DAG.getVectorShuffle(PVT, dl, V1, DAG.getUNDEF(PVT), &SplatMask[0]);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003634 return DAG.getNode(ISD::BITCAST, dl, VT, V1);
Evan Chengc575ca22006-04-17 20:43:08 +00003635}
3636
Evan Chengba05f722006-04-21 23:03:30 +00003637/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00003638/// vector of zero or undef vector. This produces a shuffle where the low
3639/// element of V2 is swizzled into the zero/undef vector, landing at element
3640/// 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 +00003641static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Chengf0df0312008-05-15 08:39:06 +00003642 bool isZero, bool HasSSE2,
3643 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003644 EVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00003645 SDValue V1 = isZero
Nate Begeman9008ca62009-04-27 18:41:29 +00003646 ? getZeroVector(VT, HasSSE2, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
3647 unsigned NumElems = VT.getVectorNumElements();
3648 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00003649 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003650 // If this is the insertion idx, put the low elt of V2 here.
3651 MaskVec.push_back(i == Idx ? NumElems : i);
3652 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00003653}
3654
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003655/// getShuffleScalarElt - Returns the scalar element that will make up the ith
3656/// element of the result of the vector shuffle.
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003657SDValue getShuffleScalarElt(SDNode *N, int Index, SelectionDAG &DAG,
3658 unsigned Depth) {
3659 if (Depth == 6)
3660 return SDValue(); // Limit search depth.
3661
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003662 SDValue V = SDValue(N, 0);
3663 EVT VT = V.getValueType();
3664 unsigned Opcode = V.getOpcode();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003665
3666 // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
3667 if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
3668 Index = SV->getMaskElt(Index);
3669
3670 if (Index < 0)
3671 return DAG.getUNDEF(VT.getVectorElementType());
3672
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003673 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003674 SDValue NewV = (Index < NumElems) ? SV->getOperand(0) : SV->getOperand(1);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003675 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG, Depth+1);
Evan Chengf26ffe92008-05-29 08:22:04 +00003676 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003677
3678 // Recurse into target specific vector shuffles to find scalars.
3679 if (isTargetShuffle(Opcode)) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003680 int NumElems = VT.getVectorNumElements();
3681 SmallVector<unsigned, 16> ShuffleMask;
3682 SDValue ImmN;
3683
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003684 switch(Opcode) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003685 case X86ISD::SHUFPS:
3686 case X86ISD::SHUFPD:
3687 ImmN = N->getOperand(N->getNumOperands()-1);
3688 DecodeSHUFPSMask(NumElems,
3689 cast<ConstantSDNode>(ImmN)->getZExtValue(),
3690 ShuffleMask);
3691 break;
3692 case X86ISD::PUNPCKHBW:
3693 case X86ISD::PUNPCKHWD:
3694 case X86ISD::PUNPCKHDQ:
3695 case X86ISD::PUNPCKHQDQ:
3696 DecodePUNPCKHMask(NumElems, ShuffleMask);
3697 break;
3698 case X86ISD::UNPCKHPS:
3699 case X86ISD::UNPCKHPD:
3700 DecodeUNPCKHPMask(NumElems, ShuffleMask);
3701 break;
3702 case X86ISD::PUNPCKLBW:
3703 case X86ISD::PUNPCKLWD:
3704 case X86ISD::PUNPCKLDQ:
3705 case X86ISD::PUNPCKLQDQ:
3706 DecodePUNPCKLMask(NumElems, ShuffleMask);
3707 break;
3708 case X86ISD::UNPCKLPS:
3709 case X86ISD::UNPCKLPD:
3710 DecodeUNPCKLPMask(NumElems, ShuffleMask);
3711 break;
3712 case X86ISD::MOVHLPS:
3713 DecodeMOVHLPSMask(NumElems, ShuffleMask);
3714 break;
3715 case X86ISD::MOVLHPS:
3716 DecodeMOVLHPSMask(NumElems, ShuffleMask);
3717 break;
3718 case X86ISD::PSHUFD:
3719 ImmN = N->getOperand(N->getNumOperands()-1);
3720 DecodePSHUFMask(NumElems,
3721 cast<ConstantSDNode>(ImmN)->getZExtValue(),
3722 ShuffleMask);
3723 break;
3724 case X86ISD::PSHUFHW:
3725 ImmN = N->getOperand(N->getNumOperands()-1);
3726 DecodePSHUFHWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
3727 ShuffleMask);
3728 break;
3729 case X86ISD::PSHUFLW:
3730 ImmN = N->getOperand(N->getNumOperands()-1);
3731 DecodePSHUFLWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
3732 ShuffleMask);
3733 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003734 case X86ISD::MOVSS:
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00003735 case X86ISD::MOVSD: {
3736 // The index 0 always comes from the first element of the second source,
3737 // this is why MOVSS and MOVSD are used in the first place. The other
3738 // elements come from the other positions of the first source vector.
3739 unsigned OpNum = (Index == 0) ? 1 : 0;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003740 return getShuffleScalarElt(V.getOperand(OpNum).getNode(), Index, DAG,
3741 Depth+1);
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00003742 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003743 default:
3744 assert("not implemented for target shuffle node");
3745 return SDValue();
3746 }
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003747
3748 Index = ShuffleMask[Index];
3749 if (Index < 0)
3750 return DAG.getUNDEF(VT.getVectorElementType());
3751
3752 SDValue NewV = (Index < NumElems) ? N->getOperand(0) : N->getOperand(1);
3753 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG,
3754 Depth+1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003755 }
3756
3757 // Actual nodes that may contain scalar elements
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003758 if (Opcode == ISD::BITCAST) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003759 V = V.getOperand(0);
3760 EVT SrcVT = V.getValueType();
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003761 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003762
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003763 if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003764 return SDValue();
3765 }
3766
3767 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
3768 return (Index == 0) ? V.getOperand(0)
3769 : DAG.getUNDEF(VT.getVectorElementType());
3770
3771 if (V.getOpcode() == ISD::BUILD_VECTOR)
3772 return V.getOperand(Index);
3773
3774 return SDValue();
3775}
3776
3777/// getNumOfConsecutiveZeros - Return the number of elements of a vector
3778/// shuffle operation which come from a consecutively from a zero. The
3779/// search can start in two diferent directions, from left or right.
3780static
3781unsigned getNumOfConsecutiveZeros(SDNode *N, int NumElems,
3782 bool ZerosFromLeft, SelectionDAG &DAG) {
3783 int i = 0;
3784
3785 while (i < NumElems) {
3786 unsigned Index = ZerosFromLeft ? i : NumElems-i-1;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003787 SDValue Elt = getShuffleScalarElt(N, Index, DAG, 0);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003788 if (!(Elt.getNode() &&
3789 (Elt.getOpcode() == ISD::UNDEF || X86::isZeroNode(Elt))))
3790 break;
3791 ++i;
3792 }
3793
3794 return i;
3795}
3796
3797/// isShuffleMaskConsecutive - Check if the shuffle mask indicies from MaskI to
3798/// MaskE correspond consecutively to elements from one of the vector operands,
3799/// starting from its index OpIdx. Also tell OpNum which source vector operand.
3800static
3801bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp, int MaskI, int MaskE,
3802 int OpIdx, int NumElems, unsigned &OpNum) {
3803 bool SeenV1 = false;
3804 bool SeenV2 = false;
3805
3806 for (int i = MaskI; i <= MaskE; ++i, ++OpIdx) {
3807 int Idx = SVOp->getMaskElt(i);
3808 // Ignore undef indicies
3809 if (Idx < 0)
3810 continue;
3811
3812 if (Idx < NumElems)
3813 SeenV1 = true;
3814 else
3815 SeenV2 = true;
3816
3817 // Only accept consecutive elements from the same vector
3818 if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
3819 return false;
3820 }
3821
3822 OpNum = SeenV1 ? 0 : 1;
3823 return true;
3824}
3825
3826/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
3827/// logical left shift of a vector.
3828static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
3829 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
3830 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
3831 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
3832 false /* check zeros from right */, DAG);
3833 unsigned OpSrc;
3834
3835 if (!NumZeros)
3836 return false;
3837
3838 // Considering the elements in the mask that are not consecutive zeros,
3839 // check if they consecutively come from only one of the source vectors.
3840 //
3841 // V1 = {X, A, B, C} 0
3842 // \ \ \ /
3843 // vector_shuffle V1, V2 <1, 2, 3, X>
3844 //
3845 if (!isShuffleMaskConsecutive(SVOp,
3846 0, // Mask Start Index
3847 NumElems-NumZeros-1, // Mask End Index
3848 NumZeros, // Where to start looking in the src vector
3849 NumElems, // Number of elements in vector
3850 OpSrc)) // Which source operand ?
3851 return false;
3852
3853 isLeft = false;
3854 ShAmt = NumZeros;
3855 ShVal = SVOp->getOperand(OpSrc);
3856 return true;
3857}
3858
3859/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
3860/// logical left shift of a vector.
3861static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
3862 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
3863 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
3864 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
3865 true /* check zeros from left */, DAG);
3866 unsigned OpSrc;
3867
3868 if (!NumZeros)
3869 return false;
3870
3871 // Considering the elements in the mask that are not consecutive zeros,
3872 // check if they consecutively come from only one of the source vectors.
3873 //
3874 // 0 { A, B, X, X } = V2
3875 // / \ / /
3876 // vector_shuffle V1, V2 <X, X, 4, 5>
3877 //
3878 if (!isShuffleMaskConsecutive(SVOp,
3879 NumZeros, // Mask Start Index
3880 NumElems-1, // Mask End Index
3881 0, // Where to start looking in the src vector
3882 NumElems, // Number of elements in vector
3883 OpSrc)) // Which source operand ?
3884 return false;
3885
3886 isLeft = true;
3887 ShAmt = NumZeros;
3888 ShVal = SVOp->getOperand(OpSrc);
3889 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00003890}
3891
3892/// isVectorShift - Returns true if the shuffle can be implemented as a
3893/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003894static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00003895 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003896 if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
3897 isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
3898 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00003899
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003900 return false;
Evan Chengf26ffe92008-05-29 08:22:04 +00003901}
3902
Evan Chengc78d3b42006-04-24 18:01:45 +00003903/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
3904///
Dan Gohman475871a2008-07-27 21:46:04 +00003905static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00003906 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00003907 SelectionDAG &DAG,
3908 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003909 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00003910 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00003911
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003912 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003913 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003914 bool First = true;
3915 for (unsigned i = 0; i < 16; ++i) {
3916 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
3917 if (ThisIsNonZero && First) {
3918 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003919 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00003920 else
Owen Anderson825b72b2009-08-11 20:47:22 +00003921 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00003922 First = false;
3923 }
3924
3925 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00003926 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003927 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
3928 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003929 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003930 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00003931 }
3932 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003933 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
3934 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
3935 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00003936 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003937 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00003938 } else
3939 ThisElt = LastElt;
3940
Gabor Greifba36cb52008-08-28 21:40:38 +00003941 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00003942 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00003943 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00003944 }
3945 }
3946
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003947 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00003948}
3949
Bill Wendlinga348c562007-03-22 18:42:45 +00003950/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00003951///
Dan Gohman475871a2008-07-27 21:46:04 +00003952static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00003953 unsigned NumNonZero, unsigned NumZero,
3954 SelectionDAG &DAG,
3955 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003956 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00003957 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00003958
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003959 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003960 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003961 bool First = true;
3962 for (unsigned i = 0; i < 8; ++i) {
3963 bool isNonZero = (NonZeros & (1 << i)) != 0;
3964 if (isNonZero) {
3965 if (First) {
3966 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003967 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00003968 else
Owen Anderson825b72b2009-08-11 20:47:22 +00003969 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00003970 First = false;
3971 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003972 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003973 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00003974 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00003975 }
3976 }
3977
3978 return V;
3979}
3980
Evan Chengf26ffe92008-05-29 08:22:04 +00003981/// getVShift - Return a vector logical shift node.
3982///
Owen Andersone50ed302009-08-10 22:56:29 +00003983static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00003984 unsigned NumBits, SelectionDAG &DAG,
3985 const TargetLowering &TLI, DebugLoc dl) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00003986 EVT ShVT = MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00003987 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003988 SrcOp = DAG.getNode(ISD::BITCAST, dl, ShVT, SrcOp);
3989 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00003990 DAG.getNode(Opc, dl, ShVT, SrcOp,
Gabor Greif327ef032008-08-28 23:19:51 +00003991 DAG.getConstant(NumBits, TLI.getShiftAmountTy())));
Evan Chengf26ffe92008-05-29 08:22:04 +00003992}
3993
Dan Gohman475871a2008-07-27 21:46:04 +00003994SDValue
Evan Chengc3630942009-12-09 21:00:30 +00003995X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00003996 SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00003997
Evan Chengc3630942009-12-09 21:00:30 +00003998 // Check if the scalar load can be widened into a vector load. And if
3999 // the address is "base + cst" see if the cst can be "absorbed" into
4000 // the shuffle mask.
4001 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
4002 SDValue Ptr = LD->getBasePtr();
4003 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
4004 return SDValue();
4005 EVT PVT = LD->getValueType(0);
4006 if (PVT != MVT::i32 && PVT != MVT::f32)
4007 return SDValue();
4008
4009 int FI = -1;
4010 int64_t Offset = 0;
4011 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
4012 FI = FINode->getIndex();
4013 Offset = 0;
4014 } else if (Ptr.getOpcode() == ISD::ADD &&
4015 isa<ConstantSDNode>(Ptr.getOperand(1)) &&
4016 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4017 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4018 Offset = Ptr.getConstantOperandVal(1);
4019 Ptr = Ptr.getOperand(0);
4020 } else {
4021 return SDValue();
4022 }
4023
4024 SDValue Chain = LD->getChain();
4025 // Make sure the stack object alignment is at least 16.
4026 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
4027 if (DAG.InferPtrAlignment(Ptr) < 16) {
4028 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00004029 // Can't change the alignment. FIXME: It's possible to compute
4030 // the exact stack offset and reference FI + adjust offset instead.
4031 // If someone *really* cares about this. That's the way to implement it.
4032 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004033 } else {
4034 MFI->setObjectAlignment(FI, 16);
4035 }
4036 }
4037
4038 // (Offset % 16) must be multiple of 4. Then address is then
4039 // Ptr + (Offset & ~15).
4040 if (Offset < 0)
4041 return SDValue();
4042 if ((Offset % 16) & 3)
4043 return SDValue();
4044 int64_t StartOffset = Offset & ~15;
4045 if (StartOffset)
4046 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
4047 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
4048
4049 int EltNo = (Offset - StartOffset) >> 2;
4050 int Mask[4] = { EltNo, EltNo, EltNo, EltNo };
4051 EVT VT = (PVT == MVT::i32) ? MVT::v4i32 : MVT::v4f32;
Chris Lattner51abfe42010-09-21 06:02:19 +00004052 SDValue V1 = DAG.getLoad(VT, dl, Chain, Ptr,
4053 LD->getPointerInfo().getWithOffset(StartOffset),
David Greene67c9d422010-02-15 16:53:33 +00004054 false, false, 0);
Evan Chengc3630942009-12-09 21:00:30 +00004055 // Canonicalize it to a v4i32 shuffle.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004056 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, V1);
4057 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Chengc3630942009-12-09 21:00:30 +00004058 DAG.getVectorShuffle(MVT::v4i32, dl, V1,
Chris Lattner51abfe42010-09-21 06:02:19 +00004059 DAG.getUNDEF(MVT::v4i32),&Mask[0]));
Evan Chengc3630942009-12-09 21:00:30 +00004060 }
4061
4062 return SDValue();
4063}
4064
Michael J. Spencerec38de22010-10-10 22:04:20 +00004065/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
4066/// vector of type 'VT', see if the elements can be replaced by a single large
Nate Begeman1449f292010-03-24 22:19:06 +00004067/// load which has the same value as a build_vector whose operands are 'elts'.
4068///
4069/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
Michael J. Spencerec38de22010-10-10 22:04:20 +00004070///
Nate Begeman1449f292010-03-24 22:19:06 +00004071/// FIXME: we'd also like to handle the case where the last elements are zero
4072/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
4073/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004074static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
Chris Lattner88641552010-09-22 00:34:38 +00004075 DebugLoc &DL, SelectionDAG &DAG) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00004076 EVT EltVT = VT.getVectorElementType();
4077 unsigned NumElems = Elts.size();
Michael J. Spencerec38de22010-10-10 22:04:20 +00004078
Nate Begemanfdea31a2010-03-24 20:49:50 +00004079 LoadSDNode *LDBase = NULL;
4080 unsigned LastLoadedElt = -1U;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004081
Nate Begeman1449f292010-03-24 22:19:06 +00004082 // For each element in the initializer, see if we've found a load or an undef.
Michael J. Spencerec38de22010-10-10 22:04:20 +00004083 // If we don't find an initial load element, or later load elements are
Nate Begeman1449f292010-03-24 22:19:06 +00004084 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004085 for (unsigned i = 0; i < NumElems; ++i) {
4086 SDValue Elt = Elts[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00004087
Nate Begemanfdea31a2010-03-24 20:49:50 +00004088 if (!Elt.getNode() ||
4089 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
4090 return SDValue();
4091 if (!LDBase) {
4092 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
4093 return SDValue();
4094 LDBase = cast<LoadSDNode>(Elt.getNode());
4095 LastLoadedElt = i;
4096 continue;
4097 }
4098 if (Elt.getOpcode() == ISD::UNDEF)
4099 continue;
4100
4101 LoadSDNode *LD = cast<LoadSDNode>(Elt);
4102 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
4103 return SDValue();
4104 LastLoadedElt = i;
4105 }
Nate Begeman1449f292010-03-24 22:19:06 +00004106
4107 // If we have found an entire vector of loads and undefs, then return a large
4108 // load of the entire vector width starting at the base pointer. If we found
4109 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004110 if (LastLoadedElt == NumElems - 1) {
4111 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
Chris Lattner88641552010-09-22 00:34:38 +00004112 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004113 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004114 LDBase->isVolatile(), LDBase->isNonTemporal(), 0);
Chris Lattner88641552010-09-22 00:34:38 +00004115 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004116 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004117 LDBase->isVolatile(), LDBase->isNonTemporal(),
4118 LDBase->getAlignment());
4119 } else if (NumElems == 4 && LastLoadedElt == 1) {
4120 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
4121 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
Chris Lattner88641552010-09-22 00:34:38 +00004122 SDValue ResNode = DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys,
4123 Ops, 2, MVT::i32,
4124 LDBase->getMemOperand());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004125 return DAG.getNode(ISD::BITCAST, DL, VT, ResNode);
Nate Begemanfdea31a2010-03-24 20:49:50 +00004126 }
4127 return SDValue();
4128}
4129
Evan Chengc3630942009-12-09 21:00:30 +00004130SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004131X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004132 DebugLoc dl = Op.getDebugLoc();
Chris Lattner6e80e442010-08-28 17:15:43 +00004133 // All zero's are handled with pxor in SSE2 and above, xorps in SSE1.
4134 // All one's are handled with pcmpeqd. In AVX, zero's are handled with
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004135 // vpxor in 128-bit and xor{pd,ps} in 256-bit, but no 256 version of pcmpeqd
4136 // is present, so AllOnes is ignored.
4137 if (ISD::isBuildVectorAllZeros(Op.getNode()) ||
4138 (Op.getValueType().getSizeInBits() != 256 &&
4139 ISD::isBuildVectorAllOnes(Op.getNode()))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004140 // Canonicalize this to <4 x i32> (SSE) to
Chris Lattner8a594482007-11-25 00:24:49 +00004141 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
4142 // eliminated on x86-32 hosts.
Dale Johannesen0488fb62010-09-30 23:57:10 +00004143 if (Op.getValueType() == MVT::v4i32)
Chris Lattner8a594482007-11-25 00:24:49 +00004144 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004145
Gabor Greifba36cb52008-08-28 21:40:38 +00004146 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00004147 return getOnesVector(Op.getValueType(), DAG, dl);
4148 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00004149 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004150
Owen Andersone50ed302009-08-10 22:56:29 +00004151 EVT VT = Op.getValueType();
4152 EVT ExtVT = VT.getVectorElementType();
4153 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004154
4155 unsigned NumElems = Op.getNumOperands();
4156 unsigned NumZero = 0;
4157 unsigned NumNonZero = 0;
4158 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004159 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00004160 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004161 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00004162 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00004163 if (Elt.getOpcode() == ISD::UNDEF)
4164 continue;
4165 Values.insert(Elt);
4166 if (Elt.getOpcode() != ISD::Constant &&
4167 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004168 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00004169 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00004170 NumZero++;
4171 else {
4172 NonZeros |= (1 << i);
4173 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004174 }
4175 }
4176
Chris Lattner97a2a562010-08-26 05:24:29 +00004177 // All undef vector. Return an UNDEF. All zero vectors were handled above.
4178 if (NumNonZero == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00004179 return DAG.getUNDEF(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004180
Chris Lattner67f453a2008-03-09 05:42:06 +00004181 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00004182 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004183 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00004184 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00004185
Chris Lattner62098042008-03-09 01:05:04 +00004186 // If this is an insertion of an i64 value on x86-32, and if the top bits of
4187 // the value are obviously zero, truncate the value to i32 and do the
4188 // insertion that way. Only do this if the value is non-constant or if the
4189 // value is a constant being inserted into element 0. It is cheaper to do
4190 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00004191 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00004192 (!IsAllConstants || Idx == 0)) {
4193 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004194 // Handle SSE only.
4195 assert(VT == MVT::v2i64 && "Expected an SSE value type!");
4196 EVT VecVT = MVT::v4i32;
4197 unsigned VecElts = 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00004198
Chris Lattner62098042008-03-09 01:05:04 +00004199 // Truncate the value (which may itself be a constant) to i32, and
4200 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00004201 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00004202 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00004203 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
4204 Subtarget->hasSSE2(), DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00004205
Chris Lattner62098042008-03-09 01:05:04 +00004206 // Now we have our 32-bit value zero extended in the low element of
4207 // a vector. If Idx != 0, swizzle it into place.
4208 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004209 SmallVector<int, 4> Mask;
4210 Mask.push_back(Idx);
4211 for (unsigned i = 1; i != VecElts; ++i)
4212 Mask.push_back(i);
4213 Item = DAG.getVectorShuffle(VecVT, dl, Item,
Eric Christopherfd179292009-08-27 18:07:15 +00004214 DAG.getUNDEF(Item.getValueType()),
Nate Begeman9008ca62009-04-27 18:41:29 +00004215 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00004216 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004217 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00004218 }
4219 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004220
Chris Lattner19f79692008-03-08 22:59:52 +00004221 // If we have a constant or non-constant insertion into the low element of
4222 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
4223 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00004224 // depending on what the source datatype is.
4225 if (Idx == 0) {
4226 if (NumZero == 0) {
4227 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Owen Anderson825b72b2009-08-11 20:47:22 +00004228 } else if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
4229 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Eli Friedman10415532009-06-06 06:05:10 +00004230 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
4231 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
4232 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget->hasSSE2(),
4233 DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00004234 } else if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
4235 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
Dale Johannesen0488fb62010-09-30 23:57:10 +00004236 assert(VT.getSizeInBits() == 128 && "Expected an SSE value type!");
4237 EVT MiddleVT = MVT::v4i32;
Eli Friedman10415532009-06-06 06:05:10 +00004238 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MiddleVT, Item);
4239 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
4240 Subtarget->hasSSE2(), DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004241 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Eli Friedman10415532009-06-06 06:05:10 +00004242 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004243 }
Evan Chengf26ffe92008-05-29 08:22:04 +00004244
4245 // Is it a vector logical left shift?
4246 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00004247 X86::isZeroNode(Op.getOperand(0)) &&
4248 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004249 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00004250 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00004251 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004252 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00004253 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004254 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004255
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004256 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00004257 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004258
Chris Lattner19f79692008-03-08 22:59:52 +00004259 // Otherwise, if this is a vector with i32 or f32 elements, and the element
4260 // is a non-constant being inserted into an element other than the low one,
4261 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
4262 // movd/movss) to move this into the low element, then shuffle it into
4263 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004264 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00004265 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00004266
Evan Cheng0db9fe62006-04-25 20:13:52 +00004267 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00004268 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
4269 Subtarget->hasSSE2(), DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00004270 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004271 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00004272 MaskVec.push_back(i == Idx ? 0 : 1);
4273 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004274 }
4275 }
4276
Chris Lattner67f453a2008-03-09 05:42:06 +00004277 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00004278 if (Values.size() == 1) {
4279 if (EVTBits == 32) {
4280 // Instead of a shuffle like this:
4281 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
4282 // Check if it's possible to issue this instead.
4283 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
4284 unsigned Idx = CountTrailingZeros_32(NonZeros);
4285 SDValue Item = Op.getOperand(Idx);
4286 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
4287 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
4288 }
Dan Gohman475871a2008-07-27 21:46:04 +00004289 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004290 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004291
Dan Gohmana3941172007-07-24 22:55:08 +00004292 // A vector full of immediates; various special cases are already
4293 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004294 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00004295 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00004296
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00004297 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00004298 if (EVTBits == 64) {
4299 if (NumNonZero == 1) {
4300 // One half is zero or undef.
4301 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00004302 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00004303 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00004304 return getShuffleVectorZeroOrUndef(V2, Idx, true,
4305 Subtarget->hasSSE2(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00004306 }
Dan Gohman475871a2008-07-27 21:46:04 +00004307 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00004308 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004309
4310 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00004311 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00004312 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00004313 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004314 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004315 }
4316
Bill Wendling826f36f2007-03-28 00:57:11 +00004317 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00004318 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Chris Lattner97a2a562010-08-26 05:24:29 +00004319 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004320 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004321 }
4322
4323 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00004324 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00004325 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004326 if (NumElems == 4 && NumZero > 0) {
4327 for (unsigned i = 0; i < 4; ++i) {
4328 bool isZero = !(NonZeros & (1 << i));
4329 if (isZero)
Dale Johannesenace16102009-02-03 19:33:06 +00004330 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004331 else
Dale Johannesenace16102009-02-03 19:33:06 +00004332 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004333 }
4334
4335 for (unsigned i = 0; i < 2; ++i) {
4336 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
4337 default: break;
4338 case 0:
4339 V[i] = V[i*2]; // Must be a zero vector.
4340 break;
4341 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00004342 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004343 break;
4344 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00004345 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004346 break;
4347 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00004348 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004349 break;
4350 }
4351 }
4352
Nate Begeman9008ca62009-04-27 18:41:29 +00004353 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004354 bool Reverse = (NonZeros & 0x3) == 2;
4355 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004356 MaskVec.push_back(Reverse ? 1-i : i);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004357 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
4358 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004359 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
4360 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004361 }
4362
Nate Begemanfdea31a2010-03-24 20:49:50 +00004363 if (Values.size() > 1 && VT.getSizeInBits() == 128) {
4364 // Check for a build vector of consecutive loads.
4365 for (unsigned i = 0; i < NumElems; ++i)
4366 V[i] = Op.getOperand(i);
Michael J. Spencerec38de22010-10-10 22:04:20 +00004367
Nate Begemanfdea31a2010-03-24 20:49:50 +00004368 // Check for elements which are consecutive loads.
4369 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
4370 if (LD.getNode())
4371 return LD;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004372
4373 // For SSE 4.1, use insertps to put the high elements into the low element.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004374 if (getSubtarget()->hasSSE41()) {
Chris Lattner24faf612010-08-28 17:59:08 +00004375 SDValue Result;
4376 if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
4377 Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
4378 else
4379 Result = DAG.getUNDEF(VT);
Michael J. Spencerec38de22010-10-10 22:04:20 +00004380
Chris Lattner24faf612010-08-28 17:59:08 +00004381 for (unsigned i = 1; i < NumElems; ++i) {
4382 if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
4383 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
Nate Begeman9008ca62009-04-27 18:41:29 +00004384 Op.getOperand(i), DAG.getIntPtrConstant(i));
Chris Lattner24faf612010-08-28 17:59:08 +00004385 }
4386 return Result;
Nate Begeman9008ca62009-04-27 18:41:29 +00004387 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00004388
Chris Lattner6e80e442010-08-28 17:15:43 +00004389 // Otherwise, expand into a number of unpckl*, start by extending each of
4390 // our (non-undef) elements to the full vector width with the element in the
4391 // bottom slot of the vector (which generates no code for SSE).
4392 for (unsigned i = 0; i < NumElems; ++i) {
4393 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
4394 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
4395 else
4396 V[i] = DAG.getUNDEF(VT);
4397 }
4398
4399 // Next, we iteratively mix elements, e.g. for v4f32:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004400 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
4401 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
4402 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Chris Lattner6e80e442010-08-28 17:15:43 +00004403 unsigned EltStride = NumElems >> 1;
4404 while (EltStride != 0) {
Chris Lattner3ddcc432010-08-28 17:28:30 +00004405 for (unsigned i = 0; i < EltStride; ++i) {
4406 // If V[i+EltStride] is undef and this is the first round of mixing,
4407 // then it is safe to just drop this shuffle: V[i] is already in the
4408 // right place, the one element (since it's the first round) being
4409 // inserted as undef can be dropped. This isn't safe for successive
4410 // rounds because they will permute elements within both vectors.
4411 if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
4412 EltStride == NumElems/2)
4413 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004414
Chris Lattner6e80e442010-08-28 17:15:43 +00004415 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
Chris Lattner3ddcc432010-08-28 17:28:30 +00004416 }
Chris Lattner6e80e442010-08-28 17:15:43 +00004417 EltStride >>= 1;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004418 }
4419 return V[0];
4420 }
Dan Gohman475871a2008-07-27 21:46:04 +00004421 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004422}
4423
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004424SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004425X86TargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const {
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004426 // We support concatenate two MMX registers and place them in a MMX
4427 // register. This is better than doing a stack convert.
4428 DebugLoc dl = Op.getDebugLoc();
4429 EVT ResVT = Op.getValueType();
4430 assert(Op.getNumOperands() == 2);
4431 assert(ResVT == MVT::v2i64 || ResVT == MVT::v4i32 ||
4432 ResVT == MVT::v8i16 || ResVT == MVT::v16i8);
4433 int Mask[2];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004434 SDValue InVec = DAG.getNode(ISD::BITCAST,dl, MVT::v1i64, Op.getOperand(0));
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004435 SDValue VecOp = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
4436 InVec = Op.getOperand(1);
4437 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4438 unsigned NumElts = ResVT.getVectorNumElements();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004439 VecOp = DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004440 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, ResVT, VecOp,
4441 InVec.getOperand(0), DAG.getIntPtrConstant(NumElts/2+1));
4442 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004443 InVec = DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, InVec);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004444 SDValue VecOp2 = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
4445 Mask[0] = 0; Mask[1] = 2;
4446 VecOp = DAG.getVectorShuffle(MVT::v2i64, dl, VecOp, VecOp2, Mask);
4447 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004448 return DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004449}
4450
Nate Begemanb9a47b82009-02-23 08:49:38 +00004451// v8i16 shuffles - Prefer shuffles in the following order:
4452// 1. [all] pshuflw, pshufhw, optional move
4453// 2. [ssse3] 1 x pshufb
4454// 3. [ssse3] 2 x pshufb + 1 x por
4455// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004456SDValue
4457X86TargetLowering::LowerVECTOR_SHUFFLEv8i16(SDValue Op,
4458 SelectionDAG &DAG) const {
4459 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00004460 SDValue V1 = SVOp->getOperand(0);
4461 SDValue V2 = SVOp->getOperand(1);
4462 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00004463 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00004464
Nate Begemanb9a47b82009-02-23 08:49:38 +00004465 // Determine if more than 1 of the words in each of the low and high quadwords
4466 // of the result come from the same quadword of one of the two inputs. Undef
4467 // mask values count as coming from any quadword, for better codegen.
4468 SmallVector<unsigned, 4> LoQuad(4);
4469 SmallVector<unsigned, 4> HiQuad(4);
4470 BitVector InputQuads(4);
4471 for (unsigned i = 0; i < 8; ++i) {
4472 SmallVectorImpl<unsigned> &Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00004473 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004474 MaskVals.push_back(EltIdx);
4475 if (EltIdx < 0) {
4476 ++Quad[0];
4477 ++Quad[1];
4478 ++Quad[2];
4479 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00004480 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004481 }
4482 ++Quad[EltIdx / 4];
4483 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00004484 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004485
Nate Begemanb9a47b82009-02-23 08:49:38 +00004486 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00004487 unsigned MaxQuad = 1;
4488 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004489 if (LoQuad[i] > MaxQuad) {
4490 BestLoQuad = i;
4491 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00004492 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004493 }
4494
Nate Begemanb9a47b82009-02-23 08:49:38 +00004495 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00004496 MaxQuad = 1;
4497 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004498 if (HiQuad[i] > MaxQuad) {
4499 BestHiQuad = i;
4500 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00004501 }
4502 }
4503
Nate Begemanb9a47b82009-02-23 08:49:38 +00004504 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00004505 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00004506 // single pshufb instruction is necessary. If There are more than 2 input
4507 // quads, disable the next transformation since it does not help SSSE3.
4508 bool V1Used = InputQuads[0] || InputQuads[1];
4509 bool V2Used = InputQuads[2] || InputQuads[3];
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004510 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004511 if (InputQuads.count() == 2 && V1Used && V2Used) {
4512 BestLoQuad = InputQuads.find_first();
4513 BestHiQuad = InputQuads.find_next(BestLoQuad);
4514 }
4515 if (InputQuads.count() > 2) {
4516 BestLoQuad = -1;
4517 BestHiQuad = -1;
4518 }
4519 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004520
Nate Begemanb9a47b82009-02-23 08:49:38 +00004521 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
4522 // the shuffle mask. If a quad is scored as -1, that means that it contains
4523 // words from all 4 input quadwords.
4524 SDValue NewV;
4525 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004526 SmallVector<int, 8> MaskV;
4527 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
4528 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
Eric Christopherfd179292009-08-27 18:07:15 +00004529 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004530 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V1),
4531 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V2), &MaskV[0]);
4532 NewV = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00004533
Nate Begemanb9a47b82009-02-23 08:49:38 +00004534 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
4535 // source words for the shuffle, to aid later transformations.
4536 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00004537 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00004538 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004539 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00004540 if (idx != (int)i)
4541 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004542 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00004543 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004544 AllWordsInNewV = false;
4545 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00004546 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004547
Nate Begemanb9a47b82009-02-23 08:49:38 +00004548 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
4549 if (AllWordsInNewV) {
4550 for (int i = 0; i != 8; ++i) {
4551 int idx = MaskVals[i];
4552 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00004553 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004554 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004555 if ((idx != i) && idx < 4)
4556 pshufhw = false;
4557 if ((idx != i) && idx > 3)
4558 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00004559 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00004560 V1 = NewV;
4561 V2Used = false;
4562 BestLoQuad = 0;
4563 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004564 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004565
Nate Begemanb9a47b82009-02-23 08:49:38 +00004566 // If we've eliminated the use of V2, and the new mask is a pshuflw or
4567 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00004568 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00004569 unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
4570 unsigned TargetMask = 0;
4571 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00004572 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00004573 TargetMask = pshufhw ? X86::getShufflePSHUFHWImmediate(NewV.getNode()):
4574 X86::getShufflePSHUFLWImmediate(NewV.getNode());
4575 V1 = NewV.getOperand(0);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00004576 return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
Evan Cheng14b32e12007-12-11 01:46:18 +00004577 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004578 }
Eric Christopherfd179292009-08-27 18:07:15 +00004579
Nate Begemanb9a47b82009-02-23 08:49:38 +00004580 // If we have SSSE3, and all words of the result are from 1 input vector,
4581 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
4582 // is present, fall back to case 4.
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004583 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004584 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00004585
Nate Begemanb9a47b82009-02-23 08:49:38 +00004586 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00004587 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00004588 // mask, and elements that come from V1 in the V2 mask, so that the two
4589 // results can be OR'd together.
4590 bool TwoInputs = V1Used && V2Used;
4591 for (unsigned i = 0; i != 8; ++i) {
4592 int EltIdx = MaskVals[i] * 2;
4593 if (TwoInputs && (EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004594 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4595 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004596 continue;
4597 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004598 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
4599 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004600 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004601 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004602 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00004603 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004604 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004605 if (!TwoInputs)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004606 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004607
Nate Begemanb9a47b82009-02-23 08:49:38 +00004608 // Calculate the shuffle mask for the second input, shuffle it, and
4609 // OR it with the first shuffled input.
4610 pshufbMask.clear();
4611 for (unsigned i = 0; i != 8; ++i) {
4612 int EltIdx = MaskVals[i] * 2;
4613 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004614 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4615 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004616 continue;
4617 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004618 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
4619 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004620 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004621 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00004622 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00004623 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004624 MVT::v16i8, &pshufbMask[0], 16));
4625 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004626 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004627 }
4628
4629 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
4630 // and update MaskVals with new element order.
4631 BitVector InOrder(8);
4632 if (BestLoQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004633 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004634 for (int i = 0; i != 4; ++i) {
4635 int idx = MaskVals[i];
4636 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004637 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004638 InOrder.set(i);
4639 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004640 MaskV.push_back(idx & 3);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004641 InOrder.set(i);
4642 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004643 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004644 }
4645 }
4646 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004647 MaskV.push_back(i);
Owen Anderson825b72b2009-08-11 20:47:22 +00004648 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00004649 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00004650
4651 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3())
4652 NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
4653 NewV.getOperand(0),
4654 X86::getShufflePSHUFLWImmediate(NewV.getNode()),
4655 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004656 }
Eric Christopherfd179292009-08-27 18:07:15 +00004657
Nate Begemanb9a47b82009-02-23 08:49:38 +00004658 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
4659 // and update MaskVals with the new element order.
4660 if (BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004661 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004662 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004663 MaskV.push_back(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004664 for (unsigned i = 4; i != 8; ++i) {
4665 int idx = MaskVals[i];
4666 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004667 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004668 InOrder.set(i);
4669 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004670 MaskV.push_back((idx & 3) + 4);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004671 InOrder.set(i);
4672 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004673 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004674 }
4675 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004676 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00004677 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00004678
4679 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3())
4680 NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
4681 NewV.getOperand(0),
4682 X86::getShufflePSHUFHWImmediate(NewV.getNode()),
4683 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004684 }
Eric Christopherfd179292009-08-27 18:07:15 +00004685
Nate Begemanb9a47b82009-02-23 08:49:38 +00004686 // In case BestHi & BestLo were both -1, which means each quadword has a word
4687 // from each of the four input quadwords, calculate the InOrder bitvector now
4688 // before falling through to the insert/extract cleanup.
4689 if (BestLoQuad == -1 && BestHiQuad == -1) {
4690 NewV = V1;
4691 for (int i = 0; i != 8; ++i)
4692 if (MaskVals[i] < 0 || MaskVals[i] == i)
4693 InOrder.set(i);
4694 }
Eric Christopherfd179292009-08-27 18:07:15 +00004695
Nate Begemanb9a47b82009-02-23 08:49:38 +00004696 // The other elements are put in the right place using pextrw and pinsrw.
4697 for (unsigned i = 0; i != 8; ++i) {
4698 if (InOrder[i])
4699 continue;
4700 int EltIdx = MaskVals[i];
4701 if (EltIdx < 0)
4702 continue;
4703 SDValue ExtOp = (EltIdx < 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00004704 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004705 DAG.getIntPtrConstant(EltIdx))
Owen Anderson825b72b2009-08-11 20:47:22 +00004706 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004707 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00004708 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004709 DAG.getIntPtrConstant(i));
4710 }
4711 return NewV;
4712}
4713
4714// v16i8 shuffles - Prefer shuffles in the following order:
4715// 1. [ssse3] 1 x pshufb
4716// 2. [ssse3] 2 x pshufb + 1 x por
4717// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
4718static
Nate Begeman9008ca62009-04-27 18:41:29 +00004719SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00004720 SelectionDAG &DAG,
4721 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004722 SDValue V1 = SVOp->getOperand(0);
4723 SDValue V2 = SVOp->getOperand(1);
4724 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00004725 SmallVector<int, 16> MaskVals;
Nate Begeman9008ca62009-04-27 18:41:29 +00004726 SVOp->getMask(MaskVals);
Eric Christopherfd179292009-08-27 18:07:15 +00004727
Nate Begemanb9a47b82009-02-23 08:49:38 +00004728 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00004729 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00004730 // present, fall back to case 3.
4731 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
4732 bool V1Only = true;
4733 bool V2Only = true;
4734 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004735 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00004736 if (EltIdx < 0)
4737 continue;
4738 if (EltIdx < 16)
4739 V2Only = false;
4740 else
4741 V1Only = false;
4742 }
Eric Christopherfd179292009-08-27 18:07:15 +00004743
Nate Begemanb9a47b82009-02-23 08:49:38 +00004744 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
4745 if (TLI.getSubtarget()->hasSSSE3()) {
4746 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00004747
Nate Begemanb9a47b82009-02-23 08:49:38 +00004748 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00004749 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00004750 //
4751 // Otherwise, we have elements from both input vectors, and must zero out
4752 // elements that come from V2 in the first mask, and V1 in the second mask
4753 // so that we can OR them together.
4754 bool TwoInputs = !(V1Only || V2Only);
4755 for (unsigned i = 0; i != 16; ++i) {
4756 int EltIdx = MaskVals[i];
4757 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004758 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004759 continue;
4760 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004761 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004762 }
4763 // If all the elements are from V2, assign it to V1 and return after
4764 // building the first pshufb.
4765 if (V2Only)
4766 V1 = V2;
Owen Anderson825b72b2009-08-11 20:47:22 +00004767 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00004768 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004769 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004770 if (!TwoInputs)
4771 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00004772
Nate Begemanb9a47b82009-02-23 08:49:38 +00004773 // Calculate the shuffle mask for the second input, shuffle it, and
4774 // OR it with the first shuffled input.
4775 pshufbMask.clear();
4776 for (unsigned i = 0; i != 16; ++i) {
4777 int EltIdx = MaskVals[i];
4778 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004779 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004780 continue;
4781 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004782 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004783 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004784 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00004785 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004786 MVT::v16i8, &pshufbMask[0], 16));
4787 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004788 }
Eric Christopherfd179292009-08-27 18:07:15 +00004789
Nate Begemanb9a47b82009-02-23 08:49:38 +00004790 // No SSSE3 - Calculate in place words and then fix all out of place words
4791 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
4792 // the 16 different words that comprise the two doublequadword input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004793 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
4794 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004795 SDValue NewV = V2Only ? V2 : V1;
4796 for (int i = 0; i != 8; ++i) {
4797 int Elt0 = MaskVals[i*2];
4798 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00004799
Nate Begemanb9a47b82009-02-23 08:49:38 +00004800 // This word of the result is all undef, skip it.
4801 if (Elt0 < 0 && Elt1 < 0)
4802 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004803
Nate Begemanb9a47b82009-02-23 08:49:38 +00004804 // This word of the result is already in the correct place, skip it.
4805 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
4806 continue;
4807 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
4808 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004809
Nate Begemanb9a47b82009-02-23 08:49:38 +00004810 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
4811 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
4812 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00004813
4814 // If Elt0 and Elt1 are defined, are consecutive, and can be load
4815 // using a single extract together, load it and store it.
4816 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004817 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00004818 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00004819 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00004820 DAG.getIntPtrConstant(i));
4821 continue;
4822 }
4823
Nate Begemanb9a47b82009-02-23 08:49:38 +00004824 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00004825 // source byte is not also odd, shift the extracted word left 8 bits
4826 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00004827 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004828 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004829 DAG.getIntPtrConstant(Elt1 / 2));
4830 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004831 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004832 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00004833 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004834 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
4835 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004836 }
4837 // If Elt0 is defined, extract it from the appropriate source. If the
4838 // source byte is not also even, shift the extracted word right 8 bits. If
4839 // Elt1 was also defined, OR the extracted values together before
4840 // inserting them in the result.
4841 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004842 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004843 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
4844 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004845 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004846 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00004847 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004848 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
4849 DAG.getConstant(0x00FF, MVT::i16));
4850 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00004851 : InsElt0;
4852 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004853 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004854 DAG.getIntPtrConstant(i));
4855 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004856 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00004857}
4858
Evan Cheng7a831ce2007-12-15 03:00:47 +00004859/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00004860/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00004861/// done when every pair / quad of shuffle mask elements point to elements in
4862/// the right sequence. e.g.
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00004863/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
Evan Cheng14b32e12007-12-11 01:46:18 +00004864static
Nate Begeman9008ca62009-04-27 18:41:29 +00004865SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00004866 SelectionDAG &DAG, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00004867 EVT VT = SVOp->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00004868 SDValue V1 = SVOp->getOperand(0);
4869 SDValue V2 = SVOp->getOperand(1);
4870 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00004871 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00004872 EVT NewVT;
Owen Anderson825b72b2009-08-11 20:47:22 +00004873 switch (VT.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004874 default: assert(false && "Unexpected!");
Owen Anderson825b72b2009-08-11 20:47:22 +00004875 case MVT::v4f32: NewVT = MVT::v2f64; break;
4876 case MVT::v4i32: NewVT = MVT::v2i64; break;
4877 case MVT::v8i16: NewVT = MVT::v4i32; break;
4878 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00004879 }
4880
Nate Begeman9008ca62009-04-27 18:41:29 +00004881 int Scale = NumElems / NewWidth;
4882 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00004883 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004884 int StartIdx = -1;
4885 for (int j = 0; j < Scale; ++j) {
4886 int EltIdx = SVOp->getMaskElt(i+j);
4887 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00004888 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00004889 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00004890 StartIdx = EltIdx - (EltIdx % Scale);
4891 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00004892 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00004893 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004894 if (StartIdx == -1)
4895 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00004896 else
Nate Begeman9008ca62009-04-27 18:41:29 +00004897 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004898 }
4899
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004900 V1 = DAG.getNode(ISD::BITCAST, dl, NewVT, V1);
4901 V2 = DAG.getNode(ISD::BITCAST, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00004902 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004903}
4904
Evan Chengd880b972008-05-09 21:53:03 +00004905/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00004906///
Owen Andersone50ed302009-08-10 22:56:29 +00004907static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00004908 SDValue SrcOp, SelectionDAG &DAG,
4909 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004910 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004911 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00004912 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00004913 LD = dyn_cast<LoadSDNode>(SrcOp);
4914 if (!LD) {
4915 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
4916 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00004917 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Duncan Sandscdfad362010-11-03 12:17:33 +00004918 if ((ExtVT != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00004919 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004920 SrcOp.getOperand(0).getOpcode() == ISD::BITCAST &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00004921 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004922 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00004923 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004924 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004925 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
4926 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
4927 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00004928 SrcOp.getOperand(0)
4929 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00004930 }
4931 }
4932 }
4933
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004934 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004935 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004936 DAG.getNode(ISD::BITCAST, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00004937 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00004938}
4939
Evan Chengace3c172008-07-22 21:13:36 +00004940/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
4941/// shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00004942static SDValue
Nate Begeman9008ca62009-04-27 18:41:29 +00004943LowerVECTOR_SHUFFLE_4wide(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
4944 SDValue V1 = SVOp->getOperand(0);
4945 SDValue V2 = SVOp->getOperand(1);
4946 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00004947 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00004948
Evan Chengace3c172008-07-22 21:13:36 +00004949 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00004950 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00004951 SmallVector<int, 8> Mask1(4U, -1);
4952 SmallVector<int, 8> PermMask;
4953 SVOp->getMask(PermMask);
4954
Evan Chengace3c172008-07-22 21:13:36 +00004955 unsigned NumHi = 0;
4956 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00004957 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004958 int Idx = PermMask[i];
4959 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00004960 Locs[i] = std::make_pair(-1, -1);
4961 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004962 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
4963 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00004964 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00004965 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004966 NumLo++;
4967 } else {
4968 Locs[i] = std::make_pair(1, NumHi);
4969 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00004970 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004971 NumHi++;
4972 }
4973 }
4974 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004975
Evan Chengace3c172008-07-22 21:13:36 +00004976 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004977 // If no more than two elements come from either vector. This can be
4978 // implemented with two shuffles. First shuffle gather the elements.
4979 // The second shuffle, which takes the first shuffle as both of its
4980 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00004981 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004982
Nate Begeman9008ca62009-04-27 18:41:29 +00004983 SmallVector<int, 8> Mask2(4U, -1);
Eric Christopherfd179292009-08-27 18:07:15 +00004984
Evan Chengace3c172008-07-22 21:13:36 +00004985 for (unsigned i = 0; i != 4; ++i) {
4986 if (Locs[i].first == -1)
4987 continue;
4988 else {
4989 unsigned Idx = (i < 2) ? 0 : 4;
4990 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00004991 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004992 }
4993 }
4994
Nate Begeman9008ca62009-04-27 18:41:29 +00004995 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004996 } else if (NumLo == 3 || NumHi == 3) {
4997 // Otherwise, we must have three elements from one vector, call it X, and
4998 // one element from the other, call it Y. First, use a shufps to build an
4999 // intermediate vector with the one element from Y and the element from X
5000 // that will be in the same half in the final destination (the indexes don't
5001 // matter). Then, use a shufps to build the final vector, taking the half
5002 // containing the element from Y from the intermediate, and the other half
5003 // from X.
5004 if (NumHi == 3) {
5005 // Normalize it so the 3 elements come from V1.
Nate Begeman9008ca62009-04-27 18:41:29 +00005006 CommuteVectorShuffleMask(PermMask, VT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005007 std::swap(V1, V2);
5008 }
5009
5010 // Find the element from V2.
5011 unsigned HiIndex;
5012 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005013 int Val = PermMask[HiIndex];
5014 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005015 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005016 if (Val >= 4)
5017 break;
5018 }
5019
Nate Begeman9008ca62009-04-27 18:41:29 +00005020 Mask1[0] = PermMask[HiIndex];
5021 Mask1[1] = -1;
5022 Mask1[2] = PermMask[HiIndex^1];
5023 Mask1[3] = -1;
5024 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005025
5026 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005027 Mask1[0] = PermMask[0];
5028 Mask1[1] = PermMask[1];
5029 Mask1[2] = HiIndex & 1 ? 6 : 4;
5030 Mask1[3] = HiIndex & 1 ? 4 : 6;
5031 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005032 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005033 Mask1[0] = HiIndex & 1 ? 2 : 0;
5034 Mask1[1] = HiIndex & 1 ? 0 : 2;
5035 Mask1[2] = PermMask[2];
5036 Mask1[3] = PermMask[3];
5037 if (Mask1[2] >= 0)
5038 Mask1[2] += 4;
5039 if (Mask1[3] >= 0)
5040 Mask1[3] += 4;
5041 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005042 }
Evan Chengace3c172008-07-22 21:13:36 +00005043 }
5044
5045 // Break it into (shuffle shuffle_hi, shuffle_lo).
5046 Locs.clear();
Nate Begeman9008ca62009-04-27 18:41:29 +00005047 SmallVector<int,8> LoMask(4U, -1);
5048 SmallVector<int,8> HiMask(4U, -1);
5049
5050 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00005051 unsigned MaskIdx = 0;
5052 unsigned LoIdx = 0;
5053 unsigned HiIdx = 2;
5054 for (unsigned i = 0; i != 4; ++i) {
5055 if (i == 2) {
5056 MaskPtr = &HiMask;
5057 MaskIdx = 1;
5058 LoIdx = 0;
5059 HiIdx = 2;
5060 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005061 int Idx = PermMask[i];
5062 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00005063 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00005064 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00005065 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00005066 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005067 LoIdx++;
5068 } else {
5069 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00005070 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005071 HiIdx++;
5072 }
5073 }
5074
Nate Begeman9008ca62009-04-27 18:41:29 +00005075 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
5076 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
5077 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00005078 for (unsigned i = 0; i != 4; ++i) {
5079 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005080 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00005081 } else {
5082 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00005083 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00005084 }
5085 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005086 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00005087}
5088
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005089static bool MayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005090 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005091 V = V.getOperand(0);
5092 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
5093 V = V.getOperand(0);
5094 if (MayFoldLoad(V))
5095 return true;
5096 return false;
5097}
5098
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005099// FIXME: the version above should always be used. Since there's
5100// a bug where several vector shuffles can't be folded because the
5101// DAG is not updated during lowering and a node claims to have two
5102// uses while it only has one, use this version, and let isel match
5103// another instruction if the load really happens to have more than
5104// one use. Remove this version after this bug get fixed.
Evan Cheng835580f2010-10-07 20:50:20 +00005105// rdar://8434668, PR8156
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005106static bool RelaxedMayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005107 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005108 V = V.getOperand(0);
5109 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
5110 V = V.getOperand(0);
5111 if (ISD::isNormalLoad(V.getNode()))
5112 return true;
5113 return false;
5114}
5115
5116/// CanFoldShuffleIntoVExtract - Check if the current shuffle is used by
5117/// a vector extract, and if both can be later optimized into a single load.
5118/// This is done in visitEXTRACT_VECTOR_ELT and the conditions are checked
5119/// here because otherwise a target specific shuffle node is going to be
5120/// emitted for this shuffle, and the optimization not done.
5121/// FIXME: This is probably not the best approach, but fix the problem
5122/// until the right path is decided.
5123static
5124bool CanXFormVExtractWithShuffleIntoLoad(SDValue V, SelectionDAG &DAG,
5125 const TargetLowering &TLI) {
5126 EVT VT = V.getValueType();
5127 ShuffleVectorSDNode *SVOp = dyn_cast<ShuffleVectorSDNode>(V);
5128
5129 // Be sure that the vector shuffle is present in a pattern like this:
5130 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), c) -> (f32 load $addr)
5131 if (!V.hasOneUse())
5132 return false;
5133
5134 SDNode *N = *V.getNode()->use_begin();
5135 if (N->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
5136 return false;
5137
5138 SDValue EltNo = N->getOperand(1);
5139 if (!isa<ConstantSDNode>(EltNo))
5140 return false;
5141
5142 // If the bit convert changed the number of elements, it is unsafe
5143 // to examine the mask.
5144 bool HasShuffleIntoBitcast = false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005145 if (V.getOpcode() == ISD::BITCAST) {
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005146 EVT SrcVT = V.getOperand(0).getValueType();
5147 if (SrcVT.getVectorNumElements() != VT.getVectorNumElements())
5148 return false;
5149 V = V.getOperand(0);
5150 HasShuffleIntoBitcast = true;
5151 }
5152
5153 // Select the input vector, guarding against out of range extract vector.
5154 unsigned NumElems = VT.getVectorNumElements();
5155 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
5156 int Idx = (Elt > NumElems) ? -1 : SVOp->getMaskElt(Elt);
5157 V = (Idx < (int)NumElems) ? V.getOperand(0) : V.getOperand(1);
5158
5159 // Skip one more bit_convert if necessary
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005160 if (V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005161 V = V.getOperand(0);
5162
5163 if (ISD::isNormalLoad(V.getNode())) {
5164 // Is the original load suitable?
5165 LoadSDNode *LN0 = cast<LoadSDNode>(V);
5166
5167 // FIXME: avoid the multi-use bug that is preventing lots of
5168 // of foldings to be detected, this is still wrong of course, but
5169 // give the temporary desired behavior, and if it happens that
5170 // the load has real more uses, during isel it will not fold, and
5171 // will generate poor code.
5172 if (!LN0 || LN0->isVolatile()) // || !LN0->hasOneUse()
5173 return false;
5174
5175 if (!HasShuffleIntoBitcast)
5176 return true;
5177
5178 // If there's a bitcast before the shuffle, check if the load type and
5179 // alignment is valid.
5180 unsigned Align = LN0->getAlignment();
5181 unsigned NewAlign =
5182 TLI.getTargetData()->getABITypeAlignment(
5183 VT.getTypeForEVT(*DAG.getContext()));
5184
5185 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
5186 return false;
5187 }
5188
5189 return true;
5190}
5191
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005192static
Evan Cheng835580f2010-10-07 20:50:20 +00005193SDValue getMOVDDup(SDValue &Op, DebugLoc &dl, SDValue V1, SelectionDAG &DAG) {
5194 EVT VT = Op.getValueType();
5195
5196 // Canonizalize to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005197 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1);
5198 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng835580f2010-10-07 20:50:20 +00005199 getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64,
5200 V1, DAG));
5201}
5202
5203static
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005204SDValue getMOVLowToHigh(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG,
5205 bool HasSSE2) {
5206 SDValue V1 = Op.getOperand(0);
5207 SDValue V2 = Op.getOperand(1);
5208 EVT VT = Op.getValueType();
5209
5210 assert(VT != MVT::v2i64 && "unsupported shuffle type");
5211
5212 if (HasSSE2 && VT == MVT::v2f64)
5213 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
5214
5215 // v4f32 or v4i32
5216 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V2, DAG);
5217}
5218
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00005219static
5220SDValue getMOVHighToLow(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG) {
5221 SDValue V1 = Op.getOperand(0);
5222 SDValue V2 = Op.getOperand(1);
5223 EVT VT = Op.getValueType();
5224
5225 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
5226 "unsupported shuffle type");
5227
5228 if (V2.getOpcode() == ISD::UNDEF)
5229 V2 = V1;
5230
5231 // v4i32 or v4f32
5232 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
5233}
5234
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005235static
5236SDValue getMOVLP(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG, bool HasSSE2) {
5237 SDValue V1 = Op.getOperand(0);
5238 SDValue V2 = Op.getOperand(1);
5239 EVT VT = Op.getValueType();
5240 unsigned NumElems = VT.getVectorNumElements();
5241
5242 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
5243 // operand of these instructions is only memory, so check if there's a
5244 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
5245 // same masks.
5246 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005247
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00005248 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005249 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005250 CanFoldLoad = true;
5251
5252 // When V1 is a load, it can be folded later into a store in isel, example:
5253 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
5254 // turns into:
5255 // (MOVLPSmr addr:$src1, VR128:$src2)
5256 // So, recognize this potential and also use MOVLPS or MOVLPD
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005257 if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005258 CanFoldLoad = true;
5259
5260 if (CanFoldLoad) {
5261 if (HasSSE2 && NumElems == 2)
5262 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
5263
5264 if (NumElems == 4)
5265 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
5266 }
5267
5268 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5269 // movl and movlp will both match v2i64, but v2i64 is never matched by
5270 // movl earlier because we make it strict to avoid messing with the movlp load
5271 // folding logic (see the code above getMOVLP call). Match it here then,
5272 // this is horrible, but will stay like this until we move all shuffle
5273 // matching to x86 specific nodes. Note that for the 1st condition all
5274 // types are matched with movsd.
5275 if ((HasSSE2 && NumElems == 2) || !X86::isMOVLMask(SVOp))
5276 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
5277 else if (HasSSE2)
5278 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
5279
5280
5281 assert(VT != MVT::v4i32 && "unsupported shuffle type");
5282
5283 // Invert the operand order and use SHUFPS to match it.
5284 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V2, V1,
5285 X86::getShuffleSHUFImmediate(SVOp), DAG);
5286}
5287
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005288static inline unsigned getUNPCKLOpcode(EVT VT) {
5289 switch(VT.getSimpleVT().SimpleTy) {
5290 case MVT::v4i32: return X86ISD::PUNPCKLDQ;
5291 case MVT::v2i64: return X86ISD::PUNPCKLQDQ;
5292 case MVT::v4f32: return X86ISD::UNPCKLPS;
5293 case MVT::v2f64: return X86ISD::UNPCKLPD;
5294 case MVT::v16i8: return X86ISD::PUNPCKLBW;
5295 case MVT::v8i16: return X86ISD::PUNPCKLWD;
5296 default:
5297 llvm_unreachable("Unknow type for unpckl");
5298 }
5299 return 0;
5300}
5301
5302static inline unsigned getUNPCKHOpcode(EVT VT) {
5303 switch(VT.getSimpleVT().SimpleTy) {
5304 case MVT::v4i32: return X86ISD::PUNPCKHDQ;
5305 case MVT::v2i64: return X86ISD::PUNPCKHQDQ;
5306 case MVT::v4f32: return X86ISD::UNPCKHPS;
5307 case MVT::v2f64: return X86ISD::UNPCKHPD;
5308 case MVT::v16i8: return X86ISD::PUNPCKHBW;
5309 case MVT::v8i16: return X86ISD::PUNPCKHWD;
5310 default:
5311 llvm_unreachable("Unknow type for unpckh");
5312 }
5313 return 0;
5314}
5315
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005316static
5317SDValue NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG,
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005318 const TargetLowering &TLI,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005319 const X86Subtarget *Subtarget) {
5320 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5321 EVT VT = Op.getValueType();
5322 DebugLoc dl = Op.getDebugLoc();
5323 SDValue V1 = Op.getOperand(0);
5324 SDValue V2 = Op.getOperand(1);
5325
5326 if (isZeroShuffle(SVOp))
5327 return getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
5328
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005329 // Handle splat operations
5330 if (SVOp->isSplat()) {
5331 // Special case, this is the only place now where it's
5332 // allowed to return a vector_shuffle operation without
5333 // using a target specific node, because *hopefully* it
5334 // will be optimized away by the dag combiner.
5335 if (VT.getVectorNumElements() <= 4 &&
5336 CanXFormVExtractWithShuffleIntoLoad(Op, DAG, TLI))
5337 return Op;
5338
5339 // Handle splats by matching through known masks
5340 if (VT.getVectorNumElements() <= 4)
5341 return SDValue();
5342
Evan Cheng835580f2010-10-07 20:50:20 +00005343 // Canonicalize all of the remaining to v4f32.
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005344 return PromoteSplat(SVOp, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005345 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005346
5347 // If the shuffle can be profitably rewritten as a narrower shuffle, then
5348 // do it!
5349 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
5350 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5351 if (NewOp.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005352 return DAG.getNode(ISD::BITCAST, dl, VT, NewOp);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005353 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
5354 // FIXME: Figure out a cleaner way to do this.
5355 // Try to make use of movq to zero out the top part.
5356 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
5357 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5358 if (NewOp.getNode()) {
5359 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
5360 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
5361 DAG, Subtarget, dl);
5362 }
5363 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
5364 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5365 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
5366 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
5367 DAG, Subtarget, dl);
5368 }
5369 }
5370 return SDValue();
5371}
5372
Dan Gohman475871a2008-07-27 21:46:04 +00005373SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005374X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00005375 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00005376 SDValue V1 = Op.getOperand(0);
5377 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00005378 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005379 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00005380 unsigned NumElems = VT.getVectorNumElements();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005381 bool isMMX = VT.getSizeInBits() == 64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005382 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
5383 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00005384 bool V1IsSplat = false;
5385 bool V2IsSplat = false;
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005386 bool HasSSE2 = Subtarget->hasSSE2() || Subtarget->hasAVX();
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00005387 bool HasSSE3 = Subtarget->hasSSE3() || Subtarget->hasAVX();
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00005388 bool HasSSSE3 = Subtarget->hasSSSE3() || Subtarget->hasAVX();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005389 MachineFunction &MF = DAG.getMachineFunction();
5390 bool OptForSize = MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005391
Dale Johannesen0488fb62010-09-30 23:57:10 +00005392 // Shuffle operations on MMX not supported.
5393 if (isMMX)
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00005394 return Op;
5395
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005396 // Vector shuffle lowering takes 3 steps:
5397 //
5398 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
5399 // narrowing and commutation of operands should be handled.
5400 // 2) Matching of shuffles with known shuffle masks to x86 target specific
5401 // shuffle nodes.
5402 // 3) Rewriting of unmatched masks into new generic shuffle operations,
5403 // so the shuffle can be broken into other shuffles and the legalizer can
5404 // try the lowering again.
5405 //
5406 // The general ideia is that no vector_shuffle operation should be left to
5407 // be matched during isel, all of them must be converted to a target specific
5408 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00005409
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005410 // Normalize the input vectors. Here splats, zeroed vectors, profitable
5411 // narrowing and commutation of operands should be handled. The actual code
5412 // doesn't include all of those, work in progress...
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005413 SDValue NewOp = NormalizeVectorShuffle(Op, DAG, *this, Subtarget);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005414 if (NewOp.getNode())
5415 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00005416
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005417 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
5418 // unpckh_undef). Only use pshufd if speed is more important than size.
5419 if (OptForSize && X86::isUNPCKL_v_undef_Mask(SVOp))
5420 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5421 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V1, DAG);
5422 if (OptForSize && X86::isUNPCKH_v_undef_Mask(SVOp))
5423 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5424 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00005425
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005426 if (X86::isMOVDDUPMask(SVOp) && HasSSE3 && V2IsUndef &&
Dale Johannesen0488fb62010-09-30 23:57:10 +00005427 RelaxedMayFoldVectorLoad(V1))
Evan Cheng835580f2010-10-07 20:50:20 +00005428 return getMOVDDup(Op, dl, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005429
Dale Johannesen0488fb62010-09-30 23:57:10 +00005430 if (X86::isMOVHLPS_v_undef_Mask(SVOp))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005431 return getMOVHighToLow(Op, dl, DAG);
5432
5433 // Use to match splats
5434 if (HasSSE2 && X86::isUNPCKHMask(SVOp) && V2IsUndef &&
5435 (VT == MVT::v2f64 || VT == MVT::v2i64))
5436 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
5437
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005438 if (X86::isPSHUFDMask(SVOp)) {
5439 // The actual implementation will match the mask in the if above and then
5440 // during isel it can match several different instructions, not only pshufd
5441 // as its name says, sad but true, emulate the behavior for now...
5442 if (X86::isMOVDDUPMask(SVOp) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
5443 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
5444
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005445 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
5446
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005447 if (HasSSE2 && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005448 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
5449
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005450 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005451 return getTargetShuffleNode(X86ISD::SHUFPD, dl, VT, V1, V1,
5452 TargetMask, DAG);
5453
5454 if (VT == MVT::v4f32)
5455 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V1, V1,
5456 TargetMask, DAG);
5457 }
Eric Christopherfd179292009-08-27 18:07:15 +00005458
Evan Chengf26ffe92008-05-29 08:22:04 +00005459 // Check if this can be converted into a logical shift.
5460 bool isLeft = false;
5461 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00005462 SDValue ShVal;
Nate Begeman9008ca62009-04-27 18:41:29 +00005463 bool isShift = getSubtarget()->hasSSE2() &&
Evan Chengc3630942009-12-09 21:00:30 +00005464 isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00005465 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00005466 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00005467 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00005468 EVT EltVT = VT.getVectorElementType();
5469 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00005470 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005471 }
Eric Christopherfd179292009-08-27 18:07:15 +00005472
Nate Begeman9008ca62009-04-27 18:41:29 +00005473 if (X86::isMOVLMask(SVOp)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005474 if (V1IsUndef)
5475 return V2;
Gabor Greifba36cb52008-08-28 21:40:38 +00005476 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00005477 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Dale Johannesen0488fb62010-09-30 23:57:10 +00005478 if (!X86::isMOVLPMask(SVOp)) {
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005479 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00005480 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
5481
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005482 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00005483 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
5484 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00005485 }
Eric Christopherfd179292009-08-27 18:07:15 +00005486
Nate Begeman9008ca62009-04-27 18:41:29 +00005487 // FIXME: fold these into legal mask.
Dale Johannesen0488fb62010-09-30 23:57:10 +00005488 if (X86::isMOVLHPSMask(SVOp) && !X86::isUNPCKLMask(SVOp))
5489 return getMOVLowToHigh(Op, dl, DAG, HasSSE2);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005490
Dale Johannesen0488fb62010-09-30 23:57:10 +00005491 if (X86::isMOVHLPSMask(SVOp))
5492 return getMOVHighToLow(Op, dl, DAG);
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00005493
Dale Johannesen0488fb62010-09-30 23:57:10 +00005494 if (X86::isMOVSHDUPMask(SVOp) && HasSSE3 && V2IsUndef && NumElems == 4)
5495 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00005496
Dale Johannesen0488fb62010-09-30 23:57:10 +00005497 if (X86::isMOVSLDUPMask(SVOp) && HasSSE3 && V2IsUndef && NumElems == 4)
5498 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00005499
Dale Johannesen0488fb62010-09-30 23:57:10 +00005500 if (X86::isMOVLPMask(SVOp))
5501 return getMOVLP(Op, dl, DAG, HasSSE2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005502
Nate Begeman9008ca62009-04-27 18:41:29 +00005503 if (ShouldXformToMOVHLPS(SVOp) ||
5504 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
5505 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005506
Evan Chengf26ffe92008-05-29 08:22:04 +00005507 if (isShift) {
5508 // No better options. Use a vshl / vsrl.
Dan Gohman8a55ce42009-09-23 21:02:20 +00005509 EVT EltVT = VT.getVectorElementType();
5510 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00005511 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005512 }
Eric Christopherfd179292009-08-27 18:07:15 +00005513
Evan Cheng9eca5e82006-10-25 21:49:50 +00005514 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00005515 // FIXME: This should also accept a bitcast of a splat? Be careful, not
5516 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00005517 V1IsSplat = isSplatVector(V1.getNode());
5518 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00005519
Chris Lattner8a594482007-11-25 00:24:49 +00005520 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00005521 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005522 Op = CommuteVectorShuffle(SVOp, DAG);
5523 SVOp = cast<ShuffleVectorSDNode>(Op);
5524 V1 = SVOp->getOperand(0);
5525 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00005526 std::swap(V1IsSplat, V2IsSplat);
5527 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00005528 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00005529 }
5530
Nate Begeman9008ca62009-04-27 18:41:29 +00005531 if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) {
5532 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00005533 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00005534 return V1;
5535 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
5536 // the instruction selector will not match, so get a canonical MOVL with
5537 // swapped operands to undo the commute.
5538 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00005539 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005540
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005541 if (X86::isUNPCKLMask(SVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005542 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005543
5544 if (X86::isUNPCKHMask(SVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005545 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V2, DAG);
Evan Chenge1113032006-10-04 18:33:38 +00005546
Evan Cheng9bbbb982006-10-25 20:48:19 +00005547 if (V2IsSplat) {
5548 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005549 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00005550 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00005551 SDValue NewMask = NormalizeMask(SVOp, DAG);
5552 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
5553 if (NSVOp != SVOp) {
5554 if (X86::isUNPCKLMask(NSVOp, true)) {
5555 return NewMask;
5556 } else if (X86::isUNPCKHMask(NSVOp, true)) {
5557 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005558 }
5559 }
5560 }
5561
Evan Cheng9eca5e82006-10-25 21:49:50 +00005562 if (Commuted) {
5563 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00005564 // FIXME: this seems wrong.
5565 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
5566 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005567
5568 if (X86::isUNPCKLMask(NewSVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005569 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V2, V1, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005570
5571 if (X86::isUNPCKHMask(NewSVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005572 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V2, V1, DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00005573 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005574
Nate Begeman9008ca62009-04-27 18:41:29 +00005575 // Normalize the node to match x86 shuffle ops if needed
Dale Johannesen0488fb62010-09-30 23:57:10 +00005576 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp))
Nate Begeman9008ca62009-04-27 18:41:29 +00005577 return CommuteVectorShuffle(SVOp, DAG);
5578
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00005579 // The checks below are all present in isShuffleMaskLegal, but they are
5580 // inlined here right now to enable us to directly emit target specific
5581 // nodes, and remove one by one until they don't return Op anymore.
5582 SmallVector<int, 16> M;
5583 SVOp->getMask(M);
5584
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00005585 if (isPALIGNRMask(M, VT, HasSSSE3))
5586 return getTargetShuffleNode(X86ISD::PALIGN, dl, VT, V1, V2,
5587 X86::getShufflePALIGNRImmediate(SVOp),
5588 DAG);
5589
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00005590 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
5591 SVOp->getSplatIndex() == 0 && V2IsUndef) {
5592 if (VT == MVT::v2f64)
5593 return getTargetShuffleNode(X86ISD::UNPCKLPD, dl, VT, V1, V1, DAG);
5594 if (VT == MVT::v2i64)
5595 return getTargetShuffleNode(X86ISD::PUNPCKLQDQ, dl, VT, V1, V1, DAG);
5596 }
5597
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00005598 if (isPSHUFHWMask(M, VT))
5599 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
5600 X86::getShufflePSHUFHWImmediate(SVOp),
5601 DAG);
5602
5603 if (isPSHUFLWMask(M, VT))
5604 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
5605 X86::getShufflePSHUFLWImmediate(SVOp),
5606 DAG);
5607
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00005608 if (isSHUFPMask(M, VT)) {
5609 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
5610 if (VT == MVT::v4f32 || VT == MVT::v4i32)
5611 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V1, V2,
5612 TargetMask, DAG);
5613 if (VT == MVT::v2f64 || VT == MVT::v2i64)
5614 return getTargetShuffleNode(X86ISD::SHUFPD, dl, VT, V1, V2,
5615 TargetMask, DAG);
5616 }
5617
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005618 if (X86::isUNPCKL_v_undef_Mask(SVOp))
5619 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5620 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V1, DAG);
5621 if (X86::isUNPCKH_v_undef_Mask(SVOp))
5622 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5623 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
5624
Evan Cheng14b32e12007-12-11 01:46:18 +00005625 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
Owen Anderson825b72b2009-08-11 20:47:22 +00005626 if (VT == MVT::v8i16) {
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005627 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00005628 if (NewOp.getNode())
Evan Cheng14b32e12007-12-11 01:46:18 +00005629 return NewOp;
5630 }
5631
Owen Anderson825b72b2009-08-11 20:47:22 +00005632 if (VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005633 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005634 if (NewOp.getNode())
5635 return NewOp;
5636 }
Eric Christopherfd179292009-08-27 18:07:15 +00005637
Dale Johannesen0488fb62010-09-30 23:57:10 +00005638 // Handle all 4 wide cases with a number of shuffles.
5639 if (NumElems == 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00005640 return LowerVECTOR_SHUFFLE_4wide(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005641
Dan Gohman475871a2008-07-27 21:46:04 +00005642 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005643}
5644
Dan Gohman475871a2008-07-27 21:46:04 +00005645SDValue
5646X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00005647 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005648 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005649 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005650 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005651 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005652 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00005653 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005654 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00005655 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005656 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00005657 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
5658 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
5659 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005660 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
5661 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005662 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005663 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00005664 Op.getOperand(0)),
5665 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00005666 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005667 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00005668 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005669 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00005670 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Owen Anderson825b72b2009-08-11 20:47:22 +00005671 } else if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00005672 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
5673 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00005674 // result has a single use which is a store or a bitcast to i32. And in
5675 // the case of a store, it's not worth it if the index is a constant 0,
5676 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00005677 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00005678 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00005679 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00005680 if ((User->getOpcode() != ISD::STORE ||
5681 (isa<ConstantSDNode>(Op.getOperand(1)) &&
5682 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005683 (User->getOpcode() != ISD::BITCAST ||
Owen Anderson825b72b2009-08-11 20:47:22 +00005684 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00005685 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00005686 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005687 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00005688 Op.getOperand(0)),
5689 Op.getOperand(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005690 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Extract);
Owen Anderson825b72b2009-08-11 20:47:22 +00005691 } else if (VT == MVT::i32) {
Mon P Wangf0fcdd82009-01-15 21:10:20 +00005692 // ExtractPS works with constant index.
5693 if (isa<ConstantSDNode>(Op.getOperand(1)))
5694 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00005695 }
Dan Gohman475871a2008-07-27 21:46:04 +00005696 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00005697}
5698
5699
Dan Gohman475871a2008-07-27 21:46:04 +00005700SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005701X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
5702 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005703 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00005704 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005705
Evan Cheng62a3f152008-03-24 21:52:23 +00005706 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005707 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00005708 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00005709 return Res;
5710 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00005711
Owen Andersone50ed302009-08-10 22:56:29 +00005712 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005713 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005714 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005715 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00005716 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005717 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00005718 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005719 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
5720 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005721 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005722 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00005723 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005724 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00005725 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00005726 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00005727 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00005728 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00005729 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00005730 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005731 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005732 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005733 if (Idx == 0)
5734 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00005735
Evan Cheng0db9fe62006-04-25 20:13:52 +00005736 // SHUFPS the element to the lowest double word, then movss.
Nate Begeman9008ca62009-04-27 18:41:29 +00005737 int Mask[4] = { Idx, -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00005738 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00005739 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00005740 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00005741 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00005742 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00005743 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00005744 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
5745 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
5746 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005747 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005748 if (Idx == 0)
5749 return Op;
5750
5751 // UNPCKHPD the element to the lowest double word, then movsd.
5752 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
5753 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00005754 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00005755 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00005756 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00005757 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00005758 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00005759 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005760 }
5761
Dan Gohman475871a2008-07-27 21:46:04 +00005762 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005763}
5764
Dan Gohman475871a2008-07-27 21:46:04 +00005765SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005766X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op,
5767 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005768 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00005769 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005770 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00005771
Dan Gohman475871a2008-07-27 21:46:04 +00005772 SDValue N0 = Op.getOperand(0);
5773 SDValue N1 = Op.getOperand(1);
5774 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00005775
Dan Gohman8a55ce42009-09-23 21:02:20 +00005776 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00005777 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00005778 unsigned Opc;
5779 if (VT == MVT::v8i16)
5780 Opc = X86ISD::PINSRW;
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00005781 else if (VT == MVT::v16i8)
5782 Opc = X86ISD::PINSRB;
5783 else
5784 Opc = X86ISD::PINSRB;
5785
Nate Begeman14d12ca2008-02-11 04:19:36 +00005786 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
5787 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00005788 if (N1.getValueType() != MVT::i32)
5789 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
5790 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005791 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00005792 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00005793 } else if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00005794 // Bits [7:6] of the constant are the source select. This will always be
5795 // zero here. The DAG Combiner may combine an extract_elt index into these
5796 // bits. For example (insert (extract, 3), 2) could be matched by putting
5797 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00005798 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00005799 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00005800 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00005801 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005802 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00005803 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00005804 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00005805 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00005806 } else if (EltVT == MVT::i32 && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00005807 // PINSR* works with constant index.
5808 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00005809 }
Dan Gohman475871a2008-07-27 21:46:04 +00005810 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00005811}
5812
Dan Gohman475871a2008-07-27 21:46:04 +00005813SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005814X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005815 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00005816 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00005817
5818 if (Subtarget->hasSSE41())
5819 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
5820
Dan Gohman8a55ce42009-09-23 21:02:20 +00005821 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00005822 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00005823
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005824 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00005825 SDValue N0 = Op.getOperand(0);
5826 SDValue N1 = Op.getOperand(1);
5827 SDValue N2 = Op.getOperand(2);
Evan Cheng794405e2007-12-12 07:55:34 +00005828
Dan Gohman8a55ce42009-09-23 21:02:20 +00005829 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00005830 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
5831 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00005832 if (N1.getValueType() != MVT::i32)
5833 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
5834 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005835 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesen0488fb62010-09-30 23:57:10 +00005836 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005837 }
Dan Gohman475871a2008-07-27 21:46:04 +00005838 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005839}
5840
Dan Gohman475871a2008-07-27 21:46:04 +00005841SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005842X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005843 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00005844
Chris Lattnerf172ecd2010-07-04 23:07:25 +00005845 if (Op.getValueType() == MVT::v1i64 &&
5846 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00005847 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00005848
Owen Anderson825b72b2009-08-11 20:47:22 +00005849 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Dale Johannesen0488fb62010-09-30 23:57:10 +00005850 assert(Op.getValueType().getSimpleVT().getSizeInBits() == 128 &&
5851 "Expected an SSE type!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005852 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(),
Dale Johannesen0488fb62010-09-30 23:57:10 +00005853 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005854}
5855
Bill Wendling056292f2008-09-16 21:48:12 +00005856// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
5857// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
5858// one of the above mentioned nodes. It has to be wrapped because otherwise
5859// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
5860// be used to form addressing mode. These wrapped nodes will be selected
5861// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00005862SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005863X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005864 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00005865
Chris Lattner41621a22009-06-26 19:22:52 +00005866 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
5867 // global base reg.
5868 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00005869 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005870 CodeModel::Model M = getTargetMachine().getCodeModel();
5871
Chris Lattner4f066492009-07-11 20:29:19 +00005872 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005873 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00005874 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00005875 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005876 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00005877 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005878 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00005879
Evan Cheng1606e8e2009-03-13 07:51:59 +00005880 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00005881 CP->getAlignment(),
5882 CP->getOffset(), OpFlag);
5883 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00005884 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00005885 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00005886 if (OpFlag) {
5887 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00005888 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00005889 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00005890 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005891 }
5892
5893 return Result;
5894}
5895
Dan Gohmand858e902010-04-17 15:26:15 +00005896SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00005897 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00005898
Chris Lattner18c59872009-06-27 04:16:01 +00005899 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
5900 // global base reg.
5901 unsigned char OpFlag = 0;
5902 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005903 CodeModel::Model M = getTargetMachine().getCodeModel();
5904
Chris Lattner4f066492009-07-11 20:29:19 +00005905 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005906 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00005907 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00005908 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005909 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00005910 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005911 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00005912
Chris Lattner18c59872009-06-27 04:16:01 +00005913 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
5914 OpFlag);
5915 DebugLoc DL = JT->getDebugLoc();
5916 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00005917
Chris Lattner18c59872009-06-27 04:16:01 +00005918 // With PIC, the address is actually $g + Offset.
Chris Lattner1e61e692010-11-15 02:46:57 +00005919 if (OpFlag)
Chris Lattner18c59872009-06-27 04:16:01 +00005920 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
5921 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00005922 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00005923 Result);
Eric Christopherfd179292009-08-27 18:07:15 +00005924
Chris Lattner18c59872009-06-27 04:16:01 +00005925 return Result;
5926}
5927
5928SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005929X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00005930 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00005931
Chris Lattner18c59872009-06-27 04:16:01 +00005932 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
5933 // global base reg.
5934 unsigned char OpFlag = 0;
5935 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005936 CodeModel::Model M = getTargetMachine().getCodeModel();
5937
Chris Lattner4f066492009-07-11 20:29:19 +00005938 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005939 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00005940 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00005941 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005942 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00005943 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005944 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00005945
Chris Lattner18c59872009-06-27 04:16:01 +00005946 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00005947
Chris Lattner18c59872009-06-27 04:16:01 +00005948 DebugLoc DL = Op.getDebugLoc();
5949 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00005950
5951
Chris Lattner18c59872009-06-27 04:16:01 +00005952 // With PIC, the address is actually $g + Offset.
5953 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00005954 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00005955 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
5956 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00005957 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00005958 Result);
5959 }
Eric Christopherfd179292009-08-27 18:07:15 +00005960
Chris Lattner18c59872009-06-27 04:16:01 +00005961 return Result;
5962}
5963
Dan Gohman475871a2008-07-27 21:46:04 +00005964SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005965X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00005966 // Create the TargetBlockAddressAddress node.
5967 unsigned char OpFlags =
5968 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00005969 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00005970 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Dan Gohman29cbade2009-11-20 23:18:13 +00005971 DebugLoc dl = Op.getDebugLoc();
5972 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(),
5973 /*isTarget=*/true, OpFlags);
5974
Dan Gohmanf705adb2009-10-30 01:28:02 +00005975 if (Subtarget->isPICStyleRIPRel() &&
5976 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00005977 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
5978 else
5979 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00005980
Dan Gohman29cbade2009-11-20 23:18:13 +00005981 // With PIC, the address is actually $g + Offset.
5982 if (isGlobalRelativeToPICBase(OpFlags)) {
5983 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
5984 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
5985 Result);
5986 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00005987
5988 return Result;
5989}
5990
5991SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00005992X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00005993 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00005994 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00005995 // Create the TargetGlobalAddress node, folding in the constant
5996 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00005997 unsigned char OpFlags =
5998 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005999 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00006000 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006001 if (OpFlags == X86II::MO_NO_FLAG &&
6002 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00006003 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00006004 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00006005 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00006006 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00006007 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00006008 }
Eric Christopherfd179292009-08-27 18:07:15 +00006009
Chris Lattner4f066492009-07-11 20:29:19 +00006010 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006011 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00006012 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
6013 else
6014 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00006015
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006016 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00006017 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00006018 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
6019 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006020 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006021 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006022
Chris Lattner36c25012009-07-10 07:34:39 +00006023 // For globals that require a load from a stub to get the address, emit the
6024 // load.
6025 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00006026 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00006027 MachinePointerInfo::getGOT(), false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006028
Dan Gohman6520e202008-10-18 02:06:02 +00006029 // If there was a non-zero offset that we didn't fold, create an explicit
6030 // addition for it.
6031 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00006032 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00006033 DAG.getConstant(Offset, getPointerTy()));
6034
Evan Cheng0db9fe62006-04-25 20:13:52 +00006035 return Result;
6036}
6037
Evan Chengda43bcf2008-09-24 00:05:32 +00006038SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006039X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00006040 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00006041 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006042 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00006043}
6044
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006045static SDValue
6046GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00006047 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Chris Lattnerb903bed2009-06-26 21:20:29 +00006048 unsigned char OperandFlags) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006049 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Owen Anderson825b72b2009-08-11 20:47:22 +00006050 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006051 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00006052 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006053 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00006054 GA->getOffset(),
6055 OperandFlags);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006056 if (InFlag) {
6057 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00006058 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006059 } else {
6060 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00006061 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006062 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006063
6064 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00006065 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006066
Rafael Espindola15f1b662009-04-24 12:59:40 +00006067 SDValue Flag = Chain.getValue(1);
6068 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006069}
6070
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006071// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00006072static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006073LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006074 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00006075 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00006076 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
6077 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006078 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006079 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006080 InFlag = Chain.getValue(1);
6081
Chris Lattnerb903bed2009-06-26 21:20:29 +00006082 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006083}
6084
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006085// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00006086static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006087LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006088 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00006089 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
6090 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006091}
6092
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006093// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
6094// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00006095static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006096 const EVT PtrVT, TLSModel::Model model,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00006097 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00006098 DebugLoc dl = GA->getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00006099
Chris Lattnerf93b90c2010-09-22 04:39:11 +00006100 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
6101 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
6102 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00006103
Michael J. Spencerec38de22010-10-10 22:04:20 +00006104 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Chris Lattnerf93b90c2010-09-22 04:39:11 +00006105 DAG.getIntPtrConstant(0),
6106 MachinePointerInfo(Ptr), false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00006107
Chris Lattnerb903bed2009-06-26 21:20:29 +00006108 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00006109 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
6110 // initialexec.
6111 unsigned WrapperKind = X86ISD::Wrapper;
6112 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00006113 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Chris Lattner18c59872009-06-27 04:16:01 +00006114 } else if (is64Bit) {
6115 assert(model == TLSModel::InitialExec);
6116 OperandFlags = X86II::MO_GOTTPOFF;
6117 WrapperKind = X86ISD::WrapperRIP;
6118 } else {
6119 assert(model == TLSModel::InitialExec);
6120 OperandFlags = X86II::MO_INDNTPOFF;
Chris Lattnerb903bed2009-06-26 21:20:29 +00006121 }
Eric Christopherfd179292009-08-27 18:07:15 +00006122
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006123 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
6124 // exec)
Michael J. Spencerec38de22010-10-10 22:04:20 +00006125 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Devang Patel0d881da2010-07-06 22:08:15 +00006126 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00006127 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00006128 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00006129
Rafael Espindola9a580232009-02-27 13:37:18 +00006130 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00006131 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00006132 MachinePointerInfo::getGOT(), false, false, 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00006133
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006134 // The address of the thread local variable is the add of the thread
6135 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00006136 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006137}
6138
Dan Gohman475871a2008-07-27 21:46:04 +00006139SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006140X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00006141
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006142 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00006143 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00006144
Eric Christopher30ef0e52010-06-03 04:07:48 +00006145 if (Subtarget->isTargetELF()) {
6146 // TODO: implement the "local dynamic" model
6147 // TODO: implement the "initial exec"model for pic executables
Michael J. Spencerec38de22010-10-10 22:04:20 +00006148
Eric Christopher30ef0e52010-06-03 04:07:48 +00006149 // If GV is an alias then use the aliasee for determining
6150 // thread-localness.
6151 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
6152 GV = GA->resolveAliasedGlobal(false);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006153
6154 TLSModel::Model model
Eric Christopher30ef0e52010-06-03 04:07:48 +00006155 = getTLSModel(GV, getTargetMachine().getRelocationModel());
Michael J. Spencerec38de22010-10-10 22:04:20 +00006156
Eric Christopher30ef0e52010-06-03 04:07:48 +00006157 switch (model) {
6158 case TLSModel::GeneralDynamic:
6159 case TLSModel::LocalDynamic: // not implemented
6160 if (Subtarget->is64Bit())
6161 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
6162 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Michael J. Spencerec38de22010-10-10 22:04:20 +00006163
Eric Christopher30ef0e52010-06-03 04:07:48 +00006164 case TLSModel::InitialExec:
6165 case TLSModel::LocalExec:
6166 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
6167 Subtarget->is64Bit());
6168 }
6169 } else if (Subtarget->isTargetDarwin()) {
6170 // Darwin only has one model of TLS. Lower to that.
6171 unsigned char OpFlag = 0;
6172 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
6173 X86ISD::WrapperRIP : X86ISD::Wrapper;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006174
Eric Christopher30ef0e52010-06-03 04:07:48 +00006175 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6176 // global base reg.
6177 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
6178 !Subtarget->is64Bit();
6179 if (PIC32)
6180 OpFlag = X86II::MO_TLVP_PIC_BASE;
6181 else
6182 OpFlag = X86II::MO_TLVP;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006183 DebugLoc DL = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00006184 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopherd8c05362010-12-09 06:25:53 +00006185 GA->getValueType(0),
Eric Christopher30ef0e52010-06-03 04:07:48 +00006186 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00006187 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006188
Eric Christopher30ef0e52010-06-03 04:07:48 +00006189 // With PIC32, the address is actually $g + Offset.
6190 if (PIC32)
6191 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
6192 DAG.getNode(X86ISD::GlobalBaseReg,
6193 DebugLoc(), getPointerTy()),
6194 Offset);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006195
Eric Christopher30ef0e52010-06-03 04:07:48 +00006196 // Lowering the machine isd will make sure everything is in the right
6197 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00006198 SDValue Chain = DAG.getEntryNode();
6199 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
6200 SDValue Args[] = { Chain, Offset };
6201 Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006202
Eric Christopher30ef0e52010-06-03 04:07:48 +00006203 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
6204 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
6205 MFI->setAdjustsStack(true);
Eric Christopher8bce7cc2010-12-09 00:27:58 +00006206
Eric Christopher30ef0e52010-06-03 04:07:48 +00006207 // And our return value (tls address) is in the standard call return value
6208 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00006209 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
6210 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy());
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006211 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00006212
Eric Christopher30ef0e52010-06-03 04:07:48 +00006213 assert(false &&
6214 "TLS not implemented for this target.");
Eric Christopherfd179292009-08-27 18:07:15 +00006215
Torok Edwinc23197a2009-07-14 16:55:14 +00006216 llvm_unreachable("Unreachable");
Chris Lattner5867de12009-04-01 22:14:45 +00006217 return SDValue();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006218}
6219
Evan Cheng0db9fe62006-04-25 20:13:52 +00006220
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006221/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michelfdc40a02009-02-17 22:15:04 +00006222/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00006223SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman4c1fa612008-03-03 22:22:09 +00006224 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00006225 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006226 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006227 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006228 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00006229 SDValue ShOpLo = Op.getOperand(0);
6230 SDValue ShOpHi = Op.getOperand(1);
6231 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00006232 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00006233 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00006234 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00006235
Dan Gohman475871a2008-07-27 21:46:04 +00006236 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006237 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00006238 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
6239 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006240 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00006241 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
6242 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006243 }
Evan Chenge3413162006-01-09 18:33:28 +00006244
Owen Anderson825b72b2009-08-11 20:47:22 +00006245 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
6246 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00006247 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00006248 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00006249
Dan Gohman475871a2008-07-27 21:46:04 +00006250 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00006251 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00006252 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
6253 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00006254
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006255 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00006256 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
6257 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006258 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00006259 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
6260 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006261 }
6262
Dan Gohman475871a2008-07-27 21:46:04 +00006263 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00006264 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006265}
Evan Chenga3195e82006-01-12 22:54:21 +00006266
Dan Gohmand858e902010-04-17 15:26:15 +00006267SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
6268 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006269 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00006270
Dale Johannesen0488fb62010-09-30 23:57:10 +00006271 if (SrcVT.isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00006272 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00006273
Owen Anderson825b72b2009-08-11 20:47:22 +00006274 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00006275 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00006276
Eli Friedman36df4992009-05-27 00:47:34 +00006277 // These are really Legal; return the operand so the caller accepts it as
6278 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00006279 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00006280 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00006281 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00006282 Subtarget->is64Bit()) {
6283 return Op;
6284 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006285
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006286 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006287 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006288 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00006289 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006290 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00006291 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00006292 StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006293 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006294 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00006295 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
6296}
Evan Cheng0db9fe62006-04-25 20:13:52 +00006297
Owen Andersone50ed302009-08-10 22:56:29 +00006298SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Michael J. Spencerec38de22010-10-10 22:04:20 +00006299 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00006300 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006301 // Build the FILD
Chris Lattner492a43e2010-09-22 01:28:21 +00006302 DebugLoc DL = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00006303 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00006304 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006305 if (useSSE)
Owen Anderson825b72b2009-08-11 20:47:22 +00006306 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
Chris Lattner5a88b832007-02-25 07:10:00 +00006307 else
Owen Anderson825b72b2009-08-11 20:47:22 +00006308 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006309
Chris Lattner492a43e2010-09-22 01:28:21 +00006310 unsigned ByteSize = SrcVT.getSizeInBits()/8;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006311
Chris Lattner492a43e2010-09-22 01:28:21 +00006312 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
6313 MachineMemOperand *MMO =
6314 DAG.getMachineFunction()
6315 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6316 MachineMemOperand::MOLoad, ByteSize, ByteSize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006317
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006318 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Chris Lattner492a43e2010-09-22 01:28:21 +00006319 SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
6320 X86ISD::FILD, DL,
6321 Tys, Ops, array_lengthof(Ops),
6322 SrcVT, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006323
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006324 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006325 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00006326 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006327
6328 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
6329 // shouldn't be necessary except that RFP cannot be live across
6330 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006331 MachineFunction &MF = DAG.getMachineFunction();
Bob Wilsoneafca4e2010-09-22 17:35:14 +00006332 unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
6333 int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006334 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00006335 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006336 SDValue Ops[] = {
6337 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
6338 };
Chris Lattner492a43e2010-09-22 01:28:21 +00006339 MachineMemOperand *MMO =
6340 DAG.getMachineFunction()
6341 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
Bob Wilsoneafca4e2010-09-22 17:35:14 +00006342 MachineMemOperand::MOStore, SSFISize, SSFISize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006343
Chris Lattner492a43e2010-09-22 01:28:21 +00006344 Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
6345 Ops, array_lengthof(Ops),
6346 Op.getValueType(), MMO);
6347 Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006348 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006349 false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006350 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006351
Evan Cheng0db9fe62006-04-25 20:13:52 +00006352 return Result;
6353}
6354
Bill Wendling8b8a6362009-01-17 03:56:04 +00006355// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00006356SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
6357 SelectionDAG &DAG) const {
Bill Wendling8b8a6362009-01-17 03:56:04 +00006358 // This algorithm is not obvious. Here it is in C code, more or less:
6359 /*
6360 double uint64_to_double( uint32_t hi, uint32_t lo ) {
6361 static const __m128i exp = { 0x4330000045300000ULL, 0 };
6362 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00006363
Bill Wendling8b8a6362009-01-17 03:56:04 +00006364 // Copy ints to xmm registers.
6365 __m128i xh = _mm_cvtsi32_si128( hi );
6366 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00006367
Bill Wendling8b8a6362009-01-17 03:56:04 +00006368 // Combine into low half of a single xmm register.
6369 __m128i x = _mm_unpacklo_epi32( xh, xl );
6370 __m128d d;
6371 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00006372
Bill Wendling8b8a6362009-01-17 03:56:04 +00006373 // Merge in appropriate exponents to give the integer bits the right
6374 // magnitude.
6375 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00006376
Bill Wendling8b8a6362009-01-17 03:56:04 +00006377 // Subtract away the biases to deal with the IEEE-754 double precision
6378 // implicit 1.
6379 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00006380
Bill Wendling8b8a6362009-01-17 03:56:04 +00006381 // All conversions up to here are exact. The correctly rounded result is
6382 // calculated using the current rounding mode using the following
6383 // horizontal add.
6384 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
6385 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
6386 // store doesn't really need to be here (except
6387 // maybe to zero the other double)
6388 return sd;
6389 }
6390 */
Dale Johannesen040225f2008-10-21 23:07:49 +00006391
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006392 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00006393 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00006394
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006395 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00006396 std::vector<Constant*> CV0;
Owen Andersoneed707b2009-07-24 23:12:02 +00006397 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x45300000)));
6398 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x43300000)));
6399 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
6400 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
Owen Andersonaf7ec972009-07-28 21:19:26 +00006401 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006402 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006403
Bill Wendling8b8a6362009-01-17 03:56:04 +00006404 std::vector<Constant*> CV1;
Owen Andersona90b3dc2009-07-15 21:51:10 +00006405 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006406 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
Owen Andersona90b3dc2009-07-15 21:51:10 +00006407 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006408 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Owen Andersonaf7ec972009-07-28 21:19:26 +00006409 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006410 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006411
Owen Anderson825b72b2009-08-11 20:47:22 +00006412 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6413 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00006414 Op.getOperand(0),
6415 DAG.getIntPtrConstant(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006416 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6417 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00006418 Op.getOperand(0),
6419 DAG.getIntPtrConstant(0)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006420 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
6421 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Chris Lattnere8639032010-09-21 06:22:23 +00006422 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006423 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00006424 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006425 SDValue XR2F = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Unpck2);
Owen Anderson825b72b2009-08-11 20:47:22 +00006426 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00006427 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006428 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00006429 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006430
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006431 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman9008ca62009-04-27 18:41:29 +00006432 int ShufMask[2] = { 1, -1 };
Owen Anderson825b72b2009-08-11 20:47:22 +00006433 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
6434 DAG.getUNDEF(MVT::v2f64), ShufMask);
6435 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
6436 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006437 DAG.getIntPtrConstant(0));
6438}
6439
Bill Wendling8b8a6362009-01-17 03:56:04 +00006440// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00006441SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
6442 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006443 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00006444 // FP constant to bias correct the final result.
6445 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00006446 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006447
6448 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00006449 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6450 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Bill Wendling8b8a6362009-01-17 03:56:04 +00006451 Op.getOperand(0),
6452 DAG.getIntPtrConstant(0)));
6453
Owen Anderson825b72b2009-08-11 20:47:22 +00006454 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006455 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00006456 DAG.getIntPtrConstant(0));
6457
6458 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00006459 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006460 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00006461 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006462 MVT::v2f64, Load)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006463 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00006464 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006465 MVT::v2f64, Bias)));
6466 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006467 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00006468 DAG.getIntPtrConstant(0));
6469
6470 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00006471 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006472
6473 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00006474 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00006475
Owen Anderson825b72b2009-08-11 20:47:22 +00006476 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006477 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00006478 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00006479 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006480 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00006481 }
6482
6483 // Handle final rounding.
6484 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00006485}
6486
Dan Gohmand858e902010-04-17 15:26:15 +00006487SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
6488 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00006489 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006490 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00006491
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006492 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00006493 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
6494 // the optimization here.
6495 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00006496 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00006497
Owen Andersone50ed302009-08-10 22:56:29 +00006498 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006499 EVT DstVT = Op.getValueType();
6500 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00006501 return LowerUINT_TO_FP_i64(Op, DAG);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006502 else if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00006503 return LowerUINT_TO_FP_i32(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00006504
6505 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00006506 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006507 if (SrcVT == MVT::i32) {
6508 SDValue WordOff = DAG.getConstant(4, getPointerTy());
6509 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
6510 getPointerTy(), StackSlot, WordOff);
6511 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006512 StackSlot, MachinePointerInfo(),
6513 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006514 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006515 OffsetSlot, MachinePointerInfo(),
6516 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006517 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
6518 return Fild;
6519 }
6520
6521 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
6522 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006523 StackSlot, MachinePointerInfo(),
6524 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006525 // For i64 source, we need to add the appropriate power of 2 if the input
6526 // was negative. This is the same as the optimization in
6527 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
6528 // we must be careful to do the computation in x87 extended precision, not
6529 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00006530 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
6531 MachineMemOperand *MMO =
6532 DAG.getMachineFunction()
6533 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6534 MachineMemOperand::MOLoad, 8, 8);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006535
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006536 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
6537 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Chris Lattner492a43e2010-09-22 01:28:21 +00006538 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, 3,
6539 MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006540
6541 APInt FF(32, 0x5F800000ULL);
6542
6543 // Check whether the sign bit is set.
6544 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
6545 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
6546 ISD::SETLT);
6547
6548 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
6549 SDValue FudgePtr = DAG.getConstantPool(
6550 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
6551 getPointerTy());
6552
6553 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
6554 SDValue Zero = DAG.getIntPtrConstant(0);
6555 SDValue Four = DAG.getIntPtrConstant(4);
6556 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
6557 Zero, Four);
6558 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
6559
6560 // Load the value out, extending it from f32 to f80.
6561 // FIXME: Avoid the extend by constructing the right constant pool?
Evan Chengbcc80172010-07-07 22:15:37 +00006562 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, MVT::f80, dl, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00006563 FudgePtr, MachinePointerInfo::getConstantPool(),
6564 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006565 // Extend everything to 80 bits to force it to be done on x87.
6566 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
6567 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00006568}
6569
Dan Gohman475871a2008-07-27 21:46:04 +00006570std::pair<SDValue,SDValue> X86TargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00006571FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) const {
Chris Lattner07290932010-09-22 01:05:16 +00006572 DebugLoc DL = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00006573
Owen Andersone50ed302009-08-10 22:56:29 +00006574 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00006575
6576 if (!IsSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006577 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
6578 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00006579 }
6580
Owen Anderson825b72b2009-08-11 20:47:22 +00006581 assert(DstTy.getSimpleVT() <= MVT::i64 &&
6582 DstTy.getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00006583 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00006584
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006585 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00006586 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00006587 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00006588 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00006589 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00006590 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00006591 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00006592 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006593
Evan Cheng87c89352007-10-15 20:11:21 +00006594 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
6595 // stack slot.
6596 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00006597 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00006598 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006599 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00006600
Michael J. Spencerec38de22010-10-10 22:04:20 +00006601
6602
Evan Cheng0db9fe62006-04-25 20:13:52 +00006603 unsigned Opc;
Owen Anderson825b72b2009-08-11 20:47:22 +00006604 switch (DstTy.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00006605 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
Owen Anderson825b72b2009-08-11 20:47:22 +00006606 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
6607 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
6608 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006609 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006610
Dan Gohman475871a2008-07-27 21:46:04 +00006611 SDValue Chain = DAG.getEntryNode();
6612 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00006613 EVT TheVT = Op.getOperand(0).getValueType();
6614 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006615 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00006616 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006617 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006618 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00006619 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00006620 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00006621 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00006622 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00006623
Chris Lattner492a43e2010-09-22 01:28:21 +00006624 MachineMemOperand *MMO =
6625 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6626 MachineMemOperand::MOLoad, MemSize, MemSize);
6627 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, 3,
6628 DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006629 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00006630 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006631 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
6632 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00006633
Chris Lattner07290932010-09-22 01:05:16 +00006634 MachineMemOperand *MMO =
6635 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6636 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006637
Evan Cheng0db9fe62006-04-25 20:13:52 +00006638 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00006639 SDValue Ops[] = { Chain, Value, StackSlot };
Chris Lattner07290932010-09-22 01:05:16 +00006640 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
6641 Ops, 3, DstTy, MMO);
Evan Chengd9558e02006-01-06 00:43:03 +00006642
Chris Lattner27a6c732007-11-24 07:07:01 +00006643 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006644}
6645
Dan Gohmand858e902010-04-17 15:26:15 +00006646SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
6647 SelectionDAG &DAG) const {
Dale Johannesen0488fb62010-09-30 23:57:10 +00006648 if (Op.getValueType().isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00006649 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00006650
Eli Friedman948e95a2009-05-23 09:59:16 +00006651 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00006652 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00006653 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
6654 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00006655
Chris Lattner27a6c732007-11-24 07:07:01 +00006656 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006657 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00006658 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00006659}
6660
Dan Gohmand858e902010-04-17 15:26:15 +00006661SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
6662 SelectionDAG &DAG) const {
Eli Friedman948e95a2009-05-23 09:59:16 +00006663 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
6664 SDValue FIST = Vals.first, StackSlot = Vals.second;
6665 assert(FIST.getNode() && "Unexpected failure");
6666
6667 // Load the result.
6668 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00006669 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00006670}
6671
Dan Gohmand858e902010-04-17 15:26:15 +00006672SDValue X86TargetLowering::LowerFABS(SDValue Op,
6673 SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00006674 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006675 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00006676 EVT VT = Op.getValueType();
6677 EVT EltVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00006678 if (VT.isVector())
6679 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006680 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00006681 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006682 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00006683 CV.push_back(C);
6684 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006685 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006686 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00006687 CV.push_back(C);
6688 CV.push_back(C);
6689 CV.push_back(C);
6690 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006691 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00006692 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006693 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006694 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00006695 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006696 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006697 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006698}
6699
Dan Gohmand858e902010-04-17 15:26:15 +00006700SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00006701 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006702 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00006703 EVT VT = Op.getValueType();
6704 EVT EltVT = VT;
Duncan Sandsda9ad382009-09-06 19:29:07 +00006705 if (VT.isVector())
Duncan Sands83ec4b62008-06-06 12:08:01 +00006706 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006707 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00006708 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006709 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00006710 CV.push_back(C);
6711 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006712 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006713 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00006714 CV.push_back(C);
6715 CV.push_back(C);
6716 CV.push_back(C);
6717 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006718 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00006719 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006720 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006721 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00006722 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006723 false, false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006724 if (VT.isVector()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006725 return DAG.getNode(ISD::BITCAST, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00006726 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006727 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00006728 Op.getOperand(0)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006729 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00006730 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00006731 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00006732 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006733}
6734
Dan Gohmand858e902010-04-17 15:26:15 +00006735SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00006736 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00006737 SDValue Op0 = Op.getOperand(0);
6738 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006739 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00006740 EVT VT = Op.getValueType();
6741 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00006742
6743 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00006744 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006745 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00006746 SrcVT = VT;
6747 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00006748 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00006749 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006750 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00006751 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00006752 }
6753
6754 // At this point the operands and the result should have the same
6755 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00006756
Evan Cheng68c47cb2007-01-05 07:55:56 +00006757 // First get the sign bit of second operand.
6758 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00006759 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006760 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
6761 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00006762 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006763 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
6764 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
6765 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
6766 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00006767 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00006768 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006769 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006770 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00006771 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006772 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006773 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00006774
6775 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00006776 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006777 // Op0 is MVT::f32, Op1 is MVT::f64.
6778 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
6779 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
6780 DAG.getConstant(32, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006781 SignBit = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, SignBit);
Owen Anderson825b72b2009-08-11 20:47:22 +00006782 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00006783 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00006784 }
6785
Evan Cheng73d6cf12007-01-05 21:37:56 +00006786 // Clear first operand sign bit.
6787 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00006788 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006789 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
6790 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00006791 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006792 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
6793 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
6794 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
6795 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00006796 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00006797 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006798 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006799 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00006800 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006801 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006802 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00006803
6804 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00006805 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00006806}
6807
Dan Gohman076aee32009-03-04 19:44:21 +00006808/// Emit nodes that will be selected as "test Op0,Op0", or something
6809/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00006810SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00006811 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00006812 DebugLoc dl = Op.getDebugLoc();
6813
Dan Gohman31125812009-03-07 01:58:32 +00006814 // CF and OF aren't always set the way we want. Determine which
6815 // of these we need.
6816 bool NeedCF = false;
6817 bool NeedOF = false;
6818 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006819 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00006820 case X86::COND_A: case X86::COND_AE:
6821 case X86::COND_B: case X86::COND_BE:
6822 NeedCF = true;
6823 break;
6824 case X86::COND_G: case X86::COND_GE:
6825 case X86::COND_L: case X86::COND_LE:
6826 case X86::COND_O: case X86::COND_NO:
6827 NeedOF = true;
6828 break;
Dan Gohman31125812009-03-07 01:58:32 +00006829 }
6830
Dan Gohman076aee32009-03-04 19:44:21 +00006831 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00006832 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
6833 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006834 if (Op.getResNo() != 0 || NeedOF || NeedCF)
6835 // Emit a CMP with 0, which is the TEST pattern.
6836 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
6837 DAG.getConstant(0, Op.getValueType()));
6838
6839 unsigned Opcode = 0;
6840 unsigned NumOperands = 0;
6841 switch (Op.getNode()->getOpcode()) {
6842 case ISD::ADD:
6843 // Due to an isel shortcoming, be conservative if this add is likely to be
6844 // selected as part of a load-modify-store instruction. When the root node
6845 // in a match is a store, isel doesn't know how to remap non-chain non-flag
6846 // uses of other nodes in the match, such as the ADD in this case. This
6847 // leads to the ADD being left around and reselected, with the result being
6848 // two adds in the output. Alas, even if none our users are stores, that
6849 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
6850 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
6851 // climbing the DAG back to the root, and it doesn't seem to be worth the
6852 // effort.
6853 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Dan Gohman076aee32009-03-04 19:44:21 +00006854 UE = Op.getNode()->use_end(); UI != UE; ++UI)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006855 if (UI->getOpcode() != ISD::CopyToReg && UI->getOpcode() != ISD::SETCC)
6856 goto default_case;
6857
6858 if (ConstantSDNode *C =
6859 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
6860 // An add of one will be selected as an INC.
6861 if (C->getAPIntValue() == 1) {
6862 Opcode = X86ISD::INC;
6863 NumOperands = 1;
6864 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00006865 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006866
6867 // An add of negative one (subtract of one) will be selected as a DEC.
6868 if (C->getAPIntValue().isAllOnesValue()) {
6869 Opcode = X86ISD::DEC;
6870 NumOperands = 1;
6871 break;
6872 }
Dan Gohman076aee32009-03-04 19:44:21 +00006873 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006874
6875 // Otherwise use a regular EFLAGS-setting add.
6876 Opcode = X86ISD::ADD;
6877 NumOperands = 2;
6878 break;
6879 case ISD::AND: {
6880 // If the primary and result isn't used, don't bother using X86ISD::AND,
6881 // because a TEST instruction will be better.
6882 bool NonFlagUse = false;
6883 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
6884 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
6885 SDNode *User = *UI;
6886 unsigned UOpNo = UI.getOperandNo();
6887 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
6888 // Look pass truncate.
6889 UOpNo = User->use_begin().getOperandNo();
6890 User = *User->use_begin();
6891 }
6892
6893 if (User->getOpcode() != ISD::BRCOND &&
6894 User->getOpcode() != ISD::SETCC &&
6895 (User->getOpcode() != ISD::SELECT || UOpNo != 0)) {
6896 NonFlagUse = true;
6897 break;
6898 }
Dan Gohman076aee32009-03-04 19:44:21 +00006899 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006900
6901 if (!NonFlagUse)
6902 break;
6903 }
6904 // FALL THROUGH
6905 case ISD::SUB:
6906 case ISD::OR:
6907 case ISD::XOR:
6908 // Due to the ISEL shortcoming noted above, be conservative if this op is
6909 // likely to be selected as part of a load-modify-store instruction.
6910 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
6911 UE = Op.getNode()->use_end(); UI != UE; ++UI)
6912 if (UI->getOpcode() == ISD::STORE)
6913 goto default_case;
6914
6915 // Otherwise use a regular EFLAGS-setting instruction.
6916 switch (Op.getNode()->getOpcode()) {
6917 default: llvm_unreachable("unexpected operator!");
6918 case ISD::SUB: Opcode = X86ISD::SUB; break;
6919 case ISD::OR: Opcode = X86ISD::OR; break;
6920 case ISD::XOR: Opcode = X86ISD::XOR; break;
6921 case ISD::AND: Opcode = X86ISD::AND; break;
6922 }
6923
6924 NumOperands = 2;
6925 break;
6926 case X86ISD::ADD:
6927 case X86ISD::SUB:
6928 case X86ISD::INC:
6929 case X86ISD::DEC:
6930 case X86ISD::OR:
6931 case X86ISD::XOR:
6932 case X86ISD::AND:
6933 return SDValue(Op.getNode(), 1);
6934 default:
6935 default_case:
6936 break;
Dan Gohman076aee32009-03-04 19:44:21 +00006937 }
6938
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006939 if (Opcode == 0)
6940 // Emit a CMP with 0, which is the TEST pattern.
6941 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
6942 DAG.getConstant(0, Op.getValueType()));
6943
6944 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
6945 SmallVector<SDValue, 4> Ops;
6946 for (unsigned i = 0; i != NumOperands; ++i)
6947 Ops.push_back(Op.getOperand(i));
6948
6949 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
6950 DAG.ReplaceAllUsesWith(Op, New);
6951 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00006952}
6953
6954/// Emit nodes that will be selected as "cmp Op0,Op1", or something
6955/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00006956SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00006957 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00006958 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
6959 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00006960 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00006961
6962 DebugLoc dl = Op0.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00006963 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00006964}
6965
Evan Chengd40d03e2010-01-06 19:38:29 +00006966/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
6967/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00006968SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
6969 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00006970 SDValue Op0 = And.getOperand(0);
6971 SDValue Op1 = And.getOperand(1);
6972 if (Op0.getOpcode() == ISD::TRUNCATE)
6973 Op0 = Op0.getOperand(0);
6974 if (Op1.getOpcode() == ISD::TRUNCATE)
6975 Op1 = Op1.getOperand(0);
6976
Evan Chengd40d03e2010-01-06 19:38:29 +00006977 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00006978 if (Op1.getOpcode() == ISD::SHL)
6979 std::swap(Op0, Op1);
6980 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00006981 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
6982 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00006983 // If we looked past a truncate, check that it's only truncating away
6984 // known zeros.
6985 unsigned BitWidth = Op0.getValueSizeInBits();
6986 unsigned AndBitWidth = And.getValueSizeInBits();
6987 if (BitWidth > AndBitWidth) {
6988 APInt Mask = APInt::getAllOnesValue(BitWidth), Zeros, Ones;
6989 DAG.ComputeMaskedBits(Op0, Mask, Zeros, Ones);
6990 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
6991 return SDValue();
6992 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00006993 LHS = Op1;
6994 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00006995 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00006996 } else if (Op1.getOpcode() == ISD::Constant) {
6997 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
6998 SDValue AndLHS = Op0;
Evan Chengd40d03e2010-01-06 19:38:29 +00006999 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
7000 LHS = AndLHS.getOperand(0);
7001 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00007002 }
Evan Chengd40d03e2010-01-06 19:38:29 +00007003 }
Evan Cheng0488db92007-09-25 01:57:46 +00007004
Evan Chengd40d03e2010-01-06 19:38:29 +00007005 if (LHS.getNode()) {
Evan Chenge5b51ac2010-04-17 06:13:15 +00007006 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00007007 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00007008 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00007009 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00007010 // Also promote i16 to i32 for performance / code size reason.
7011 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00007012 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00007013 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00007014
Evan Chengd40d03e2010-01-06 19:38:29 +00007015 // If the operand types disagree, extend the shift amount to match. Since
7016 // BT ignores high bits (like shifts) we can use anyextend.
7017 if (LHS.getValueType() != RHS.getValueType())
7018 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00007019
Evan Chengd40d03e2010-01-06 19:38:29 +00007020 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
7021 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
7022 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
7023 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00007024 }
7025
Evan Cheng54de3ea2010-01-05 06:52:31 +00007026 return SDValue();
7027}
7028
Dan Gohmand858e902010-04-17 15:26:15 +00007029SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng54de3ea2010-01-05 06:52:31 +00007030 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
7031 SDValue Op0 = Op.getOperand(0);
7032 SDValue Op1 = Op.getOperand(1);
7033 DebugLoc dl = Op.getDebugLoc();
7034 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
7035
7036 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00007037 // Lower (X & (1 << N)) == 0 to BT(X, N).
7038 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
7039 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
7040 if (Op0.getOpcode() == ISD::AND &&
7041 Op0.hasOneUse() &&
7042 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane368b462010-06-18 14:22:04 +00007043 cast<ConstantSDNode>(Op1)->isNullValue() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00007044 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
7045 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
7046 if (NewSetCC.getNode())
7047 return NewSetCC;
7048 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00007049
Chris Lattnerb20e0b12010-12-05 07:30:36 +00007050 // Look for "(setcc) == / != 1" to avoid unnecessary setcc.
Evan Cheng2c755ba2010-02-27 07:36:59 +00007051 if (Op0.getOpcode() == X86ISD::SETCC &&
7052 Op1.getOpcode() == ISD::Constant &&
7053 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
7054 cast<ConstantSDNode>(Op1)->isNullValue()) &&
7055 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
7056 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
7057 bool Invert = (CC == ISD::SETNE) ^
7058 cast<ConstantSDNode>(Op1)->isNullValue();
7059 if (Invert)
7060 CCode = X86::GetOppositeBranchCondition(CCode);
7061 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
7062 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
7063 }
7064
Evan Chenge5b51ac2010-04-17 06:13:15 +00007065 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00007066 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00007067 if (X86CC == X86::COND_INVALID)
7068 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00007069
Evan Cheng552f09a2010-04-26 19:06:11 +00007070 SDValue Cond = EmitCmp(Op0, Op1, X86CC, DAG);
Evan Chengad9c0a32009-12-15 00:53:42 +00007071
7072 // Use sbb x, x to materialize carry bit into a GPR.
Evan Cheng2e489c42009-12-16 00:53:11 +00007073 if (X86CC == X86::COND_B)
Evan Chengad9c0a32009-12-15 00:53:42 +00007074 return DAG.getNode(ISD::AND, dl, MVT::i8,
7075 DAG.getNode(X86ISD::SETCC_CARRY, dl, MVT::i8,
7076 DAG.getConstant(X86CC, MVT::i8), Cond),
7077 DAG.getConstant(1, MVT::i8));
Evan Chengad9c0a32009-12-15 00:53:42 +00007078
Owen Anderson825b72b2009-08-11 20:47:22 +00007079 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
7080 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00007081}
7082
Dan Gohmand858e902010-04-17 15:26:15 +00007083SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00007084 SDValue Cond;
7085 SDValue Op0 = Op.getOperand(0);
7086 SDValue Op1 = Op.getOperand(1);
7087 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00007088 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00007089 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
7090 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007091 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00007092
7093 if (isFP) {
7094 unsigned SSECC = 8;
Owen Andersone50ed302009-08-10 22:56:29 +00007095 EVT VT0 = Op0.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00007096 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
7097 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00007098 bool Swap = false;
7099
7100 switch (SetCCOpcode) {
7101 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00007102 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00007103 case ISD::SETEQ: SSECC = 0; break;
Scott Michelfdc40a02009-02-17 22:15:04 +00007104 case ISD::SETOGT:
Nate Begeman30a0de92008-07-17 16:51:19 +00007105 case ISD::SETGT: Swap = true; // Fallthrough
7106 case ISD::SETLT:
7107 case ISD::SETOLT: SSECC = 1; break;
7108 case ISD::SETOGE:
7109 case ISD::SETGE: Swap = true; // Fallthrough
7110 case ISD::SETLE:
7111 case ISD::SETOLE: SSECC = 2; break;
7112 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00007113 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00007114 case ISD::SETNE: SSECC = 4; break;
7115 case ISD::SETULE: Swap = true;
7116 case ISD::SETUGE: SSECC = 5; break;
7117 case ISD::SETULT: Swap = true;
7118 case ISD::SETUGT: SSECC = 6; break;
7119 case ISD::SETO: SSECC = 7; break;
7120 }
7121 if (Swap)
7122 std::swap(Op0, Op1);
7123
Nate Begemanfb8ead02008-07-25 19:05:58 +00007124 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00007125 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00007126 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00007127 SDValue UNORD, EQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00007128 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
7129 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00007130 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00007131 }
7132 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00007133 SDValue ORD, NEQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00007134 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
7135 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00007136 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00007137 }
Torok Edwinc23197a2009-07-14 16:55:14 +00007138 llvm_unreachable("Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00007139 }
7140 // Handle all other FP comparisons here.
Owen Anderson825b72b2009-08-11 20:47:22 +00007141 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00007142 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007143
Nate Begeman30a0de92008-07-17 16:51:19 +00007144 // We are handling one of the integer comparisons here. Since SSE only has
7145 // GT and EQ comparisons for integer, swapping operands and multiple
7146 // operations may be required for some comparisons.
7147 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
7148 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00007149
Owen Anderson825b72b2009-08-11 20:47:22 +00007150 switch (VT.getSimpleVT().SimpleTy) {
Nate Begeman30a0de92008-07-17 16:51:19 +00007151 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007152 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007153 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007154 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
7155 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00007156 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007157
Nate Begeman30a0de92008-07-17 16:51:19 +00007158 switch (SetCCOpcode) {
7159 default: break;
7160 case ISD::SETNE: Invert = true;
7161 case ISD::SETEQ: Opc = EQOpc; break;
7162 case ISD::SETLT: Swap = true;
7163 case ISD::SETGT: Opc = GTOpc; break;
7164 case ISD::SETGE: Swap = true;
7165 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
7166 case ISD::SETULT: Swap = true;
7167 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
7168 case ISD::SETUGE: Swap = true;
7169 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
7170 }
7171 if (Swap)
7172 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007173
Nate Begeman30a0de92008-07-17 16:51:19 +00007174 // Since SSE has no unsigned integer comparisons, we need to flip the sign
7175 // bits of the inputs before performing those operations.
7176 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00007177 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00007178 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
7179 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00007180 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00007181 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
7182 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00007183 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
7184 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00007185 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007186
Dale Johannesenace16102009-02-03 19:33:06 +00007187 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00007188
7189 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00007190 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00007191 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00007192
Nate Begeman30a0de92008-07-17 16:51:19 +00007193 return Result;
7194}
Evan Cheng0488db92007-09-25 01:57:46 +00007195
Evan Cheng370e5342008-12-03 08:38:43 +00007196// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00007197static bool isX86LogicalCmp(SDValue Op) {
7198 unsigned Opc = Op.getNode()->getOpcode();
7199 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
7200 return true;
7201 if (Op.getResNo() == 1 &&
7202 (Opc == X86ISD::ADD ||
7203 Opc == X86ISD::SUB ||
7204 Opc == X86ISD::SMUL ||
7205 Opc == X86ISD::UMUL ||
7206 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00007207 Opc == X86ISD::DEC ||
7208 Opc == X86ISD::OR ||
7209 Opc == X86ISD::XOR ||
7210 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00007211 return true;
7212
Chris Lattner9637d5b2010-12-05 07:49:54 +00007213 if (Op.getResNo() == 2 && Opc == X86ISD::UMUL)
7214 return true;
7215
Dan Gohman076aee32009-03-04 19:44:21 +00007216 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00007217}
7218
Chris Lattnera2b56002010-12-05 01:23:24 +00007219static bool isZero(SDValue V) {
7220 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
7221 return C && C->isNullValue();
7222}
7223
Chris Lattner96908b12010-12-05 02:00:51 +00007224static bool isAllOnes(SDValue V) {
7225 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
7226 return C && C->isAllOnesValue();
7227}
7228
Dan Gohmand858e902010-04-17 15:26:15 +00007229SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00007230 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00007231 SDValue Cond = Op.getOperand(0);
Chris Lattnera2b56002010-12-05 01:23:24 +00007232 SDValue Op1 = Op.getOperand(1);
7233 SDValue Op2 = Op.getOperand(2);
7234 DebugLoc DL = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00007235 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00007236
Dan Gohman1a492952009-10-20 16:22:37 +00007237 if (Cond.getOpcode() == ISD::SETCC) {
7238 SDValue NewCond = LowerSETCC(Cond, DAG);
7239 if (NewCond.getNode())
7240 Cond = NewCond;
7241 }
Evan Cheng734503b2006-09-11 02:19:56 +00007242
Chris Lattnera2b56002010-12-05 01:23:24 +00007243 // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00007244 // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
Chris Lattnera2b56002010-12-05 01:23:24 +00007245 // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00007246 // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007247 if (Cond.getOpcode() == X86ISD::SETCC &&
Chris Lattner96908b12010-12-05 02:00:51 +00007248 Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
7249 isZero(Cond.getOperand(1).getOperand(1))) {
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007250 SDValue Cmp = Cond.getOperand(1);
Chris Lattnera2b56002010-12-05 01:23:24 +00007251
7252 unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
7253
Chris Lattner96908b12010-12-05 02:00:51 +00007254 if ((isAllOnes(Op1) || isAllOnes(Op2)) &&
7255 (CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
7256 SDValue Y = isAllOnes(Op2) ? Op1 : Op2;
Chris Lattnera2b56002010-12-05 01:23:24 +00007257
7258 SDValue CmpOp0 = Cmp.getOperand(0);
7259 Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32,
7260 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
7261
Chris Lattner96908b12010-12-05 02:00:51 +00007262 SDValue Res = // Res = 0 or -1.
Chris Lattnera2b56002010-12-05 01:23:24 +00007263 DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
7264 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
Chris Lattner96908b12010-12-05 02:00:51 +00007265
7266 if (isAllOnes(Op1) != (CondCode == X86::COND_E))
7267 Res = DAG.getNOT(DL, Res, Res.getValueType());
7268
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007269 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
Chris Lattnera2b56002010-12-05 01:23:24 +00007270 if (N2C == 0 || !N2C->isNullValue())
7271 Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
7272 return Res;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007273 }
7274 }
7275
Chris Lattnera2b56002010-12-05 01:23:24 +00007276 // Look past (and (setcc_carry (cmp ...)), 1).
Evan Chengad9c0a32009-12-15 00:53:42 +00007277 if (Cond.getOpcode() == ISD::AND &&
7278 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
7279 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00007280 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00007281 Cond = Cond.getOperand(0);
7282 }
7283
Evan Cheng3f41d662007-10-08 22:16:29 +00007284 // If condition flag is set by a X86ISD::CMP, then use it as the condition
7285 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00007286 if (Cond.getOpcode() == X86ISD::SETCC ||
7287 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00007288 CC = Cond.getOperand(0);
7289
Dan Gohman475871a2008-07-27 21:46:04 +00007290 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00007291 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00007292 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00007293
Evan Cheng3f41d662007-10-08 22:16:29 +00007294 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007295 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00007296 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00007297 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00007298
Chris Lattnerd1980a52009-03-12 06:52:53 +00007299 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
7300 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00007301 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00007302 addTest = false;
7303 }
7304 }
7305
7306 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007307 // Look pass the truncate.
7308 if (Cond.getOpcode() == ISD::TRUNCATE)
7309 Cond = Cond.getOperand(0);
7310
7311 // We know the result of AND is compared against zero. Try to match
7312 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00007313 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Chris Lattnera2b56002010-12-05 01:23:24 +00007314 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG);
Evan Chengd40d03e2010-01-06 19:38:29 +00007315 if (NewSetCC.getNode()) {
7316 CC = NewSetCC.getOperand(0);
7317 Cond = NewSetCC.getOperand(1);
7318 addTest = false;
7319 }
7320 }
7321 }
7322
7323 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007324 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00007325 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00007326 }
7327
Evan Cheng0488db92007-09-25 01:57:46 +00007328 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
7329 // condition is true.
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007330 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Flag);
7331 SDValue Ops[] = { Op2, Op1, CC, Cond };
Chris Lattnera2b56002010-12-05 01:23:24 +00007332 return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00007333}
7334
Evan Cheng370e5342008-12-03 08:38:43 +00007335// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
7336// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
7337// from the AND / OR.
7338static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
7339 Opc = Op.getOpcode();
7340 if (Opc != ISD::OR && Opc != ISD::AND)
7341 return false;
7342 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
7343 Op.getOperand(0).hasOneUse() &&
7344 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
7345 Op.getOperand(1).hasOneUse());
7346}
7347
Evan Cheng961d6d42009-02-02 08:19:07 +00007348// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
7349// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00007350static bool isXor1OfSetCC(SDValue Op) {
7351 if (Op.getOpcode() != ISD::XOR)
7352 return false;
7353 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
7354 if (N1C && N1C->getAPIntValue() == 1) {
7355 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
7356 Op.getOperand(0).hasOneUse();
7357 }
7358 return false;
7359}
7360
Dan Gohmand858e902010-04-17 15:26:15 +00007361SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00007362 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00007363 SDValue Chain = Op.getOperand(0);
7364 SDValue Cond = Op.getOperand(1);
7365 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007366 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00007367 SDValue CC;
Evan Cheng734503b2006-09-11 02:19:56 +00007368
Dan Gohman1a492952009-10-20 16:22:37 +00007369 if (Cond.getOpcode() == ISD::SETCC) {
7370 SDValue NewCond = LowerSETCC(Cond, DAG);
7371 if (NewCond.getNode())
7372 Cond = NewCond;
7373 }
Chris Lattnere55484e2008-12-25 05:34:37 +00007374#if 0
7375 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00007376 else if (Cond.getOpcode() == X86ISD::ADD ||
7377 Cond.getOpcode() == X86ISD::SUB ||
7378 Cond.getOpcode() == X86ISD::SMUL ||
7379 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00007380 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00007381#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00007382
Evan Chengad9c0a32009-12-15 00:53:42 +00007383 // Look pass (and (setcc_carry (cmp ...)), 1).
7384 if (Cond.getOpcode() == ISD::AND &&
7385 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
7386 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00007387 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00007388 Cond = Cond.getOperand(0);
7389 }
7390
Evan Cheng3f41d662007-10-08 22:16:29 +00007391 // If condition flag is set by a X86ISD::CMP, then use it as the condition
7392 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00007393 if (Cond.getOpcode() == X86ISD::SETCC ||
7394 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00007395 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007396
Dan Gohman475871a2008-07-27 21:46:04 +00007397 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00007398 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00007399 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00007400 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00007401 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00007402 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00007403 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00007404 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00007405 default: break;
7406 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00007407 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00007408 // These can only come from an arithmetic instruction with overflow,
7409 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00007410 Cond = Cond.getNode()->getOperand(1);
7411 addTest = false;
7412 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00007413 }
Evan Cheng0488db92007-09-25 01:57:46 +00007414 }
Evan Cheng370e5342008-12-03 08:38:43 +00007415 } else {
7416 unsigned CondOpc;
7417 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
7418 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00007419 if (CondOpc == ISD::OR) {
7420 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
7421 // two branches instead of an explicit OR instruction with a
7422 // separate test.
7423 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00007424 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00007425 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007426 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00007427 Chain, Dest, CC, Cmp);
7428 CC = Cond.getOperand(1).getOperand(0);
7429 Cond = Cmp;
7430 addTest = false;
7431 }
7432 } else { // ISD::AND
7433 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
7434 // two branches instead of an explicit AND instruction with a
7435 // separate test. However, we only do this if this block doesn't
7436 // have a fall-through edge, because this requires an explicit
7437 // jmp when the condition is false.
7438 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00007439 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00007440 Op.getNode()->hasOneUse()) {
7441 X86::CondCode CCode =
7442 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
7443 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007444 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00007445 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00007446 // Look for an unconditional branch following this conditional branch.
7447 // We need this because we need to reverse the successors in order
7448 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00007449 if (User->getOpcode() == ISD::BR) {
7450 SDValue FalseBB = User->getOperand(1);
7451 SDNode *NewBR =
7452 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00007453 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00007454 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00007455 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00007456
Dale Johannesene4d209d2009-02-03 20:21:25 +00007457 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00007458 Chain, Dest, CC, Cmp);
7459 X86::CondCode CCode =
7460 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
7461 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007462 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00007463 Cond = Cmp;
7464 addTest = false;
7465 }
7466 }
Dan Gohman279c22e2008-10-21 03:29:32 +00007467 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00007468 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
7469 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
7470 // It should be transformed during dag combiner except when the condition
7471 // is set by a arithmetics with overflow node.
7472 X86::CondCode CCode =
7473 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
7474 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007475 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00007476 Cond = Cond.getOperand(0).getOperand(1);
7477 addTest = false;
Dan Gohman279c22e2008-10-21 03:29:32 +00007478 }
Evan Cheng0488db92007-09-25 01:57:46 +00007479 }
7480
7481 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007482 // Look pass the truncate.
7483 if (Cond.getOpcode() == ISD::TRUNCATE)
7484 Cond = Cond.getOperand(0);
7485
7486 // We know the result of AND is compared against zero. Try to match
7487 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00007488 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007489 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
7490 if (NewSetCC.getNode()) {
7491 CC = NewSetCC.getOperand(0);
7492 Cond = NewSetCC.getOperand(1);
7493 addTest = false;
7494 }
7495 }
7496 }
7497
7498 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007499 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00007500 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00007501 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00007502 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00007503 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00007504}
7505
Anton Korobeynikove060b532007-04-17 19:34:00 +00007506
7507// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
7508// Calls to _alloca is needed to probe the stack when allocating more than 4k
7509// bytes in one go. Touching the stack at 4K increments is necessary to ensure
7510// that the guard pages used by the OS virtual memory manager are allocated in
7511// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00007512SDValue
7513X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00007514 SelectionDAG &DAG) const {
Duncan Sands1e1ca0b2010-10-21 16:02:12 +00007515 assert((Subtarget->isTargetCygMing() || Subtarget->isTargetWindows()) &&
Michael J. Spencere9c253e2010-10-21 01:41:01 +00007516 "This should be used only on Windows targets");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007517 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007518
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007519 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00007520 SDValue Chain = Op.getOperand(0);
7521 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007522 // FIXME: Ensure alignment here
7523
Dan Gohman475871a2008-07-27 21:46:04 +00007524 SDValue Flag;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007525
Owen Anderson825b72b2009-08-11 20:47:22 +00007526 EVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007527
Dale Johannesendd64c412009-02-04 00:33:20 +00007528 Chain = DAG.getCopyToReg(Chain, dl, X86::EAX, Size, Flag);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00007529 Flag = Chain.getValue(1);
7530
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00007531 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00007532
Michael J. Spencere9c253e2010-10-21 01:41:01 +00007533 Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag);
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00007534 Flag = Chain.getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007535
Dale Johannesendd64c412009-02-04 00:33:20 +00007536 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007537
Dan Gohman475871a2008-07-27 21:46:04 +00007538 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Dale Johannesene4d209d2009-02-03 20:21:25 +00007539 return DAG.getMergeValues(Ops1, 2, dl);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007540}
7541
Dan Gohmand858e902010-04-17 15:26:15 +00007542SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00007543 MachineFunction &MF = DAG.getMachineFunction();
7544 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
7545
Dan Gohman69de1932008-02-06 22:27:42 +00007546 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00007547 DebugLoc DL = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00007548
Anton Korobeynikove7beda12010-10-03 22:52:07 +00007549 if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00007550 // vastart just stores the address of the VarArgsFrameIndex slot into the
7551 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +00007552 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
7553 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00007554 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
7555 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007556 }
7557
7558 // __va_list_tag:
7559 // gp_offset (0 - 6 * 8)
7560 // fp_offset (48 - 48 + 8 * 16)
7561 // overflow_arg_area (point to parameters coming in memory).
7562 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00007563 SmallVector<SDValue, 8> MemOps;
7564 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00007565 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00007566 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00007567 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
7568 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007569 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007570 MemOps.push_back(Store);
7571
7572 // Store fp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00007573 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007574 FIN, DAG.getIntPtrConstant(4));
Chris Lattner8026a9d2010-09-21 17:50:43 +00007575 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00007576 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
7577 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007578 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007579 MemOps.push_back(Store);
7580
7581 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +00007582 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007583 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +00007584 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
7585 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00007586 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
7587 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +00007588 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007589 MemOps.push_back(Store);
7590
7591 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +00007592 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007593 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +00007594 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
7595 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00007596 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
7597 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007598 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +00007599 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00007600 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00007601}
7602
Dan Gohmand858e902010-04-17 15:26:15 +00007603SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman320afb82010-10-12 18:00:49 +00007604 assert(Subtarget->is64Bit() &&
7605 "LowerVAARG only handles 64-bit va_arg!");
7606 assert((Subtarget->isTargetLinux() ||
7607 Subtarget->isTargetDarwin()) &&
7608 "Unhandled target in LowerVAARG");
7609 assert(Op.getNode()->getNumOperands() == 4);
7610 SDValue Chain = Op.getOperand(0);
7611 SDValue SrcPtr = Op.getOperand(1);
7612 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
7613 unsigned Align = Op.getConstantOperandVal(3);
7614 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9018e832008-05-10 01:26:14 +00007615
Dan Gohman320afb82010-10-12 18:00:49 +00007616 EVT ArgVT = Op.getNode()->getValueType(0);
7617 const Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
7618 uint32_t ArgSize = getTargetData()->getTypeAllocSize(ArgTy);
7619 uint8_t ArgMode;
7620
7621 // Decide which area this value should be read from.
7622 // TODO: Implement the AMD64 ABI in its entirety. This simple
7623 // selection mechanism works only for the basic types.
7624 if (ArgVT == MVT::f80) {
7625 llvm_unreachable("va_arg for f80 not yet implemented");
7626 } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) {
7627 ArgMode = 2; // Argument passed in XMM register. Use fp_offset.
7628 } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) {
7629 ArgMode = 1; // Argument passed in GPR64 register(s). Use gp_offset.
7630 } else {
7631 llvm_unreachable("Unhandled argument type in LowerVAARG");
7632 }
7633
7634 if (ArgMode == 2) {
7635 // Sanity Check: Make sure using fp_offset makes sense.
Michael J. Spencer87b86652010-10-19 07:32:42 +00007636 assert(!UseSoftFloat &&
Eric Christopher52b45052010-10-12 19:44:17 +00007637 !(DAG.getMachineFunction()
7638 .getFunction()->hasFnAttr(Attribute::NoImplicitFloat)) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00007639 Subtarget->hasXMM());
Dan Gohman320afb82010-10-12 18:00:49 +00007640 }
7641
7642 // Insert VAARG_64 node into the DAG
7643 // VAARG_64 returns two values: Variable Argument Address, Chain
7644 SmallVector<SDValue, 11> InstOps;
7645 InstOps.push_back(Chain);
7646 InstOps.push_back(SrcPtr);
7647 InstOps.push_back(DAG.getConstant(ArgSize, MVT::i32));
7648 InstOps.push_back(DAG.getConstant(ArgMode, MVT::i8));
7649 InstOps.push_back(DAG.getConstant(Align, MVT::i32));
7650 SDVTList VTs = DAG.getVTList(getPointerTy(), MVT::Other);
7651 SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl,
7652 VTs, &InstOps[0], InstOps.size(),
7653 MVT::i64,
7654 MachinePointerInfo(SV),
7655 /*Align=*/0,
7656 /*Volatile=*/false,
7657 /*ReadMem=*/true,
7658 /*WriteMem=*/true);
7659 Chain = VAARG.getValue(1);
7660
7661 // Load the next argument and return it
7662 return DAG.getLoad(ArgVT, dl,
7663 Chain,
7664 VAARG,
7665 MachinePointerInfo(),
7666 false, false, 0);
Dan Gohman9018e832008-05-10 01:26:14 +00007667}
7668
Dan Gohmand858e902010-04-17 15:26:15 +00007669SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) const {
Evan Chengae642192007-03-02 23:16:35 +00007670 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00007671 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00007672 SDValue Chain = Op.getOperand(0);
7673 SDValue DstPtr = Op.getOperand(1);
7674 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00007675 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
7676 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Chris Lattnere72f2022010-09-21 05:40:29 +00007677 DebugLoc DL = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00007678
Chris Lattnere72f2022010-09-21 05:40:29 +00007679 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +00007680 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Michael J. Spencerec38de22010-10-10 22:04:20 +00007681 false,
Chris Lattnere72f2022010-09-21 05:40:29 +00007682 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +00007683}
7684
Dan Gohman475871a2008-07-27 21:46:04 +00007685SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007686X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007687 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007688 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007689 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00007690 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00007691 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00007692 case Intrinsic::x86_sse_comieq_ss:
7693 case Intrinsic::x86_sse_comilt_ss:
7694 case Intrinsic::x86_sse_comile_ss:
7695 case Intrinsic::x86_sse_comigt_ss:
7696 case Intrinsic::x86_sse_comige_ss:
7697 case Intrinsic::x86_sse_comineq_ss:
7698 case Intrinsic::x86_sse_ucomieq_ss:
7699 case Intrinsic::x86_sse_ucomilt_ss:
7700 case Intrinsic::x86_sse_ucomile_ss:
7701 case Intrinsic::x86_sse_ucomigt_ss:
7702 case Intrinsic::x86_sse_ucomige_ss:
7703 case Intrinsic::x86_sse_ucomineq_ss:
7704 case Intrinsic::x86_sse2_comieq_sd:
7705 case Intrinsic::x86_sse2_comilt_sd:
7706 case Intrinsic::x86_sse2_comile_sd:
7707 case Intrinsic::x86_sse2_comigt_sd:
7708 case Intrinsic::x86_sse2_comige_sd:
7709 case Intrinsic::x86_sse2_comineq_sd:
7710 case Intrinsic::x86_sse2_ucomieq_sd:
7711 case Intrinsic::x86_sse2_ucomilt_sd:
7712 case Intrinsic::x86_sse2_ucomile_sd:
7713 case Intrinsic::x86_sse2_ucomigt_sd:
7714 case Intrinsic::x86_sse2_ucomige_sd:
7715 case Intrinsic::x86_sse2_ucomineq_sd: {
7716 unsigned Opc = 0;
7717 ISD::CondCode CC = ISD::SETCC_INVALID;
7718 switch (IntNo) {
7719 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007720 case Intrinsic::x86_sse_comieq_ss:
7721 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007722 Opc = X86ISD::COMI;
7723 CC = ISD::SETEQ;
7724 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00007725 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007726 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007727 Opc = X86ISD::COMI;
7728 CC = ISD::SETLT;
7729 break;
7730 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007731 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007732 Opc = X86ISD::COMI;
7733 CC = ISD::SETLE;
7734 break;
7735 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007736 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007737 Opc = X86ISD::COMI;
7738 CC = ISD::SETGT;
7739 break;
7740 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007741 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007742 Opc = X86ISD::COMI;
7743 CC = ISD::SETGE;
7744 break;
7745 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007746 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007747 Opc = X86ISD::COMI;
7748 CC = ISD::SETNE;
7749 break;
7750 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007751 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007752 Opc = X86ISD::UCOMI;
7753 CC = ISD::SETEQ;
7754 break;
7755 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007756 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007757 Opc = X86ISD::UCOMI;
7758 CC = ISD::SETLT;
7759 break;
7760 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007761 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007762 Opc = X86ISD::UCOMI;
7763 CC = ISD::SETLE;
7764 break;
7765 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007766 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007767 Opc = X86ISD::UCOMI;
7768 CC = ISD::SETGT;
7769 break;
7770 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007771 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007772 Opc = X86ISD::UCOMI;
7773 CC = ISD::SETGE;
7774 break;
7775 case Intrinsic::x86_sse_ucomineq_ss:
7776 case Intrinsic::x86_sse2_ucomineq_sd:
7777 Opc = X86ISD::UCOMI;
7778 CC = ISD::SETNE;
7779 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00007780 }
Evan Cheng734503b2006-09-11 02:19:56 +00007781
Dan Gohman475871a2008-07-27 21:46:04 +00007782 SDValue LHS = Op.getOperand(1);
7783 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00007784 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00007785 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00007786 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
7787 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
7788 DAG.getConstant(X86CC, MVT::i8), Cond);
7789 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00007790 }
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007791 // ptest and testp intrinsics. The intrinsic these come from are designed to
7792 // return an integer value, not just an instruction so lower it to the ptest
7793 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00007794 case Intrinsic::x86_sse41_ptestz:
7795 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007796 case Intrinsic::x86_sse41_ptestnzc:
7797 case Intrinsic::x86_avx_ptestz_256:
7798 case Intrinsic::x86_avx_ptestc_256:
7799 case Intrinsic::x86_avx_ptestnzc_256:
7800 case Intrinsic::x86_avx_vtestz_ps:
7801 case Intrinsic::x86_avx_vtestc_ps:
7802 case Intrinsic::x86_avx_vtestnzc_ps:
7803 case Intrinsic::x86_avx_vtestz_pd:
7804 case Intrinsic::x86_avx_vtestc_pd:
7805 case Intrinsic::x86_avx_vtestnzc_pd:
7806 case Intrinsic::x86_avx_vtestz_ps_256:
7807 case Intrinsic::x86_avx_vtestc_ps_256:
7808 case Intrinsic::x86_avx_vtestnzc_ps_256:
7809 case Intrinsic::x86_avx_vtestz_pd_256:
7810 case Intrinsic::x86_avx_vtestc_pd_256:
7811 case Intrinsic::x86_avx_vtestnzc_pd_256: {
7812 bool IsTestPacked = false;
Eric Christopher71c67532009-07-29 00:28:05 +00007813 unsigned X86CC = 0;
7814 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00007815 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007816 case Intrinsic::x86_avx_vtestz_ps:
7817 case Intrinsic::x86_avx_vtestz_pd:
7818 case Intrinsic::x86_avx_vtestz_ps_256:
7819 case Intrinsic::x86_avx_vtestz_pd_256:
7820 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00007821 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007822 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +00007823 // ZF = 1
7824 X86CC = X86::COND_E;
7825 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007826 case Intrinsic::x86_avx_vtestc_ps:
7827 case Intrinsic::x86_avx_vtestc_pd:
7828 case Intrinsic::x86_avx_vtestc_ps_256:
7829 case Intrinsic::x86_avx_vtestc_pd_256:
7830 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00007831 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007832 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00007833 // CF = 1
7834 X86CC = X86::COND_B;
7835 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007836 case Intrinsic::x86_avx_vtestnzc_ps:
7837 case Intrinsic::x86_avx_vtestnzc_pd:
7838 case Intrinsic::x86_avx_vtestnzc_ps_256:
7839 case Intrinsic::x86_avx_vtestnzc_pd_256:
7840 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +00007841 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007842 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00007843 // ZF and CF = 0
7844 X86CC = X86::COND_A;
7845 break;
7846 }
Eric Christopherfd179292009-08-27 18:07:15 +00007847
Eric Christopher71c67532009-07-29 00:28:05 +00007848 SDValue LHS = Op.getOperand(1);
7849 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007850 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
7851 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00007852 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
7853 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
7854 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +00007855 }
Evan Cheng5759f972008-05-04 09:15:50 +00007856
7857 // Fix vector shift instructions where the last operand is a non-immediate
7858 // i32 value.
7859 case Intrinsic::x86_sse2_pslli_w:
7860 case Intrinsic::x86_sse2_pslli_d:
7861 case Intrinsic::x86_sse2_pslli_q:
7862 case Intrinsic::x86_sse2_psrli_w:
7863 case Intrinsic::x86_sse2_psrli_d:
7864 case Intrinsic::x86_sse2_psrli_q:
7865 case Intrinsic::x86_sse2_psrai_w:
7866 case Intrinsic::x86_sse2_psrai_d:
7867 case Intrinsic::x86_mmx_pslli_w:
7868 case Intrinsic::x86_mmx_pslli_d:
7869 case Intrinsic::x86_mmx_pslli_q:
7870 case Intrinsic::x86_mmx_psrli_w:
7871 case Intrinsic::x86_mmx_psrli_d:
7872 case Intrinsic::x86_mmx_psrli_q:
7873 case Intrinsic::x86_mmx_psrai_w:
7874 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00007875 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00007876 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00007877 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00007878
7879 unsigned NewIntNo = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00007880 EVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00007881 switch (IntNo) {
7882 case Intrinsic::x86_sse2_pslli_w:
7883 NewIntNo = Intrinsic::x86_sse2_psll_w;
7884 break;
7885 case Intrinsic::x86_sse2_pslli_d:
7886 NewIntNo = Intrinsic::x86_sse2_psll_d;
7887 break;
7888 case Intrinsic::x86_sse2_pslli_q:
7889 NewIntNo = Intrinsic::x86_sse2_psll_q;
7890 break;
7891 case Intrinsic::x86_sse2_psrli_w:
7892 NewIntNo = Intrinsic::x86_sse2_psrl_w;
7893 break;
7894 case Intrinsic::x86_sse2_psrli_d:
7895 NewIntNo = Intrinsic::x86_sse2_psrl_d;
7896 break;
7897 case Intrinsic::x86_sse2_psrli_q:
7898 NewIntNo = Intrinsic::x86_sse2_psrl_q;
7899 break;
7900 case Intrinsic::x86_sse2_psrai_w:
7901 NewIntNo = Intrinsic::x86_sse2_psra_w;
7902 break;
7903 case Intrinsic::x86_sse2_psrai_d:
7904 NewIntNo = Intrinsic::x86_sse2_psra_d;
7905 break;
7906 default: {
Owen Anderson825b72b2009-08-11 20:47:22 +00007907 ShAmtVT = MVT::v2i32;
Evan Cheng5759f972008-05-04 09:15:50 +00007908 switch (IntNo) {
7909 case Intrinsic::x86_mmx_pslli_w:
7910 NewIntNo = Intrinsic::x86_mmx_psll_w;
7911 break;
7912 case Intrinsic::x86_mmx_pslli_d:
7913 NewIntNo = Intrinsic::x86_mmx_psll_d;
7914 break;
7915 case Intrinsic::x86_mmx_pslli_q:
7916 NewIntNo = Intrinsic::x86_mmx_psll_q;
7917 break;
7918 case Intrinsic::x86_mmx_psrli_w:
7919 NewIntNo = Intrinsic::x86_mmx_psrl_w;
7920 break;
7921 case Intrinsic::x86_mmx_psrli_d:
7922 NewIntNo = Intrinsic::x86_mmx_psrl_d;
7923 break;
7924 case Intrinsic::x86_mmx_psrli_q:
7925 NewIntNo = Intrinsic::x86_mmx_psrl_q;
7926 break;
7927 case Intrinsic::x86_mmx_psrai_w:
7928 NewIntNo = Intrinsic::x86_mmx_psra_w;
7929 break;
7930 case Intrinsic::x86_mmx_psrai_d:
7931 NewIntNo = Intrinsic::x86_mmx_psra_d;
7932 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00007933 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng5759f972008-05-04 09:15:50 +00007934 }
7935 break;
7936 }
7937 }
Mon P Wangefa42202009-09-03 19:56:25 +00007938
7939 // The vector shift intrinsics with scalars uses 32b shift amounts but
7940 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
7941 // to be zero.
7942 SDValue ShOps[4];
7943 ShOps[0] = ShAmt;
7944 ShOps[1] = DAG.getConstant(0, MVT::i32);
7945 if (ShAmtVT == MVT::v4i32) {
7946 ShOps[2] = DAG.getUNDEF(MVT::i32);
7947 ShOps[3] = DAG.getUNDEF(MVT::i32);
7948 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 4);
7949 } else {
7950 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 2);
Dale Johannesen0488fb62010-09-30 23:57:10 +00007951// FIXME this must be lowered to get rid of the invalid type.
Mon P Wangefa42202009-09-03 19:56:25 +00007952 }
7953
Owen Andersone50ed302009-08-10 22:56:29 +00007954 EVT VT = Op.getValueType();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007955 ShAmt = DAG.getNode(ISD::BITCAST, dl, VT, ShAmt);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007956 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007957 DAG.getConstant(NewIntNo, MVT::i32),
Evan Cheng5759f972008-05-04 09:15:50 +00007958 Op.getOperand(1), ShAmt);
7959 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00007960 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007961}
Evan Cheng72261582005-12-20 06:22:03 +00007962
Dan Gohmand858e902010-04-17 15:26:15 +00007963SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
7964 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +00007965 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7966 MFI->setReturnAddressIsTaken(true);
7967
Bill Wendling64e87322009-01-16 19:25:27 +00007968 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007969 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00007970
7971 if (Depth > 0) {
7972 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
7973 SDValue Offset =
7974 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +00007975 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007976 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00007977 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007978 FrameAddr, Offset),
Chris Lattner51abfe42010-09-21 06:02:19 +00007979 MachinePointerInfo(), false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +00007980 }
7981
7982 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00007983 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00007984 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Chris Lattner51abfe42010-09-21 06:02:19 +00007985 RetAddrFI, MachinePointerInfo(), false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00007986}
7987
Dan Gohmand858e902010-04-17 15:26:15 +00007988SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +00007989 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7990 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00007991
Owen Andersone50ed302009-08-10 22:56:29 +00007992 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007993 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00007994 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
7995 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00007996 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00007997 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +00007998 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
7999 MachinePointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +00008000 false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00008001 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00008002}
8003
Dan Gohman475871a2008-07-27 21:46:04 +00008004SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008005 SelectionDAG &DAG) const {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00008006 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008007}
8008
Dan Gohmand858e902010-04-17 15:26:15 +00008009SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008010 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00008011 SDValue Chain = Op.getOperand(0);
8012 SDValue Offset = Op.getOperand(1);
8013 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008014 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008015
Dan Gohmand8816272010-08-11 18:14:00 +00008016 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
8017 Subtarget->is64Bit() ? X86::RBP : X86::EBP,
8018 getPointerTy());
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008019 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008020
Dan Gohmand8816272010-08-11 18:14:00 +00008021 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Frame,
8022 DAG.getIntPtrConstant(TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008023 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +00008024 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
8025 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00008026 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008027 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008028
Dale Johannesene4d209d2009-02-03 20:21:25 +00008029 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008030 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008031 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008032}
8033
Dan Gohman475871a2008-07-27 21:46:04 +00008034SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008035 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00008036 SDValue Root = Op.getOperand(0);
8037 SDValue Trmp = Op.getOperand(1); // trampoline
8038 SDValue FPtr = Op.getOperand(2); // nested function
8039 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008040 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008041
Dan Gohman69de1932008-02-06 22:27:42 +00008042 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008043
8044 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00008045 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00008046
8047 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +00008048 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
8049 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +00008050
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00008051 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
8052 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00008053
8054 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
8055
8056 // Load the pointer to the nested function into R11.
8057 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00008058 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00008059 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008060 Addr, MachinePointerInfo(TrmpAddr),
8061 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008062
Owen Anderson825b72b2009-08-11 20:47:22 +00008063 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8064 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008065 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
8066 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +00008067 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00008068
8069 // Load the 'nest' parameter value into R10.
8070 // R10 is specified in X86CallingConv.td
8071 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +00008072 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8073 DAG.getConstant(10, MVT::i64));
8074 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008075 Addr, MachinePointerInfo(TrmpAddr, 10),
8076 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008077
Owen Anderson825b72b2009-08-11 20:47:22 +00008078 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8079 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008080 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
8081 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +00008082 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00008083
8084 // Jump to the nested function.
8085 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +00008086 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8087 DAG.getConstant(20, MVT::i64));
8088 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008089 Addr, MachinePointerInfo(TrmpAddr, 20),
8090 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008091
8092 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +00008093 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8094 DAG.getConstant(22, MVT::i64));
8095 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00008096 MachinePointerInfo(TrmpAddr, 22),
8097 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008098
Dan Gohman475871a2008-07-27 21:46:04 +00008099 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00008100 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008101 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008102 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00008103 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00008104 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00008105 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00008106 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008107
8108 switch (CC) {
8109 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00008110 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +00008111 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00008112 case CallingConv::X86_StdCall: {
8113 // Pass 'nest' parameter in ECX.
8114 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00008115 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008116
8117 // Check that ECX wasn't needed by an 'inreg' parameter.
8118 const FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00008119 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008120
Chris Lattner58d74912008-03-12 17:45:29 +00008121 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00008122 unsigned InRegCount = 0;
8123 unsigned Idx = 1;
8124
8125 for (FunctionType::param_iterator I = FTy->param_begin(),
8126 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00008127 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00008128 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00008129 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008130
8131 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +00008132 report_fatal_error("Nest register in use - reduce number of inreg"
8133 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +00008134 }
8135 }
8136 break;
8137 }
8138 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +00008139 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00008140 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00008141 // Pass 'nest' parameter in EAX.
8142 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00008143 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008144 break;
8145 }
8146
Dan Gohman475871a2008-07-27 21:46:04 +00008147 SDValue OutChains[4];
8148 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008149
Owen Anderson825b72b2009-08-11 20:47:22 +00008150 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8151 DAG.getConstant(10, MVT::i32));
8152 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008153
Chris Lattnera62fe662010-02-05 19:20:30 +00008154 // This is storing the opcode for MOV32ri.
8155 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00008156 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00008157 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008158 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008159 Trmp, MachinePointerInfo(TrmpAddr),
8160 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008161
Owen Anderson825b72b2009-08-11 20:47:22 +00008162 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8163 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008164 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
8165 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +00008166 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008167
Chris Lattnera62fe662010-02-05 19:20:30 +00008168 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +00008169 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8170 DAG.getConstant(5, MVT::i32));
8171 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00008172 MachinePointerInfo(TrmpAddr, 5),
8173 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008174
Owen Anderson825b72b2009-08-11 20:47:22 +00008175 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8176 DAG.getConstant(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008177 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
8178 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +00008179 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008180
Dan Gohman475871a2008-07-27 21:46:04 +00008181 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00008182 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008183 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008184 }
8185}
8186
Dan Gohmand858e902010-04-17 15:26:15 +00008187SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
8188 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008189 /*
8190 The rounding mode is in bits 11:10 of FPSR, and has the following
8191 settings:
8192 00 Round to nearest
8193 01 Round to -inf
8194 10 Round to +inf
8195 11 Round to 0
8196
8197 FLT_ROUNDS, on the other hand, expects the following:
8198 -1 Undefined
8199 0 Round to 0
8200 1 Round to nearest
8201 2 Round to +inf
8202 3 Round to -inf
8203
8204 To perform the conversion, we do:
8205 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
8206 */
8207
8208 MachineFunction &MF = DAG.getMachineFunction();
8209 const TargetMachine &TM = MF.getTarget();
8210 const TargetFrameInfo &TFI = *TM.getFrameInfo();
8211 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00008212 EVT VT = Op.getValueType();
Chris Lattner2156b792010-09-22 01:11:26 +00008213 DebugLoc DL = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008214
8215 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +00008216 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +00008217 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008218
Michael J. Spencerec38de22010-10-10 22:04:20 +00008219
Chris Lattner2156b792010-09-22 01:11:26 +00008220 MachineMemOperand *MMO =
8221 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8222 MachineMemOperand::MOStore, 2, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008223
Chris Lattner2156b792010-09-22 01:11:26 +00008224 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
8225 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
8226 DAG.getVTList(MVT::Other),
8227 Ops, 2, MVT::i16, MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008228
8229 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +00008230 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Chris Lattner51abfe42010-09-21 06:02:19 +00008231 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008232
8233 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00008234 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +00008235 DAG.getNode(ISD::SRL, DL, MVT::i16,
8236 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00008237 CWD, DAG.getConstant(0x800, MVT::i16)),
8238 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00008239 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +00008240 DAG.getNode(ISD::SRL, DL, MVT::i16,
8241 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00008242 CWD, DAG.getConstant(0x400, MVT::i16)),
8243 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008244
Dan Gohman475871a2008-07-27 21:46:04 +00008245 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +00008246 DAG.getNode(ISD::AND, DL, MVT::i16,
8247 DAG.getNode(ISD::ADD, DL, MVT::i16,
8248 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +00008249 DAG.getConstant(1, MVT::i16)),
8250 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008251
8252
Duncan Sands83ec4b62008-06-06 12:08:01 +00008253 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +00008254 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008255}
8256
Dan Gohmand858e902010-04-17 15:26:15 +00008257SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008258 EVT VT = Op.getValueType();
8259 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008260 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008261 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00008262
8263 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008264 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00008265 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +00008266 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00008267 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008268 }
Evan Cheng18efe262007-12-14 02:13:44 +00008269
Evan Cheng152804e2007-12-14 08:30:15 +00008270 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008271 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008272 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00008273
8274 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00008275 SDValue Ops[] = {
8276 Op,
8277 DAG.getConstant(NumBits+NumBits-1, OpVT),
8278 DAG.getConstant(X86::COND_E, MVT::i8),
8279 Op.getValue(1)
8280 };
8281 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00008282
8283 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +00008284 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +00008285
Owen Anderson825b72b2009-08-11 20:47:22 +00008286 if (VT == MVT::i8)
8287 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008288 return Op;
8289}
8290
Dan Gohmand858e902010-04-17 15:26:15 +00008291SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008292 EVT VT = Op.getValueType();
8293 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008294 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008295 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00008296
8297 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008298 if (VT == MVT::i8) {
8299 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00008300 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008301 }
Evan Cheng152804e2007-12-14 08:30:15 +00008302
8303 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008304 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008305 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00008306
8307 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00008308 SDValue Ops[] = {
8309 Op,
8310 DAG.getConstant(NumBits, OpVT),
8311 DAG.getConstant(X86::COND_E, MVT::i8),
8312 Op.getValue(1)
8313 };
8314 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00008315
Owen Anderson825b72b2009-08-11 20:47:22 +00008316 if (VT == MVT::i8)
8317 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008318 return Op;
8319}
8320
Dan Gohmand858e902010-04-17 15:26:15 +00008321SDValue X86TargetLowering::LowerMUL_V2I64(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008322 EVT VT = Op.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00008323 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008324 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00008325
Mon P Wangaf9b9522008-12-18 21:42:19 +00008326 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
8327 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
8328 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
8329 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
8330 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
8331 //
8332 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
8333 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
8334 // return AloBlo + AloBhi + AhiBlo;
8335
8336 SDValue A = Op.getOperand(0);
8337 SDValue B = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008338
Dale Johannesene4d209d2009-02-03 20:21:25 +00008339 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008340 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
8341 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008342 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008343 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
8344 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008345 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008346 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008347 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008348 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008349 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008350 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008351 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008352 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008353 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008354 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008355 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
8356 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008357 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008358 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
8359 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008360 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
8361 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +00008362 return Res;
8363}
8364
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008365SDValue X86TargetLowering::LowerSHL(SDValue Op, SelectionDAG &DAG) const {
8366 EVT VT = Op.getValueType();
8367 DebugLoc dl = Op.getDebugLoc();
8368 SDValue R = Op.getOperand(0);
8369
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008370 LLVMContext *Context = DAG.getContext();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008371
Nate Begeman51409212010-07-28 00:21:48 +00008372 assert(Subtarget->hasSSE41() && "Cannot lower SHL without SSE4.1 or later");
8373
8374 if (VT == MVT::v4i32) {
8375 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8376 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
8377 Op.getOperand(1), DAG.getConstant(23, MVT::i32));
8378
8379 ConstantInt *CI = ConstantInt::get(*Context, APInt(32, 0x3f800000U));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008380
Nate Begeman51409212010-07-28 00:21:48 +00008381 std::vector<Constant*> CV(4, CI);
8382 Constant *C = ConstantVector::get(CV);
8383 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8384 SDValue Addend = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008385 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +00008386 false, false, 16);
8387
8388 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Addend);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008389 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op);
Nate Begeman51409212010-07-28 00:21:48 +00008390 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
8391 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
8392 }
8393 if (VT == MVT::v16i8) {
8394 // a = a << 5;
8395 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8396 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
8397 Op.getOperand(1), DAG.getConstant(5, MVT::i32));
8398
8399 ConstantInt *CM1 = ConstantInt::get(*Context, APInt(8, 15));
8400 ConstantInt *CM2 = ConstantInt::get(*Context, APInt(8, 63));
8401
8402 std::vector<Constant*> CVM1(16, CM1);
8403 std::vector<Constant*> CVM2(16, CM2);
8404 Constant *C = ConstantVector::get(CVM1);
8405 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8406 SDValue M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008407 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +00008408 false, false, 16);
8409
8410 // r = pblendv(r, psllw(r & (char16)15, 4), a);
8411 M = DAG.getNode(ISD::AND, dl, VT, R, M);
8412 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8413 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
8414 DAG.getConstant(4, MVT::i32));
8415 R = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8416 DAG.getConstant(Intrinsic::x86_sse41_pblendvb, MVT::i32),
8417 R, M, Op);
8418 // a += a
8419 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008420
Nate Begeman51409212010-07-28 00:21:48 +00008421 C = ConstantVector::get(CVM2);
8422 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8423 M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008424 MachinePointerInfo::getConstantPool(),
Chris Lattner51abfe42010-09-21 06:02:19 +00008425 false, false, 16);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008426
Nate Begeman51409212010-07-28 00:21:48 +00008427 // r = pblendv(r, psllw(r & (char16)63, 2), a);
8428 M = DAG.getNode(ISD::AND, dl, VT, R, M);
8429 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8430 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
8431 DAG.getConstant(2, MVT::i32));
8432 R = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8433 DAG.getConstant(Intrinsic::x86_sse41_pblendvb, MVT::i32),
8434 R, M, Op);
8435 // a += a
8436 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008437
Nate Begeman51409212010-07-28 00:21:48 +00008438 // return pblendv(r, r+r, a);
8439 R = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8440 DAG.getConstant(Intrinsic::x86_sse41_pblendvb, MVT::i32),
8441 R, DAG.getNode(ISD::ADD, dl, VT, R, R), Op);
8442 return R;
8443 }
8444 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008445}
Mon P Wangaf9b9522008-12-18 21:42:19 +00008446
Dan Gohmand858e902010-04-17 15:26:15 +00008447SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
Bill Wendling74c37652008-12-09 22:08:41 +00008448 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
8449 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +00008450 // looks for this combo and may remove the "setcc" instruction if the "setcc"
8451 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +00008452 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +00008453 SDValue LHS = N->getOperand(0);
8454 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +00008455 unsigned BaseOp = 0;
8456 unsigned Cond = 0;
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008457 DebugLoc DL = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +00008458 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008459 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +00008460 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +00008461 // A subtract of one will be selected as a INC. Note that INC doesn't
8462 // set CF, so we can't do this for UADDO.
8463 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
8464 if (C->getAPIntValue() == 1) {
8465 BaseOp = X86ISD::INC;
8466 Cond = X86::COND_O;
8467 break;
8468 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008469 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +00008470 Cond = X86::COND_O;
8471 break;
8472 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008473 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +00008474 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00008475 break;
8476 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +00008477 // A subtract of one will be selected as a DEC. Note that DEC doesn't
8478 // set CF, so we can't do this for USUBO.
8479 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
8480 if (C->getAPIntValue() == 1) {
8481 BaseOp = X86ISD::DEC;
8482 Cond = X86::COND_O;
8483 break;
8484 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008485 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +00008486 Cond = X86::COND_O;
8487 break;
8488 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008489 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +00008490 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00008491 break;
8492 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00008493 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +00008494 Cond = X86::COND_O;
8495 break;
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008496 case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs
8497 SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0),
8498 MVT::i32);
8499 SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS);
8500
8501 SDValue SetCC =
8502 DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
8503 DAG.getConstant(X86::COND_O, MVT::i32),
8504 SDValue(Sum.getNode(), 2));
8505
8506 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
8507 return Sum;
8508 }
Bill Wendling74c37652008-12-09 22:08:41 +00008509 }
Bill Wendling3fafd932008-11-26 22:37:40 +00008510
Bill Wendling61edeb52008-12-02 01:06:39 +00008511 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008512 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008513 SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +00008514
Bill Wendling61edeb52008-12-02 01:06:39 +00008515 SDValue SetCC =
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008516 DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1),
8517 DAG.getConstant(Cond, MVT::i32),
8518 SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +00008519
Bill Wendling61edeb52008-12-02 01:06:39 +00008520 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
8521 return Sum;
Bill Wendling41ea7e72008-11-24 19:21:46 +00008522}
8523
Eric Christopher9a9d2752010-07-22 02:48:34 +00008524SDValue X86TargetLowering::LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG) const{
8525 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00008526
Eric Christopherb6729dc2010-08-04 23:03:04 +00008527 if (!Subtarget->hasSSE2()) {
Eric Christopherc0b2a202010-08-14 21:51:50 +00008528 SDValue Chain = Op.getOperand(0);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008529 SDValue Zero = DAG.getConstant(0,
Eric Christopherb6729dc2010-08-04 23:03:04 +00008530 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Eric Christopherc0b2a202010-08-14 21:51:50 +00008531 SDValue Ops[] = {
8532 DAG.getRegister(X86::ESP, MVT::i32), // Base
8533 DAG.getTargetConstant(1, MVT::i8), // Scale
8534 DAG.getRegister(0, MVT::i32), // Index
8535 DAG.getTargetConstant(0, MVT::i32), // Disp
8536 DAG.getRegister(0, MVT::i32), // Segment.
8537 Zero,
8538 Chain
8539 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00008540 SDNode *Res =
Eric Christopherc0b2a202010-08-14 21:51:50 +00008541 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
8542 array_lengthof(Ops));
8543 return SDValue(Res, 0);
Eric Christopherb6729dc2010-08-04 23:03:04 +00008544 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00008545
Eric Christopher9a9d2752010-07-22 02:48:34 +00008546 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
Chris Lattner132929a2010-08-14 17:26:09 +00008547 if (!isDev)
Eric Christopher9a9d2752010-07-22 02:48:34 +00008548 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008549
Chris Lattner132929a2010-08-14 17:26:09 +00008550 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
8551 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
8552 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
8553 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
Michael J. Spencerec38de22010-10-10 22:04:20 +00008554
Chris Lattner132929a2010-08-14 17:26:09 +00008555 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
8556 if (!Op1 && !Op2 && !Op3 && Op4)
8557 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008558
Chris Lattner132929a2010-08-14 17:26:09 +00008559 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
8560 if (Op1 && !Op2 && !Op3 && !Op4)
8561 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008562
8563 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
Chris Lattner132929a2010-08-14 17:26:09 +00008564 // (MFENCE)>;
8565 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
Eric Christopher9a9d2752010-07-22 02:48:34 +00008566}
8567
Dan Gohmand858e902010-04-17 15:26:15 +00008568SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008569 EVT T = Op.getValueType();
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008570 DebugLoc DL = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +00008571 unsigned Reg = 0;
8572 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00008573 switch(T.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00008574 default:
8575 assert(false && "Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +00008576 case MVT::i8: Reg = X86::AL; size = 1; break;
8577 case MVT::i16: Reg = X86::AX; size = 2; break;
8578 case MVT::i32: Reg = X86::EAX; size = 4; break;
8579 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +00008580 assert(Subtarget->is64Bit() && "Node not type legal!");
8581 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +00008582 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00008583 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008584 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +00008585 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +00008586 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +00008587 Op.getOperand(1),
8588 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +00008589 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +00008590 cpIn.getValue(1) };
Owen Anderson825b72b2009-08-11 20:47:22 +00008591 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008592 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
8593 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
8594 Ops, 5, T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +00008595 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008596 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +00008597 return cpOut;
8598}
8599
Duncan Sands1607f052008-12-01 11:39:25 +00008600SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008601 SelectionDAG &DAG) const {
Duncan Sands1607f052008-12-01 11:39:25 +00008602 assert(Subtarget->is64Bit() && "Result not type legalized?");
Owen Anderson825b72b2009-08-11 20:47:22 +00008603 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Duncan Sands1607f052008-12-01 11:39:25 +00008604 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008605 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +00008606 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00008607 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
8608 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +00008609 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +00008610 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
8611 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +00008612 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +00008613 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +00008614 rdx.getValue(1)
8615 };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008616 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008617}
8618
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008619SDValue X86TargetLowering::LowerBITCAST(SDValue Op,
Dale Johannesen7d07b482010-05-21 00:52:33 +00008620 SelectionDAG &DAG) const {
8621 EVT SrcVT = Op.getOperand(0).getValueType();
8622 EVT DstVT = Op.getValueType();
Michael J. Spencerec38de22010-10-10 22:04:20 +00008623 assert((Subtarget->is64Bit() && !Subtarget->hasSSE2() &&
Dale Johannesen7d07b482010-05-21 00:52:33 +00008624 Subtarget->hasMMX() && !DisableMMX) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008625 "Unexpected custom BITCAST");
Michael J. Spencerec38de22010-10-10 22:04:20 +00008626 assert((DstVT == MVT::i64 ||
Dale Johannesen7d07b482010-05-21 00:52:33 +00008627 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008628 "Unexpected custom BITCAST");
Dale Johannesen7d07b482010-05-21 00:52:33 +00008629 // i64 <=> MMX conversions are Legal.
8630 if (SrcVT==MVT::i64 && DstVT.isVector())
8631 return Op;
8632 if (DstVT==MVT::i64 && SrcVT.isVector())
8633 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +00008634 // MMX <=> MMX conversions are Legal.
8635 if (SrcVT.isVector() && DstVT.isVector())
8636 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +00008637 // All other conversions need to be expanded.
8638 return SDValue();
8639}
Dan Gohmand858e902010-04-17 15:26:15 +00008640SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen71d1bf52008-09-29 22:25:26 +00008641 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +00008642 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008643 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008644 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +00008645 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008646 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008647 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +00008648 Node->getOperand(0),
8649 Node->getOperand(1), negOp,
8650 cast<AtomicSDNode>(Node)->getSrcValue(),
8651 cast<AtomicSDNode>(Node)->getAlignment());
Mon P Wang63307c32008-05-05 19:05:59 +00008652}
8653
Evan Cheng0db9fe62006-04-25 20:13:52 +00008654/// LowerOperation - Provide custom lowering hooks for some operations.
8655///
Dan Gohmand858e902010-04-17 15:26:15 +00008656SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00008657 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008658 default: llvm_unreachable("Should not custom lower this!");
Eric Christopher9a9d2752010-07-22 02:48:34 +00008659 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op,DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008660 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
8661 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008662 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00008663 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008664 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
8665 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
8666 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
8667 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
8668 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
8669 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00008670 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +00008671 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +00008672 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008673 case ISD::SHL_PARTS:
8674 case ISD::SRA_PARTS:
8675 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
8676 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00008677 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008678 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00008679 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008680 case ISD::FABS: return LowerFABS(Op, DAG);
8681 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008682 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00008683 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman30a0de92008-07-17 16:51:19 +00008684 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00008685 case ISD::SELECT: return LowerSELECT(Op, DAG);
8686 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008687 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008688 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +00008689 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +00008690 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008691 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00008692 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
8693 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008694 case ISD::FRAME_TO_ARGS_OFFSET:
8695 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008696 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008697 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008698 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +00008699 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +00008700 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
8701 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Mon P Wangaf9b9522008-12-18 21:42:19 +00008702 case ISD::MUL: return LowerMUL_V2I64(Op, DAG);
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008703 case ISD::SHL: return LowerSHL(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +00008704 case ISD::SADDO:
8705 case ISD::UADDO:
8706 case ISD::SSUBO:
8707 case ISD::USUBO:
8708 case ISD::SMULO:
8709 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +00008710 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008711 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008712 }
Chris Lattner27a6c732007-11-24 07:07:01 +00008713}
8714
Duncan Sands1607f052008-12-01 11:39:25 +00008715void X86TargetLowering::
8716ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00008717 SelectionDAG &DAG, unsigned NewOp) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008718 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008719 DebugLoc dl = Node->getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00008720 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +00008721
8722 SDValue Chain = Node->getOperand(0);
8723 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00008724 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00008725 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00008726 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00008727 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +00008728 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +00008729 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +00008730 SDValue Result =
8731 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
8732 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +00008733 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +00008734 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00008735 Results.push_back(Result.getValue(2));
8736}
8737
Duncan Sands126d9072008-07-04 11:47:58 +00008738/// ReplaceNodeResults - Replace a node with an illegal result type
8739/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00008740void X86TargetLowering::ReplaceNodeResults(SDNode *N,
8741 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00008742 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +00008743 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +00008744 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +00008745 default:
Duncan Sands1607f052008-12-01 11:39:25 +00008746 assert(false && "Do not know how to custom type legalize this operation!");
8747 return;
8748 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +00008749 std::pair<SDValue,SDValue> Vals =
8750 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +00008751 SDValue FIST = Vals.first, StackSlot = Vals.second;
8752 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +00008753 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +00008754 // Return a load from the stack slot.
Chris Lattner51abfe42010-09-21 06:02:19 +00008755 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
8756 MachinePointerInfo(), false, false, 0));
Duncan Sands1607f052008-12-01 11:39:25 +00008757 }
8758 return;
8759 }
8760 case ISD::READCYCLECOUNTER: {
Owen Anderson825b72b2009-08-11 20:47:22 +00008761 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Duncan Sands1607f052008-12-01 11:39:25 +00008762 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008763 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00008764 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +00008765 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00008766 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00008767 eax.getValue(2));
8768 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
8769 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +00008770 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00008771 Results.push_back(edx.getValue(1));
8772 return;
8773 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008774 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +00008775 EVT T = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008776 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
Duncan Sands1607f052008-12-01 11:39:25 +00008777 SDValue cpInL, cpInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00008778 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
8779 DAG.getConstant(0, MVT::i32));
8780 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
8781 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00008782 cpInL = DAG.getCopyToReg(N->getOperand(0), dl, X86::EAX, cpInL, SDValue());
8783 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl, X86::EDX, cpInH,
Duncan Sands1607f052008-12-01 11:39:25 +00008784 cpInL.getValue(1));
8785 SDValue swapInL, swapInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00008786 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
8787 DAG.getConstant(0, MVT::i32));
8788 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
8789 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00008790 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl, X86::EBX, swapInL,
Duncan Sands1607f052008-12-01 11:39:25 +00008791 cpInH.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00008792 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl, X86::ECX, swapInH,
Duncan Sands1607f052008-12-01 11:39:25 +00008793 swapInL.getValue(1));
8794 SDValue Ops[] = { swapInH.getValue(0),
8795 N->getOperand(1),
8796 swapInH.getValue(1) };
Owen Anderson825b72b2009-08-11 20:47:22 +00008797 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Andrew Trick1a2cf3b2010-10-11 19:02:04 +00008798 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
8799 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG8_DAG, dl, Tys,
8800 Ops, 3, T, MMO);
Dale Johannesendd64c412009-02-04 00:33:20 +00008801 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl, X86::EAX,
Owen Anderson825b72b2009-08-11 20:47:22 +00008802 MVT::i32, Result.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00008803 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl, X86::EDX,
Owen Anderson825b72b2009-08-11 20:47:22 +00008804 MVT::i32, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +00008805 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Owen Anderson825b72b2009-08-11 20:47:22 +00008806 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00008807 Results.push_back(cpOutH.getValue(1));
8808 return;
8809 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008810 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +00008811 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
8812 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008813 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +00008814 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
8815 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008816 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +00008817 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
8818 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008819 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +00008820 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
8821 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008822 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +00008823 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
8824 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008825 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +00008826 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
8827 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008828 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +00008829 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
8830 return;
Chris Lattner27a6c732007-11-24 07:07:01 +00008831 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00008832}
8833
Evan Cheng72261582005-12-20 06:22:03 +00008834const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
8835 switch (Opcode) {
8836 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +00008837 case X86ISD::BSF: return "X86ISD::BSF";
8838 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +00008839 case X86ISD::SHLD: return "X86ISD::SHLD";
8840 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00008841 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +00008842 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +00008843 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +00008844 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +00008845 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00008846 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00008847 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
8848 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
8849 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00008850 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00008851 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +00008852 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +00008853 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +00008854 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +00008855 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +00008856 case X86ISD::COMI: return "X86ISD::COMI";
8857 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +00008858 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +00008859 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Evan Cheng72261582005-12-20 06:22:03 +00008860 case X86ISD::CMOV: return "X86ISD::CMOV";
8861 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00008862 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00008863 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
8864 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +00008865 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00008866 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +00008867 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +00008868 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +00008869 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +00008870 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
8871 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +00008872 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +00008873 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Nate Begemanb65c1752010-12-17 22:55:37 +00008874 case X86ISD::PANDN: return "X86ISD::PANDN";
8875 case X86ISD::PSIGNB: return "X86ISD::PSIGNB";
8876 case X86ISD::PSIGNW: return "X86ISD::PSIGNW";
8877 case X86ISD::PSIGND: return "X86ISD::PSIGND";
Evan Cheng8ca29322006-11-10 21:43:37 +00008878 case X86ISD::FMAX: return "X86ISD::FMAX";
8879 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +00008880 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
8881 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00008882 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +00008883 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008884 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00008885 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008886 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +00008887 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
8888 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008889 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
8890 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
8891 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
8892 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
8893 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
8894 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +00008895 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
8896 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +00008897 case X86ISD::VSHL: return "X86ISD::VSHL";
8898 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +00008899 case X86ISD::CMPPD: return "X86ISD::CMPPD";
8900 case X86ISD::CMPPS: return "X86ISD::CMPPS";
8901 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
8902 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
8903 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
8904 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
8905 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
8906 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
8907 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
8908 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008909 case X86ISD::ADD: return "X86ISD::ADD";
8910 case X86ISD::SUB: return "X86ISD::SUB";
Bill Wendlingd350e022008-12-12 21:15:41 +00008911 case X86ISD::SMUL: return "X86ISD::SMUL";
8912 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +00008913 case X86ISD::INC: return "X86ISD::INC";
8914 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +00008915 case X86ISD::OR: return "X86ISD::OR";
8916 case X86ISD::XOR: return "X86ISD::XOR";
8917 case X86ISD::AND: return "X86ISD::AND";
Evan Cheng73f24c92009-03-30 21:36:47 +00008918 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +00008919 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008920 case X86ISD::TESTP: return "X86ISD::TESTP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00008921 case X86ISD::PALIGN: return "X86ISD::PALIGN";
8922 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
8923 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
8924 case X86ISD::PSHUFHW_LD: return "X86ISD::PSHUFHW_LD";
8925 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
8926 case X86ISD::PSHUFLW_LD: return "X86ISD::PSHUFLW_LD";
8927 case X86ISD::SHUFPS: return "X86ISD::SHUFPS";
8928 case X86ISD::SHUFPD: return "X86ISD::SHUFPD";
8929 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00008930 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00008931 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00008932 case X86ISD::MOVHLPD: return "X86ISD::MOVHLPD";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00008933 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
8934 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00008935 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
8936 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
8937 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
8938 case X86ISD::MOVSHDUP_LD: return "X86ISD::MOVSHDUP_LD";
8939 case X86ISD::MOVSLDUP_LD: return "X86ISD::MOVSLDUP_LD";
8940 case X86ISD::MOVSD: return "X86ISD::MOVSD";
8941 case X86ISD::MOVSS: return "X86ISD::MOVSS";
8942 case X86ISD::UNPCKLPS: return "X86ISD::UNPCKLPS";
8943 case X86ISD::UNPCKLPD: return "X86ISD::UNPCKLPD";
8944 case X86ISD::UNPCKHPS: return "X86ISD::UNPCKHPS";
8945 case X86ISD::UNPCKHPD: return "X86ISD::UNPCKHPD";
8946 case X86ISD::PUNPCKLBW: return "X86ISD::PUNPCKLBW";
8947 case X86ISD::PUNPCKLWD: return "X86ISD::PUNPCKLWD";
8948 case X86ISD::PUNPCKLDQ: return "X86ISD::PUNPCKLDQ";
8949 case X86ISD::PUNPCKLQDQ: return "X86ISD::PUNPCKLQDQ";
8950 case X86ISD::PUNPCKHBW: return "X86ISD::PUNPCKHBW";
8951 case X86ISD::PUNPCKHWD: return "X86ISD::PUNPCKHWD";
8952 case X86ISD::PUNPCKHDQ: return "X86ISD::PUNPCKHDQ";
8953 case X86ISD::PUNPCKHQDQ: return "X86ISD::PUNPCKHQDQ";
Dan Gohmand6708ea2009-08-15 01:38:56 +00008954 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Dan Gohman320afb82010-10-12 18:00:49 +00008955 case X86ISD::VAARG_64: return "X86ISD::VAARG_64";
Michael J. Spencere9c253e2010-10-21 01:41:01 +00008956 case X86ISD::WIN_ALLOCA: return "X86ISD::WIN_ALLOCA";
Evan Cheng72261582005-12-20 06:22:03 +00008957 }
8958}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008959
Chris Lattnerc9addb72007-03-30 23:15:24 +00008960// isLegalAddressingMode - Return true if the addressing mode represented
8961// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +00008962bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerc9addb72007-03-30 23:15:24 +00008963 const Type *Ty) const {
8964 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00008965 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +00008966 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +00008967
Chris Lattnerc9addb72007-03-30 23:15:24 +00008968 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00008969 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +00008970 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00008971
Chris Lattnerc9addb72007-03-30 23:15:24 +00008972 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +00008973 unsigned GVFlags =
8974 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00008975
Chris Lattnerdfed4132009-07-10 07:38:24 +00008976 // If a reference to this global requires an extra load, we can't fold it.
8977 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +00008978 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00008979
Chris Lattnerdfed4132009-07-10 07:38:24 +00008980 // If BaseGV requires a register for the PIC base, we cannot also have a
8981 // BaseReg specified.
8982 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +00008983 return false;
Evan Cheng52787842007-08-01 23:46:47 +00008984
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00008985 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +00008986 if ((M != CodeModel::Small || R != Reloc::Static) &&
8987 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00008988 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +00008989 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008990
Chris Lattnerc9addb72007-03-30 23:15:24 +00008991 switch (AM.Scale) {
8992 case 0:
8993 case 1:
8994 case 2:
8995 case 4:
8996 case 8:
8997 // These scales always work.
8998 break;
8999 case 3:
9000 case 5:
9001 case 9:
9002 // These scales are formed with basereg+scalereg. Only accept if there is
9003 // no basereg yet.
9004 if (AM.HasBaseReg)
9005 return false;
9006 break;
9007 default: // Other stuff never works.
9008 return false;
9009 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009010
Chris Lattnerc9addb72007-03-30 23:15:24 +00009011 return true;
9012}
9013
9014
Evan Cheng2bd122c2007-10-26 01:56:11 +00009015bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00009016 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +00009017 return false;
Evan Chenge127a732007-10-29 07:57:50 +00009018 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
9019 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00009020 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +00009021 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00009022 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +00009023}
9024
Owen Andersone50ed302009-08-10 22:56:29 +00009025bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +00009026 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009027 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009028 unsigned NumBits1 = VT1.getSizeInBits();
9029 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00009030 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009031 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00009032 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009033}
Evan Cheng2bd122c2007-10-26 01:56:11 +00009034
Dan Gohman97121ba2009-04-08 00:15:30 +00009035bool X86TargetLowering::isZExtFree(const Type *Ty1, const Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00009036 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00009037 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00009038}
9039
Owen Andersone50ed302009-08-10 22:56:29 +00009040bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00009041 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00009042 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00009043}
9044
Owen Andersone50ed302009-08-10 22:56:29 +00009045bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +00009046 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +00009047 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +00009048}
9049
Evan Cheng60c07e12006-07-05 22:17:51 +00009050/// isShuffleMaskLegal - Targets can use this to indicate that they only
9051/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
9052/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
9053/// are assumed to be legal.
9054bool
Eric Christopherfd179292009-08-27 18:07:15 +00009055X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +00009056 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +00009057 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +00009058 if (VT.getSizeInBits() == 64)
Eric Christophercff6f852010-04-15 01:40:20 +00009059 return isPALIGNRMask(M, VT, Subtarget->hasSSSE3());
Nate Begeman9008ca62009-04-27 18:41:29 +00009060
Nate Begemana09008b2009-10-19 02:17:23 +00009061 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +00009062 return (VT.getVectorNumElements() == 2 ||
9063 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
9064 isMOVLMask(M, VT) ||
9065 isSHUFPMask(M, VT) ||
9066 isPSHUFDMask(M, VT) ||
9067 isPSHUFHWMask(M, VT) ||
9068 isPSHUFLWMask(M, VT) ||
Nate Begemana09008b2009-10-19 02:17:23 +00009069 isPALIGNRMask(M, VT, Subtarget->hasSSSE3()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +00009070 isUNPCKLMask(M, VT) ||
9071 isUNPCKHMask(M, VT) ||
9072 isUNPCKL_v_undef_Mask(M, VT) ||
9073 isUNPCKH_v_undef_Mask(M, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00009074}
9075
Dan Gohman7d8143f2008-04-09 20:09:42 +00009076bool
Nate Begeman5a5ca152009-04-29 05:20:52 +00009077X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +00009078 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00009079 unsigned NumElts = VT.getVectorNumElements();
9080 // FIXME: This collection of masks seems suspect.
9081 if (NumElts == 2)
9082 return true;
9083 if (NumElts == 4 && VT.getSizeInBits() == 128) {
9084 return (isMOVLMask(Mask, VT) ||
9085 isCommutedMOVLMask(Mask, VT, true) ||
9086 isSHUFPMask(Mask, VT) ||
9087 isCommutedSHUFPMask(Mask, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00009088 }
9089 return false;
9090}
9091
9092//===----------------------------------------------------------------------===//
9093// X86 Scheduler Hooks
9094//===----------------------------------------------------------------------===//
9095
Mon P Wang63307c32008-05-05 19:05:59 +00009096// private utility function
9097MachineBasicBlock *
9098X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
9099 MachineBasicBlock *MBB,
9100 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009101 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009102 unsigned LoadOpc,
9103 unsigned CXchgOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009104 unsigned notOpc,
9105 unsigned EAXreg,
9106 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009107 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00009108 // For the atomic bitwise operator, we generate
9109 // thisMBB:
9110 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00009111 // ld t1 = [bitinstr.addr]
9112 // op t2 = t1, [bitinstr.val]
9113 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00009114 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
9115 // bz newMBB
9116 // fallthrough -->nextMBB
9117 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9118 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009119 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00009120 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009121
Mon P Wang63307c32008-05-05 19:05:59 +00009122 /// First build the CFG
9123 MachineFunction *F = MBB->getParent();
9124 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009125 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9126 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9127 F->insert(MBBIter, newMBB);
9128 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009129
Dan Gohman14152b42010-07-06 20:24:04 +00009130 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9131 nextMBB->splice(nextMBB->begin(), thisMBB,
9132 llvm::next(MachineBasicBlock::iterator(bInstr)),
9133 thisMBB->end());
9134 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009135
Mon P Wang63307c32008-05-05 19:05:59 +00009136 // Update thisMBB to fall through to newMBB
9137 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009138
Mon P Wang63307c32008-05-05 19:05:59 +00009139 // newMBB jumps to itself and fall through to nextMBB
9140 newMBB->addSuccessor(nextMBB);
9141 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009142
Mon P Wang63307c32008-05-05 19:05:59 +00009143 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009144 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009145 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +00009146 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00009147 MachineOperand& destOper = bInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009148 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00009149 int numArgs = bInstr->getNumOperands() - 1;
9150 for (int i=0; i < numArgs; ++i)
9151 argOpers[i] = &bInstr->getOperand(i+1);
9152
9153 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009154 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009155 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00009156
Dale Johannesen140be2d2008-08-19 18:47:28 +00009157 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009158 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00009159 for (int i=0; i <= lastAddrIndx; ++i)
9160 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009161
Dale Johannesen140be2d2008-08-19 18:47:28 +00009162 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009163 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00009164 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009165 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009166 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009167 tt = t1;
9168
Dale Johannesen140be2d2008-08-19 18:47:28 +00009169 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +00009170 assert((argOpers[valArgIndx]->isReg() ||
9171 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00009172 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +00009173 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009174 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00009175 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009176 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009177 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +00009178 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009179
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009180 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +00009181 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009182
Dale Johannesene4d209d2009-02-03 20:21:25 +00009183 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +00009184 for (int i=0; i <= lastAddrIndx; ++i)
9185 (*MIB).addOperand(*argOpers[i]);
9186 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +00009187 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00009188 (*MIB).setMemRefs(bInstr->memoperands_begin(),
9189 bInstr->memoperands_end());
Mon P Wangf5952662008-07-17 04:54:06 +00009190
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009191 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +00009192 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +00009193
Mon P Wang63307c32008-05-05 19:05:59 +00009194 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009195 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00009196
Dan Gohman14152b42010-07-06 20:24:04 +00009197 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00009198 return nextMBB;
9199}
9200
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00009201// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +00009202MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009203X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
9204 MachineBasicBlock *MBB,
9205 unsigned regOpcL,
9206 unsigned regOpcH,
9207 unsigned immOpcL,
9208 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009209 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009210 // For the atomic bitwise operator, we generate
9211 // thisMBB (instructions are in pairs, except cmpxchg8b)
9212 // ld t1,t2 = [bitinstr.addr]
9213 // newMBB:
9214 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
9215 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +00009216 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009217 // mov ECX, EBX <- t5, t6
9218 // mov EAX, EDX <- t1, t2
9219 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
9220 // mov t3, t4 <- EAX, EDX
9221 // bz newMBB
9222 // result in out1, out2
9223 // fallthrough -->nextMBB
9224
9225 const TargetRegisterClass *RC = X86::GR32RegisterClass;
9226 const unsigned LoadOpc = X86::MOV32rm;
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009227 const unsigned NotOpc = X86::NOT32r;
9228 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9229 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
9230 MachineFunction::iterator MBBIter = MBB;
9231 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009232
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009233 /// First build the CFG
9234 MachineFunction *F = MBB->getParent();
9235 MachineBasicBlock *thisMBB = MBB;
9236 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9237 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9238 F->insert(MBBIter, newMBB);
9239 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009240
Dan Gohman14152b42010-07-06 20:24:04 +00009241 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9242 nextMBB->splice(nextMBB->begin(), thisMBB,
9243 llvm::next(MachineBasicBlock::iterator(bInstr)),
9244 thisMBB->end());
9245 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009246
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009247 // Update thisMBB to fall through to newMBB
9248 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009249
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009250 // newMBB jumps to itself and fall through to nextMBB
9251 newMBB->addSuccessor(nextMBB);
9252 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009253
Dale Johannesene4d209d2009-02-03 20:21:25 +00009254 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009255 // Insert instructions into newMBB based on incoming instruction
9256 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009257 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009258 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009259 MachineOperand& dest1Oper = bInstr->getOperand(0);
9260 MachineOperand& dest2Oper = bInstr->getOperand(1);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009261 MachineOperand* argOpers[2 + X86::AddrNumOperands];
9262 for (int i=0; i < 2 + X86::AddrNumOperands; ++i) {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009263 argOpers[i] = &bInstr->getOperand(i+2);
9264
Dan Gohman71ea4e52010-05-14 21:01:44 +00009265 // We use some of the operands multiple times, so conservatively just
9266 // clear any kill flags that might be present.
9267 if (argOpers[i]->isReg() && argOpers[i]->isUse())
9268 argOpers[i]->setIsKill(false);
9269 }
9270
Evan Chengad5b52f2010-01-08 19:14:57 +00009271 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009272 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +00009273
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009274 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009275 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009276 for (int i=0; i <= lastAddrIndx; ++i)
9277 (*MIB).addOperand(*argOpers[i]);
9278 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009279 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +00009280 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +00009281 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009282 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +00009283 MachineOperand newOp3 = *(argOpers[3]);
9284 if (newOp3.isImm())
9285 newOp3.setImm(newOp3.getImm()+4);
9286 else
9287 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009288 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +00009289 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009290
9291 // t3/4 are defined later, at the bottom of the loop
9292 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
9293 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009294 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009295 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009296 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009297 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
9298
Evan Cheng306b4ca2010-01-08 23:41:50 +00009299 // The subsequent operations should be using the destination registers of
9300 //the PHI instructions.
Scott Michelfdc40a02009-02-17 22:15:04 +00009301 if (invSrc) {
Evan Cheng306b4ca2010-01-08 23:41:50 +00009302 t1 = F->getRegInfo().createVirtualRegister(RC);
9303 t2 = F->getRegInfo().createVirtualRegister(RC);
9304 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t1).addReg(dest1Oper.getReg());
9305 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t2).addReg(dest2Oper.getReg());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009306 } else {
Evan Cheng306b4ca2010-01-08 23:41:50 +00009307 t1 = dest1Oper.getReg();
9308 t2 = dest2Oper.getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009309 }
9310
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009311 int valArgIndx = lastAddrIndx + 1;
9312 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +00009313 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009314 "invalid operand");
9315 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
9316 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009317 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009318 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009319 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009320 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +00009321 if (regOpcL != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00009322 MIB.addReg(t1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009323 (*MIB).addOperand(*argOpers[valArgIndx]);
9324 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00009325 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009326 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00009327 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009328 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009329 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009330 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009331 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +00009332 if (regOpcH != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00009333 MIB.addReg(t2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009334 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009335
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009336 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009337 MIB.addReg(t1);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009338 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009339 MIB.addReg(t2);
9340
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009341 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009342 MIB.addReg(t5);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009343 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009344 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +00009345
Dale Johannesene4d209d2009-02-03 20:21:25 +00009346 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009347 for (int i=0; i <= lastAddrIndx; ++i)
9348 (*MIB).addOperand(*argOpers[i]);
9349
9350 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00009351 (*MIB).setMemRefs(bInstr->memoperands_begin(),
9352 bInstr->memoperands_end());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009353
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009354 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009355 MIB.addReg(X86::EAX);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009356 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009357 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +00009358
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009359 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009360 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009361
Dan Gohman14152b42010-07-06 20:24:04 +00009362 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009363 return nextMBB;
9364}
9365
9366// private utility function
9367MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +00009368X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
9369 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009370 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00009371 // For the atomic min/max operator, we generate
9372 // thisMBB:
9373 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00009374 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +00009375 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +00009376 // cmp t1, t2
9377 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +00009378 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00009379 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
9380 // bz newMBB
9381 // fallthrough -->nextMBB
9382 //
9383 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9384 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009385 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00009386 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009387
Mon P Wang63307c32008-05-05 19:05:59 +00009388 /// First build the CFG
9389 MachineFunction *F = MBB->getParent();
9390 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009391 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9392 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9393 F->insert(MBBIter, newMBB);
9394 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009395
Dan Gohman14152b42010-07-06 20:24:04 +00009396 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9397 nextMBB->splice(nextMBB->begin(), thisMBB,
9398 llvm::next(MachineBasicBlock::iterator(mInstr)),
9399 thisMBB->end());
9400 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009401
Mon P Wang63307c32008-05-05 19:05:59 +00009402 // Update thisMBB to fall through to newMBB
9403 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009404
Mon P Wang63307c32008-05-05 19:05:59 +00009405 // newMBB jumps to newMBB and fall through to nextMBB
9406 newMBB->addSuccessor(nextMBB);
9407 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009408
Dale Johannesene4d209d2009-02-03 20:21:25 +00009409 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00009410 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009411 assert(mInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009412 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +00009413 MachineOperand& destOper = mInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009414 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00009415 int numArgs = mInstr->getNumOperands() - 1;
9416 for (int i=0; i < numArgs; ++i)
9417 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009418
Mon P Wang63307c32008-05-05 19:05:59 +00009419 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009420 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009421 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00009422
Mon P Wangab3e7472008-05-05 22:56:23 +00009423 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009424 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00009425 for (int i=0; i <= lastAddrIndx; ++i)
9426 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +00009427
Mon P Wang63307c32008-05-05 19:05:59 +00009428 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +00009429 assert((argOpers[valArgIndx]->isReg() ||
9430 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00009431 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +00009432
9433 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +00009434 if (argOpers[valArgIndx]->isReg())
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009435 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +00009436 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009437 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00009438 (*MIB).addOperand(*argOpers[valArgIndx]);
9439
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009440 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +00009441 MIB.addReg(t1);
9442
Dale Johannesene4d209d2009-02-03 20:21:25 +00009443 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +00009444 MIB.addReg(t1);
9445 MIB.addReg(t2);
9446
9447 // Generate movc
9448 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009449 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +00009450 MIB.addReg(t2);
9451 MIB.addReg(t1);
9452
9453 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +00009454 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +00009455 for (int i=0; i <= lastAddrIndx; ++i)
9456 (*MIB).addOperand(*argOpers[i]);
9457 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +00009458 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00009459 (*MIB).setMemRefs(mInstr->memoperands_begin(),
9460 mInstr->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +00009461
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009462 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +00009463 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +00009464
Mon P Wang63307c32008-05-05 19:05:59 +00009465 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009466 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00009467
Dan Gohman14152b42010-07-06 20:24:04 +00009468 mInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00009469 return nextMBB;
9470}
9471
Eric Christopherf83a5de2009-08-27 18:08:16 +00009472// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00009473// or XMM0_V32I8 in AVX all of this code can be replaced with that
9474// in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +00009475MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +00009476X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +00009477 unsigned numArgs, bool memArg) const {
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00009478 assert((Subtarget->hasSSE42() || Subtarget->hasAVX()) &&
9479 "Target must have SSE4.2 or AVX features enabled");
9480
Eric Christopherb120ab42009-08-18 22:50:32 +00009481 DebugLoc dl = MI->getDebugLoc();
9482 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Eric Christopherb120ab42009-08-18 22:50:32 +00009483 unsigned Opc;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00009484 if (!Subtarget->hasAVX()) {
9485 if (memArg)
9486 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
9487 else
9488 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
9489 } else {
9490 if (memArg)
9491 Opc = numArgs == 3 ? X86::VPCMPISTRM128rm : X86::VPCMPESTRM128rm;
9492 else
9493 Opc = numArgs == 3 ? X86::VPCMPISTRM128rr : X86::VPCMPESTRM128rr;
9494 }
Eric Christopherb120ab42009-08-18 22:50:32 +00009495
Eric Christopher41c902f2010-11-30 08:20:21 +00009496 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Eric Christopherb120ab42009-08-18 22:50:32 +00009497 for (unsigned i = 0; i < numArgs; ++i) {
9498 MachineOperand &Op = MI->getOperand(i+1);
Eric Christopherb120ab42009-08-18 22:50:32 +00009499 if (!(Op.isReg() && Op.isImplicit()))
9500 MIB.addOperand(Op);
9501 }
Eric Christopher41c902f2010-11-30 08:20:21 +00009502 BuildMI(*BB, MI, dl, TII->get(X86::MOVAPSrr), MI->getOperand(0).getReg())
Eric Christopherb120ab42009-08-18 22:50:32 +00009503 .addReg(X86::XMM0);
9504
Dan Gohman14152b42010-07-06 20:24:04 +00009505 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +00009506 return BB;
9507}
9508
9509MachineBasicBlock *
Eric Christopher228232b2010-11-30 07:20:12 +00009510X86TargetLowering::EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +00009511 DebugLoc dl = MI->getDebugLoc();
9512 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9513
9514 // Address into RAX/EAX, other two args into ECX, EDX.
9515 unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
9516 unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
9517 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg);
9518 for (int i = 0; i < X86::AddrNumOperands; ++i)
Eric Christopher82be2202010-11-30 08:10:28 +00009519 MIB.addOperand(MI->getOperand(i));
Eric Christopher228232b2010-11-30 07:20:12 +00009520
9521 unsigned ValOps = X86::AddrNumOperands;
9522 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
9523 .addReg(MI->getOperand(ValOps).getReg());
9524 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX)
9525 .addReg(MI->getOperand(ValOps+1).getReg());
9526
9527 // The instruction doesn't actually take any operands though.
9528 BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr));
9529
9530 MI->eraseFromParent(); // The pseudo is gone now.
9531 return BB;
9532}
9533
9534MachineBasicBlock *
9535X86TargetLowering::EmitMwait(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +00009536 DebugLoc dl = MI->getDebugLoc();
9537 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9538
9539 // First arg in ECX, the second in EAX.
9540 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
9541 .addReg(MI->getOperand(0).getReg());
9542 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EAX)
9543 .addReg(MI->getOperand(1).getReg());
9544
9545 // The instruction doesn't actually take any operands though.
9546 BuildMI(*BB, MI, dl, TII->get(X86::MWAITrr));
9547
9548 MI->eraseFromParent(); // The pseudo is gone now.
9549 return BB;
9550}
9551
9552MachineBasicBlock *
Dan Gohman320afb82010-10-12 18:00:49 +00009553X86TargetLowering::EmitVAARG64WithCustomInserter(
9554 MachineInstr *MI,
9555 MachineBasicBlock *MBB) const {
9556 // Emit va_arg instruction on X86-64.
9557
9558 // Operands to this pseudo-instruction:
9559 // 0 ) Output : destination address (reg)
9560 // 1-5) Input : va_list address (addr, i64mem)
9561 // 6 ) ArgSize : Size (in bytes) of vararg type
9562 // 7 ) ArgMode : 0=overflow only, 1=use gp_offset, 2=use fp_offset
9563 // 8 ) Align : Alignment of type
9564 // 9 ) EFLAGS (implicit-def)
9565
9566 assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
9567 assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands");
9568
9569 unsigned DestReg = MI->getOperand(0).getReg();
9570 MachineOperand &Base = MI->getOperand(1);
9571 MachineOperand &Scale = MI->getOperand(2);
9572 MachineOperand &Index = MI->getOperand(3);
9573 MachineOperand &Disp = MI->getOperand(4);
9574 MachineOperand &Segment = MI->getOperand(5);
9575 unsigned ArgSize = MI->getOperand(6).getImm();
9576 unsigned ArgMode = MI->getOperand(7).getImm();
9577 unsigned Align = MI->getOperand(8).getImm();
9578
9579 // Memory Reference
9580 assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand");
9581 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
9582 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
9583
9584 // Machine Information
9585 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9586 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
9587 const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64);
9588 const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32);
9589 DebugLoc DL = MI->getDebugLoc();
9590
9591 // struct va_list {
9592 // i32 gp_offset
9593 // i32 fp_offset
9594 // i64 overflow_area (address)
9595 // i64 reg_save_area (address)
9596 // }
9597 // sizeof(va_list) = 24
9598 // alignment(va_list) = 8
9599
9600 unsigned TotalNumIntRegs = 6;
9601 unsigned TotalNumXMMRegs = 8;
9602 bool UseGPOffset = (ArgMode == 1);
9603 bool UseFPOffset = (ArgMode == 2);
9604 unsigned MaxOffset = TotalNumIntRegs * 8 +
9605 (UseFPOffset ? TotalNumXMMRegs * 16 : 0);
9606
9607 /* Align ArgSize to a multiple of 8 */
9608 unsigned ArgSizeA8 = (ArgSize + 7) & ~7;
9609 bool NeedsAlign = (Align > 8);
9610
9611 MachineBasicBlock *thisMBB = MBB;
9612 MachineBasicBlock *overflowMBB;
9613 MachineBasicBlock *offsetMBB;
9614 MachineBasicBlock *endMBB;
9615
9616 unsigned OffsetDestReg = 0; // Argument address computed by offsetMBB
9617 unsigned OverflowDestReg = 0; // Argument address computed by overflowMBB
9618 unsigned OffsetReg = 0;
9619
9620 if (!UseGPOffset && !UseFPOffset) {
9621 // If we only pull from the overflow region, we don't create a branch.
9622 // We don't need to alter control flow.
9623 OffsetDestReg = 0; // unused
9624 OverflowDestReg = DestReg;
9625
9626 offsetMBB = NULL;
9627 overflowMBB = thisMBB;
9628 endMBB = thisMBB;
9629 } else {
9630 // First emit code to check if gp_offset (or fp_offset) is below the bound.
9631 // If so, pull the argument from reg_save_area. (branch to offsetMBB)
9632 // If not, pull from overflow_area. (branch to overflowMBB)
9633 //
9634 // thisMBB
9635 // | .
9636 // | .
9637 // offsetMBB overflowMBB
9638 // | .
9639 // | .
9640 // endMBB
9641
9642 // Registers for the PHI in endMBB
9643 OffsetDestReg = MRI.createVirtualRegister(AddrRegClass);
9644 OverflowDestReg = MRI.createVirtualRegister(AddrRegClass);
9645
9646 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
9647 MachineFunction *MF = MBB->getParent();
9648 overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB);
9649 offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB);
9650 endMBB = MF->CreateMachineBasicBlock(LLVM_BB);
9651
9652 MachineFunction::iterator MBBIter = MBB;
9653 ++MBBIter;
9654
9655 // Insert the new basic blocks
9656 MF->insert(MBBIter, offsetMBB);
9657 MF->insert(MBBIter, overflowMBB);
9658 MF->insert(MBBIter, endMBB);
9659
9660 // Transfer the remainder of MBB and its successor edges to endMBB.
9661 endMBB->splice(endMBB->begin(), thisMBB,
9662 llvm::next(MachineBasicBlock::iterator(MI)),
9663 thisMBB->end());
9664 endMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
9665
9666 // Make offsetMBB and overflowMBB successors of thisMBB
9667 thisMBB->addSuccessor(offsetMBB);
9668 thisMBB->addSuccessor(overflowMBB);
9669
9670 // endMBB is a successor of both offsetMBB and overflowMBB
9671 offsetMBB->addSuccessor(endMBB);
9672 overflowMBB->addSuccessor(endMBB);
9673
9674 // Load the offset value into a register
9675 OffsetReg = MRI.createVirtualRegister(OffsetRegClass);
9676 BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg)
9677 .addOperand(Base)
9678 .addOperand(Scale)
9679 .addOperand(Index)
9680 .addDisp(Disp, UseFPOffset ? 4 : 0)
9681 .addOperand(Segment)
9682 .setMemRefs(MMOBegin, MMOEnd);
9683
9684 // Check if there is enough room left to pull this argument.
9685 BuildMI(thisMBB, DL, TII->get(X86::CMP32ri))
9686 .addReg(OffsetReg)
9687 .addImm(MaxOffset + 8 - ArgSizeA8);
9688
9689 // Branch to "overflowMBB" if offset >= max
9690 // Fall through to "offsetMBB" otherwise
9691 BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE)))
9692 .addMBB(overflowMBB);
9693 }
9694
9695 // In offsetMBB, emit code to use the reg_save_area.
9696 if (offsetMBB) {
9697 assert(OffsetReg != 0);
9698
9699 // Read the reg_save_area address.
9700 unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass);
9701 BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg)
9702 .addOperand(Base)
9703 .addOperand(Scale)
9704 .addOperand(Index)
9705 .addDisp(Disp, 16)
9706 .addOperand(Segment)
9707 .setMemRefs(MMOBegin, MMOEnd);
9708
9709 // Zero-extend the offset
9710 unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass);
9711 BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64)
9712 .addImm(0)
9713 .addReg(OffsetReg)
9714 .addImm(X86::sub_32bit);
9715
9716 // Add the offset to the reg_save_area to get the final address.
9717 BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg)
9718 .addReg(OffsetReg64)
9719 .addReg(RegSaveReg);
9720
9721 // Compute the offset for the next argument
9722 unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass);
9723 BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg)
9724 .addReg(OffsetReg)
9725 .addImm(UseFPOffset ? 16 : 8);
9726
9727 // Store it back into the va_list.
9728 BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr))
9729 .addOperand(Base)
9730 .addOperand(Scale)
9731 .addOperand(Index)
9732 .addDisp(Disp, UseFPOffset ? 4 : 0)
9733 .addOperand(Segment)
9734 .addReg(NextOffsetReg)
9735 .setMemRefs(MMOBegin, MMOEnd);
9736
9737 // Jump to endMBB
9738 BuildMI(offsetMBB, DL, TII->get(X86::JMP_4))
9739 .addMBB(endMBB);
9740 }
9741
9742 //
9743 // Emit code to use overflow area
9744 //
9745
9746 // Load the overflow_area address into a register.
9747 unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass);
9748 BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg)
9749 .addOperand(Base)
9750 .addOperand(Scale)
9751 .addOperand(Index)
9752 .addDisp(Disp, 8)
9753 .addOperand(Segment)
9754 .setMemRefs(MMOBegin, MMOEnd);
9755
9756 // If we need to align it, do so. Otherwise, just copy the address
9757 // to OverflowDestReg.
9758 if (NeedsAlign) {
9759 // Align the overflow address
9760 assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2");
9761 unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass);
9762
9763 // aligned_addr = (addr + (align-1)) & ~(align-1)
9764 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg)
9765 .addReg(OverflowAddrReg)
9766 .addImm(Align-1);
9767
9768 BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg)
9769 .addReg(TmpReg)
9770 .addImm(~(uint64_t)(Align-1));
9771 } else {
9772 BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg)
9773 .addReg(OverflowAddrReg);
9774 }
9775
9776 // Compute the next overflow address after this argument.
9777 // (the overflow address should be kept 8-byte aligned)
9778 unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass);
9779 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg)
9780 .addReg(OverflowDestReg)
9781 .addImm(ArgSizeA8);
9782
9783 // Store the new overflow address.
9784 BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr))
9785 .addOperand(Base)
9786 .addOperand(Scale)
9787 .addOperand(Index)
9788 .addDisp(Disp, 8)
9789 .addOperand(Segment)
9790 .addReg(NextAddrReg)
9791 .setMemRefs(MMOBegin, MMOEnd);
9792
9793 // If we branched, emit the PHI to the front of endMBB.
9794 if (offsetMBB) {
9795 BuildMI(*endMBB, endMBB->begin(), DL,
9796 TII->get(X86::PHI), DestReg)
9797 .addReg(OffsetDestReg).addMBB(offsetMBB)
9798 .addReg(OverflowDestReg).addMBB(overflowMBB);
9799 }
9800
9801 // Erase the pseudo instruction
9802 MI->eraseFromParent();
9803
9804 return endMBB;
9805}
9806
9807MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +00009808X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
9809 MachineInstr *MI,
9810 MachineBasicBlock *MBB) const {
9811 // Emit code to save XMM registers to the stack. The ABI says that the
9812 // number of registers to save is given in %al, so it's theoretically
9813 // possible to do an indirect jump trick to avoid saving all of them,
9814 // however this code takes a simpler approach and just executes all
9815 // of the stores if %al is non-zero. It's less code, and it's probably
9816 // easier on the hardware branch predictor, and stores aren't all that
9817 // expensive anyway.
9818
9819 // Create the new basic blocks. One block contains all the XMM stores,
9820 // and one block is the final destination regardless of whether any
9821 // stores were performed.
9822 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
9823 MachineFunction *F = MBB->getParent();
9824 MachineFunction::iterator MBBIter = MBB;
9825 ++MBBIter;
9826 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
9827 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
9828 F->insert(MBBIter, XMMSaveMBB);
9829 F->insert(MBBIter, EndMBB);
9830
Dan Gohman14152b42010-07-06 20:24:04 +00009831 // Transfer the remainder of MBB and its successor edges to EndMBB.
9832 EndMBB->splice(EndMBB->begin(), MBB,
9833 llvm::next(MachineBasicBlock::iterator(MI)),
9834 MBB->end());
9835 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
9836
Dan Gohmand6708ea2009-08-15 01:38:56 +00009837 // The original block will now fall through to the XMM save block.
9838 MBB->addSuccessor(XMMSaveMBB);
9839 // The XMMSaveMBB will fall through to the end block.
9840 XMMSaveMBB->addSuccessor(EndMBB);
9841
9842 // Now add the instructions.
9843 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9844 DebugLoc DL = MI->getDebugLoc();
9845
9846 unsigned CountReg = MI->getOperand(0).getReg();
9847 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
9848 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
9849
9850 if (!Subtarget->isTargetWin64()) {
9851 // If %al is 0, branch around the XMM save block.
9852 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009853 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +00009854 MBB->addSuccessor(EndMBB);
9855 }
9856
9857 // In the XMM save block, save all the XMM argument registers.
9858 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
9859 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +00009860 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +00009861 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +00009862 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +00009863 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +00009864 /*Size=*/16, /*Align=*/16);
Dan Gohmand6708ea2009-08-15 01:38:56 +00009865 BuildMI(XMMSaveMBB, DL, TII->get(X86::MOVAPSmr))
9866 .addFrameIndex(RegSaveFrameIndex)
9867 .addImm(/*Scale=*/1)
9868 .addReg(/*IndexReg=*/0)
9869 .addImm(/*Disp=*/Offset)
9870 .addReg(/*Segment=*/0)
9871 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +00009872 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +00009873 }
9874
Dan Gohman14152b42010-07-06 20:24:04 +00009875 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +00009876
9877 return EndMBB;
9878}
Mon P Wang63307c32008-05-05 19:05:59 +00009879
Evan Cheng60c07e12006-07-05 22:17:51 +00009880MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +00009881X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00009882 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +00009883 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9884 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +00009885
Chris Lattner52600972009-09-02 05:57:00 +00009886 // To "insert" a SELECT_CC instruction, we actually have to insert the
9887 // diamond control-flow pattern. The incoming instruction knows the
9888 // destination vreg to set, the condition code register to branch on, the
9889 // true/false values to select between, and a branch opcode to use.
9890 const BasicBlock *LLVM_BB = BB->getBasicBlock();
9891 MachineFunction::iterator It = BB;
9892 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +00009893
Chris Lattner52600972009-09-02 05:57:00 +00009894 // thisMBB:
9895 // ...
9896 // TrueVal = ...
9897 // cmpTY ccX, r1, r2
9898 // bCC copy1MBB
9899 // fallthrough --> copy0MBB
9900 MachineBasicBlock *thisMBB = BB;
9901 MachineFunction *F = BB->getParent();
9902 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
9903 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +00009904 F->insert(It, copy0MBB);
9905 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +00009906
Bill Wendling730c07e2010-06-25 20:48:10 +00009907 // If the EFLAGS register isn't dead in the terminator, then claim that it's
9908 // live into the sink and copy blocks.
9909 const MachineFunction *MF = BB->getParent();
9910 const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo();
9911 BitVector ReservedRegs = TRI->getReservedRegs(*MF);
Bill Wendling730c07e2010-06-25 20:48:10 +00009912
Dan Gohman14152b42010-07-06 20:24:04 +00009913 for (unsigned I = 0, E = MI->getNumOperands(); I != E; ++I) {
9914 const MachineOperand &MO = MI->getOperand(I);
9915 if (!MO.isReg() || !MO.isUse() || MO.isKill()) continue;
Bill Wendling730c07e2010-06-25 20:48:10 +00009916 unsigned Reg = MO.getReg();
9917 if (Reg != X86::EFLAGS) continue;
9918 copy0MBB->addLiveIn(Reg);
9919 sinkMBB->addLiveIn(Reg);
9920 }
9921
Dan Gohman14152b42010-07-06 20:24:04 +00009922 // Transfer the remainder of BB and its successor edges to sinkMBB.
9923 sinkMBB->splice(sinkMBB->begin(), BB,
9924 llvm::next(MachineBasicBlock::iterator(MI)),
9925 BB->end());
9926 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
9927
9928 // Add the true and fallthrough blocks as its successors.
9929 BB->addSuccessor(copy0MBB);
9930 BB->addSuccessor(sinkMBB);
9931
9932 // Create the conditional branch instruction.
9933 unsigned Opc =
9934 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
9935 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
9936
Chris Lattner52600972009-09-02 05:57:00 +00009937 // copy0MBB:
9938 // %FalseValue = ...
9939 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +00009940 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +00009941
Chris Lattner52600972009-09-02 05:57:00 +00009942 // sinkMBB:
9943 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
9944 // ...
Dan Gohman14152b42010-07-06 20:24:04 +00009945 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
9946 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +00009947 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
9948 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
9949
Dan Gohman14152b42010-07-06 20:24:04 +00009950 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +00009951 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +00009952}
9953
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00009954MachineBasicBlock *
Michael J. Spencere9c253e2010-10-21 01:41:01 +00009955X86TargetLowering::EmitLoweredWinAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00009956 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00009957 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9958 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00009959
9960 // The lowering is pretty easy: we're just emitting the call to _alloca. The
9961 // non-trivial part is impdef of ESP.
9962 // FIXME: The code should be tweaked as soon as we'll try to do codegen for
9963 // mingw-w64.
9964
Michael J. Spencere9c253e2010-10-21 01:41:01 +00009965 const char *StackProbeSymbol =
9966 Subtarget->isTargetWindows() ? "_chkstk" : "_alloca";
9967
Dan Gohman14152b42010-07-06 20:24:04 +00009968 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
Michael J. Spencere9c253e2010-10-21 01:41:01 +00009969 .addExternalSymbol(StackProbeSymbol)
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00009970 .addReg(X86::EAX, RegState::Implicit)
9971 .addReg(X86::ESP, RegState::Implicit)
9972 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
Anton Korobeynikov9f7f83b2010-08-25 07:50:11 +00009973 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
9974 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00009975
Dan Gohman14152b42010-07-06 20:24:04 +00009976 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00009977 return BB;
9978}
Chris Lattner52600972009-09-02 05:57:00 +00009979
9980MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +00009981X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
9982 MachineBasicBlock *BB) const {
9983 // This is pretty easy. We're taking the value that we received from
9984 // our load from the relocation, sticking it in either RDI (x86-64)
9985 // or EAX and doing an indirect call. The return value will then
9986 // be in the normal return register.
Michael J. Spencerec38de22010-10-10 22:04:20 +00009987 const X86InstrInfo *TII
Eric Christopher54415362010-06-08 22:04:25 +00009988 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +00009989 DebugLoc DL = MI->getDebugLoc();
9990 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +00009991
9992 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +00009993 assert(MI->getOperand(3).isGlobal() && "This should be a global");
Michael J. Spencerec38de22010-10-10 22:04:20 +00009994
Eric Christopher30ef0e52010-06-03 04:07:48 +00009995 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +00009996 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
9997 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +00009998 .addReg(X86::RIP)
9999 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000010000 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000010001 MI->getOperand(3).getTargetFlags())
10002 .addReg(0);
Eric Christopher722d3152010-09-27 06:01:51 +000010003 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +000010004 addDirectMem(MIB, X86::RDI);
Eric Christopher61025492010-06-15 23:08:42 +000010005 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +000010006 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
10007 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +000010008 .addReg(0)
10009 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000010010 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher61025492010-06-15 23:08:42 +000010011 MI->getOperand(3).getTargetFlags())
10012 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000010013 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000010014 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010015 } else {
Dan Gohman14152b42010-07-06 20:24:04 +000010016 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
10017 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +000010018 .addReg(TII->getGlobalBaseReg(F))
10019 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000010020 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000010021 MI->getOperand(3).getTargetFlags())
10022 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000010023 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000010024 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010025 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000010026
Dan Gohman14152b42010-07-06 20:24:04 +000010027 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +000010028 return BB;
10029}
10030
10031MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +000010032X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010033 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +000010034 switch (MI->getOpcode()) {
10035 default: assert(false && "Unexpected instr type to insert");
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010036 case X86::WIN_ALLOCA:
10037 return EmitLoweredWinAlloca(MI, BB);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010038 case X86::TLSCall_32:
10039 case X86::TLSCall_64:
10040 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +000010041 case X86::CMOV_GR8:
Evan Cheng60c07e12006-07-05 22:17:51 +000010042 case X86::CMOV_FR32:
10043 case X86::CMOV_FR64:
10044 case X86::CMOV_V4F32:
10045 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +000010046 case X86::CMOV_V2I64:
Chris Lattner314a1132010-03-14 18:31:44 +000010047 case X86::CMOV_GR16:
10048 case X86::CMOV_GR32:
10049 case X86::CMOV_RFP32:
10050 case X86::CMOV_RFP64:
10051 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010052 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000010053
Dale Johannesen849f2142007-07-03 00:53:03 +000010054 case X86::FP32_TO_INT16_IN_MEM:
10055 case X86::FP32_TO_INT32_IN_MEM:
10056 case X86::FP32_TO_INT64_IN_MEM:
10057 case X86::FP64_TO_INT16_IN_MEM:
10058 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +000010059 case X86::FP64_TO_INT64_IN_MEM:
10060 case X86::FP80_TO_INT16_IN_MEM:
10061 case X86::FP80_TO_INT32_IN_MEM:
10062 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +000010063 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10064 DebugLoc DL = MI->getDebugLoc();
10065
Evan Cheng60c07e12006-07-05 22:17:51 +000010066 // Change the floating point control register to use "round towards zero"
10067 // mode when truncating to an integer value.
10068 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +000010069 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +000010070 addFrameReference(BuildMI(*BB, MI, DL,
10071 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010072
10073 // Load the old value of the high byte of the control word...
10074 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +000010075 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohman14152b42010-07-06 20:24:04 +000010076 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +000010077 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010078
10079 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +000010080 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000010081 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +000010082
10083 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +000010084 addFrameReference(BuildMI(*BB, MI, DL,
10085 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010086
10087 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +000010088 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000010089 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +000010090
10091 // Get the X86 opcode to use.
10092 unsigned Opc;
10093 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000010094 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +000010095 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
10096 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
10097 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
10098 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
10099 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
10100 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +000010101 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
10102 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
10103 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +000010104 }
10105
10106 X86AddressMode AM;
10107 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +000010108 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000010109 AM.BaseType = X86AddressMode::RegBase;
10110 AM.Base.Reg = Op.getReg();
10111 } else {
10112 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +000010113 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +000010114 }
10115 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +000010116 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000010117 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010118 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +000010119 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000010120 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010121 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +000010122 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000010123 AM.GV = Op.getGlobal();
10124 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +000010125 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010126 }
Dan Gohman14152b42010-07-06 20:24:04 +000010127 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010128 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +000010129
10130 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +000010131 addFrameReference(BuildMI(*BB, MI, DL,
10132 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010133
Dan Gohman14152b42010-07-06 20:24:04 +000010134 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +000010135 return BB;
10136 }
Eric Christopherb120ab42009-08-18 22:50:32 +000010137 // String/text processing lowering.
10138 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010139 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000010140 return EmitPCMP(MI, BB, 3, false /* in-mem */);
10141 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010142 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000010143 return EmitPCMP(MI, BB, 3, true /* in-mem */);
10144 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010145 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000010146 return EmitPCMP(MI, BB, 5, false /* in mem */);
10147 case X86::PCMPESTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010148 case X86::VPCMPESTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000010149 return EmitPCMP(MI, BB, 5, true /* in mem */);
10150
Eric Christopher228232b2010-11-30 07:20:12 +000010151 // Thread synchronization.
10152 case X86::MONITOR:
10153 return EmitMonitor(MI, BB);
10154 case X86::MWAIT:
10155 return EmitMwait(MI, BB);
10156
Eric Christopherb120ab42009-08-18 22:50:32 +000010157 // Atomic Lowering.
Mon P Wang63307c32008-05-05 19:05:59 +000010158 case X86::ATOMAND32:
10159 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010160 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010161 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010162 X86::NOT32r, X86::EAX,
10163 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000010164 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +000010165 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
10166 X86::OR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010167 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010168 X86::NOT32r, X86::EAX,
10169 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000010170 case X86::ATOMXOR32:
10171 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010172 X86::XOR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010173 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010174 X86::NOT32r, X86::EAX,
10175 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010176 case X86::ATOMNAND32:
10177 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010178 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010179 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010180 X86::NOT32r, X86::EAX,
10181 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +000010182 case X86::ATOMMIN32:
10183 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
10184 case X86::ATOMMAX32:
10185 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
10186 case X86::ATOMUMIN32:
10187 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
10188 case X86::ATOMUMAX32:
10189 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +000010190
10191 case X86::ATOMAND16:
10192 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
10193 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010194 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010195 X86::NOT16r, X86::AX,
10196 X86::GR16RegisterClass);
10197 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +000010198 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010199 X86::OR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010200 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010201 X86::NOT16r, X86::AX,
10202 X86::GR16RegisterClass);
10203 case X86::ATOMXOR16:
10204 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
10205 X86::XOR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010206 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010207 X86::NOT16r, X86::AX,
10208 X86::GR16RegisterClass);
10209 case X86::ATOMNAND16:
10210 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
10211 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010212 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010213 X86::NOT16r, X86::AX,
10214 X86::GR16RegisterClass, true);
10215 case X86::ATOMMIN16:
10216 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
10217 case X86::ATOMMAX16:
10218 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
10219 case X86::ATOMUMIN16:
10220 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
10221 case X86::ATOMUMAX16:
10222 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
10223
10224 case X86::ATOMAND8:
10225 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
10226 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010227 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010228 X86::NOT8r, X86::AL,
10229 X86::GR8RegisterClass);
10230 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +000010231 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010232 X86::OR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010233 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010234 X86::NOT8r, X86::AL,
10235 X86::GR8RegisterClass);
10236 case X86::ATOMXOR8:
10237 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
10238 X86::XOR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010239 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010240 X86::NOT8r, X86::AL,
10241 X86::GR8RegisterClass);
10242 case X86::ATOMNAND8:
10243 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
10244 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010245 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010246 X86::NOT8r, X86::AL,
10247 X86::GR8RegisterClass, true);
10248 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010249 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +000010250 case X86::ATOMAND64:
10251 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010252 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010253 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010254 X86::NOT64r, X86::RAX,
10255 X86::GR64RegisterClass);
10256 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +000010257 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
10258 X86::OR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010259 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010260 X86::NOT64r, X86::RAX,
10261 X86::GR64RegisterClass);
10262 case X86::ATOMXOR64:
10263 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010264 X86::XOR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010265 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010266 X86::NOT64r, X86::RAX,
10267 X86::GR64RegisterClass);
10268 case X86::ATOMNAND64:
10269 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
10270 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010271 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010272 X86::NOT64r, X86::RAX,
10273 X86::GR64RegisterClass, true);
10274 case X86::ATOMMIN64:
10275 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
10276 case X86::ATOMMAX64:
10277 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
10278 case X86::ATOMUMIN64:
10279 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
10280 case X86::ATOMUMAX64:
10281 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010282
10283 // This group does 64-bit operations on a 32-bit host.
10284 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010285 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010286 X86::AND32rr, X86::AND32rr,
10287 X86::AND32ri, X86::AND32ri,
10288 false);
10289 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010290 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010291 X86::OR32rr, X86::OR32rr,
10292 X86::OR32ri, X86::OR32ri,
10293 false);
10294 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010295 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010296 X86::XOR32rr, X86::XOR32rr,
10297 X86::XOR32ri, X86::XOR32ri,
10298 false);
10299 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010300 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010301 X86::AND32rr, X86::AND32rr,
10302 X86::AND32ri, X86::AND32ri,
10303 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010304 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010305 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010306 X86::ADD32rr, X86::ADC32rr,
10307 X86::ADD32ri, X86::ADC32ri,
10308 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010309 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010310 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010311 X86::SUB32rr, X86::SBB32rr,
10312 X86::SUB32ri, X86::SBB32ri,
10313 false);
Dale Johannesen880ae362008-10-03 22:25:52 +000010314 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010315 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +000010316 X86::MOV32rr, X86::MOV32rr,
10317 X86::MOV32ri, X86::MOV32ri,
10318 false);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010319 case X86::VASTART_SAVE_XMM_REGS:
10320 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Dan Gohman320afb82010-10-12 18:00:49 +000010321
10322 case X86::VAARG_64:
10323 return EmitVAARG64WithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000010324 }
10325}
10326
10327//===----------------------------------------------------------------------===//
10328// X86 Optimization Hooks
10329//===----------------------------------------------------------------------===//
10330
Dan Gohman475871a2008-07-27 21:46:04 +000010331void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +000010332 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000010333 APInt &KnownZero,
10334 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +000010335 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +000010336 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010337 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +000010338 assert((Opc >= ISD::BUILTIN_OP_END ||
10339 Opc == ISD::INTRINSIC_WO_CHAIN ||
10340 Opc == ISD::INTRINSIC_W_CHAIN ||
10341 Opc == ISD::INTRINSIC_VOID) &&
10342 "Should use MaskedValueIsZero if you don't know whether Op"
10343 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010344
Dan Gohmanf4f92f52008-02-13 23:07:24 +000010345 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010346 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +000010347 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +000010348 case X86ISD::ADD:
10349 case X86ISD::SUB:
10350 case X86ISD::SMUL:
10351 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +000010352 case X86ISD::INC:
10353 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +000010354 case X86ISD::OR:
10355 case X86ISD::XOR:
10356 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000010357 // These nodes' second result is a boolean.
10358 if (Op.getResNo() == 0)
10359 break;
10360 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010361 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000010362 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
10363 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +000010364 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010365 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010366}
Chris Lattner259e97c2006-01-31 19:43:35 +000010367
Owen Andersonbc146b02010-09-21 20:42:50 +000010368unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
10369 unsigned Depth) const {
10370 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
10371 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
10372 return Op.getValueType().getScalarType().getSizeInBits();
Michael J. Spencerec38de22010-10-10 22:04:20 +000010373
Owen Andersonbc146b02010-09-21 20:42:50 +000010374 // Fallback case.
10375 return 1;
10376}
10377
Evan Cheng206ee9d2006-07-07 08:33:52 +000010378/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +000010379/// node is a GlobalAddress + offset.
10380bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +000010381 const GlobalValue* &GA,
10382 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +000010383 if (N->getOpcode() == X86ISD::Wrapper) {
10384 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +000010385 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +000010386 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +000010387 return true;
10388 }
Evan Cheng206ee9d2006-07-07 08:33:52 +000010389 }
Evan Chengad4196b2008-05-12 19:56:52 +000010390 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +000010391}
10392
Evan Cheng206ee9d2006-07-07 08:33:52 +000010393/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
10394/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
10395/// if the load addresses are consecutive, non-overlapping, and in the right
Nate Begemanfdea31a2010-03-24 20:49:50 +000010396/// order.
Dan Gohman475871a2008-07-27 21:46:04 +000010397static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Nate Begeman9008ca62009-04-27 18:41:29 +000010398 const TargetLowering &TLI) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000010399 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000010400 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +000010401
Eli Friedman7a5e5552009-06-07 06:52:44 +000010402 if (VT.getSizeInBits() != 128)
10403 return SDValue();
10404
Nate Begemanfdea31a2010-03-24 20:49:50 +000010405 SmallVector<SDValue, 16> Elts;
10406 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000010407 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +000010408
Nate Begemanfdea31a2010-03-24 20:49:50 +000010409 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000010410}
Evan Chengd880b972008-05-09 21:53:03 +000010411
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +000010412/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
10413/// generation and convert it from being a bunch of shuffles and extracts
10414/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010415static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
10416 const TargetLowering &TLI) {
10417 SDValue InputVector = N->getOperand(0);
10418
10419 // Only operate on vectors of 4 elements, where the alternative shuffling
10420 // gets to be more expensive.
10421 if (InputVector.getValueType() != MVT::v4i32)
10422 return SDValue();
10423
10424 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
10425 // single use which is a sign-extend or zero-extend, and all elements are
10426 // used.
10427 SmallVector<SDNode *, 4> Uses;
10428 unsigned ExtractedElements = 0;
10429 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
10430 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
10431 if (UI.getUse().getResNo() != InputVector.getResNo())
10432 return SDValue();
10433
10434 SDNode *Extract = *UI;
10435 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
10436 return SDValue();
10437
10438 if (Extract->getValueType(0) != MVT::i32)
10439 return SDValue();
10440 if (!Extract->hasOneUse())
10441 return SDValue();
10442 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
10443 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
10444 return SDValue();
10445 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
10446 return SDValue();
10447
10448 // Record which element was extracted.
10449 ExtractedElements |=
10450 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
10451
10452 Uses.push_back(Extract);
10453 }
10454
10455 // If not all the elements were used, this may not be worthwhile.
10456 if (ExtractedElements != 15)
10457 return SDValue();
10458
10459 // Ok, we've now decided to do the transformation.
10460 DebugLoc dl = InputVector.getDebugLoc();
10461
10462 // Store the value to a temporary stack slot.
10463 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000010464 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
10465 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010466
10467 // Replace each use (extract) with a load of the appropriate element.
10468 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
10469 UE = Uses.end(); UI != UE; ++UI) {
10470 SDNode *Extract = *UI;
10471
10472 // Compute the element's address.
10473 SDValue Idx = Extract->getOperand(1);
10474 unsigned EltSize =
10475 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
10476 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
10477 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
10478
Eric Christopher90eb4022010-07-22 00:26:08 +000010479 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, Idx.getValueType(),
Chris Lattner51abfe42010-09-21 06:02:19 +000010480 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010481
10482 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000010483 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000010484 ScalarAddr, MachinePointerInfo(),
10485 false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010486
10487 // Replace the exact with the load.
10488 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
10489 }
10490
10491 // The replacement was made in place; don't return anything.
10492 return SDValue();
10493}
10494
Chris Lattner83e6c992006-10-04 06:57:07 +000010495/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000010496static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner47b4ce82009-03-11 05:48:52 +000010497 const X86Subtarget *Subtarget) {
10498 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +000010499 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000010500 // Get the LHS/RHS of the select.
10501 SDValue LHS = N->getOperand(1);
10502 SDValue RHS = N->getOperand(2);
Eric Christopherfd179292009-08-27 18:07:15 +000010503
Dan Gohman670e5392009-09-21 18:03:22 +000010504 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000010505 // instructions match the semantics of the common C idiom x<y?x:y but not
10506 // x<=y?x:y, because of how they handle negative zero (which can be
10507 // ignored in unsafe-math mode).
Chris Lattner83e6c992006-10-04 06:57:07 +000010508 if (Subtarget->hasSSE2() &&
Owen Anderson825b72b2009-08-11 20:47:22 +000010509 (LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64) &&
Chris Lattner47b4ce82009-03-11 05:48:52 +000010510 Cond.getOpcode() == ISD::SETCC) {
10511 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010512
Chris Lattner47b4ce82009-03-11 05:48:52 +000010513 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000010514 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000010515 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
10516 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000010517 switch (CC) {
10518 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000010519 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000010520 // Converting this to a min would handle NaNs incorrectly, and swapping
10521 // the operands would cause it to handle comparisons between positive
10522 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000010523 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000010524 if (!UnsafeFPMath &&
10525 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
10526 break;
10527 std::swap(LHS, RHS);
10528 }
Dan Gohman670e5392009-09-21 18:03:22 +000010529 Opcode = X86ISD::FMIN;
10530 break;
10531 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000010532 // Converting this to a min would handle comparisons between positive
10533 // and negative zero incorrectly.
10534 if (!UnsafeFPMath &&
10535 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
10536 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010537 Opcode = X86ISD::FMIN;
10538 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000010539 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000010540 // Converting this to a min would handle both negative zeros and NaNs
10541 // incorrectly, but we can swap the operands to fix both.
10542 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000010543 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010544 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000010545 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010546 Opcode = X86ISD::FMIN;
10547 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010548
Dan Gohman670e5392009-09-21 18:03:22 +000010549 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000010550 // Converting this to a max would handle comparisons between positive
10551 // and negative zero incorrectly.
10552 if (!UnsafeFPMath &&
10553 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(LHS))
10554 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010555 Opcode = X86ISD::FMAX;
10556 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000010557 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000010558 // Converting this to a max would handle NaNs incorrectly, and swapping
10559 // the operands would cause it to handle comparisons between positive
10560 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000010561 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000010562 if (!UnsafeFPMath &&
10563 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
10564 break;
10565 std::swap(LHS, RHS);
10566 }
Dan Gohman670e5392009-09-21 18:03:22 +000010567 Opcode = X86ISD::FMAX;
10568 break;
10569 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000010570 // Converting this to a max would handle both negative zeros and NaNs
10571 // incorrectly, but we can swap the operands to fix both.
10572 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000010573 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010574 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010575 case ISD::SETGE:
10576 Opcode = X86ISD::FMAX;
10577 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000010578 }
Dan Gohman670e5392009-09-21 18:03:22 +000010579 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000010580 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
10581 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000010582 switch (CC) {
10583 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000010584 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000010585 // Converting this to a min would handle comparisons between positive
10586 // and negative zero incorrectly, and swapping the operands would
10587 // cause it to handle NaNs incorrectly.
10588 if (!UnsafeFPMath &&
10589 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000010590 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000010591 break;
10592 std::swap(LHS, RHS);
10593 }
Dan Gohman670e5392009-09-21 18:03:22 +000010594 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000010595 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010596 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000010597 // Converting this to a min would handle NaNs incorrectly.
10598 if (!UnsafeFPMath &&
10599 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
10600 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010601 Opcode = X86ISD::FMIN;
10602 break;
10603 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000010604 // Converting this to a min would handle both negative zeros and NaNs
10605 // incorrectly, but we can swap the operands to fix both.
10606 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000010607 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010608 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010609 case ISD::SETGE:
10610 Opcode = X86ISD::FMIN;
10611 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010612
Dan Gohman670e5392009-09-21 18:03:22 +000010613 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000010614 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000010615 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000010616 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010617 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000010618 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010619 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000010620 // Converting this to a max would handle comparisons between positive
10621 // and negative zero incorrectly, and swapping the operands would
10622 // cause it to handle NaNs incorrectly.
10623 if (!UnsafeFPMath &&
10624 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000010625 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000010626 break;
10627 std::swap(LHS, RHS);
10628 }
Dan Gohman670e5392009-09-21 18:03:22 +000010629 Opcode = X86ISD::FMAX;
10630 break;
10631 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000010632 // Converting this to a max would handle both negative zeros and NaNs
10633 // incorrectly, but we can swap the operands to fix both.
10634 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000010635 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010636 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000010637 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010638 Opcode = X86ISD::FMAX;
10639 break;
10640 }
Chris Lattner83e6c992006-10-04 06:57:07 +000010641 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010642
Chris Lattner47b4ce82009-03-11 05:48:52 +000010643 if (Opcode)
10644 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000010645 }
Eric Christopherfd179292009-08-27 18:07:15 +000010646
Chris Lattnerd1980a52009-03-12 06:52:53 +000010647 // If this is a select between two integer constants, try to do some
10648 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000010649 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
10650 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000010651 // Don't do this for crazy integer types.
10652 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
10653 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000010654 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000010655 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000010656
Chris Lattnercee56e72009-03-13 05:53:31 +000010657 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000010658 // Efficiently invertible.
10659 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
10660 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
10661 isa<ConstantSDNode>(Cond.getOperand(1))))) {
10662 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000010663 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000010664 }
Eric Christopherfd179292009-08-27 18:07:15 +000010665
Chris Lattnerd1980a52009-03-12 06:52:53 +000010666 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000010667 if (FalseC->getAPIntValue() == 0 &&
10668 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000010669 if (NeedsCondInvert) // Invert the condition if needed.
10670 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
10671 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000010672
Chris Lattnerd1980a52009-03-12 06:52:53 +000010673 // Zero extend the condition if needed.
10674 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000010675
Chris Lattnercee56e72009-03-13 05:53:31 +000010676 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000010677 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000010678 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000010679 }
Eric Christopherfd179292009-08-27 18:07:15 +000010680
Chris Lattner97a29a52009-03-13 05:22:11 +000010681 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000010682 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +000010683 if (NeedsCondInvert) // Invert the condition if needed.
10684 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
10685 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000010686
Chris Lattner97a29a52009-03-13 05:22:11 +000010687 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000010688 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
10689 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000010690 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000010691 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000010692 }
Eric Christopherfd179292009-08-27 18:07:15 +000010693
Chris Lattnercee56e72009-03-13 05:53:31 +000010694 // Optimize cases that will turn into an LEA instruction. This requires
10695 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000010696 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000010697 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000010698 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000010699
Chris Lattnercee56e72009-03-13 05:53:31 +000010700 bool isFastMultiplier = false;
10701 if (Diff < 10) {
10702 switch ((unsigned char)Diff) {
10703 default: break;
10704 case 1: // result = add base, cond
10705 case 2: // result = lea base( , cond*2)
10706 case 3: // result = lea base(cond, cond*2)
10707 case 4: // result = lea base( , cond*4)
10708 case 5: // result = lea base(cond, cond*4)
10709 case 8: // result = lea base( , cond*8)
10710 case 9: // result = lea base(cond, cond*8)
10711 isFastMultiplier = true;
10712 break;
10713 }
10714 }
Eric Christopherfd179292009-08-27 18:07:15 +000010715
Chris Lattnercee56e72009-03-13 05:53:31 +000010716 if (isFastMultiplier) {
10717 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
10718 if (NeedsCondInvert) // Invert the condition if needed.
10719 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
10720 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000010721
Chris Lattnercee56e72009-03-13 05:53:31 +000010722 // Zero extend the condition if needed.
10723 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
10724 Cond);
10725 // Scale the condition by the difference.
10726 if (Diff != 1)
10727 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
10728 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000010729
Chris Lattnercee56e72009-03-13 05:53:31 +000010730 // Add the base if non-zero.
10731 if (FalseC->getAPIntValue() != 0)
10732 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
10733 SDValue(FalseC, 0));
10734 return Cond;
10735 }
Eric Christopherfd179292009-08-27 18:07:15 +000010736 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000010737 }
10738 }
Eric Christopherfd179292009-08-27 18:07:15 +000010739
Dan Gohman475871a2008-07-27 21:46:04 +000010740 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000010741}
10742
Chris Lattnerd1980a52009-03-12 06:52:53 +000010743/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
10744static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
10745 TargetLowering::DAGCombinerInfo &DCI) {
10746 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +000010747
Chris Lattnerd1980a52009-03-12 06:52:53 +000010748 // If the flag operand isn't dead, don't touch this CMOV.
10749 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
10750 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +000010751
Chris Lattnerd1980a52009-03-12 06:52:53 +000010752 // If this is a select between two integer constants, try to do some
10753 // optimizations. Note that the operands are ordered the opposite of SELECT
10754 // operands.
10755 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
10756 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
10757 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
10758 // larger than FalseC (the false value).
10759 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
Eric Christopherfd179292009-08-27 18:07:15 +000010760
Chris Lattnerd1980a52009-03-12 06:52:53 +000010761 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
10762 CC = X86::GetOppositeBranchCondition(CC);
10763 std::swap(TrueC, FalseC);
10764 }
Eric Christopherfd179292009-08-27 18:07:15 +000010765
Chris Lattnerd1980a52009-03-12 06:52:53 +000010766 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000010767 // This is efficient for any integer data type (including i8/i16) and
10768 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000010769 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
10770 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +000010771 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
10772 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000010773
Chris Lattnerd1980a52009-03-12 06:52:53 +000010774 // Zero extend the condition if needed.
10775 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000010776
Chris Lattnerd1980a52009-03-12 06:52:53 +000010777 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
10778 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000010779 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000010780 if (N->getNumValues() == 2) // Dead flag value?
10781 return DCI.CombineTo(N, Cond, SDValue());
10782 return Cond;
10783 }
Eric Christopherfd179292009-08-27 18:07:15 +000010784
Chris Lattnercee56e72009-03-13 05:53:31 +000010785 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
10786 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000010787 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
10788 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +000010789 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
10790 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000010791
Chris Lattner97a29a52009-03-13 05:22:11 +000010792 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000010793 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
10794 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000010795 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
10796 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000010797
Chris Lattner97a29a52009-03-13 05:22:11 +000010798 if (N->getNumValues() == 2) // Dead flag value?
10799 return DCI.CombineTo(N, Cond, SDValue());
10800 return Cond;
10801 }
Eric Christopherfd179292009-08-27 18:07:15 +000010802
Chris Lattnercee56e72009-03-13 05:53:31 +000010803 // Optimize cases that will turn into an LEA instruction. This requires
10804 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000010805 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000010806 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000010807 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000010808
Chris Lattnercee56e72009-03-13 05:53:31 +000010809 bool isFastMultiplier = false;
10810 if (Diff < 10) {
10811 switch ((unsigned char)Diff) {
10812 default: break;
10813 case 1: // result = add base, cond
10814 case 2: // result = lea base( , cond*2)
10815 case 3: // result = lea base(cond, cond*2)
10816 case 4: // result = lea base( , cond*4)
10817 case 5: // result = lea base(cond, cond*4)
10818 case 8: // result = lea base( , cond*8)
10819 case 9: // result = lea base(cond, cond*8)
10820 isFastMultiplier = true;
10821 break;
10822 }
10823 }
Eric Christopherfd179292009-08-27 18:07:15 +000010824
Chris Lattnercee56e72009-03-13 05:53:31 +000010825 if (isFastMultiplier) {
10826 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
10827 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +000010828 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
10829 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000010830 // Zero extend the condition if needed.
10831 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
10832 Cond);
10833 // Scale the condition by the difference.
10834 if (Diff != 1)
10835 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
10836 DAG.getConstant(Diff, Cond.getValueType()));
10837
10838 // Add the base if non-zero.
10839 if (FalseC->getAPIntValue() != 0)
10840 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
10841 SDValue(FalseC, 0));
10842 if (N->getNumValues() == 2) // Dead flag value?
10843 return DCI.CombineTo(N, Cond, SDValue());
10844 return Cond;
10845 }
Eric Christopherfd179292009-08-27 18:07:15 +000010846 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000010847 }
10848 }
10849 return SDValue();
10850}
10851
10852
Evan Cheng0b0cd912009-03-28 05:57:29 +000010853/// PerformMulCombine - Optimize a single multiply with constant into two
10854/// in order to implement it with two cheaper instructions, e.g.
10855/// LEA + SHL, LEA + LEA.
10856static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
10857 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000010858 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
10859 return SDValue();
10860
Owen Andersone50ed302009-08-10 22:56:29 +000010861 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010862 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000010863 return SDValue();
10864
10865 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
10866 if (!C)
10867 return SDValue();
10868 uint64_t MulAmt = C->getZExtValue();
10869 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
10870 return SDValue();
10871
10872 uint64_t MulAmt1 = 0;
10873 uint64_t MulAmt2 = 0;
10874 if ((MulAmt % 9) == 0) {
10875 MulAmt1 = 9;
10876 MulAmt2 = MulAmt / 9;
10877 } else if ((MulAmt % 5) == 0) {
10878 MulAmt1 = 5;
10879 MulAmt2 = MulAmt / 5;
10880 } else if ((MulAmt % 3) == 0) {
10881 MulAmt1 = 3;
10882 MulAmt2 = MulAmt / 3;
10883 }
10884 if (MulAmt2 &&
10885 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
10886 DebugLoc DL = N->getDebugLoc();
10887
10888 if (isPowerOf2_64(MulAmt2) &&
10889 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
10890 // If second multiplifer is pow2, issue it first. We want the multiply by
10891 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
10892 // is an add.
10893 std::swap(MulAmt1, MulAmt2);
10894
10895 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000010896 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000010897 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000010898 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000010899 else
Evan Cheng73f24c92009-03-30 21:36:47 +000010900 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000010901 DAG.getConstant(MulAmt1, VT));
10902
Eric Christopherfd179292009-08-27 18:07:15 +000010903 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000010904 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000010905 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000010906 else
Evan Cheng73f24c92009-03-30 21:36:47 +000010907 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000010908 DAG.getConstant(MulAmt2, VT));
10909
10910 // Do not add new nodes to DAG combiner worklist.
10911 DCI.CombineTo(N, NewMul, false);
10912 }
10913 return SDValue();
10914}
10915
Evan Chengad9c0a32009-12-15 00:53:42 +000010916static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
10917 SDValue N0 = N->getOperand(0);
10918 SDValue N1 = N->getOperand(1);
10919 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
10920 EVT VT = N0.getValueType();
10921
10922 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
10923 // since the result of setcc_c is all zero's or all ones.
10924 if (N1C && N0.getOpcode() == ISD::AND &&
10925 N0.getOperand(1).getOpcode() == ISD::Constant) {
10926 SDValue N00 = N0.getOperand(0);
10927 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
10928 ((N00.getOpcode() == ISD::ANY_EXTEND ||
10929 N00.getOpcode() == ISD::ZERO_EXTEND) &&
10930 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
10931 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
10932 APInt ShAmt = N1C->getAPIntValue();
10933 Mask = Mask.shl(ShAmt);
10934 if (Mask != 0)
10935 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
10936 N00, DAG.getConstant(Mask, VT));
10937 }
10938 }
10939
10940 return SDValue();
10941}
Evan Cheng0b0cd912009-03-28 05:57:29 +000010942
Nate Begeman740ab032009-01-26 00:52:55 +000010943/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
10944/// when possible.
10945static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
10946 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +000010947 EVT VT = N->getValueType(0);
10948 if (!VT.isVector() && VT.isInteger() &&
10949 N->getOpcode() == ISD::SHL)
10950 return PerformSHLCombine(N, DAG);
10951
Nate Begeman740ab032009-01-26 00:52:55 +000010952 // On X86 with SSE2 support, we can transform this to a vector shift if
10953 // all elements are shifted by the same amount. We can't do this in legalize
10954 // because the a constant vector is typically transformed to a constant pool
10955 // so we have no knowledge of the shift amount.
Nate Begemanc2fd67f2009-01-26 03:15:31 +000010956 if (!Subtarget->hasSSE2())
10957 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000010958
Owen Anderson825b72b2009-08-11 20:47:22 +000010959 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
Nate Begemanc2fd67f2009-01-26 03:15:31 +000010960 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000010961
Mon P Wang3becd092009-01-28 08:12:05 +000010962 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +000010963 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +000010964 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +000010965 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +000010966 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
10967 unsigned NumElts = VT.getVectorNumElements();
10968 unsigned i = 0;
10969 for (; i != NumElts; ++i) {
10970 SDValue Arg = ShAmtOp.getOperand(i);
10971 if (Arg.getOpcode() == ISD::UNDEF) continue;
10972 BaseShAmt = Arg;
10973 break;
10974 }
10975 for (; i != NumElts; ++i) {
10976 SDValue Arg = ShAmtOp.getOperand(i);
10977 if (Arg.getOpcode() == ISD::UNDEF) continue;
10978 if (Arg != BaseShAmt) {
10979 return SDValue();
10980 }
10981 }
10982 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +000010983 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +000010984 SDValue InVec = ShAmtOp.getOperand(0);
10985 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
10986 unsigned NumElts = InVec.getValueType().getVectorNumElements();
10987 unsigned i = 0;
10988 for (; i != NumElts; ++i) {
10989 SDValue Arg = InVec.getOperand(i);
10990 if (Arg.getOpcode() == ISD::UNDEF) continue;
10991 BaseShAmt = Arg;
10992 break;
10993 }
10994 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
10995 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +000010996 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +000010997 if (C->getZExtValue() == SplatIdx)
10998 BaseShAmt = InVec.getOperand(1);
10999 }
11000 }
11001 if (BaseShAmt.getNode() == 0)
11002 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
11003 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +000011004 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011005 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000011006
Mon P Wangefa42202009-09-03 19:56:25 +000011007 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +000011008 if (EltVT.bitsGT(MVT::i32))
11009 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
11010 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +000011011 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +000011012
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011013 // The shift amount is identical so we can do a vector shift.
11014 SDValue ValOp = N->getOperand(0);
11015 switch (N->getOpcode()) {
11016 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000011017 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011018 break;
11019 case ISD::SHL:
Owen Anderson825b72b2009-08-11 20:47:22 +000011020 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011021 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011022 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011023 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011024 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011025 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011026 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011027 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011028 if (VT == MVT::v8i16)
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_pslli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011031 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011032 break;
11033 case ISD::SRA:
Owen Anderson825b72b2009-08-11 20:47:22 +000011034 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011035 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011036 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011037 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011038 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011039 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011040 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011041 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011042 break;
11043 case ISD::SRL:
Owen Anderson825b72b2009-08-11 20:47:22 +000011044 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011045 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011046 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011047 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011048 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011049 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011050 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011051 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011052 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011053 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011054 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011055 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011056 break;
Nate Begeman740ab032009-01-26 00:52:55 +000011057 }
11058 return SDValue();
11059}
11060
Nate Begemanb65c1752010-12-17 22:55:37 +000011061
11062static SDValue PerformAndCombine(SDNode *N, SelectionDAG &DAG,
11063 TargetLowering::DAGCombinerInfo &DCI,
11064 const X86Subtarget *Subtarget) {
11065 if (DCI.isBeforeLegalizeOps())
11066 return SDValue();
11067
11068 // Want to form PANDN nodes, in the hopes of then easily combining them with
11069 // OR and AND nodes to form PBLEND/PSIGN.
11070 EVT VT = N->getValueType(0);
11071 if (VT != MVT::v2i64)
11072 return SDValue();
11073
11074 SDValue N0 = N->getOperand(0);
11075 SDValue N1 = N->getOperand(1);
11076 DebugLoc DL = N->getDebugLoc();
11077
11078 // Check LHS for vnot
11079 if (N0.getOpcode() == ISD::XOR &&
11080 ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode()))
11081 return DAG.getNode(X86ISD::PANDN, DL, VT, N0.getOperand(0), N1);
11082
11083 // Check RHS for vnot
11084 if (N1.getOpcode() == ISD::XOR &&
11085 ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode()))
11086 return DAG.getNode(X86ISD::PANDN, DL, VT, N1.getOperand(0), N0);
11087
11088 return SDValue();
11089}
11090
Evan Cheng760d1942010-01-04 21:22:48 +000011091static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000011092 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000011093 const X86Subtarget *Subtarget) {
Evan Cheng39cfeec2010-04-28 02:25:18 +000011094 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000011095 return SDValue();
11096
Evan Cheng760d1942010-01-04 21:22:48 +000011097 EVT VT = N->getValueType(0);
Nate Begemanb65c1752010-12-17 22:55:37 +000011098 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64 && VT != MVT::v2i64)
Evan Cheng760d1942010-01-04 21:22:48 +000011099 return SDValue();
11100
Evan Cheng760d1942010-01-04 21:22:48 +000011101 SDValue N0 = N->getOperand(0);
11102 SDValue N1 = N->getOperand(1);
Nate Begemanb65c1752010-12-17 22:55:37 +000011103
11104 // look for psign/blend
11105 if (Subtarget->hasSSSE3()) {
11106 if (VT == MVT::v2i64) {
11107 // Canonicalize pandn to RHS
11108 if (N0.getOpcode() == X86ISD::PANDN)
11109 std::swap(N0, N1);
11110 // or (and (m, x), (pandn m, y))
11111 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::PANDN) {
11112 SDValue Mask = N1.getOperand(0);
11113 SDValue X = N1.getOperand(1);
11114 SDValue Y;
11115 if (N0.getOperand(0) == Mask)
11116 Y = N0.getOperand(1);
11117 if (N0.getOperand(1) == Mask)
11118 Y = N0.getOperand(0);
11119
11120 // Check to see if the mask appeared in both the AND and PANDN and
11121 if (!Y.getNode())
11122 return SDValue();
11123
11124 // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them.
11125 if (Mask.getOpcode() != ISD::BITCAST ||
11126 X.getOpcode() != ISD::BITCAST ||
11127 Y.getOpcode() != ISD::BITCAST)
11128 return SDValue();
11129
11130 // Look through mask bitcast.
11131 Mask = Mask.getOperand(0);
11132 EVT MaskVT = Mask.getValueType();
11133
11134 // Validate that the Mask operand is a vector sra node. The sra node
11135 // will be an intrinsic.
11136 if (Mask.getOpcode() != ISD::INTRINSIC_WO_CHAIN)
11137 return SDValue();
11138
11139 // FIXME: what to do for bytes, since there is a psignb/pblendvb, but
11140 // there is no psrai.b
11141 switch (cast<ConstantSDNode>(Mask.getOperand(0))->getZExtValue()) {
11142 case Intrinsic::x86_sse2_psrai_w:
11143 case Intrinsic::x86_sse2_psrai_d:
11144 break;
11145 default: return SDValue();
11146 }
11147
11148 // Check that the SRA is all signbits.
11149 SDValue SraC = Mask.getOperand(2);
11150 unsigned SraAmt = cast<ConstantSDNode>(SraC)->getZExtValue();
11151 unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits();
11152 if ((SraAmt + 1) != EltBits)
11153 return SDValue();
11154
11155 DebugLoc DL = N->getDebugLoc();
11156
11157 // Now we know we at least have a plendvb with the mask val. See if
11158 // we can form a psignb/w/d.
11159 // psign = x.type == y.type == mask.type && y = sub(0, x);
11160 X = X.getOperand(0);
11161 Y = Y.getOperand(0);
11162 if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X &&
11163 ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) &&
11164 X.getValueType() == MaskVT && X.getValueType() == Y.getValueType()){
11165 unsigned Opc = 0;
11166 switch (EltBits) {
11167 case 8: Opc = X86ISD::PSIGNB; break;
11168 case 16: Opc = X86ISD::PSIGNW; break;
11169 case 32: Opc = X86ISD::PSIGND; break;
11170 default: break;
11171 }
11172 if (Opc) {
11173 SDValue Sign = DAG.getNode(Opc, DL, MaskVT, X, Mask.getOperand(1));
11174 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Sign);
11175 }
11176 }
11177 // PBLENDVB only available on SSE 4.1
11178 if (!Subtarget->hasSSE41())
11179 return SDValue();
11180
11181 unsigned IID = Intrinsic::x86_sse41_pblendvb;
11182 X = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, X);
11183 Y = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Y);
11184 Mask = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Mask);
11185 Mask = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, MVT::v16i8,
11186 DAG.getConstant(IID, MVT::i32), X, Y, Mask);
11187 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Mask);
11188 }
11189 }
11190 }
11191
11192 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
Evan Cheng760d1942010-01-04 21:22:48 +000011193 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
11194 std::swap(N0, N1);
11195 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
11196 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000011197 if (!N0.hasOneUse() || !N1.hasOneUse())
11198 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000011199
11200 SDValue ShAmt0 = N0.getOperand(1);
11201 if (ShAmt0.getValueType() != MVT::i8)
11202 return SDValue();
11203 SDValue ShAmt1 = N1.getOperand(1);
11204 if (ShAmt1.getValueType() != MVT::i8)
11205 return SDValue();
11206 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
11207 ShAmt0 = ShAmt0.getOperand(0);
11208 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
11209 ShAmt1 = ShAmt1.getOperand(0);
11210
11211 DebugLoc DL = N->getDebugLoc();
11212 unsigned Opc = X86ISD::SHLD;
11213 SDValue Op0 = N0.getOperand(0);
11214 SDValue Op1 = N1.getOperand(0);
11215 if (ShAmt0.getOpcode() == ISD::SUB) {
11216 Opc = X86ISD::SHRD;
11217 std::swap(Op0, Op1);
11218 std::swap(ShAmt0, ShAmt1);
11219 }
11220
Evan Cheng8b1190a2010-04-28 01:18:01 +000011221 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000011222 if (ShAmt1.getOpcode() == ISD::SUB) {
11223 SDValue Sum = ShAmt1.getOperand(0);
11224 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000011225 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
11226 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
11227 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
11228 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000011229 return DAG.getNode(Opc, DL, VT,
11230 Op0, Op1,
11231 DAG.getNode(ISD::TRUNCATE, DL,
11232 MVT::i8, ShAmt0));
11233 }
11234 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
11235 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
11236 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000011237 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000011238 return DAG.getNode(Opc, DL, VT,
11239 N0.getOperand(0), N1.getOperand(0),
11240 DAG.getNode(ISD::TRUNCATE, DL,
11241 MVT::i8, ShAmt0));
11242 }
Nate Begemanb65c1752010-12-17 22:55:37 +000011243
Evan Cheng760d1942010-01-04 21:22:48 +000011244 return SDValue();
11245}
11246
Chris Lattner149a4e52008-02-22 02:09:43 +000011247/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000011248static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000011249 const X86Subtarget *Subtarget) {
Chris Lattner149a4e52008-02-22 02:09:43 +000011250 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
11251 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000011252 // A preferable solution to the general problem is to figure out the right
11253 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000011254
11255 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng7e2ff772008-05-08 00:57:18 +000011256 StoreSDNode *St = cast<StoreSDNode>(N);
Owen Andersone50ed302009-08-10 22:56:29 +000011257 EVT VT = St->getValue().getValueType();
Evan Cheng536e6672009-03-12 05:59:15 +000011258 if (VT.getSizeInBits() != 64)
11259 return SDValue();
11260
Devang Patel578efa92009-06-05 21:57:13 +000011261 const Function *F = DAG.getMachineFunction().getFunction();
11262 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Eric Christopherfd179292009-08-27 18:07:15 +000011263 bool F64IsLegal = !UseSoftFloat && !NoImplicitFloatOps
Devang Patel578efa92009-06-05 21:57:13 +000011264 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +000011265 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000011266 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000011267 isa<LoadSDNode>(St->getValue()) &&
11268 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
11269 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000011270 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000011271 LoadSDNode *Ld = 0;
11272 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000011273 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000011274 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000011275 // Must be a store of a load. We currently handle two cases: the load
11276 // is a direct child, and it's under an intervening TokenFactor. It is
11277 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000011278 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000011279 Ld = cast<LoadSDNode>(St->getChain());
11280 else if (St->getValue().hasOneUse() &&
11281 ChainVal->getOpcode() == ISD::TokenFactor) {
11282 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000011283 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000011284 TokenFactorIndex = i;
11285 Ld = cast<LoadSDNode>(St->getValue());
11286 } else
11287 Ops.push_back(ChainVal->getOperand(i));
11288 }
11289 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000011290
Evan Cheng536e6672009-03-12 05:59:15 +000011291 if (!Ld || !ISD::isNormalLoad(Ld))
11292 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000011293
Evan Cheng536e6672009-03-12 05:59:15 +000011294 // If this is not the MMX case, i.e. we are just turning i64 load/store
11295 // into f64 load/store, avoid the transformation if there are multiple
11296 // uses of the loaded value.
11297 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
11298 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000011299
Evan Cheng536e6672009-03-12 05:59:15 +000011300 DebugLoc LdDL = Ld->getDebugLoc();
11301 DebugLoc StDL = N->getDebugLoc();
11302 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
11303 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
11304 // pair instead.
11305 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000011306 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000011307 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
11308 Ld->getPointerInfo(), Ld->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000011309 Ld->isNonTemporal(), Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000011310 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000011311 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000011312 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000011313 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000011314 Ops.size());
11315 }
Evan Cheng536e6672009-03-12 05:59:15 +000011316 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000011317 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000011318 St->isVolatile(), St->isNonTemporal(),
11319 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000011320 }
Evan Cheng536e6672009-03-12 05:59:15 +000011321
11322 // Otherwise, lower to two pairs of 32-bit loads / stores.
11323 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000011324 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
11325 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000011326
Owen Anderson825b72b2009-08-11 20:47:22 +000011327 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000011328 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000011329 Ld->isVolatile(), Ld->isNonTemporal(),
11330 Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000011331 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000011332 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000011333 Ld->isVolatile(), Ld->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000011334 MinAlign(Ld->getAlignment(), 4));
11335
11336 SDValue NewChain = LoLd.getValue(1);
11337 if (TokenFactorIndex != -1) {
11338 Ops.push_back(LoLd);
11339 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000011340 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000011341 Ops.size());
11342 }
11343
11344 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000011345 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
11346 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000011347
11348 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000011349 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000011350 St->isVolatile(), St->isNonTemporal(),
11351 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000011352 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000011353 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000011354 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000011355 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000011356 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000011357 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000011358 }
Dan Gohman475871a2008-07-27 21:46:04 +000011359 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000011360}
11361
Chris Lattner6cf73262008-01-25 06:14:17 +000011362/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
11363/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000011364static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000011365 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
11366 // F[X]OR(0.0, x) -> x
11367 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000011368 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
11369 if (C->getValueAPF().isPosZero())
11370 return N->getOperand(1);
11371 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
11372 if (C->getValueAPF().isPosZero())
11373 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000011374 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000011375}
11376
11377/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000011378static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000011379 // FAND(0.0, x) -> 0.0
11380 // FAND(x, 0.0) -> 0.0
11381 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
11382 if (C->getValueAPF().isPosZero())
11383 return N->getOperand(0);
11384 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
11385 if (C->getValueAPF().isPosZero())
11386 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000011387 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000011388}
11389
Dan Gohmane5af2d32009-01-29 01:59:02 +000011390static SDValue PerformBTCombine(SDNode *N,
11391 SelectionDAG &DAG,
11392 TargetLowering::DAGCombinerInfo &DCI) {
11393 // BT ignores high bits in the bit index operand.
11394 SDValue Op1 = N->getOperand(1);
11395 if (Op1.hasOneUse()) {
11396 unsigned BitWidth = Op1.getValueSizeInBits();
11397 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
11398 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000011399 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
11400 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000011401 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000011402 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
11403 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
11404 DCI.CommitTargetLoweringOpt(TLO);
11405 }
11406 return SDValue();
11407}
Chris Lattner83e6c992006-10-04 06:57:07 +000011408
Eli Friedman7a5e5552009-06-07 06:52:44 +000011409static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
11410 SDValue Op = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011411 if (Op.getOpcode() == ISD::BITCAST)
Eli Friedman7a5e5552009-06-07 06:52:44 +000011412 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000011413 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000011414 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000011415 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000011416 OpVT.getVectorElementType().getSizeInBits()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011417 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Eli Friedman7a5e5552009-06-07 06:52:44 +000011418 }
11419 return SDValue();
11420}
11421
Evan Cheng2e489c42009-12-16 00:53:11 +000011422static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG) {
11423 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
11424 // (and (i32 x86isd::setcc_carry), 1)
11425 // This eliminates the zext. This transformation is necessary because
11426 // ISD::SETCC is always legalized to i8.
11427 DebugLoc dl = N->getDebugLoc();
11428 SDValue N0 = N->getOperand(0);
11429 EVT VT = N->getValueType(0);
11430 if (N0.getOpcode() == ISD::AND &&
11431 N0.hasOneUse() &&
11432 N0.getOperand(0).hasOneUse()) {
11433 SDValue N00 = N0.getOperand(0);
11434 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
11435 return SDValue();
11436 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
11437 if (!C || C->getZExtValue() != 1)
11438 return SDValue();
11439 return DAG.getNode(ISD::AND, dl, VT,
11440 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
11441 N00.getOperand(0), N00.getOperand(1)),
11442 DAG.getConstant(1, VT));
11443 }
11444
11445 return SDValue();
11446}
11447
Dan Gohman475871a2008-07-27 21:46:04 +000011448SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000011449 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000011450 SelectionDAG &DAG = DCI.DAG;
11451 switch (N->getOpcode()) {
11452 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000011453 case ISD::EXTRACT_VECTOR_ELT:
11454 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, *this);
Chris Lattneraf723b92008-01-25 05:46:26 +000011455 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +000011456 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000011457 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000011458 case ISD::SHL:
11459 case ISD::SRA:
11460 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Nate Begemanb65c1752010-12-17 22:55:37 +000011461 case ISD::AND: return PerformAndCombine(N, DAG, DCI, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000011462 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000011463 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +000011464 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000011465 case X86ISD::FOR: return PerformFORCombine(N, DAG);
11466 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000011467 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000011468 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Evan Cheng2e489c42009-12-16 00:53:11 +000011469 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000011470 case X86ISD::SHUFPS: // Handle all target specific shuffles
11471 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +000011472 case X86ISD::PALIGN:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000011473 case X86ISD::PUNPCKHBW:
11474 case X86ISD::PUNPCKHWD:
11475 case X86ISD::PUNPCKHDQ:
11476 case X86ISD::PUNPCKHQDQ:
11477 case X86ISD::UNPCKHPS:
11478 case X86ISD::UNPCKHPD:
11479 case X86ISD::PUNPCKLBW:
11480 case X86ISD::PUNPCKLWD:
11481 case X86ISD::PUNPCKLDQ:
11482 case X86ISD::PUNPCKLQDQ:
11483 case X86ISD::UNPCKLPS:
11484 case X86ISD::UNPCKLPD:
11485 case X86ISD::MOVHLPS:
11486 case X86ISD::MOVLHPS:
11487 case X86ISD::PSHUFD:
11488 case X86ISD::PSHUFHW:
11489 case X86ISD::PSHUFLW:
11490 case X86ISD::MOVSS:
11491 case X86ISD::MOVSD:
11492 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, *this);
Evan Cheng206ee9d2006-07-07 08:33:52 +000011493 }
11494
Dan Gohman475871a2008-07-27 21:46:04 +000011495 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000011496}
11497
Evan Chenge5b51ac2010-04-17 06:13:15 +000011498/// isTypeDesirableForOp - Return true if the target has native support for
11499/// the specified value type and it is 'desirable' to use the type for the
11500/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
11501/// instruction encodings are longer and some i16 instructions are slow.
11502bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
11503 if (!isTypeLegal(VT))
11504 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000011505 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000011506 return true;
11507
11508 switch (Opc) {
11509 default:
11510 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000011511 case ISD::LOAD:
11512 case ISD::SIGN_EXTEND:
11513 case ISD::ZERO_EXTEND:
11514 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000011515 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000011516 case ISD::SRL:
11517 case ISD::SUB:
11518 case ISD::ADD:
11519 case ISD::MUL:
11520 case ISD::AND:
11521 case ISD::OR:
11522 case ISD::XOR:
11523 return false;
11524 }
11525}
11526
11527/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000011528/// beneficial for dag combiner to promote the specified node. If true, it
11529/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000011530bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000011531 EVT VT = Op.getValueType();
11532 if (VT != MVT::i16)
11533 return false;
11534
Evan Cheng4c26e932010-04-19 19:29:22 +000011535 bool Promote = false;
11536 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000011537 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000011538 default: break;
11539 case ISD::LOAD: {
11540 LoadSDNode *LD = cast<LoadSDNode>(Op);
11541 // If the non-extending load has a single use and it's not live out, then it
11542 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000011543 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
11544 Op.hasOneUse()*/) {
11545 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
11546 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
11547 // The only case where we'd want to promote LOAD (rather then it being
11548 // promoted as an operand is when it's only use is liveout.
11549 if (UI->getOpcode() != ISD::CopyToReg)
11550 return false;
11551 }
11552 }
Evan Cheng4c26e932010-04-19 19:29:22 +000011553 Promote = true;
11554 break;
11555 }
11556 case ISD::SIGN_EXTEND:
11557 case ISD::ZERO_EXTEND:
11558 case ISD::ANY_EXTEND:
11559 Promote = true;
11560 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000011561 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000011562 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000011563 SDValue N0 = Op.getOperand(0);
11564 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000011565 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000011566 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000011567 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000011568 break;
11569 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000011570 case ISD::ADD:
11571 case ISD::MUL:
11572 case ISD::AND:
11573 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000011574 case ISD::XOR:
11575 Commute = true;
11576 // fallthrough
11577 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000011578 SDValue N0 = Op.getOperand(0);
11579 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000011580 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000011581 return false;
11582 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000011583 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000011584 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000011585 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000011586 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000011587 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000011588 }
11589 }
11590
11591 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000011592 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000011593}
11594
Evan Cheng60c07e12006-07-05 22:17:51 +000011595//===----------------------------------------------------------------------===//
11596// X86 Inline Assembly Support
11597//===----------------------------------------------------------------------===//
11598
Chris Lattnerb8105652009-07-20 17:51:36 +000011599static bool LowerToBSwap(CallInst *CI) {
11600 // FIXME: this should verify that we are targetting a 486 or better. If not,
11601 // we will turn this bswap into something that will be lowered to logical ops
11602 // instead of emitting the bswap asm. For now, we don't support 486 or lower
11603 // so don't worry about this.
Eric Christopherfd179292009-08-27 18:07:15 +000011604
Chris Lattnerb8105652009-07-20 17:51:36 +000011605 // Verify this is a simple bswap.
Gabor Greife1c2b9c2010-06-30 13:03:37 +000011606 if (CI->getNumArgOperands() != 1 ||
Gabor Greif1cfe44a2010-06-26 11:51:52 +000011607 CI->getType() != CI->getArgOperand(0)->getType() ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000011608 !CI->getType()->isIntegerTy())
Chris Lattnerb8105652009-07-20 17:51:36 +000011609 return false;
Eric Christopherfd179292009-08-27 18:07:15 +000011610
Chris Lattnerb8105652009-07-20 17:51:36 +000011611 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
11612 if (!Ty || Ty->getBitWidth() % 16 != 0)
11613 return false;
Eric Christopherfd179292009-08-27 18:07:15 +000011614
Chris Lattnerb8105652009-07-20 17:51:36 +000011615 // Okay, we can do this xform, do so now.
11616 const Type *Tys[] = { Ty };
11617 Module *M = CI->getParent()->getParent()->getParent();
11618 Constant *Int = Intrinsic::getDeclaration(M, Intrinsic::bswap, Tys, 1);
Eric Christopherfd179292009-08-27 18:07:15 +000011619
Gabor Greif1cfe44a2010-06-26 11:51:52 +000011620 Value *Op = CI->getArgOperand(0);
Chris Lattnerb8105652009-07-20 17:51:36 +000011621 Op = CallInst::Create(Int, Op, CI->getName(), CI);
Eric Christopherfd179292009-08-27 18:07:15 +000011622
Chris Lattnerb8105652009-07-20 17:51:36 +000011623 CI->replaceAllUsesWith(Op);
11624 CI->eraseFromParent();
11625 return true;
11626}
11627
11628bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
11629 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
John Thompson44ab89e2010-10-29 17:29:13 +000011630 InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
Chris Lattnerb8105652009-07-20 17:51:36 +000011631
11632 std::string AsmStr = IA->getAsmString();
11633
11634 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000011635 SmallVector<StringRef, 4> AsmPieces;
Peter Collingbourne98361182010-11-13 19:54:23 +000011636 SplitString(AsmStr, AsmPieces, ";\n");
Chris Lattnerb8105652009-07-20 17:51:36 +000011637
11638 switch (AsmPieces.size()) {
11639 default: return false;
11640 case 1:
11641 AsmStr = AsmPieces[0];
11642 AsmPieces.clear();
11643 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
11644
11645 // bswap $0
11646 if (AsmPieces.size() == 2 &&
11647 (AsmPieces[0] == "bswap" ||
11648 AsmPieces[0] == "bswapq" ||
11649 AsmPieces[0] == "bswapl") &&
11650 (AsmPieces[1] == "$0" ||
11651 AsmPieces[1] == "${0:q}")) {
11652 // No need to check constraints, nothing other than the equivalent of
11653 // "=r,0" would be valid here.
11654 return LowerToBSwap(CI);
11655 }
11656 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000011657 if (CI->getType()->isIntegerTy(16) &&
Chris Lattnerb8105652009-07-20 17:51:36 +000011658 AsmPieces.size() == 3 &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000011659 (AsmPieces[0] == "rorw" || AsmPieces[0] == "rolw") &&
Chris Lattnerb8105652009-07-20 17:51:36 +000011660 AsmPieces[1] == "$$8," &&
11661 AsmPieces[2] == "${0:w}" &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000011662 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
11663 AsmPieces.clear();
Benjamin Kramer018cbd52010-03-12 13:54:59 +000011664 const std::string &Constraints = IA->getConstraintString();
11665 SplitString(StringRef(Constraints).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000011666 std::sort(AsmPieces.begin(), AsmPieces.end());
11667 if (AsmPieces.size() == 4 &&
11668 AsmPieces[0] == "~{cc}" &&
11669 AsmPieces[1] == "~{dirflag}" &&
11670 AsmPieces[2] == "~{flags}" &&
11671 AsmPieces[3] == "~{fpsr}") {
11672 return LowerToBSwap(CI);
11673 }
Chris Lattnerb8105652009-07-20 17:51:36 +000011674 }
11675 break;
11676 case 3:
Peter Collingbourne948cf022010-11-13 19:54:30 +000011677 if (CI->getType()->isIntegerTy(32) &&
11678 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
11679 SmallVector<StringRef, 4> Words;
11680 SplitString(AsmPieces[0], Words, " \t,");
11681 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
11682 Words[2] == "${0:w}") {
11683 Words.clear();
11684 SplitString(AsmPieces[1], Words, " \t,");
11685 if (Words.size() == 3 && Words[0] == "rorl" && Words[1] == "$$16" &&
11686 Words[2] == "$0") {
11687 Words.clear();
11688 SplitString(AsmPieces[2], Words, " \t,");
11689 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
11690 Words[2] == "${0:w}") {
11691 AsmPieces.clear();
11692 const std::string &Constraints = IA->getConstraintString();
11693 SplitString(StringRef(Constraints).substr(5), AsmPieces, ",");
11694 std::sort(AsmPieces.begin(), AsmPieces.end());
11695 if (AsmPieces.size() == 4 &&
11696 AsmPieces[0] == "~{cc}" &&
11697 AsmPieces[1] == "~{dirflag}" &&
11698 AsmPieces[2] == "~{flags}" &&
11699 AsmPieces[3] == "~{fpsr}") {
11700 return LowerToBSwap(CI);
11701 }
11702 }
11703 }
11704 }
11705 }
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000011706 if (CI->getType()->isIntegerTy(64) &&
Owen Anderson1d0be152009-08-13 21:58:54 +000011707 Constraints.size() >= 2 &&
Chris Lattnerb8105652009-07-20 17:51:36 +000011708 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
11709 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
11710 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
Benjamin Kramerd4f19592010-01-11 18:03:24 +000011711 SmallVector<StringRef, 4> Words;
Chris Lattnerb8105652009-07-20 17:51:36 +000011712 SplitString(AsmPieces[0], Words, " \t");
11713 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
11714 Words.clear();
11715 SplitString(AsmPieces[1], Words, " \t");
11716 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
11717 Words.clear();
11718 SplitString(AsmPieces[2], Words, " \t,");
11719 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
11720 Words[2] == "%edx") {
11721 return LowerToBSwap(CI);
11722 }
11723 }
11724 }
11725 }
11726 break;
11727 }
11728 return false;
11729}
11730
11731
11732
Chris Lattnerf4dff842006-07-11 02:54:03 +000011733/// getConstraintType - Given a constraint letter, return the type of
11734/// constraint it is for this target.
11735X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000011736X86TargetLowering::getConstraintType(const std::string &Constraint) const {
11737 if (Constraint.size() == 1) {
11738 switch (Constraint[0]) {
Chris Lattner4234f572007-03-25 02:14:49 +000011739 case 'R':
Chris Lattner4234f572007-03-25 02:14:49 +000011740 case 'q':
11741 case 'Q':
John Thompson44ab89e2010-10-29 17:29:13 +000011742 case 'f':
11743 case 't':
11744 case 'u':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000011745 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +000011746 case 'x':
Chris Lattner4234f572007-03-25 02:14:49 +000011747 case 'Y':
11748 return C_RegisterClass;
John Thompson44ab89e2010-10-29 17:29:13 +000011749 case 'a':
11750 case 'b':
11751 case 'c':
11752 case 'd':
11753 case 'S':
11754 case 'D':
11755 case 'A':
11756 return C_Register;
11757 case 'I':
11758 case 'J':
11759 case 'K':
11760 case 'L':
11761 case 'M':
11762 case 'N':
11763 case 'G':
11764 case 'C':
Dale Johannesen78e3e522009-02-12 20:58:09 +000011765 case 'e':
11766 case 'Z':
11767 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000011768 default:
11769 break;
11770 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000011771 }
Chris Lattner4234f572007-03-25 02:14:49 +000011772 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000011773}
11774
John Thompson44ab89e2010-10-29 17:29:13 +000011775/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +000011776/// This object must already have been set up with the operand type
11777/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +000011778TargetLowering::ConstraintWeight
11779 X86TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +000011780 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +000011781 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +000011782 Value *CallOperandVal = info.CallOperandVal;
11783 // If we don't have a value, we can't do a match,
11784 // but allow it at the lowest weight.
11785 if (CallOperandVal == NULL)
John Thompson44ab89e2010-10-29 17:29:13 +000011786 return CW_Default;
11787 const Type *type = CallOperandVal->getType();
John Thompsoneac6e1d2010-09-13 18:15:37 +000011788 // Look at the constraint type.
11789 switch (*constraint) {
11790 default:
John Thompson44ab89e2010-10-29 17:29:13 +000011791 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
11792 case 'R':
11793 case 'q':
11794 case 'Q':
11795 case 'a':
11796 case 'b':
11797 case 'c':
11798 case 'd':
11799 case 'S':
11800 case 'D':
11801 case 'A':
11802 if (CallOperandVal->getType()->isIntegerTy())
11803 weight = CW_SpecificReg;
11804 break;
11805 case 'f':
11806 case 't':
11807 case 'u':
11808 if (type->isFloatingPointTy())
11809 weight = CW_SpecificReg;
11810 break;
11811 case 'y':
11812 if (type->isX86_MMXTy() && !DisableMMX && Subtarget->hasMMX())
11813 weight = CW_SpecificReg;
11814 break;
11815 case 'x':
11816 case 'Y':
Nate Begeman2ea8ee72010-12-10 00:26:57 +000011817 if ((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasXMM())
John Thompson44ab89e2010-10-29 17:29:13 +000011818 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +000011819 break;
11820 case 'I':
11821 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
11822 if (C->getZExtValue() <= 31)
John Thompson44ab89e2010-10-29 17:29:13 +000011823 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +000011824 }
11825 break;
John Thompson44ab89e2010-10-29 17:29:13 +000011826 case 'J':
11827 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
11828 if (C->getZExtValue() <= 63)
11829 weight = CW_Constant;
11830 }
11831 break;
11832 case 'K':
11833 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
11834 if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f))
11835 weight = CW_Constant;
11836 }
11837 break;
11838 case 'L':
11839 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
11840 if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff))
11841 weight = CW_Constant;
11842 }
11843 break;
11844 case 'M':
11845 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
11846 if (C->getZExtValue() <= 3)
11847 weight = CW_Constant;
11848 }
11849 break;
11850 case 'N':
11851 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
11852 if (C->getZExtValue() <= 0xff)
11853 weight = CW_Constant;
11854 }
11855 break;
11856 case 'G':
11857 case 'C':
11858 if (dyn_cast<ConstantFP>(CallOperandVal)) {
11859 weight = CW_Constant;
11860 }
11861 break;
11862 case 'e':
11863 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
11864 if ((C->getSExtValue() >= -0x80000000LL) &&
11865 (C->getSExtValue() <= 0x7fffffffLL))
11866 weight = CW_Constant;
11867 }
11868 break;
11869 case 'Z':
11870 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
11871 if (C->getZExtValue() <= 0xffffffff)
11872 weight = CW_Constant;
11873 }
11874 break;
John Thompsoneac6e1d2010-09-13 18:15:37 +000011875 }
11876 return weight;
11877}
11878
Dale Johannesenba2a0b92008-01-29 02:21:21 +000011879/// LowerXConstraint - try to replace an X constraint, which matches anything,
11880/// with another that has more specific requirements based on the type of the
11881/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000011882const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000011883LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000011884 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
11885 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000011886 if (ConstraintVT.isFloatingPoint()) {
Nate Begeman2ea8ee72010-12-10 00:26:57 +000011887 if (Subtarget->hasXMMInt())
Chris Lattner5e764232008-04-26 23:02:14 +000011888 return "Y";
Nate Begeman2ea8ee72010-12-10 00:26:57 +000011889 if (Subtarget->hasXMM())
Chris Lattner5e764232008-04-26 23:02:14 +000011890 return "x";
11891 }
Scott Michelfdc40a02009-02-17 22:15:04 +000011892
Chris Lattner5e764232008-04-26 23:02:14 +000011893 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000011894}
11895
Chris Lattner48884cd2007-08-25 00:47:38 +000011896/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
11897/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000011898void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattner48884cd2007-08-25 00:47:38 +000011899 char Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000011900 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000011901 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000011902 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000011903
Chris Lattner22aaf1d2006-10-31 20:13:11 +000011904 switch (Constraint) {
11905 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000011906 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000011907 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000011908 if (C->getZExtValue() <= 31) {
11909 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000011910 break;
11911 }
Devang Patel84f7fd22007-03-17 00:13:28 +000011912 }
Chris Lattner48884cd2007-08-25 00:47:38 +000011913 return;
Evan Cheng364091e2008-09-22 23:57:37 +000011914 case 'J':
11915 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000011916 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000011917 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
11918 break;
11919 }
11920 }
11921 return;
11922 case 'K':
11923 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000011924 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +000011925 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
11926 break;
11927 }
11928 }
11929 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000011930 case 'N':
11931 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000011932 if (C->getZExtValue() <= 255) {
11933 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000011934 break;
11935 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000011936 }
Chris Lattner48884cd2007-08-25 00:47:38 +000011937 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000011938 case 'e': {
11939 // 32-bit signed value
11940 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000011941 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
11942 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000011943 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000011944 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000011945 break;
11946 }
11947 // FIXME gcc accepts some relocatable values here too, but only in certain
11948 // memory models; it's complicated.
11949 }
11950 return;
11951 }
11952 case 'Z': {
11953 // 32-bit unsigned value
11954 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000011955 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
11956 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000011957 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
11958 break;
11959 }
11960 }
11961 // FIXME gcc accepts some relocatable values here too, but only in certain
11962 // memory models; it's complicated.
11963 return;
11964 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000011965 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000011966 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000011967 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000011968 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000011969 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000011970 break;
11971 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011972
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000011973 // In any sort of PIC mode addresses need to be computed at runtime by
11974 // adding in a register or some sort of table lookup. These can't
11975 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000011976 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000011977 return;
11978
Chris Lattnerdc43a882007-05-03 16:52:29 +000011979 // If we are in non-pic codegen mode, we allow the address of a global (with
11980 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000011981 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000011982 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000011983
Chris Lattner49921962009-05-08 18:23:14 +000011984 // Match either (GA), (GA+C), (GA+C1+C2), etc.
11985 while (1) {
11986 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
11987 Offset += GA->getOffset();
11988 break;
11989 } else if (Op.getOpcode() == ISD::ADD) {
11990 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
11991 Offset += C->getZExtValue();
11992 Op = Op.getOperand(0);
11993 continue;
11994 }
11995 } else if (Op.getOpcode() == ISD::SUB) {
11996 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
11997 Offset += -C->getZExtValue();
11998 Op = Op.getOperand(0);
11999 continue;
12000 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000012001 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000012002
Chris Lattner49921962009-05-08 18:23:14 +000012003 // Otherwise, this isn't something we can handle, reject it.
12004 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000012005 }
Eric Christopherfd179292009-08-27 18:07:15 +000012006
Dan Gohman46510a72010-04-15 01:51:59 +000012007 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000012008 // If we require an extra load to get this address, as in PIC mode, we
12009 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000012010 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
12011 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000012012 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000012013
Devang Patel0d881da2010-07-06 22:08:15 +000012014 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
12015 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000012016 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012017 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000012018 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012019
Gabor Greifba36cb52008-08-28 21:40:38 +000012020 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000012021 Ops.push_back(Result);
12022 return;
12023 }
Dale Johannesen1784d162010-06-25 21:55:36 +000012024 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012025}
12026
Chris Lattner259e97c2006-01-31 19:43:35 +000012027std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +000012028getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000012029 EVT VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +000012030 if (Constraint.size() == 1) {
12031 // FIXME: not handling fp-stack yet!
Chris Lattner259e97c2006-01-31 19:43:35 +000012032 switch (Constraint[0]) { // GCC X86 Constraint Letters
Chris Lattnerf4dff842006-07-11 02:54:03 +000012033 default: break; // Unknown constraint letter
Evan Cheng47e9fab2009-07-17 22:13:25 +000012034 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
12035 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000012036 if (VT == MVT::i32)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012037 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX,
12038 X86::ESI, X86::EDI, X86::R8D, X86::R9D,
12039 X86::R10D,X86::R11D,X86::R12D,
12040 X86::R13D,X86::R14D,X86::R15D,
12041 X86::EBP, X86::ESP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012042 else if (VT == MVT::i16)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012043 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX,
12044 X86::SI, X86::DI, X86::R8W,X86::R9W,
12045 X86::R10W,X86::R11W,X86::R12W,
12046 X86::R13W,X86::R14W,X86::R15W,
12047 X86::BP, X86::SP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012048 else if (VT == MVT::i8)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012049 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL,
12050 X86::SIL, X86::DIL, X86::R8B,X86::R9B,
12051 X86::R10B,X86::R11B,X86::R12B,
12052 X86::R13B,X86::R14B,X86::R15B,
12053 X86::BPL, X86::SPL, 0);
12054
Owen Anderson825b72b2009-08-11 20:47:22 +000012055 else if (VT == MVT::i64)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012056 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX,
12057 X86::RSI, X86::RDI, X86::R8, X86::R9,
12058 X86::R10, X86::R11, X86::R12,
12059 X86::R13, X86::R14, X86::R15,
12060 X86::RBP, X86::RSP, 0);
12061
12062 break;
12063 }
Eric Christopherfd179292009-08-27 18:07:15 +000012064 // 32-bit fallthrough
Chris Lattner259e97c2006-01-31 19:43:35 +000012065 case 'Q': // Q_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +000012066 if (VT == MVT::i32)
Chris Lattner80a7ecc2006-05-06 00:29:37 +000012067 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012068 else if (VT == MVT::i16)
Chris Lattner80a7ecc2006-05-06 00:29:37 +000012069 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012070 else if (VT == MVT::i8)
Evan Cheng12914382007-08-13 23:27:11 +000012071 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012072 else if (VT == MVT::i64)
Chris Lattner03e6c702007-11-04 06:51:12 +000012073 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
12074 break;
Chris Lattner259e97c2006-01-31 19:43:35 +000012075 }
12076 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012077
Chris Lattner1efa40f2006-02-22 00:56:39 +000012078 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +000012079}
Chris Lattnerf76d1802006-07-31 23:26:50 +000012080
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012081std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000012082X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000012083 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000012084 // First, see if this is a constraint that directly corresponds to an LLVM
12085 // register class.
12086 if (Constraint.size() == 1) {
12087 // GCC Constraint Letters
12088 switch (Constraint[0]) {
12089 default: break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000012090 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000012091 case 'l': // INDEX_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +000012092 if (VT == MVT::i8)
Chris Lattner0f65cad2007-04-09 05:49:22 +000012093 return std::make_pair(0U, X86::GR8RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012094 if (VT == MVT::i16)
Chris Lattner1fa71982008-10-17 18:15:05 +000012095 return std::make_pair(0U, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012096 if (VT == MVT::i32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +000012097 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +000012098 return std::make_pair(0U, X86::GR64RegisterClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000012099 case 'R': // LEGACY_REGS
12100 if (VT == MVT::i8)
12101 return std::make_pair(0U, X86::GR8_NOREXRegisterClass);
12102 if (VT == MVT::i16)
12103 return std::make_pair(0U, X86::GR16_NOREXRegisterClass);
12104 if (VT == MVT::i32 || !Subtarget->is64Bit())
12105 return std::make_pair(0U, X86::GR32_NOREXRegisterClass);
12106 return std::make_pair(0U, X86::GR64_NOREXRegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000012107 case 'f': // FP Stack registers.
12108 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
12109 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000012110 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000012111 return std::make_pair(0U, X86::RFP32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012112 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000012113 return std::make_pair(0U, X86::RFP64RegisterClass);
12114 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000012115 case 'y': // MMX_REGS if MMX allowed.
12116 if (!Subtarget->hasMMX()) break;
12117 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000012118 case 'Y': // SSE_REGS if SSE2 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012119 if (!Subtarget->hasXMMInt()) break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000012120 // FALL THROUGH.
12121 case 'x': // SSE_REGS if SSE1 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012122 if (!Subtarget->hasXMM()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000012123
Owen Anderson825b72b2009-08-11 20:47:22 +000012124 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000012125 default: break;
12126 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000012127 case MVT::f32:
12128 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +000012129 return std::make_pair(0U, X86::FR32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012130 case MVT::f64:
12131 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +000012132 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000012133 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000012134 case MVT::v16i8:
12135 case MVT::v8i16:
12136 case MVT::v4i32:
12137 case MVT::v2i64:
12138 case MVT::v4f32:
12139 case MVT::v2f64:
Chris Lattner0f65cad2007-04-09 05:49:22 +000012140 return std::make_pair(0U, X86::VR128RegisterClass);
12141 }
Chris Lattnerad043e82007-04-09 05:11:28 +000012142 break;
12143 }
12144 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012145
Chris Lattnerf76d1802006-07-31 23:26:50 +000012146 // Use the default implementation in TargetLowering to convert the register
12147 // constraint into a member of a register class.
12148 std::pair<unsigned, const TargetRegisterClass*> Res;
12149 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000012150
12151 // Not found as a standard register?
12152 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000012153 // Map st(0) -> st(7) -> ST0
12154 if (Constraint.size() == 7 && Constraint[0] == '{' &&
12155 tolower(Constraint[1]) == 's' &&
12156 tolower(Constraint[2]) == 't' &&
12157 Constraint[3] == '(' &&
12158 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
12159 Constraint[5] == ')' &&
12160 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000012161
Chris Lattner56d77c72009-09-13 22:41:48 +000012162 Res.first = X86::ST0+Constraint[4]-'0';
12163 Res.second = X86::RFP80RegisterClass;
12164 return Res;
12165 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000012166
Chris Lattner56d77c72009-09-13 22:41:48 +000012167 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000012168 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000012169 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +000012170 Res.second = X86::RFP80RegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000012171 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000012172 }
Chris Lattner56d77c72009-09-13 22:41:48 +000012173
12174 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000012175 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000012176 Res.first = X86::EFLAGS;
12177 Res.second = X86::CCRRegisterClass;
12178 return Res;
12179 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000012180
Dale Johannesen330169f2008-11-13 21:52:36 +000012181 // 'A' means EAX + EDX.
12182 if (Constraint == "A") {
12183 Res.first = X86::EAX;
Dan Gohman68a31c22009-07-30 17:02:08 +000012184 Res.second = X86::GR32_ADRegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000012185 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000012186 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000012187 return Res;
12188 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012189
Chris Lattnerf76d1802006-07-31 23:26:50 +000012190 // Otherwise, check to see if this is a register class of the wrong value
12191 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
12192 // turn into {ax},{dx}.
12193 if (Res.second->hasType(VT))
12194 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012195
Chris Lattnerf76d1802006-07-31 23:26:50 +000012196 // All of the single-register GCC register classes map their values onto
12197 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
12198 // really want an 8-bit or 32-bit register, map to the appropriate register
12199 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +000012200 if (Res.second == X86::GR16RegisterClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000012201 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000012202 unsigned DestReg = 0;
12203 switch (Res.first) {
12204 default: break;
12205 case X86::AX: DestReg = X86::AL; break;
12206 case X86::DX: DestReg = X86::DL; break;
12207 case X86::CX: DestReg = X86::CL; break;
12208 case X86::BX: DestReg = X86::BL; break;
12209 }
12210 if (DestReg) {
12211 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000012212 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000012213 }
Owen Anderson825b72b2009-08-11 20:47:22 +000012214 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000012215 unsigned DestReg = 0;
12216 switch (Res.first) {
12217 default: break;
12218 case X86::AX: DestReg = X86::EAX; break;
12219 case X86::DX: DestReg = X86::EDX; break;
12220 case X86::CX: DestReg = X86::ECX; break;
12221 case X86::BX: DestReg = X86::EBX; break;
12222 case X86::SI: DestReg = X86::ESI; break;
12223 case X86::DI: DestReg = X86::EDI; break;
12224 case X86::BP: DestReg = X86::EBP; break;
12225 case X86::SP: DestReg = X86::ESP; break;
12226 }
12227 if (DestReg) {
12228 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000012229 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000012230 }
Owen Anderson825b72b2009-08-11 20:47:22 +000012231 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000012232 unsigned DestReg = 0;
12233 switch (Res.first) {
12234 default: break;
12235 case X86::AX: DestReg = X86::RAX; break;
12236 case X86::DX: DestReg = X86::RDX; break;
12237 case X86::CX: DestReg = X86::RCX; break;
12238 case X86::BX: DestReg = X86::RBX; break;
12239 case X86::SI: DestReg = X86::RSI; break;
12240 case X86::DI: DestReg = X86::RDI; break;
12241 case X86::BP: DestReg = X86::RBP; break;
12242 case X86::SP: DestReg = X86::RSP; break;
12243 }
12244 if (DestReg) {
12245 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000012246 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000012247 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000012248 }
Chris Lattner6ba50a92008-08-26 06:19:02 +000012249 } else if (Res.second == X86::FR32RegisterClass ||
12250 Res.second == X86::FR64RegisterClass ||
12251 Res.second == X86::VR128RegisterClass) {
12252 // Handle references to XMM physical registers that got mapped into the
12253 // wrong class. This can happen with constraints like {xmm0} where the
12254 // target independent register mapper will just pick the first match it can
12255 // find, ignoring the required type.
Owen Anderson825b72b2009-08-11 20:47:22 +000012256 if (VT == MVT::f32)
Chris Lattner6ba50a92008-08-26 06:19:02 +000012257 Res.second = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +000012258 else if (VT == MVT::f64)
Chris Lattner6ba50a92008-08-26 06:19:02 +000012259 Res.second = X86::FR64RegisterClass;
12260 else if (X86::VR128RegisterClass->hasType(VT))
12261 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000012262 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012263
Chris Lattnerf76d1802006-07-31 23:26:50 +000012264 return Res;
12265}