blob: e4561aad41abeb3c8ecea57a5b4e2c34fc529df3 [file] [log] [blame]
Arnold Schwaighofer92226dd2007-10-12 21:53:12 +00001//===-- X86ISelLowering.cpp - X86 DAG Lowering Implementation -------------===//
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that X86 uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
Evan Chengb1712452010-01-27 06:25:16 +000015#define DEBUG_TYPE "x86-isel"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000016#include "X86.h"
Evan Cheng0cc39452006-01-16 21:21:29 +000017#include "X86InstrBuilder.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000018#include "X86ISelLowering.h"
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000019#include "X86ShuffleDecode.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000020#include "X86TargetMachine.h"
Chris Lattner8c6ed052009-09-16 01:46:41 +000021#include "X86TargetObjectFile.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000022#include "llvm/CallingConv.h"
Evan Cheng223547a2006-01-31 22:28:30 +000023#include "llvm/Constants.h"
Evan Cheng347d5f72006-04-28 21:29:37 +000024#include "llvm/DerivedTypes.h"
Chris Lattnerb903bed2009-06-26 21:20:29 +000025#include "llvm/GlobalAlias.h"
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000026#include "llvm/GlobalVariable.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000027#include "llvm/Function.h"
Chris Lattnerb8105652009-07-20 17:51:36 +000028#include "llvm/Instructions.h"
Evan Cheng6be2c582006-04-05 23:38:46 +000029#include "llvm/Intrinsics.h"
Owen Andersona90b3dc2009-07-15 21:51:10 +000030#include "llvm/LLVMContext.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000031#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng4a460802006-01-11 00:33:36 +000032#include "llvm/CodeGen/MachineFunction.h"
33#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner5e1df8d2010-01-25 23:38:14 +000034#include "llvm/CodeGen/MachineJumpTableInfo.h"
Evan Chenga844bde2008-02-02 04:07:54 +000035#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000036#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman69de1932008-02-06 22:27:42 +000037#include "llvm/CodeGen/PseudoSourceValue.h"
Chris Lattner589c6f62010-01-26 06:28:43 +000038#include "llvm/MC/MCAsmInfo.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000039#include "llvm/MC/MCContext.h"
Daniel Dunbar4e815f82010-03-15 23:51:06 +000040#include "llvm/MC/MCExpr.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000041#include "llvm/MC/MCSymbol.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000042#include "llvm/ADT/BitVector.h"
Evan Cheng14b32e12007-12-11 01:46:18 +000043#include "llvm/ADT/SmallSet.h"
Evan Chengb1712452010-01-27 06:25:16 +000044#include "llvm/ADT/Statistic.h"
Chris Lattner1a60aa72006-10-31 19:42:44 +000045#include "llvm/ADT/StringExtras.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000046#include "llvm/ADT/VectorExtras.h"
Mon P Wang3c81d352008-11-23 04:37:22 +000047#include "llvm/Support/CommandLine.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000048#include "llvm/Support/Debug.h"
Bill Wendlingec041eb2010-03-12 19:20:40 +000049#include "llvm/Support/Dwarf.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000050#include "llvm/Support/ErrorHandling.h"
51#include "llvm/Support/MathExtras.h"
Torok Edwindac237e2009-07-08 20:53:28 +000052#include "llvm/Support/raw_ostream.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000053using namespace llvm;
Bill Wendlingec041eb2010-03-12 19:20:40 +000054using namespace dwarf;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000055
Evan Chengb1712452010-01-27 06:25:16 +000056STATISTIC(NumTailCalls, "Number of tail calls");
57
Evan Cheng10e86422008-04-25 19:11:04 +000058// Forward declarations.
Owen Andersone50ed302009-08-10 22:56:29 +000059static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +000060 SDValue V2);
Evan Cheng10e86422008-04-25 19:11:04 +000061
Chris Lattnerf0144122009-07-28 03:13:23 +000062static TargetLoweringObjectFile *createTLOF(X86TargetMachine &TM) {
Michael J. Spencerec38de22010-10-10 22:04:20 +000063
Eric Christopher62f35a22010-07-05 19:26:33 +000064 bool is64Bit = TM.getSubtarget<X86Subtarget>().is64Bit();
Michael J. Spencerec38de22010-10-10 22:04:20 +000065
Eric Christopher62f35a22010-07-05 19:26:33 +000066 if (TM.getSubtarget<X86Subtarget>().isTargetDarwin()) {
Chris Lattnere019ec12010-12-19 20:07:10 +000067 if (is64Bit)
68 return new X8664_MachoTargetObjectFile();
Anton Korobeynikov293d5922010-02-21 20:28:15 +000069 return new TargetLoweringObjectFileMachO();
Michael J. Spencerec38de22010-10-10 22:04:20 +000070 }
Chris Lattnere019ec12010-12-19 20:07:10 +000071
72 if (TM.getSubtarget<X86Subtarget>().isTargetELF() ){
73 if (is64Bit)
74 return new X8664_ELFTargetObjectFile(TM);
75 return new X8632_ELFTargetObjectFile(TM);
76 }
77 if (TM.getSubtarget<X86Subtarget>().isTargetCOFF())
78 return new TargetLoweringObjectFileCOFF();
Eric Christopher62f35a22010-07-05 19:26:33 +000079 llvm_unreachable("unknown subtarget type");
Chris Lattnerf0144122009-07-28 03:13:23 +000080}
81
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +000082X86TargetLowering::X86TargetLowering(X86TargetMachine &TM)
Chris Lattnerf0144122009-07-28 03:13:23 +000083 : TargetLowering(TM, createTLOF(TM)) {
Evan Cheng559806f2006-01-27 08:10:46 +000084 Subtarget = &TM.getSubtarget<X86Subtarget>();
Nate Begeman2ea8ee72010-12-10 00:26:57 +000085 X86ScalarSSEf64 = Subtarget->hasXMMInt();
86 X86ScalarSSEf32 = Subtarget->hasXMM();
Evan Cheng25ab6902006-09-08 06:48:29 +000087 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000088
Anton Korobeynikov2365f512007-07-14 14:06:15 +000089 RegInfo = TM.getRegisterInfo();
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000090 TD = getTargetData();
Anton Korobeynikov2365f512007-07-14 14:06:15 +000091
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000092 // Set up the TargetLowering object.
Chris Lattnera34b3cf2010-12-19 20:03:11 +000093 static MVT IntVTs[] = { MVT::i8, MVT::i16, MVT::i32, MVT::i64 };
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000094
95 // X86 is weird, it always uses i8 for shift amounts and setcc results.
Owen Anderson825b72b2009-08-11 20:47:22 +000096 setShiftAmountType(MVT::i8);
Duncan Sands03228082008-11-23 15:47:28 +000097 setBooleanContents(ZeroOrOneBooleanContent);
Evan Cheng211ffa12010-05-19 20:19:50 +000098 setSchedulingPreference(Sched::RegPressure);
Evan Cheng25ab6902006-09-08 06:48:29 +000099 setStackPointerRegisterToSaveRestore(X86StackPtr);
Evan Cheng714554d2006-03-16 21:47:42 +0000100
Michael J. Spencer92bf38c2010-10-10 23:11:06 +0000101 if (Subtarget->isTargetWindows() && !Subtarget->isTargetCygMing()) {
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000102 // Setup Windows compiler runtime calls.
103 setLibcallName(RTLIB::SDIV_I64, "_alldiv");
Michael J. Spencer335b8062010-10-11 05:29:15 +0000104 setLibcallName(RTLIB::UDIV_I64, "_aulldiv");
105 setLibcallName(RTLIB::FPTOUINT_F64_I64, "_ftol2");
Michael J. Spencer94f7eeb2010-10-19 07:32:52 +0000106 setLibcallName(RTLIB::FPTOUINT_F32_I64, "_ftol2");
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000107 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::X86_StdCall);
Michael J. Spencer335b8062010-10-11 05:29:15 +0000108 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::X86_StdCall);
Michael J. Spencer6dad10e2010-10-27 18:52:38 +0000109 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I64, CallingConv::C);
110 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I64, CallingConv::C);
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000111 }
112
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000113 if (Subtarget->isTargetDarwin()) {
Evan Chengdf57fa02006-03-17 20:31:41 +0000114 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000115 setUseUnderscoreSetJmp(false);
116 setUseUnderscoreLongJmp(false);
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000117 } else if (Subtarget->isTargetMingw()) {
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000118 // MS runtime is weird: it exports _setjmp, but longjmp!
119 setUseUnderscoreSetJmp(true);
120 setUseUnderscoreLongJmp(false);
121 } else {
122 setUseUnderscoreSetJmp(true);
123 setUseUnderscoreLongJmp(true);
124 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000125
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000126 // Set up the register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000127 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
Dan Gohman71edb242010-04-30 18:30:26 +0000128 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000129 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
Evan Cheng25ab6902006-09-08 06:48:29 +0000130 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000131 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000132
Owen Anderson825b72b2009-08-11 20:47:22 +0000133 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Evan Chengc5484282006-10-04 00:56:09 +0000134
Scott Michelfdc40a02009-02-17 22:15:04 +0000135 // We don't accept any truncstore of integer registers.
Owen Anderson825b72b2009-08-11 20:47:22 +0000136 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000137 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000138 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000139 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000140 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
141 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
Evan Cheng7f042682008-10-15 02:05:31 +0000142
143 // SETOEQ and SETUNE require checking two conditions.
Owen Anderson825b72b2009-08-11 20:47:22 +0000144 setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand);
145 setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand);
146 setCondCodeAction(ISD::SETOEQ, MVT::f80, Expand);
147 setCondCodeAction(ISD::SETUNE, MVT::f32, Expand);
148 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
149 setCondCodeAction(ISD::SETUNE, MVT::f80, Expand);
Chris Lattnerddf89562008-01-17 19:59:44 +0000150
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000151 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
152 // operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000153 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
154 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
155 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +0000156
Evan Cheng25ab6902006-09-08 06:48:29 +0000157 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000158 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
159 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
Eli Friedman948e95a2009-05-23 09:59:16 +0000160 } else if (!UseSoftFloat) {
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000161 // We have an algorithm for SSE2->double, and we turn this into a
162 // 64-bit FILD followed by conditional FADD for other targets.
163 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Eli Friedman948e95a2009-05-23 09:59:16 +0000164 // We have an algorithm for SSE2, and we turn this into a 64-bit
165 // FILD for other targets.
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000166 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000167 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000168
169 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
170 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000171 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
172 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000173
Devang Patel6a784892009-06-05 18:48:29 +0000174 if (!UseSoftFloat) {
Bill Wendling105be5a2009-03-13 08:41:47 +0000175 // SSE has no i16 to fp conversion, only i32
176 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000177 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000178 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000179 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000180 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000181 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
182 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000183 }
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000184 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000185 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
186 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Promote);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000187 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000188
Dale Johannesen73328d12007-09-19 23:55:34 +0000189 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
190 // are Legal, f80 is custom lowered.
Owen Anderson825b72b2009-08-11 20:47:22 +0000191 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
192 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Evan Cheng6dab0532006-01-30 08:02:57 +0000193
Evan Cheng02568ff2006-01-30 22:13:22 +0000194 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
195 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000196 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
197 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
Evan Cheng02568ff2006-01-30 22:13:22 +0000198
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000199 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000200 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000201 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000202 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000203 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000204 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
205 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000206 }
207
208 // Handle FP_TO_UINT by promoting the destination to a larger signed
209 // conversion.
Owen Anderson825b72b2009-08-11 20:47:22 +0000210 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
211 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
212 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000213
Evan Cheng25ab6902006-09-08 06:48:29 +0000214 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000215 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
216 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Eli Friedman948e95a2009-05-23 09:59:16 +0000217 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000218 if (X86ScalarSSEf32 && !Subtarget->hasSSE3())
Evan Cheng25ab6902006-09-08 06:48:29 +0000219 // Expand FP_TO_UINT into a select.
220 // FIXME: We would like to use a Custom expander here eventually to do
221 // the optimal thing for SSE vs. the default expansion in the legalizer.
Owen Anderson825b72b2009-08-11 20:47:22 +0000222 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000223 else
Eli Friedman948e95a2009-05-23 09:59:16 +0000224 // With SSE3 we can use fisttpll to convert to a signed i64; without
225 // SSE, we're stuck with a fistpll.
Owen Anderson825b72b2009-08-11 20:47:22 +0000226 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000227 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000228
Chris Lattner399610a2006-12-05 18:22:22 +0000229 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Michael J. Spencerec38de22010-10-10 22:04:20 +0000230 if (!X86ScalarSSEf64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000231 setOperationAction(ISD::BITCAST , MVT::f32 , Expand);
232 setOperationAction(ISD::BITCAST , MVT::i32 , Expand);
Dale Johannesene39859a2010-05-21 18:40:15 +0000233 if (Subtarget->is64Bit()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000234 setOperationAction(ISD::BITCAST , MVT::f64 , Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000235 // Without SSE, i64->f64 goes through memory.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000236 setOperationAction(ISD::BITCAST , MVT::i64 , Expand);
Dale Johannesen7d07b482010-05-21 00:52:33 +0000237 }
Chris Lattnerf3597a12006-12-05 18:45:06 +0000238 }
Chris Lattner21f66852005-12-23 05:15:23 +0000239
Dan Gohmanb00ee212008-02-18 19:34:53 +0000240 // Scalar integer divide and remainder are lowered to use operations that
241 // produce two results, to match the available instructions. This exposes
242 // the two-result form to trivial CSE, which is able to combine x/y and x%y
243 // into a single instruction.
244 //
245 // Scalar integer multiply-high is also lowered to use two-result
246 // operations, to match the available instructions. However, plain multiply
247 // (low) operations are left as Legal, as there are single-result
248 // instructions for this in x86. Using the two-result multiply instructions
249 // when both high and low results are needed must be arranged by dagcombine.
Chris Lattnere019ec12010-12-19 20:07:10 +0000250 for (unsigned i = 0, e = 4; i != e; ++i) {
251 MVT VT = IntVTs[i];
252 setOperationAction(ISD::MULHS, VT, Expand);
253 setOperationAction(ISD::MULHU, VT, Expand);
254 setOperationAction(ISD::SDIV, VT, Expand);
255 setOperationAction(ISD::UDIV, VT, Expand);
256 setOperationAction(ISD::SREM, VT, Expand);
257 setOperationAction(ISD::UREM, VT, Expand);
Chris Lattnerd8ff7ec2010-12-20 01:03:27 +0000258
259 // Add/Sub overflow ops with MVT::Flags are lowered to EFLAGS dependences.
260 setOperationAction(ISD::ADDC, VT, Custom);
261 setOperationAction(ISD::ADDE, VT, Custom);
262 setOperationAction(ISD::SUBC, VT, Custom);
263 setOperationAction(ISD::SUBE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000264 }
Dan Gohmana37c9f72007-09-25 18:23:27 +0000265
Owen Anderson825b72b2009-08-11 20:47:22 +0000266 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
267 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
268 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
269 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000270 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000271 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
272 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
273 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
274 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
275 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
276 setOperationAction(ISD::FREM , MVT::f32 , Expand);
277 setOperationAction(ISD::FREM , MVT::f64 , Expand);
278 setOperationAction(ISD::FREM , MVT::f80 , Expand);
279 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000280
Owen Anderson825b72b2009-08-11 20:47:22 +0000281 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
282 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000283 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
284 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000285 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
286 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000287 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000288 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
289 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000290 }
291
Benjamin Kramer1292c222010-12-04 20:32:23 +0000292 if (Subtarget->hasPOPCNT()) {
293 setOperationAction(ISD::CTPOP , MVT::i8 , Promote);
294 } else {
295 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
296 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
297 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
298 if (Subtarget->is64Bit())
299 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
300 }
301
Owen Anderson825b72b2009-08-11 20:47:22 +0000302 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
303 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000304
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000305 // These should be promoted to a larger select which is supported.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000306 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000307 // X86 wants to expand cmov itself.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000308 setOperationAction(ISD::SELECT , MVT::i8 , Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000309 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000310 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
311 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
312 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
313 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
314 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000315 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000316 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
317 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
318 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
319 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000320 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000321 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
322 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000323 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000324 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000325
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000326 // Darwin ABI issue.
Owen Anderson825b72b2009-08-11 20:47:22 +0000327 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
328 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
329 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
330 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000331 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000332 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
333 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000334 setOperationAction(ISD::BlockAddress , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000335 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000336 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
337 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
338 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
339 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000340 setOperationAction(ISD::BlockAddress , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000341 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000342 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Owen Anderson825b72b2009-08-11 20:47:22 +0000343 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
344 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
345 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000346 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000347 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
348 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
349 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000350 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000351
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000352 if (Subtarget->hasXMM())
Owen Anderson825b72b2009-08-11 20:47:22 +0000353 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Cheng27b7db52008-03-08 00:58:38 +0000354
Eric Christopher9a9d2752010-07-22 02:48:34 +0000355 // We may not have a libcall for MEMBARRIER so we should lower this.
356 setOperationAction(ISD::MEMBARRIER , MVT::Other, Custom);
Michael J. Spencerec38de22010-10-10 22:04:20 +0000357
Jim Grosbachf1ab49e2010-06-23 16:25:07 +0000358 // On X86 and X86-64, atomic operations are lowered to locked instructions.
359 // Locked instructions, in turn, have implicit fence semantics (all memory
360 // operations are flushed before issuing the locked instruction, and they
361 // are not buffered), so we can fold away the common pattern of
362 // fence-atomic-fence.
363 setShouldFoldAtomicFences(true);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000364
Mon P Wang63307c32008-05-05 19:05:59 +0000365 // Expand certain atomics
Chris Lattnere019ec12010-12-19 20:07:10 +0000366 for (unsigned i = 0, e = 4; i != e; ++i) {
367 MVT VT = IntVTs[i];
368 setOperationAction(ISD::ATOMIC_CMP_SWAP, VT, Custom);
369 setOperationAction(ISD::ATOMIC_LOAD_SUB, VT, Custom);
370 }
371
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000372 if (!Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000373 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
374 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
375 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
376 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
377 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
378 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
379 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000380 }
381
Evan Cheng3c992d22006-03-07 02:02:57 +0000382 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000383 if (!Subtarget->isTargetDarwin() &&
384 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000385 !Subtarget->isTargetCygMing()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000386 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Dan Gohman44066042008-07-01 00:05:16 +0000387 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000388
Owen Anderson825b72b2009-08-11 20:47:22 +0000389 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
390 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
391 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
392 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000393 if (Subtarget->is64Bit()) {
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000394 setExceptionPointerRegister(X86::RAX);
395 setExceptionSelectorRegister(X86::RDX);
396 } else {
397 setExceptionPointerRegister(X86::EAX);
398 setExceptionSelectorRegister(X86::EDX);
399 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000400 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
401 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000402
Owen Anderson825b72b2009-08-11 20:47:22 +0000403 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000404
Owen Anderson825b72b2009-08-11 20:47:22 +0000405 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000406
Nate Begemanacc398c2006-01-25 18:21:52 +0000407 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson825b72b2009-08-11 20:47:22 +0000408 setOperationAction(ISD::VASTART , MVT::Other, Custom);
409 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000410 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000411 setOperationAction(ISD::VAARG , MVT::Other, Custom);
412 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000413 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000414 setOperationAction(ISD::VAARG , MVT::Other, Expand);
415 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000416 }
Evan Chengae642192007-03-02 23:16:35 +0000417
Owen Anderson825b72b2009-08-11 20:47:22 +0000418 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
419 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000420 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000421 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Michael J. Spencere9c253e2010-10-21 01:41:01 +0000422 if (Subtarget->isTargetCygMing() || Subtarget->isTargetWindows())
Owen Anderson825b72b2009-08-11 20:47:22 +0000423 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +0000424 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000425 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000426
Evan Chengc7ce29b2009-02-13 22:36:38 +0000427 if (!UseSoftFloat && X86ScalarSSEf64) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000428 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000429 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000430 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
431 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000432
Evan Cheng223547a2006-01-31 22:28:30 +0000433 // Use ANDPD to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000434 setOperationAction(ISD::FABS , MVT::f64, Custom);
435 setOperationAction(ISD::FABS , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000436
437 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000438 setOperationAction(ISD::FNEG , MVT::f64, Custom);
439 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000440
Evan Cheng68c47cb2007-01-05 07:55:56 +0000441 // Use ANDPD and ORPD to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000442 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
443 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000444
Evan Chengd25e9e82006-02-02 00:28:23 +0000445 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000446 setOperationAction(ISD::FSIN , MVT::f64, Expand);
447 setOperationAction(ISD::FCOS , MVT::f64, Expand);
448 setOperationAction(ISD::FSIN , MVT::f32, Expand);
449 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000450
Chris Lattnera54aa942006-01-29 06:26:08 +0000451 // Expand FP immediates into loads from the stack, except for the special
452 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000453 addLegalFPImmediate(APFloat(+0.0)); // xorpd
454 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Evan Chengc7ce29b2009-02-13 22:36:38 +0000455 } else if (!UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000456 // Use SSE for f32, x87 for f64.
457 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000458 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
459 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000460
461 // Use ANDPS to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000462 setOperationAction(ISD::FABS , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000463
464 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000465 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000466
Owen Anderson825b72b2009-08-11 20:47:22 +0000467 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000468
469 // Use ANDPS and ORPS to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000470 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
471 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000472
473 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000474 setOperationAction(ISD::FSIN , MVT::f32, Expand);
475 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000476
Nate Begemane1795842008-02-14 08:57:00 +0000477 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000478 addLegalFPImmediate(APFloat(+0.0f)); // xorps
479 addLegalFPImmediate(APFloat(+0.0)); // FLD0
480 addLegalFPImmediate(APFloat(+1.0)); // FLD1
481 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
482 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
483
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000484 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000485 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
486 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000487 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000488 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000489 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000490 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000491 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
492 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000493
Owen Anderson825b72b2009-08-11 20:47:22 +0000494 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
495 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
496 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
497 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000498
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000499 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000500 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
501 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000502 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000503 addLegalFPImmediate(APFloat(+0.0)); // FLD0
504 addLegalFPImmediate(APFloat(+1.0)); // FLD1
505 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
506 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000507 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
508 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
509 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
510 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000511 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000512
Dale Johannesen59a58732007-08-05 18:49:15 +0000513 // Long double always uses X87.
Evan Cheng92722532009-03-26 23:06:32 +0000514 if (!UseSoftFloat) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000515 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
516 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
517 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000518 {
Benjamin Kramer98383962010-12-04 14:22:24 +0000519 APFloat TmpFlt = APFloat::getZero(APFloat::x87DoubleExtended);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000520 addLegalFPImmediate(TmpFlt); // FLD0
521 TmpFlt.changeSign();
522 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
Benjamin Kramer98383962010-12-04 14:22:24 +0000523
524 bool ignored;
Evan Chengc7ce29b2009-02-13 22:36:38 +0000525 APFloat TmpFlt2(+1.0);
526 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
527 &ignored);
528 addLegalFPImmediate(TmpFlt2); // FLD1
529 TmpFlt2.changeSign();
530 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
531 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000532
Evan Chengc7ce29b2009-02-13 22:36:38 +0000533 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000534 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
535 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000536 }
Dale Johannesen2f429012007-09-26 21:10:55 +0000537 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000538
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000539 // Always use a library call for pow.
Owen Anderson825b72b2009-08-11 20:47:22 +0000540 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
541 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
542 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000543
Owen Anderson825b72b2009-08-11 20:47:22 +0000544 setOperationAction(ISD::FLOG, MVT::f80, Expand);
545 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
546 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
547 setOperationAction(ISD::FEXP, MVT::f80, Expand);
548 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000549
Mon P Wangf007a8b2008-11-06 05:31:54 +0000550 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000551 // (for widening) or expand (for scalarization). Then we will selectively
552 // turn on ones that can be effectively codegen'd.
Owen Anderson825b72b2009-08-11 20:47:22 +0000553 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
554 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
555 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
556 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
557 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
558 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
559 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
560 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
561 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
562 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
563 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
564 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
565 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
566 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
567 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
568 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
569 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
570 setOperationAction(ISD::EXTRACT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
571 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
572 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
573 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
574 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
575 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
576 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
577 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
578 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
579 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
580 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
581 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
582 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
583 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
584 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
585 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
586 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
587 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
588 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
589 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
590 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
591 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
592 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
593 setOperationAction(ISD::VSETCC, (MVT::SimpleValueType)VT, Expand);
594 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
595 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
596 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
597 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
598 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
599 setOperationAction(ISD::FP_TO_UINT, (MVT::SimpleValueType)VT, Expand);
600 setOperationAction(ISD::FP_TO_SINT, (MVT::SimpleValueType)VT, Expand);
601 setOperationAction(ISD::UINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
602 setOperationAction(ISD::SINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
Dan Gohman87862e72009-12-11 21:31:27 +0000603 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,Expand);
Dan Gohman2e141d72009-12-14 23:40:38 +0000604 setOperationAction(ISD::TRUNCATE, (MVT::SimpleValueType)VT, Expand);
605 setOperationAction(ISD::SIGN_EXTEND, (MVT::SimpleValueType)VT, Expand);
606 setOperationAction(ISD::ZERO_EXTEND, (MVT::SimpleValueType)VT, Expand);
607 setOperationAction(ISD::ANY_EXTEND, (MVT::SimpleValueType)VT, Expand);
608 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
609 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
610 setTruncStoreAction((MVT::SimpleValueType)VT,
611 (MVT::SimpleValueType)InnerVT, Expand);
612 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
613 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
614 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000615 }
616
Evan Chengc7ce29b2009-02-13 22:36:38 +0000617 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
618 // with -msoft-float, disable use of MMX as well.
Chris Lattner2a786eb2010-12-19 20:19:20 +0000619 if (!UseSoftFloat && Subtarget->hasMMX()) {
Dale Johannesene93d99c2010-10-20 21:32:10 +0000620 addRegisterClass(MVT::x86mmx, X86::VR64RegisterClass);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000621 // No operations on x86mmx supported, everything uses intrinsics.
Evan Cheng470a6ad2006-02-22 02:26:30 +0000622 }
623
Dale Johannesen0488fb62010-09-30 23:57:10 +0000624 // MMX-sized vectors (other than x86mmx) are expected to be expanded
625 // into smaller operations.
626 setOperationAction(ISD::MULHS, MVT::v8i8, Expand);
627 setOperationAction(ISD::MULHS, MVT::v4i16, Expand);
628 setOperationAction(ISD::MULHS, MVT::v2i32, Expand);
629 setOperationAction(ISD::MULHS, MVT::v1i64, Expand);
630 setOperationAction(ISD::AND, MVT::v8i8, Expand);
631 setOperationAction(ISD::AND, MVT::v4i16, Expand);
632 setOperationAction(ISD::AND, MVT::v2i32, Expand);
633 setOperationAction(ISD::AND, MVT::v1i64, Expand);
634 setOperationAction(ISD::OR, MVT::v8i8, Expand);
635 setOperationAction(ISD::OR, MVT::v4i16, Expand);
636 setOperationAction(ISD::OR, MVT::v2i32, Expand);
637 setOperationAction(ISD::OR, MVT::v1i64, Expand);
638 setOperationAction(ISD::XOR, MVT::v8i8, Expand);
639 setOperationAction(ISD::XOR, MVT::v4i16, Expand);
640 setOperationAction(ISD::XOR, MVT::v2i32, Expand);
641 setOperationAction(ISD::XOR, MVT::v1i64, Expand);
642 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Expand);
643 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Expand);
644 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Expand);
645 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Expand);
646 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v1i64, Expand);
647 setOperationAction(ISD::SELECT, MVT::v8i8, Expand);
648 setOperationAction(ISD::SELECT, MVT::v4i16, Expand);
649 setOperationAction(ISD::SELECT, MVT::v2i32, Expand);
650 setOperationAction(ISD::SELECT, MVT::v1i64, Expand);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000651 setOperationAction(ISD::BITCAST, MVT::v8i8, Expand);
652 setOperationAction(ISD::BITCAST, MVT::v4i16, Expand);
653 setOperationAction(ISD::BITCAST, MVT::v2i32, Expand);
654 setOperationAction(ISD::BITCAST, MVT::v1i64, Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000655
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000656 if (!UseSoftFloat && Subtarget->hasXMM()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000657 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000658
Owen Anderson825b72b2009-08-11 20:47:22 +0000659 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
660 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
661 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
662 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
663 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
664 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
665 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
666 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
667 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
668 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
669 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
670 setOperationAction(ISD::VSETCC, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000671 }
672
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000673 if (!UseSoftFloat && Subtarget->hasXMMInt()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000674 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000675
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000676 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
677 // registers cannot be used even for integer operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000678 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
679 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
680 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
681 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000682
Owen Anderson825b72b2009-08-11 20:47:22 +0000683 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
684 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
685 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
686 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
687 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
688 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
689 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
690 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
691 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
692 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
693 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
694 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
695 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
696 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
697 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
698 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000699
Owen Anderson825b72b2009-08-11 20:47:22 +0000700 setOperationAction(ISD::VSETCC, MVT::v2f64, Custom);
701 setOperationAction(ISD::VSETCC, MVT::v16i8, Custom);
702 setOperationAction(ISD::VSETCC, MVT::v8i16, Custom);
703 setOperationAction(ISD::VSETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000704
Owen Anderson825b72b2009-08-11 20:47:22 +0000705 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
706 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
707 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
708 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
709 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000710
Mon P Wangeb38ebf2010-01-24 00:05:03 +0000711 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2f64, Custom);
712 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2i64, Custom);
713 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i8, Custom);
714 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i16, Custom);
715 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i32, Custom);
716
Evan Cheng2c3ae372006-04-12 21:21:57 +0000717 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Owen Anderson825b72b2009-08-11 20:47:22 +0000718 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
719 EVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000720 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000721 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000722 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000723 // Do not attempt to custom lower non-128-bit vectors
724 if (!VT.is128BitVector())
725 continue;
Owen Anderson825b72b2009-08-11 20:47:22 +0000726 setOperationAction(ISD::BUILD_VECTOR,
727 VT.getSimpleVT().SimpleTy, Custom);
728 setOperationAction(ISD::VECTOR_SHUFFLE,
729 VT.getSimpleVT().SimpleTy, Custom);
730 setOperationAction(ISD::EXTRACT_VECTOR_ELT,
731 VT.getSimpleVT().SimpleTy, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000732 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000733
Owen Anderson825b72b2009-08-11 20:47:22 +0000734 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
735 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
736 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
737 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
738 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
739 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000740
Nate Begemancdd1eec2008-02-12 22:51:28 +0000741 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000742 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
743 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000744 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000745
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000746 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Owen Anderson825b72b2009-08-11 20:47:22 +0000747 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; i++) {
748 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
Owen Andersone50ed302009-08-10 22:56:29 +0000749 EVT VT = SVT;
David Greene9b9838d2009-06-29 16:47:10 +0000750
751 // Do not attempt to promote non-128-bit vectors
Chris Lattner32b4b5a2010-07-05 05:53:14 +0000752 if (!VT.is128BitVector())
David Greene9b9838d2009-06-29 16:47:10 +0000753 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +0000754
Owen Andersond6662ad2009-08-10 20:46:15 +0000755 setOperationAction(ISD::AND, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000756 AddPromotedToType (ISD::AND, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000757 setOperationAction(ISD::OR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000758 AddPromotedToType (ISD::OR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000759 setOperationAction(ISD::XOR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000760 AddPromotedToType (ISD::XOR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000761 setOperationAction(ISD::LOAD, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000762 AddPromotedToType (ISD::LOAD, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000763 setOperationAction(ISD::SELECT, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000764 AddPromotedToType (ISD::SELECT, SVT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000765 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000766
Owen Anderson825b72b2009-08-11 20:47:22 +0000767 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000768
Evan Cheng2c3ae372006-04-12 21:21:57 +0000769 // Custom lower v2i64 and v2f64 selects.
Owen Anderson825b72b2009-08-11 20:47:22 +0000770 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
771 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
772 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
773 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000774
Owen Anderson825b72b2009-08-11 20:47:22 +0000775 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
776 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000777 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000778
Nate Begeman14d12ca2008-02-11 04:19:36 +0000779 if (Subtarget->hasSSE41()) {
Dale Johannesen54feef22010-05-27 20:12:41 +0000780 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
781 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
782 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
783 setOperationAction(ISD::FRINT, MVT::f32, Legal);
784 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
785 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
786 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
787 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
788 setOperationAction(ISD::FRINT, MVT::f64, Legal);
789 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
790
Nate Begeman14d12ca2008-02-11 04:19:36 +0000791 // FIXME: Do we need to handle scalar-to-vector here?
Owen Anderson825b72b2009-08-11 20:47:22 +0000792 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000793
Nate Begemanbdcb5af2010-07-27 22:37:06 +0000794 // Can turn SHL into an integer multiply.
795 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
Nate Begeman51409212010-07-28 00:21:48 +0000796 setOperationAction(ISD::SHL, MVT::v16i8, Custom);
Nate Begemanbdcb5af2010-07-27 22:37:06 +0000797
Nate Begeman14d12ca2008-02-11 04:19:36 +0000798 // i8 and i16 vectors are custom , because the source register and source
799 // source memory operand types are not the same width. f32 vectors are
800 // custom since the immediate controlling the insert encodes additional
801 // information.
Owen Anderson825b72b2009-08-11 20:47:22 +0000802 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
803 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
804 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
805 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000806
Owen Anderson825b72b2009-08-11 20:47:22 +0000807 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
808 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
809 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
810 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000811
812 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000813 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
814 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000815 }
816 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000817
Chris Lattnera34b3cf2010-12-19 20:03:11 +0000818 if (Subtarget->hasSSE42())
Owen Anderson825b72b2009-08-11 20:47:22 +0000819 setOperationAction(ISD::VSETCC, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000820
David Greene9b9838d2009-06-29 16:47:10 +0000821 if (!UseSoftFloat && Subtarget->hasAVX()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000822 addRegisterClass(MVT::v8f32, X86::VR256RegisterClass);
823 addRegisterClass(MVT::v4f64, X86::VR256RegisterClass);
824 addRegisterClass(MVT::v8i32, X86::VR256RegisterClass);
825 addRegisterClass(MVT::v4i64, X86::VR256RegisterClass);
Bruno Cardoso Lopes405f11b2010-08-10 01:43:16 +0000826 addRegisterClass(MVT::v32i8, X86::VR256RegisterClass);
David Greened94c1012009-06-29 22:50:51 +0000827
Owen Anderson825b72b2009-08-11 20:47:22 +0000828 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
829 setOperationAction(ISD::LOAD, MVT::v8i32, Legal);
830 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
831 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
832 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
833 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
834 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
835 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
836 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
837 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +0000838 setOperationAction(ISD::BUILD_VECTOR, MVT::v8f32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000839 //setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8f32, Custom);
840 //setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8f32, Custom);
841 //setOperationAction(ISD::SELECT, MVT::v8f32, Custom);
842 //setOperationAction(ISD::VSETCC, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000843
844 // Operations to consider commented out -v16i16 v32i8
Owen Anderson825b72b2009-08-11 20:47:22 +0000845 //setOperationAction(ISD::ADD, MVT::v16i16, Legal);
846 setOperationAction(ISD::ADD, MVT::v8i32, Custom);
847 setOperationAction(ISD::ADD, MVT::v4i64, Custom);
848 //setOperationAction(ISD::SUB, MVT::v32i8, Legal);
849 //setOperationAction(ISD::SUB, MVT::v16i16, Legal);
850 setOperationAction(ISD::SUB, MVT::v8i32, Custom);
851 setOperationAction(ISD::SUB, MVT::v4i64, Custom);
852 //setOperationAction(ISD::MUL, MVT::v16i16, Legal);
853 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
854 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
855 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
856 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
857 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
858 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000859
Owen Anderson825b72b2009-08-11 20:47:22 +0000860 setOperationAction(ISD::VSETCC, MVT::v4f64, Custom);
861 // setOperationAction(ISD::VSETCC, MVT::v32i8, Custom);
862 // setOperationAction(ISD::VSETCC, MVT::v16i16, Custom);
863 setOperationAction(ISD::VSETCC, MVT::v8i32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000864
Owen Anderson825b72b2009-08-11 20:47:22 +0000865 // setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v32i8, Custom);
866 // setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i16, Custom);
867 // setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i16, Custom);
868 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i32, Custom);
869 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000870
Owen Anderson825b72b2009-08-11 20:47:22 +0000871 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f64, Custom);
872 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i64, Custom);
873 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f64, Custom);
874 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i64, Custom);
875 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f64, Custom);
876 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000877
878#if 0
879 // Not sure we want to do this since there are no 256-bit integer
880 // operations in AVX
881
882 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
883 // This includes 256-bit vectors
Owen Anderson825b72b2009-08-11 20:47:22 +0000884 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v4i64; ++i) {
885 EVT VT = (MVT::SimpleValueType)i;
David Greene9b9838d2009-06-29 16:47:10 +0000886
887 // Do not attempt to custom lower non-power-of-2 vectors
888 if (!isPowerOf2_32(VT.getVectorNumElements()))
889 continue;
890
891 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
892 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
893 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
894 }
895
896 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000897 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i64, Custom);
898 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i64, Custom);
Eric Christopherfd179292009-08-27 18:07:15 +0000899 }
David Greene9b9838d2009-06-29 16:47:10 +0000900#endif
901
902#if 0
903 // Not sure we want to do this since there are no 256-bit integer
904 // operations in AVX
905
906 // Promote v32i8, v16i16, v8i32 load, select, and, or, xor to v4i64.
907 // Including 256-bit vectors
Owen Anderson825b72b2009-08-11 20:47:22 +0000908 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v4i64; i++) {
909 EVT VT = (MVT::SimpleValueType)i;
David Greene9b9838d2009-06-29 16:47:10 +0000910
911 if (!VT.is256BitVector()) {
912 continue;
913 }
914 setOperationAction(ISD::AND, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000915 AddPromotedToType (ISD::AND, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000916 setOperationAction(ISD::OR, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000917 AddPromotedToType (ISD::OR, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000918 setOperationAction(ISD::XOR, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000919 AddPromotedToType (ISD::XOR, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000920 setOperationAction(ISD::LOAD, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000921 AddPromotedToType (ISD::LOAD, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000922 setOperationAction(ISD::SELECT, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000923 AddPromotedToType (ISD::SELECT, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000924 }
925
Owen Anderson825b72b2009-08-11 20:47:22 +0000926 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
David Greene9b9838d2009-06-29 16:47:10 +0000927#endif
928 }
929
Evan Cheng6be2c582006-04-05 23:38:46 +0000930 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000931 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +0000932
Chris Lattnera34b3cf2010-12-19 20:03:11 +0000933
Eli Friedman962f5492010-06-02 19:35:46 +0000934 // Only custom-lower 64-bit SADDO and friends on 64-bit because we don't
935 // handle type legalization for these operations here.
Dan Gohman71c62a22010-06-02 19:13:40 +0000936 //
Eli Friedman962f5492010-06-02 19:35:46 +0000937 // FIXME: We really should do custom legalization for addition and
938 // subtraction on x86-32 once PR3203 is fixed. We really can't do much better
939 // than generic legalization for 64-bit multiplication-with-overflow, though.
Chris Lattnera34b3cf2010-12-19 20:03:11 +0000940 for (unsigned i = 0, e = 3+Subtarget->is64Bit(); i != e; ++i) {
941 // Add/Sub/Mul with overflow operations are custom lowered.
942 MVT VT = IntVTs[i];
943 setOperationAction(ISD::SADDO, VT, Custom);
944 setOperationAction(ISD::UADDO, VT, Custom);
945 setOperationAction(ISD::SSUBO, VT, Custom);
946 setOperationAction(ISD::USUBO, VT, Custom);
947 setOperationAction(ISD::SMULO, VT, Custom);
948 setOperationAction(ISD::UMULO, VT, Custom);
Eli Friedmana993f0a2010-06-02 00:27:18 +0000949 }
Chris Lattnera34b3cf2010-12-19 20:03:11 +0000950
951 // There are no 8-bit 3-address imul/mul instructions
952 setOperationAction(ISD::SMULO, MVT::i8, Expand);
953 setOperationAction(ISD::UMULO, MVT::i8, Expand);
Bill Wendling41ea7e72008-11-24 19:21:46 +0000954
Evan Chengd54f2d52009-03-31 19:38:51 +0000955 if (!Subtarget->is64Bit()) {
956 // These libcalls are not available in 32-bit.
957 setLibcallName(RTLIB::SHL_I128, 0);
958 setLibcallName(RTLIB::SRL_I128, 0);
959 setLibcallName(RTLIB::SRA_I128, 0);
960 }
961
Evan Cheng206ee9d2006-07-07 08:33:52 +0000962 // We have target-specific dag combine patterns for the following nodes:
963 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Dan Gohman1bbf72b2010-03-15 23:23:03 +0000964 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Evan Chengd880b972008-05-09 21:53:03 +0000965 setTargetDAGCombine(ISD::BUILD_VECTOR);
Chris Lattner83e6c992006-10-04 06:57:07 +0000966 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +0000967 setTargetDAGCombine(ISD::SHL);
968 setTargetDAGCombine(ISD::SRA);
969 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +0000970 setTargetDAGCombine(ISD::OR);
Nate Begemanb65c1752010-12-17 22:55:37 +0000971 setTargetDAGCombine(ISD::AND);
Chris Lattner149a4e52008-02-22 02:09:43 +0000972 setTargetDAGCombine(ISD::STORE);
Evan Cheng2e489c42009-12-16 00:53:11 +0000973 setTargetDAGCombine(ISD::ZERO_EXTEND);
Evan Cheng0b0cd912009-03-28 05:57:29 +0000974 if (Subtarget->is64Bit())
975 setTargetDAGCombine(ISD::MUL);
Evan Cheng206ee9d2006-07-07 08:33:52 +0000976
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000977 computeRegisterProperties();
978
Evan Cheng87ed7162006-02-14 08:25:08 +0000979 // FIXME: These should be based on subtarget info. Plus, the values should
980 // be smaller when we are in optimizing for size mode.
Dan Gohman87060f52008-06-30 21:00:56 +0000981 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
Evan Cheng255f20f2010-04-01 06:04:33 +0000982 maxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
Dan Gohman87060f52008-06-30 21:00:56 +0000983 maxStoresPerMemmove = 3; // For @llvm.memmove -> sequence of stores
Evan Chengfb8075d2008-02-28 00:43:03 +0000984 setPrefLoopAlignment(16);
Evan Cheng6ebf7bc2009-05-13 21:42:09 +0000985 benefitFromCodePlacementOpt = true;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000986}
987
Scott Michel5b8f82e2008-03-10 15:42:14 +0000988
Owen Anderson825b72b2009-08-11 20:47:22 +0000989MVT::SimpleValueType X86TargetLowering::getSetCCResultType(EVT VT) const {
990 return MVT::i8;
Scott Michel5b8f82e2008-03-10 15:42:14 +0000991}
992
993
Evan Cheng29286502008-01-23 23:17:41 +0000994/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
995/// the desired ByVal argument alignment.
996static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
997 if (MaxAlign == 16)
998 return;
999 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
1000 if (VTy->getBitWidth() == 128)
1001 MaxAlign = 16;
Evan Cheng29286502008-01-23 23:17:41 +00001002 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
1003 unsigned EltAlign = 0;
1004 getMaxByValAlign(ATy->getElementType(), EltAlign);
1005 if (EltAlign > MaxAlign)
1006 MaxAlign = EltAlign;
1007 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
1008 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1009 unsigned EltAlign = 0;
1010 getMaxByValAlign(STy->getElementType(i), EltAlign);
1011 if (EltAlign > MaxAlign)
1012 MaxAlign = EltAlign;
1013 if (MaxAlign == 16)
1014 break;
1015 }
1016 }
1017 return;
1018}
1019
1020/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1021/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001022/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1023/// are at 4-byte boundaries.
Evan Cheng29286502008-01-23 23:17:41 +00001024unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001025 if (Subtarget->is64Bit()) {
1026 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001027 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001028 if (TyAlign > 8)
1029 return TyAlign;
1030 return 8;
1031 }
1032
Evan Cheng29286502008-01-23 23:17:41 +00001033 unsigned Align = 4;
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001034 if (Subtarget->hasXMM())
Dale Johannesen0c191872008-02-08 19:48:20 +00001035 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001036 return Align;
1037}
Chris Lattner2b02a442007-02-25 08:29:00 +00001038
Evan Chengf0df0312008-05-15 08:39:06 +00001039/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengc3b0c342010-04-08 07:37:57 +00001040/// and store operations as a result of memset, memcpy, and memmove
1041/// lowering. If DstAlign is zero that means it's safe to destination
1042/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1043/// means there isn't a need to check it against alignment requirement,
1044/// probably because the source does not need to be loaded. If
1045/// 'NonScalarIntSafe' is true, that means it's safe to return a
1046/// non-scalar-integer type, e.g. empty string source, constant, or loaded
1047/// from memory. 'MemcpyStrSrc' indicates whether the memcpy source is
1048/// constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001049/// It returns EVT::Other if the type should be determined using generic
1050/// target-independent logic.
Owen Andersone50ed302009-08-10 22:56:29 +00001051EVT
Evan Cheng255f20f2010-04-01 06:04:33 +00001052X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1053 unsigned DstAlign, unsigned SrcAlign,
Evan Chengf28f8bc2010-04-02 19:36:14 +00001054 bool NonScalarIntSafe,
Evan Chengc3b0c342010-04-08 07:37:57 +00001055 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00001056 MachineFunction &MF) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001057 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
1058 // linux. This is because the stack realignment code can't handle certain
1059 // cases like PR2962. This should be removed when PR2962 is fixed.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001060 const Function *F = MF.getFunction();
Evan Chengf28f8bc2010-04-02 19:36:14 +00001061 if (NonScalarIntSafe &&
1062 !F->hasFnAttr(Attribute::NoImplicitFloat)) {
Evan Cheng255f20f2010-04-01 06:04:33 +00001063 if (Size >= 16 &&
1064 (Subtarget->isUnalignedMemAccessFast() ||
Chandler Carruthae1d41c2010-04-02 01:31:24 +00001065 ((DstAlign == 0 || DstAlign >= 16) &&
1066 (SrcAlign == 0 || SrcAlign >= 16))) &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001067 Subtarget->getStackAlignment() >= 16) {
1068 if (Subtarget->hasSSE2())
1069 return MVT::v4i32;
Evan Chengf28f8bc2010-04-02 19:36:14 +00001070 if (Subtarget->hasSSE1())
Evan Cheng255f20f2010-04-01 06:04:33 +00001071 return MVT::v4f32;
Evan Chengc3b0c342010-04-08 07:37:57 +00001072 } else if (!MemcpyStrSrc && Size >= 8 &&
Evan Cheng3ea97552010-04-01 20:27:45 +00001073 !Subtarget->is64Bit() &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001074 Subtarget->getStackAlignment() >= 8 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001075 Subtarget->hasXMMInt()) {
Evan Chengc3b0c342010-04-08 07:37:57 +00001076 // Do not use f64 to lower memcpy if source is string constant. It's
1077 // better to use i32 to avoid the loads.
Evan Cheng255f20f2010-04-01 06:04:33 +00001078 return MVT::f64;
Evan Chengc3b0c342010-04-08 07:37:57 +00001079 }
Chris Lattner4002a1b2008-10-28 05:49:35 +00001080 }
Evan Chengf0df0312008-05-15 08:39:06 +00001081 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001082 return MVT::i64;
1083 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001084}
1085
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001086/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1087/// current function. The returned value is a member of the
1088/// MachineJumpTableInfo::JTEntryKind enum.
1089unsigned X86TargetLowering::getJumpTableEncoding() const {
1090 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1091 // symbol.
1092 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1093 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001094 return MachineJumpTableInfo::EK_Custom32;
Michael J. Spencerec38de22010-10-10 22:04:20 +00001095
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001096 // Otherwise, use the normal jump table encoding heuristics.
1097 return TargetLowering::getJumpTableEncoding();
1098}
1099
Chris Lattnerc64daab2010-01-26 05:02:42 +00001100const MCExpr *
1101X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1102 const MachineBasicBlock *MBB,
1103 unsigned uid,MCContext &Ctx) const{
1104 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1105 Subtarget->isPICStyleGOT());
1106 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1107 // entries.
Daniel Dunbar4e815f82010-03-15 23:51:06 +00001108 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1109 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001110}
1111
Evan Chengcc415862007-11-09 01:32:10 +00001112/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1113/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001114SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001115 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001116 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001117 // This doesn't have DebugLoc associated with it, but is not really the
1118 // same as a Register.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001119 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001120 return Table;
1121}
1122
Chris Lattner589c6f62010-01-26 06:28:43 +00001123/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1124/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1125/// MCExpr.
1126const MCExpr *X86TargetLowering::
1127getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1128 MCContext &Ctx) const {
1129 // X86-64 uses RIP relative addressing based on the jump table label.
1130 if (Subtarget->isPICStyleRIPRel())
1131 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1132
1133 // Otherwise, the reference is relative to the PIC base.
Chris Lattner142b5312010-11-14 22:48:15 +00001134 return MCSymbolRefExpr::Create(MF->getPICBaseSymbol(), Ctx);
Chris Lattner589c6f62010-01-26 06:28:43 +00001135}
1136
Bill Wendlingb4202b82009-07-01 18:50:55 +00001137/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +00001138unsigned X86TargetLowering::getFunctionAlignment(const Function *F) const {
Dan Gohman25103a22009-08-18 00:20:06 +00001139 return F->hasFnAttr(Attribute::OptimizeForSize) ? 0 : 4;
Bill Wendling20c568f2009-06-30 22:38:32 +00001140}
1141
Evan Chengdee81012010-07-26 21:50:05 +00001142std::pair<const TargetRegisterClass*, uint8_t>
1143X86TargetLowering::findRepresentativeClass(EVT VT) const{
1144 const TargetRegisterClass *RRC = 0;
1145 uint8_t Cost = 1;
1146 switch (VT.getSimpleVT().SimpleTy) {
1147 default:
1148 return TargetLowering::findRepresentativeClass(VT);
1149 case MVT::i8: case MVT::i16: case MVT::i32: case MVT::i64:
1150 RRC = (Subtarget->is64Bit()
1151 ? X86::GR64RegisterClass : X86::GR32RegisterClass);
1152 break;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001153 case MVT::x86mmx:
Evan Chengdee81012010-07-26 21:50:05 +00001154 RRC = X86::VR64RegisterClass;
1155 break;
1156 case MVT::f32: case MVT::f64:
1157 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1158 case MVT::v4f32: case MVT::v2f64:
1159 case MVT::v32i8: case MVT::v8i32: case MVT::v4i64: case MVT::v8f32:
1160 case MVT::v4f64:
1161 RRC = X86::VR128RegisterClass;
1162 break;
1163 }
1164 return std::make_pair(RRC, Cost);
1165}
1166
Evan Cheng70017e42010-07-24 00:39:05 +00001167unsigned
1168X86TargetLowering::getRegPressureLimit(const TargetRegisterClass *RC,
1169 MachineFunction &MF) const {
Anton Korobeynikovd0c38172010-11-18 21:19:35 +00001170 const TargetFrameInfo *TFI = MF.getTarget().getFrameInfo();
1171
1172 unsigned FPDiff = TFI->hasFP(MF) ? 1 : 0;
Evan Cheng70017e42010-07-24 00:39:05 +00001173 switch (RC->getID()) {
1174 default:
1175 return 0;
1176 case X86::GR32RegClassID:
1177 return 4 - FPDiff;
1178 case X86::GR64RegClassID:
1179 return 8 - FPDiff;
1180 case X86::VR128RegClassID:
1181 return Subtarget->is64Bit() ? 10 : 4;
1182 case X86::VR64RegClassID:
1183 return 4;
1184 }
1185}
1186
Eric Christopherf7a0c7b2010-07-06 05:18:56 +00001187bool X86TargetLowering::getStackCookieLocation(unsigned &AddressSpace,
1188 unsigned &Offset) const {
1189 if (!Subtarget->isTargetLinux())
1190 return false;
1191
1192 if (Subtarget->is64Bit()) {
1193 // %fs:0x28, unless we're using a Kernel code model, in which case it's %gs:
1194 Offset = 0x28;
1195 if (getTargetMachine().getCodeModel() == CodeModel::Kernel)
1196 AddressSpace = 256;
1197 else
1198 AddressSpace = 257;
1199 } else {
1200 // %gs:0x14 on i386
1201 Offset = 0x14;
1202 AddressSpace = 256;
1203 }
1204 return true;
1205}
1206
1207
Chris Lattner2b02a442007-02-25 08:29:00 +00001208//===----------------------------------------------------------------------===//
1209// Return Value Calling Convention Implementation
1210//===----------------------------------------------------------------------===//
1211
Chris Lattner59ed56b2007-02-28 04:55:35 +00001212#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001213
Michael J. Spencerec38de22010-10-10 22:04:20 +00001214bool
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001215X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv, bool isVarArg,
Dan Gohman84023e02010-07-10 09:00:22 +00001216 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001217 LLVMContext &Context) const {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001218 SmallVector<CCValAssign, 16> RVLocs;
1219 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001220 RVLocs, Context);
Dan Gohman84023e02010-07-10 09:00:22 +00001221 return CCInfo.CheckReturn(Outs, RetCC_X86);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001222}
1223
Dan Gohman98ca4f22009-08-05 01:29:28 +00001224SDValue
1225X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001226 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001227 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001228 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001229 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001230 MachineFunction &MF = DAG.getMachineFunction();
1231 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001232
Chris Lattner9774c912007-02-27 05:28:59 +00001233 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001234 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1235 RVLocs, *DAG.getContext());
1236 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001237
Evan Chengdcea1632010-02-04 02:40:39 +00001238 // Add the regs to the liveout set for the function.
1239 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1240 for (unsigned i = 0; i != RVLocs.size(); ++i)
1241 if (RVLocs[i].isRegLoc() && !MRI.isLiveOut(RVLocs[i].getLocReg()))
1242 MRI.addLiveOut(RVLocs[i].getLocReg());
Scott Michelfdc40a02009-02-17 22:15:04 +00001243
Dan Gohman475871a2008-07-27 21:46:04 +00001244 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001245
Dan Gohman475871a2008-07-27 21:46:04 +00001246 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001247 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1248 // Operand #1 = Bytes To Pop
Dan Gohman1e93df62010-04-17 14:41:14 +00001249 RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1250 MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001251
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001252 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001253 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1254 CCValAssign &VA = RVLocs[i];
1255 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohmanc9403652010-07-07 15:54:55 +00001256 SDValue ValToCopy = OutVals[i];
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001257 EVT ValVT = ValToCopy.getValueType();
1258
Dale Johannesenc4510512010-09-24 19:05:48 +00001259 // If this is x86-64, and we disabled SSE, we can't return FP values,
1260 // or SSE or MMX vectors.
1261 if ((ValVT == MVT::f32 || ValVT == MVT::f64 ||
1262 VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001263 (Subtarget->is64Bit() && !Subtarget->hasXMM())) {
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001264 report_fatal_error("SSE register return with SSE disabled");
1265 }
1266 // Likewise we can't return F64 values with SSE1 only. gcc does so, but
1267 // llvm-gcc has never done it right and no one has noticed, so this
1268 // should be OK for now.
1269 if (ValVT == MVT::f64 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001270 (Subtarget->is64Bit() && !Subtarget->hasXMMInt()))
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001271 report_fatal_error("SSE2 register return with SSE2 disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001272
Chris Lattner447ff682008-03-11 03:23:40 +00001273 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1274 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001275 if (VA.getLocReg() == X86::ST0 ||
1276 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001277 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1278 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001279 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001280 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001281 RetOps.push_back(ValToCopy);
1282 // Don't emit a copytoreg.
1283 continue;
1284 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001285
Evan Cheng242b38b2009-02-23 09:03:22 +00001286 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1287 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001288 if (Subtarget->is64Bit()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001289 if (ValVT == MVT::x86mmx) {
Chris Lattner97a2a562010-08-26 05:24:29 +00001290 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001291 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ValToCopy);
Eric Christopher90eb4022010-07-22 00:26:08 +00001292 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
1293 ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001294 // If we don't have SSE2 available, convert to v4f32 so the generated
1295 // register is legal.
1296 if (!Subtarget->hasSSE2())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001297 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32,ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001298 }
Evan Cheng242b38b2009-02-23 09:03:22 +00001299 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001300 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00001301
Dale Johannesendd64c412009-02-04 00:33:20 +00001302 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001303 Flag = Chain.getValue(1);
1304 }
Dan Gohman61a92132008-04-21 23:59:07 +00001305
1306 // The x86-64 ABI for returning structs by value requires that we copy
1307 // the sret argument into %rax for the return. We saved the argument into
1308 // a virtual register in the entry block, so now we copy the value out
1309 // and into %rax.
1310 if (Subtarget->is64Bit() &&
1311 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1312 MachineFunction &MF = DAG.getMachineFunction();
1313 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1314 unsigned Reg = FuncInfo->getSRetReturnReg();
Michael J. Spencerec38de22010-10-10 22:04:20 +00001315 assert(Reg &&
Zhongxing Xuc2798a12010-05-26 08:10:02 +00001316 "SRetReturnReg should have been set in LowerFormalArguments().");
Dale Johannesendd64c412009-02-04 00:33:20 +00001317 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001318
Dale Johannesendd64c412009-02-04 00:33:20 +00001319 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001320 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001321
1322 // RAX now acts like a return value.
Evan Chengdcea1632010-02-04 02:40:39 +00001323 MRI.addLiveOut(X86::RAX);
Dan Gohman61a92132008-04-21 23:59:07 +00001324 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001325
Chris Lattner447ff682008-03-11 03:23:40 +00001326 RetOps[0] = Chain; // Update chain.
1327
1328 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001329 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001330 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001331
1332 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001333 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001334}
1335
Evan Cheng3d2125c2010-11-30 23:55:39 +00001336bool X86TargetLowering::isUsedByReturnOnly(SDNode *N) const {
1337 if (N->getNumValues() != 1)
1338 return false;
1339 if (!N->hasNUsesOfValue(1, 0))
1340 return false;
1341
1342 SDNode *Copy = *N->use_begin();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001343 if (Copy->getOpcode() != ISD::CopyToReg &&
1344 Copy->getOpcode() != ISD::FP_EXTEND)
Evan Cheng3d2125c2010-11-30 23:55:39 +00001345 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001346
1347 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001348 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001349 UI != UE; ++UI) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001350 if (UI->getOpcode() != X86ISD::RET_FLAG)
1351 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001352 HasRet = true;
1353 }
Evan Cheng3d2125c2010-11-30 23:55:39 +00001354
Evan Cheng1bf891a2010-12-01 22:59:46 +00001355 return HasRet;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001356}
1357
Dan Gohman98ca4f22009-08-05 01:29:28 +00001358/// LowerCallResult - Lower the result values of a call into the
1359/// appropriate copies out of appropriate physical registers.
1360///
1361SDValue
1362X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001363 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001364 const SmallVectorImpl<ISD::InputArg> &Ins,
1365 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001366 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001367
Chris Lattnere32bbf62007-02-28 07:09:55 +00001368 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001369 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001370 bool Is64Bit = Subtarget->is64Bit();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001371 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +00001372 RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001373 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001374
Chris Lattner3085e152007-02-25 08:59:22 +00001375 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001376 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001377 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001378 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001379
Torok Edwin3f142c32009-02-01 18:15:56 +00001380 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001381 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001382 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasXMM())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001383 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001384 }
1385
Evan Cheng79fb3b42009-02-20 20:43:02 +00001386 SDValue Val;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001387
1388 // If this is a call to a function that returns an fp value on the floating
1389 // point stack, we must guarantee the the value is popped from the stack, so
1390 // a CopyFromReg is not good enough - the copy instruction may be eliminated
1391 // if the return value is not used. We use the FpGET_ST0 instructions
1392 // instead.
1393 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1) {
1394 // If we prefer to use the value in xmm registers, copy it out as f80 and
1395 // use a truncate to move it from fp stack reg to xmm reg.
1396 if (isScalarFPTypeInSSEReg(VA.getValVT())) CopyVT = MVT::f80;
1397 bool isST0 = VA.getLocReg() == X86::ST0;
1398 unsigned Opc = 0;
1399 if (CopyVT == MVT::f32) Opc = isST0 ? X86::FpGET_ST0_32:X86::FpGET_ST1_32;
1400 if (CopyVT == MVT::f64) Opc = isST0 ? X86::FpGET_ST0_64:X86::FpGET_ST1_64;
1401 if (CopyVT == MVT::f80) Opc = isST0 ? X86::FpGET_ST0_80:X86::FpGET_ST1_80;
1402 SDValue Ops[] = { Chain, InFlag };
1403 Chain = SDValue(DAG.getMachineNode(Opc, dl, CopyVT, MVT::Other, MVT::Flag,
1404 Ops, 2), 1);
1405 Val = Chain.getValue(0);
1406
1407 // Round the f80 to the right size, which also moves it to the appropriate
1408 // xmm register.
1409 if (CopyVT != VA.getValVT())
1410 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
1411 // This truncation won't change the value.
1412 DAG.getIntPtrConstant(1));
1413 } else if (Is64Bit && CopyVT.isVector() && CopyVT.getSizeInBits() == 64) {
Evan Cheng242b38b2009-02-23 09:03:22 +00001414 // For x86-64, MMX values are returned in XMM0 / XMM1 except for v1i64.
1415 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
1416 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001417 MVT::v2i64, InFlag).getValue(1);
Evan Cheng242b38b2009-02-23 09:03:22 +00001418 Val = Chain.getValue(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00001419 Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64,
1420 Val, DAG.getConstant(0, MVT::i64));
Evan Cheng242b38b2009-02-23 09:03:22 +00001421 } else {
1422 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001423 MVT::i64, InFlag).getValue(1);
Evan Cheng242b38b2009-02-23 09:03:22 +00001424 Val = Chain.getValue(0);
1425 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001426 Val = DAG.getNode(ISD::BITCAST, dl, CopyVT, Val);
Evan Cheng79fb3b42009-02-20 20:43:02 +00001427 } else {
1428 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1429 CopyVT, InFlag).getValue(1);
1430 Val = Chain.getValue(0);
1431 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001432 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001433 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001434 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001435
Dan Gohman98ca4f22009-08-05 01:29:28 +00001436 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001437}
1438
1439
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001440//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001441// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001442//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001443// StdCall calling convention seems to be standard for many Windows' API
1444// routines and around. It differs from C calling convention just a little:
1445// callee should clean up the stack, not caller. Symbols should be also
1446// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001447// For info on fast calling convention see Fast Calling Convention (tail call)
1448// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001449
Dan Gohman98ca4f22009-08-05 01:29:28 +00001450/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001451/// semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001452static bool CallIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
1453 if (Outs.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001454 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001455
Dan Gohman98ca4f22009-08-05 01:29:28 +00001456 return Outs[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001457}
1458
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001459/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001460/// return semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001461static bool
1462ArgsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
1463 if (Ins.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001464 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001465
Dan Gohman98ca4f22009-08-05 01:29:28 +00001466 return Ins[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001467}
1468
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001469/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1470/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001471/// the specific parameter attribute. The copy will be passed as a byval
1472/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001473static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001474CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001475 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1476 DebugLoc dl) {
Chris Lattnere72f2022010-09-21 05:40:29 +00001477 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Michael J. Spencerec38de22010-10-10 22:04:20 +00001478
Dale Johannesendd64c412009-02-04 00:33:20 +00001479 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Mon P Wang20adc9d2010-04-04 03:10:48 +00001480 /*isVolatile*/false, /*AlwaysInline=*/true,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001481 MachinePointerInfo(), MachinePointerInfo());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001482}
1483
Chris Lattner29689432010-03-11 00:22:57 +00001484/// IsTailCallConvention - Return true if the calling convention is one that
1485/// supports tail call optimization.
1486static bool IsTailCallConvention(CallingConv::ID CC) {
1487 return (CC == CallingConv::Fast || CC == CallingConv::GHC);
1488}
1489
Evan Cheng0c439eb2010-01-27 00:07:07 +00001490/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1491/// a tailcall target by changing its ABI.
1492static bool FuncIsMadeTailCallSafe(CallingConv::ID CC) {
Chris Lattner29689432010-03-11 00:22:57 +00001493 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001494}
1495
Dan Gohman98ca4f22009-08-05 01:29:28 +00001496SDValue
1497X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001498 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001499 const SmallVectorImpl<ISD::InputArg> &Ins,
1500 DebugLoc dl, SelectionDAG &DAG,
1501 const CCValAssign &VA,
1502 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00001503 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00001504 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001505 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Evan Cheng0c439eb2010-01-27 00:07:07 +00001506 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001507 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001508 EVT ValVT;
1509
1510 // If value is passed by pointer we have address passed instead of the value
1511 // itself.
1512 if (VA.getLocInfo() == CCValAssign::Indirect)
1513 ValVT = VA.getLocVT();
1514 else
1515 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001516
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001517 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001518 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001519 // In case of tail call optimization mark all arguments mutable. Since they
1520 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001521 if (Flags.isByVal()) {
1522 int FI = MFI->CreateFixedObject(Flags.getByValSize(),
Evan Chenged2ae132010-07-03 00:40:23 +00001523 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001524 return DAG.getFrameIndex(FI, getPointerTy());
1525 } else {
1526 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00001527 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001528 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1529 return DAG.getLoad(ValVT, dl, Chain, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001530 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00001531 false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001532 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001533}
1534
Dan Gohman475871a2008-07-27 21:46:04 +00001535SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001536X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001537 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001538 bool isVarArg,
1539 const SmallVectorImpl<ISD::InputArg> &Ins,
1540 DebugLoc dl,
1541 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001542 SmallVectorImpl<SDValue> &InVals)
1543 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00001544 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001545 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001546
Gordon Henriksen86737662008-01-05 16:56:59 +00001547 const Function* Fn = MF.getFunction();
1548 if (Fn->hasExternalLinkage() &&
1549 Subtarget->isTargetCygMing() &&
1550 Fn->getName() == "main")
1551 FuncInfo->setForceFramePointer(true);
1552
Evan Cheng1bc78042006-04-26 01:20:17 +00001553 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001554 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001555 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001556
Chris Lattner29689432010-03-11 00:22:57 +00001557 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1558 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001559
Chris Lattner638402b2007-02-28 07:00:42 +00001560 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001561 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001562 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1563 ArgLocs, *DAG.getContext());
Duncan Sands45907662010-10-31 13:21:44 +00001564 CCInfo.AnalyzeFormalArguments(Ins, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001565
Chris Lattnerf39f7712007-02-28 05:46:49 +00001566 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001567 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001568 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1569 CCValAssign &VA = ArgLocs[i];
1570 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1571 // places.
1572 assert(VA.getValNo() != LastVal &&
1573 "Don't support value assigned to multiple locs yet");
1574 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001575
Chris Lattnerf39f7712007-02-28 05:46:49 +00001576 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001577 EVT RegVT = VA.getLocVT();
Devang Patel8a84e442009-01-05 17:31:22 +00001578 TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001579 if (RegVT == MVT::i32)
Chris Lattnerf39f7712007-02-28 05:46:49 +00001580 RC = X86::GR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001581 else if (Is64Bit && RegVT == MVT::i64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001582 RC = X86::GR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001583 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001584 RC = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001585 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001586 RC = X86::FR64RegisterClass;
Bruno Cardoso Lopesac098352010-08-05 23:35:51 +00001587 else if (RegVT.isVector() && RegVT.getSizeInBits() == 256)
1588 RC = X86::VR256RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001589 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001590 RC = X86::VR128RegisterClass;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001591 else if (RegVT == MVT::x86mmx)
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001592 RC = X86::VR64RegisterClass;
1593 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001594 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001595
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001596 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001597 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001598
Chris Lattnerf39f7712007-02-28 05:46:49 +00001599 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1600 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1601 // right size.
1602 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001603 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001604 DAG.getValueType(VA.getValVT()));
1605 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001606 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001607 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001608 else if (VA.getLocInfo() == CCValAssign::BCvt)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001609 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001610
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001611 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001612 // Handle MMX values passed in XMM regs.
1613 if (RegVT.isVector()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001614 ArgValue = DAG.getNode(X86ISD::MOVDQ2Q, dl, VA.getValVT(),
1615 ArgValue);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001616 } else
1617 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001618 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001619 } else {
1620 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001621 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001622 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001623
1624 // If value is passed via pointer - do a load.
1625 if (VA.getLocInfo() == CCValAssign::Indirect)
Chris Lattner51abfe42010-09-21 06:02:19 +00001626 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue,
1627 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001628
Dan Gohman98ca4f22009-08-05 01:29:28 +00001629 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001630 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001631
Dan Gohman61a92132008-04-21 23:59:07 +00001632 // The x86-64 ABI for returning structs by value requires that we copy
1633 // the sret argument into %rax for the return. Save the argument into
1634 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001635 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001636 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1637 unsigned Reg = FuncInfo->getSRetReturnReg();
1638 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001639 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001640 FuncInfo->setSRetReturnReg(Reg);
1641 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001642 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001643 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001644 }
1645
Chris Lattnerf39f7712007-02-28 05:46:49 +00001646 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00001647 // Align stack specially for tail calls.
1648 if (FuncIsMadeTailCallSafe(CallConv))
Gordon Henriksenae636f82008-01-03 16:47:34 +00001649 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001650
Evan Cheng1bc78042006-04-26 01:20:17 +00001651 // If the function takes variable number of arguments, make a frame index for
1652 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001653 if (isVarArg) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001654 if (!IsWin64 && (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
1655 CallConv != CallingConv::X86_ThisCall))) {
Jakob Stoklund Olesenb2eeed72010-07-29 17:42:27 +00001656 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,true));
Gordon Henriksen86737662008-01-05 16:56:59 +00001657 }
1658 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001659 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1660
1661 // FIXME: We should really autogenerate these arrays
1662 static const unsigned GPR64ArgRegsWin64[] = {
1663 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001664 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001665 static const unsigned GPR64ArgRegs64Bit[] = {
1666 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1667 };
1668 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001669 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1670 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1671 };
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001672 const unsigned *GPR64ArgRegs;
1673 unsigned NumXMMRegs = 0;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001674
1675 if (IsWin64) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001676 // The XMM registers which might contain var arg parameters are shadowed
1677 // in their paired GPR. So we only need to save the GPR to their home
1678 // slots.
1679 TotalNumIntRegs = 4;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001680 GPR64ArgRegs = GPR64ArgRegsWin64;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001681 } else {
1682 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1683 GPR64ArgRegs = GPR64ArgRegs64Bit;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001684
1685 NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs64Bit, TotalNumXMMRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001686 }
1687 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1688 TotalNumIntRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001689
Devang Patel578efa92009-06-05 21:57:13 +00001690 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001691 assert(!(NumXMMRegs && !Subtarget->hasXMM()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001692 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001693 assert(!(NumXMMRegs && UseSoftFloat && NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00001694 "SSE register cannot be used when SSE is disabled!");
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001695 if (UseSoftFloat || NoImplicitFloatOps || !Subtarget->hasXMM())
Torok Edwin3f142c32009-02-01 18:15:56 +00001696 // Kernel mode asks for SSE to be disabled, so don't push them
1697 // on the stack.
1698 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001699
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001700 if (IsWin64) {
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001701 const TargetFrameInfo &TFI = *getTargetMachine().getFrameInfo();
1702 // Get to the caller-allocated home save location. Add 8 to account
1703 // for the return address.
1704 int HomeOffset = TFI.getOffsetOfLocalArea() + 8;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001705 FuncInfo->setRegSaveFrameIndex(
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001706 MFI->CreateFixedObject(1, NumIntRegs * 8 + HomeOffset, false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001707 FuncInfo->setVarArgsFrameIndex(FuncInfo->getRegSaveFrameIndex());
1708 } else {
1709 // For X86-64, if there are vararg parameters that are passed via
1710 // registers, then we must store them to their spots on the stack so they
1711 // may be loaded by deferencing the result of va_next.
1712 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
1713 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
1714 FuncInfo->setRegSaveFrameIndex(
1715 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
Dan Gohman1e93df62010-04-17 14:41:14 +00001716 false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001717 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001718
Gordon Henriksen86737662008-01-05 16:56:59 +00001719 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001720 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00001721 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
1722 getPointerTy());
1723 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001724 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00001725 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
1726 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00001727 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
1728 X86::GR64RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00001729 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00001730 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001731 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001732 MachinePointerInfo::getFixedStack(
1733 FuncInfo->getRegSaveFrameIndex(), Offset),
1734 false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001735 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001736 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001737 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001738
Dan Gohmanface41a2009-08-16 21:24:25 +00001739 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
1740 // Now store the XMM (fp + vector) parameter registers.
1741 SmallVector<SDValue, 11> SaveXMMOps;
1742 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001743
Dan Gohmanface41a2009-08-16 21:24:25 +00001744 unsigned AL = MF.addLiveIn(X86::AL, X86::GR8RegisterClass);
1745 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
1746 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001747
Dan Gohman1e93df62010-04-17 14:41:14 +00001748 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1749 FuncInfo->getRegSaveFrameIndex()));
1750 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1751 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00001752
Dan Gohmanface41a2009-08-16 21:24:25 +00001753 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001754 unsigned VReg = MF.addLiveIn(XMMArgRegs64Bit[NumXMMRegs],
Dan Gohmanface41a2009-08-16 21:24:25 +00001755 X86::VR128RegisterClass);
1756 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
1757 SaveXMMOps.push_back(Val);
1758 }
1759 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
1760 MVT::Other,
1761 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00001762 }
Dan Gohmanface41a2009-08-16 21:24:25 +00001763
1764 if (!MemOps.empty())
1765 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1766 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001767 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001768 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001769
Gordon Henriksen86737662008-01-05 16:56:59 +00001770 // Some CCs need callee pop.
Dan Gohman4d3d6e12010-05-27 18:43:40 +00001771 if (Subtarget->IsCalleePop(isVarArg, CallConv)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001772 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001773 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00001774 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001775 // If this is an sret function, the return should pop the hidden pointer.
Chris Lattner29689432010-03-11 00:22:57 +00001776 if (!Is64Bit && !IsTailCallConvention(CallConv) && ArgsAreStructReturn(Ins))
Dan Gohman1e93df62010-04-17 14:41:14 +00001777 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001778 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001779
Gordon Henriksen86737662008-01-05 16:56:59 +00001780 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001781 // RegSaveFrameIndex is X86-64 only.
1782 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Anton Korobeynikovded05e32010-05-16 09:08:45 +00001783 if (CallConv == CallingConv::X86_FastCall ||
1784 CallConv == CallingConv::X86_ThisCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00001785 // fastcc functions can't have varargs.
1786 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00001787 }
Evan Cheng25caf632006-05-23 21:06:34 +00001788
Dan Gohman98ca4f22009-08-05 01:29:28 +00001789 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001790}
1791
Dan Gohman475871a2008-07-27 21:46:04 +00001792SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001793X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
1794 SDValue StackPtr, SDValue Arg,
1795 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00001796 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001797 ISD::ArgFlagsTy Flags) const {
Anton Korobeynikovc7c62bb2010-09-02 22:31:32 +00001798 const unsigned FirstStackArgOffset = (Subtarget->isTargetWin64() ? 32 : 0);
1799 unsigned LocMemOffset = FirstStackArgOffset + VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00001800 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00001801 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001802 if (Flags.isByVal())
Dale Johannesendd64c412009-02-04 00:33:20 +00001803 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001804
1805 return DAG.getStore(Chain, dl, Arg, PtrOff,
1806 MachinePointerInfo::getStack(LocMemOffset),
David Greene67c9d422010-02-15 16:53:33 +00001807 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00001808}
1809
Bill Wendling64e87322009-01-16 19:25:27 +00001810/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001811/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00001812SDValue
1813X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00001814 SDValue &OutRetAddr, SDValue Chain,
1815 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00001816 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001817 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00001818 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001819 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00001820
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001821 // Load the "old" Return address.
Chris Lattner51abfe42010-09-21 06:02:19 +00001822 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, MachinePointerInfo(),
1823 false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001824 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001825}
1826
1827/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1828/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00001829static SDValue
1830EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00001831 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00001832 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001833 // Store the return address to the appropriate stack slot.
1834 if (!FPDiff) return Chain;
1835 // Calculate the new stack slot for the return address.
1836 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00001837 int NewReturnAddrFI =
Evan Chenged2ae132010-07-03 00:40:23 +00001838 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false);
Owen Anderson825b72b2009-08-11 20:47:22 +00001839 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00001840 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001841 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00001842 MachinePointerInfo::getFixedStack(NewReturnAddrFI),
David Greene67c9d422010-02-15 16:53:33 +00001843 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001844 return Chain;
1845}
1846
Dan Gohman98ca4f22009-08-05 01:29:28 +00001847SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001848X86TargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001849 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001850 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001851 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001852 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001853 const SmallVectorImpl<ISD::InputArg> &Ins,
1854 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001855 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001856 MachineFunction &MF = DAG.getMachineFunction();
1857 bool Is64Bit = Subtarget->is64Bit();
1858 bool IsStructRet = CallIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00001859 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001860
Evan Cheng5f941932010-02-05 02:21:12 +00001861 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00001862 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00001863 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1864 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001865 Outs, OutVals, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00001866
1867 // Sibcalls are automatically detected tailcalls which do not require
1868 // ABI changes.
Dan Gohman1797ed52010-02-08 20:27:50 +00001869 if (!GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00001870 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00001871
1872 if (isTailCall)
1873 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00001874 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00001875
Chris Lattner29689432010-03-11 00:22:57 +00001876 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1877 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001878
Chris Lattner638402b2007-02-28 07:00:42 +00001879 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00001880 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001881 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1882 ArgLocs, *DAG.getContext());
Duncan Sands45907662010-10-31 13:21:44 +00001883 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001884
Chris Lattner423c5f42007-02-28 05:31:48 +00001885 // Get a count of how many bytes are to be pushed on the stack.
1886 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00001887 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00001888 // This is a sibcall. The memory operands are available in caller's
1889 // own caller's stack.
1890 NumBytes = 0;
Chris Lattner29689432010-03-11 00:22:57 +00001891 else if (GuaranteedTailCallOpt && IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00001892 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001893
Gordon Henriksen86737662008-01-05 16:56:59 +00001894 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00001895 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001896 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00001897 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00001898 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1899 FPDiff = NumBytesCallerPushed - NumBytes;
1900
1901 // Set the delta of movement of the returnaddr stackslot.
1902 // But only set if delta is greater than previous delta.
1903 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1904 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1905 }
1906
Evan Chengf22f9b32010-02-06 03:28:46 +00001907 if (!IsSibcall)
1908 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001909
Dan Gohman475871a2008-07-27 21:46:04 +00001910 SDValue RetAddrFrIdx;
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001911 // Load return adress for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00001912 if (isTailCall && FPDiff)
1913 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
1914 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00001915
Dan Gohman475871a2008-07-27 21:46:04 +00001916 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
1917 SmallVector<SDValue, 8> MemOpChains;
1918 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00001919
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001920 // Walk the register/memloc assignments, inserting copies/loads. In the case
1921 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00001922 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1923 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001924 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00001925 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00001926 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00001927 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00001928
Chris Lattner423c5f42007-02-28 05:31:48 +00001929 // Promote the value if needed.
1930 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001931 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00001932 case CCValAssign::Full: break;
1933 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001934 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001935 break;
1936 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001937 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001938 break;
1939 case CCValAssign::AExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001940 if (RegVT.isVector() && RegVT.getSizeInBits() == 128) {
1941 // Special case: passing MMX values in XMM registers.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001942 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00001943 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
1944 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001945 } else
1946 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
1947 break;
1948 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001949 Arg = DAG.getNode(ISD::BITCAST, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001950 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001951 case CCValAssign::Indirect: {
1952 // Store the argument.
1953 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00001954 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001955 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00001956 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00001957 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001958 Arg = SpillSlot;
1959 break;
1960 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00001961 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001962
Chris Lattner423c5f42007-02-28 05:31:48 +00001963 if (VA.isRegLoc()) {
1964 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00001965 if (isVarArg && Subtarget->isTargetWin64()) {
1966 // Win64 ABI requires argument XMM reg to be copied to the corresponding
1967 // shadow reg if callee is a varargs function.
1968 unsigned ShadowReg = 0;
1969 switch (VA.getLocReg()) {
1970 case X86::XMM0: ShadowReg = X86::RCX; break;
1971 case X86::XMM1: ShadowReg = X86::RDX; break;
1972 case X86::XMM2: ShadowReg = X86::R8; break;
1973 case X86::XMM3: ShadowReg = X86::R9; break;
1974 }
1975 if (ShadowReg)
1976 RegsToPass.push_back(std::make_pair(ShadowReg, Arg));
1977 }
Evan Chengf22f9b32010-02-06 03:28:46 +00001978 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00001979 assert(VA.isMemLoc());
1980 if (StackPtr.getNode() == 0)
1981 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
1982 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1983 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001984 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001985 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001986
Evan Cheng32fe1032006-05-25 00:59:30 +00001987 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001988 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001989 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001990
Evan Cheng347d5f72006-04-28 21:29:37 +00001991 // Build a sequence of copy-to-reg nodes chained together with token chain
1992 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001993 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001994 // Tail call byval lowering might overwrite argument registers so in case of
1995 // tail call optimization the copies to registers are lowered later.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001996 if (!isTailCall)
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001997 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001998 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00001999 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002000 InFlag = Chain.getValue(1);
2001 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002002
Chris Lattner88e1fd52009-07-09 04:24:46 +00002003 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002004 // ELF / PIC requires GOT in the EBX register before function calls via PLT
2005 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002006 if (!isTailCall) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002007 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
2008 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00002009 DebugLoc(), getPointerTy()),
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002010 InFlag);
2011 InFlag = Chain.getValue(1);
2012 } else {
2013 // If we are tail calling and generating PIC/GOT style code load the
2014 // address of the callee into ECX. The value in ecx is used as target of
2015 // the tail jump. This is done to circumvent the ebx/callee-saved problem
2016 // for tail calls on PIC/GOT architectures. Normally we would just put the
2017 // address of GOT into ebx and then call target@PLT. But for tail calls
2018 // ebx would be restored (since ebx is callee saved) before jumping to the
2019 // target@PLT.
2020
2021 // Note: The actual moving to ECX is done further down.
2022 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
2023 if (G && !G->getGlobal()->hasHiddenVisibility() &&
2024 !G->getGlobal()->hasProtectedVisibility())
2025 Callee = LowerGlobalAddress(Callee, DAG);
2026 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00002027 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002028 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002029 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002030
Nate Begemanc8ea6732010-07-21 20:49:52 +00002031 if (Is64Bit && isVarArg && !Subtarget->isTargetWin64()) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002032 // From AMD64 ABI document:
2033 // For calls that may call functions that use varargs or stdargs
2034 // (prototype-less calls or calls to functions containing ellipsis (...) in
2035 // the declaration) %al is used as hidden argument to specify the number
2036 // of SSE registers used. The contents of %al do not need to match exactly
2037 // the number of registers, but must be an ubound on the number of SSE
2038 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002039
Gordon Henriksen86737662008-01-05 16:56:59 +00002040 // Count the number of XMM registers allocated.
2041 static const unsigned XMMArgRegs[] = {
2042 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2043 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2044 };
2045 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00002046 assert((Subtarget->hasXMM() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00002047 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00002048
Dale Johannesendd64c412009-02-04 00:33:20 +00002049 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Owen Anderson825b72b2009-08-11 20:47:22 +00002050 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002051 InFlag = Chain.getValue(1);
2052 }
2053
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002054
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002055 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002056 if (isTailCall) {
2057 // Force all the incoming stack arguments to be loaded from the stack
2058 // before any new outgoing arguments are stored to the stack, because the
2059 // outgoing stack slots may alias the incoming argument stack slots, and
2060 // the alias isn't otherwise explicit. This is slightly more conservative
2061 // than necessary, because it means that each store effectively depends
2062 // on every argument instead of just those arguments it would clobber.
2063 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2064
Dan Gohman475871a2008-07-27 21:46:04 +00002065 SmallVector<SDValue, 8> MemOpChains2;
2066 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002067 int FI = 0;
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002068 // Do not flag preceeding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00002069 InFlag = SDValue();
Dan Gohman1797ed52010-02-08 20:27:50 +00002070 if (GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002071 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2072 CCValAssign &VA = ArgLocs[i];
2073 if (VA.isRegLoc())
2074 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002075 assert(VA.isMemLoc());
Dan Gohmanc9403652010-07-07 15:54:55 +00002076 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002077 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002078 // Create frame index.
2079 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002080 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002081 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002082 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002083
Duncan Sands276dcbd2008-03-21 09:14:45 +00002084 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002085 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002086 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002087 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00002088 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00002089 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002090 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002091
Dan Gohman98ca4f22009-08-05 01:29:28 +00002092 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2093 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002094 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002095 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002096 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002097 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002098 DAG.getStore(ArgChain, dl, Arg, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002099 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002100 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002101 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002102 }
2103 }
2104
2105 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002106 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002107 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002108
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002109 // Copy arguments to their registers.
2110 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002111 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002112 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002113 InFlag = Chain.getValue(1);
2114 }
Dan Gohman475871a2008-07-27 21:46:04 +00002115 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002116
Gordon Henriksen86737662008-01-05 16:56:59 +00002117 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002118 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00002119 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002120 }
2121
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002122 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2123 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2124 // In the 64-bit large code model, we have to make all calls
2125 // through a register, since the call instruction's 32-bit
2126 // pc-relative offset may not be large enough to hold the whole
2127 // address.
2128 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002129 // If the callee is a GlobalAddress node (quite common, every direct call
2130 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2131 // it.
2132
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002133 // We should use extra load for direct calls to dllimported functions in
2134 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002135 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002136 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002137 unsigned char OpFlags = 0;
Eric Christopherfd179292009-08-27 18:07:15 +00002138
Chris Lattner48a7d022009-07-09 05:02:21 +00002139 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2140 // external symbols most go through the PLT in PIC mode. If the symbol
2141 // has hidden or protected visibility, or if it is static or local, then
2142 // we don't need to use the PLT - we can directly call it.
2143 if (Subtarget->isTargetELF() &&
2144 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002145 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002146 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002147 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner80945782010-09-27 06:34:01 +00002148 (GV->isDeclaration() || GV->isWeakForLinker()) &&
2149 Subtarget->getDarwinVers() < 9) {
Chris Lattner74e726e2009-07-09 05:27:35 +00002150 // PC-relative references to external symbols should go through $stub,
2151 // unless we're building with the leopard linker or later, which
2152 // automatically synthesizes these stubs.
2153 OpFlags = X86II::MO_DARWIN_STUB;
2154 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002155
Devang Patel0d881da2010-07-06 22:08:15 +00002156 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002157 G->getOffset(), OpFlags);
2158 }
Bill Wendling056292f2008-09-16 21:48:12 +00002159 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002160 unsigned char OpFlags = 0;
2161
Evan Cheng1bf891a2010-12-01 22:59:46 +00002162 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to
2163 // external symbols should go through the PLT.
2164 if (Subtarget->isTargetELF() &&
2165 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
2166 OpFlags = X86II::MO_PLT;
2167 } else if (Subtarget->isPICStyleStubAny() &&
2168 Subtarget->getDarwinVers() < 9) {
2169 // PC-relative references to external symbols should go through $stub,
2170 // unless we're building with the leopard linker or later, which
2171 // automatically synthesizes these stubs.
2172 OpFlags = X86II::MO_DARWIN_STUB;
Chris Lattner74e726e2009-07-09 05:27:35 +00002173 }
Eric Christopherfd179292009-08-27 18:07:15 +00002174
Chris Lattner48a7d022009-07-09 05:02:21 +00002175 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2176 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002177 }
2178
Chris Lattnerd96d0722007-02-25 06:40:16 +00002179 // Returns a chain & a flag for retval copy to use.
Owen Anderson825b72b2009-08-11 20:47:22 +00002180 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00002181 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002182
Evan Chengf22f9b32010-02-06 03:28:46 +00002183 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002184 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2185 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002186 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002187 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002188
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002189 Ops.push_back(Chain);
2190 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002191
Dan Gohman98ca4f22009-08-05 01:29:28 +00002192 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002193 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002194
Gordon Henriksen86737662008-01-05 16:56:59 +00002195 // Add argument registers to the end of the list so that they are known live
2196 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002197 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2198 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2199 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002200
Evan Cheng586ccac2008-03-18 23:36:35 +00002201 // Add an implicit use GOT pointer in EBX.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002202 if (!isTailCall && Subtarget->isPICStyleGOT())
Evan Cheng586ccac2008-03-18 23:36:35 +00002203 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
2204
Anton Korobeynikov3a1e54a2010-08-17 21:06:07 +00002205 // Add an implicit use of AL for non-Windows x86 64-bit vararg functions.
2206 if (Is64Bit && isVarArg && !Subtarget->isTargetWin64())
Owen Anderson825b72b2009-08-11 20:47:22 +00002207 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
Evan Cheng586ccac2008-03-18 23:36:35 +00002208
Gabor Greifba36cb52008-08-28 21:40:38 +00002209 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002210 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002211
Dan Gohman98ca4f22009-08-05 01:29:28 +00002212 if (isTailCall) {
Dale Johannesen88004c22010-06-05 00:30:45 +00002213 // We used to do:
2214 //// If this is the first return lowered for this function, add the regs
2215 //// to the liveout set for the function.
2216 // This isn't right, although it's probably harmless on x86; liveouts
2217 // should be computed from returns not tail calls. Consider a void
2218 // function making a tail call to a function returning int.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002219 return DAG.getNode(X86ISD::TC_RETURN, dl,
2220 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002221 }
2222
Dale Johannesenace16102009-02-03 19:33:06 +00002223 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002224 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002225
Chris Lattner2d297092006-05-23 18:50:38 +00002226 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002227 unsigned NumBytesForCalleeToPush;
Dan Gohman4d3d6e12010-05-27 18:43:40 +00002228 if (Subtarget->IsCalleePop(isVarArg, CallConv))
Gordon Henriksen86737662008-01-05 16:56:59 +00002229 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Chris Lattner29689432010-03-11 00:22:57 +00002230 else if (!Is64Bit && !IsTailCallConvention(CallConv) && IsStructRet)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002231 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002232 // pops the hidden struct pointer, so we have to push it back.
2233 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002234 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002235 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002236 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002237
Gordon Henriksenae636f82008-01-03 16:47:34 +00002238 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002239 if (!IsSibcall) {
2240 Chain = DAG.getCALLSEQ_END(Chain,
2241 DAG.getIntPtrConstant(NumBytes, true),
2242 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2243 true),
2244 InFlag);
2245 InFlag = Chain.getValue(1);
2246 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002247
Chris Lattner3085e152007-02-25 08:59:22 +00002248 // Handle result values, copying them out of physregs into vregs that we
2249 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002250 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2251 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002252}
2253
Evan Cheng25ab6902006-09-08 06:48:29 +00002254
2255//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002256// Fast Calling Convention (tail call) implementation
2257//===----------------------------------------------------------------------===//
2258
2259// Like std call, callee cleans arguments, convention except that ECX is
2260// reserved for storing the tail called function address. Only 2 registers are
2261// free for argument passing (inreg). Tail call optimization is performed
2262// provided:
2263// * tailcallopt is enabled
2264// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002265// On X86_64 architecture with GOT-style position independent code only local
2266// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002267// To keep the stack aligned according to platform abi the function
2268// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2269// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002270// If a tail called function callee has more arguments than the caller the
2271// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002272// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002273// original REtADDR, but before the saved framepointer or the spilled registers
2274// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2275// stack layout:
2276// arg1
2277// arg2
2278// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002279// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002280// move area ]
2281// (possible EBP)
2282// ESI
2283// EDI
2284// local1 ..
2285
2286/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2287/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002288unsigned
2289X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2290 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002291 MachineFunction &MF = DAG.getMachineFunction();
2292 const TargetMachine &TM = MF.getTarget();
2293 const TargetFrameInfo &TFI = *TM.getFrameInfo();
2294 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002295 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002296 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002297 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002298 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2299 // Number smaller than 12 so just add the difference.
2300 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2301 } else {
2302 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002303 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002304 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002305 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002306 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002307}
2308
Evan Cheng5f941932010-02-05 02:21:12 +00002309/// MatchingStackOffset - Return true if the given stack call argument is
2310/// already available in the same position (relatively) of the caller's
2311/// incoming argument stack.
2312static
2313bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2314 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2315 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002316 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2317 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002318 if (Arg.getOpcode() == ISD::CopyFromReg) {
2319 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
2320 if (!VR || TargetRegisterInfo::isPhysicalRegister(VR))
2321 return false;
2322 MachineInstr *Def = MRI->getVRegDef(VR);
2323 if (!Def)
2324 return false;
2325 if (!Flags.isByVal()) {
2326 if (!TII->isLoadFromStackSlot(Def, FI))
2327 return false;
2328 } else {
2329 unsigned Opcode = Def->getOpcode();
2330 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2331 Def->getOperand(1).isFI()) {
2332 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002333 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002334 } else
2335 return false;
2336 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002337 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2338 if (Flags.isByVal())
2339 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002340 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002341 // define @foo(%struct.X* %A) {
2342 // tail call @bar(%struct.X* byval %A)
2343 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002344 return false;
2345 SDValue Ptr = Ld->getBasePtr();
2346 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2347 if (!FINode)
2348 return false;
2349 FI = FINode->getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002350 } else
2351 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002352
Evan Cheng4cae1332010-03-05 08:38:04 +00002353 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002354 if (!MFI->isFixedObjectIndex(FI))
2355 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002356 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002357}
2358
Dan Gohman98ca4f22009-08-05 01:29:28 +00002359/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2360/// for tail call optimization. Targets which want to do tail call
2361/// optimization should implement this function.
2362bool
2363X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002364 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002365 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002366 bool isCalleeStructRet,
2367 bool isCallerStructRet,
Evan Chengb1712452010-01-27 06:25:16 +00002368 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002369 const SmallVectorImpl<SDValue> &OutVals,
Evan Chengb1712452010-01-27 06:25:16 +00002370 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002371 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002372 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002373 CalleeCC != CallingConv::C)
2374 return false;
2375
Evan Cheng7096ae42010-01-29 06:45:59 +00002376 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002377 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002378 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Cheng13617962010-04-30 01:12:32 +00002379 CallingConv::ID CallerCC = CallerF->getCallingConv();
2380 bool CCMatch = CallerCC == CalleeCC;
2381
Dan Gohman1797ed52010-02-08 20:27:50 +00002382 if (GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002383 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002384 return true;
2385 return false;
2386 }
2387
Dale Johannesen2f05cc02010-05-28 23:24:28 +00002388 // Look for obvious safe cases to perform tail call optimization that do not
2389 // require ABI changes. This is what gcc calls sibcall.
Evan Chengb2c92902010-02-02 02:22:50 +00002390
Evan Cheng2c12cb42010-03-26 16:26:03 +00002391 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2392 // emit a special epilogue.
2393 if (RegInfo->needsStackRealignment(MF))
2394 return false;
2395
Eric Christopher90eb4022010-07-22 00:26:08 +00002396 // Do not sibcall optimize vararg calls unless the call site is not passing
2397 // any arguments.
Evan Cheng3c262ee2010-03-26 02:13:13 +00002398 if (isVarArg && !Outs.empty())
Evan Cheng843bd692010-01-31 06:44:49 +00002399 return false;
2400
Evan Chenga375d472010-03-15 18:54:48 +00002401 // Also avoid sibcall optimization if either caller or callee uses struct
2402 // return semantics.
2403 if (isCalleeStructRet || isCallerStructRet)
2404 return false;
2405
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002406 // If the call result is in ST0 / ST1, it needs to be popped off the x87 stack.
2407 // Therefore if it's not used by the call it is not safe to optimize this into
2408 // a sibcall.
2409 bool Unused = false;
2410 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2411 if (!Ins[i].Used) {
2412 Unused = true;
2413 break;
2414 }
2415 }
2416 if (Unused) {
2417 SmallVector<CCValAssign, 16> RVLocs;
2418 CCState CCInfo(CalleeCC, false, getTargetMachine(),
2419 RVLocs, *DAG.getContext());
2420 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002421 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002422 CCValAssign &VA = RVLocs[i];
2423 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2424 return false;
2425 }
2426 }
2427
Evan Cheng13617962010-04-30 01:12:32 +00002428 // If the calling conventions do not match, then we'd better make sure the
2429 // results are returned in the same way as what the caller expects.
2430 if (!CCMatch) {
2431 SmallVector<CCValAssign, 16> RVLocs1;
2432 CCState CCInfo1(CalleeCC, false, getTargetMachine(),
2433 RVLocs1, *DAG.getContext());
2434 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2435
2436 SmallVector<CCValAssign, 16> RVLocs2;
2437 CCState CCInfo2(CallerCC, false, getTargetMachine(),
2438 RVLocs2, *DAG.getContext());
2439 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2440
2441 if (RVLocs1.size() != RVLocs2.size())
2442 return false;
2443 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2444 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2445 return false;
2446 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2447 return false;
2448 if (RVLocs1[i].isRegLoc()) {
2449 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2450 return false;
2451 } else {
2452 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2453 return false;
2454 }
2455 }
2456 }
2457
Evan Chenga6bff982010-01-30 01:22:00 +00002458 // If the callee takes no arguments then go on to check the results of the
2459 // call.
2460 if (!Outs.empty()) {
2461 // Check if stack adjustment is needed. For now, do not do this if any
2462 // argument is passed on the stack.
2463 SmallVector<CCValAssign, 16> ArgLocs;
2464 CCState CCInfo(CalleeCC, isVarArg, getTargetMachine(),
2465 ArgLocs, *DAG.getContext());
Duncan Sands45907662010-10-31 13:21:44 +00002466 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Evan Chengb2c92902010-02-02 02:22:50 +00002467 if (CCInfo.getNextStackOffset()) {
2468 MachineFunction &MF = DAG.getMachineFunction();
2469 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2470 return false;
2471 if (Subtarget->isTargetWin64())
2472 // Win64 ABI has additional complications.
2473 return false;
2474
2475 // Check if the arguments are already laid out in the right way as
2476 // the caller's fixed stack objects.
2477 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002478 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2479 const X86InstrInfo *TII =
2480 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002481 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2482 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00002483 SDValue Arg = OutVals[i];
Evan Chengb2c92902010-02-02 02:22:50 +00002484 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002485 if (VA.getLocInfo() == CCValAssign::Indirect)
2486 return false;
2487 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002488 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2489 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002490 return false;
2491 }
2492 }
2493 }
Evan Cheng9c044672010-05-29 01:35:22 +00002494
2495 // If the tailcall address may be in a register, then make sure it's
2496 // possible to register allocate for it. In 32-bit, the call address can
2497 // only target EAX, EDX, or ECX since the tail call must be scheduled after
Evan Chengdedd9742010-07-14 06:44:01 +00002498 // callee-saved registers are restored. These happen to be the same
2499 // registers used to pass 'inreg' arguments so watch out for those.
2500 if (!Subtarget->is64Bit() &&
2501 !isa<GlobalAddressSDNode>(Callee) &&
Evan Cheng9c044672010-05-29 01:35:22 +00002502 !isa<ExternalSymbolSDNode>(Callee)) {
Evan Cheng9c044672010-05-29 01:35:22 +00002503 unsigned NumInRegs = 0;
2504 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2505 CCValAssign &VA = ArgLocs[i];
Evan Chengdedd9742010-07-14 06:44:01 +00002506 if (!VA.isRegLoc())
2507 continue;
2508 unsigned Reg = VA.getLocReg();
2509 switch (Reg) {
2510 default: break;
2511 case X86::EAX: case X86::EDX: case X86::ECX:
2512 if (++NumInRegs == 3)
Evan Cheng9c044672010-05-29 01:35:22 +00002513 return false;
Evan Chengdedd9742010-07-14 06:44:01 +00002514 break;
Evan Cheng9c044672010-05-29 01:35:22 +00002515 }
2516 }
2517 }
Evan Chenga6bff982010-01-30 01:22:00 +00002518 }
Evan Chengb1712452010-01-27 06:25:16 +00002519
Dale Johannesend155d7e2010-10-25 22:17:05 +00002520 // An stdcall caller is expected to clean up its arguments; the callee
Dale Johannesen0e034562010-11-12 00:43:18 +00002521 // isn't going to do that.
Dale Johannesend155d7e2010-10-25 22:17:05 +00002522 if (!CCMatch && CallerCC==CallingConv::X86_StdCall)
2523 return false;
2524
Evan Cheng86809cc2010-02-03 03:28:02 +00002525 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002526}
2527
Dan Gohman3df24e62008-09-03 23:12:08 +00002528FastISel *
Dan Gohmana4160c32010-07-07 16:29:44 +00002529X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
2530 return X86::createFastISel(funcInfo);
Dan Gohmand9f3c482008-08-19 21:32:53 +00002531}
2532
2533
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002534//===----------------------------------------------------------------------===//
2535// Other Lowering Hooks
2536//===----------------------------------------------------------------------===//
2537
Bruno Cardoso Lopese654b562010-09-01 00:51:36 +00002538static bool MayFoldLoad(SDValue Op) {
2539 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
2540}
2541
2542static bool MayFoldIntoStore(SDValue Op) {
2543 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
2544}
2545
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002546static bool isTargetShuffle(unsigned Opcode) {
2547 switch(Opcode) {
2548 default: return false;
2549 case X86ISD::PSHUFD:
2550 case X86ISD::PSHUFHW:
2551 case X86ISD::PSHUFLW:
2552 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002553 case X86ISD::PALIGN:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002554 case X86ISD::SHUFPS:
2555 case X86ISD::MOVLHPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002556 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002557 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002558 case X86ISD::MOVLPS:
2559 case X86ISD::MOVLPD:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002560 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002561 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002562 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002563 case X86ISD::MOVSS:
2564 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002565 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002566 case X86ISD::UNPCKLPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002567 case X86ISD::PUNPCKLWD:
2568 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002569 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002570 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002571 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002572 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002573 case X86ISD::PUNPCKHWD:
2574 case X86ISD::PUNPCKHBW:
2575 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002576 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002577 return true;
2578 }
2579 return false;
2580}
2581
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002582static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002583 SDValue V1, SelectionDAG &DAG) {
2584 switch(Opc) {
2585 default: llvm_unreachable("Unknown x86 shuffle node");
2586 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002587 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002588 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002589 return DAG.getNode(Opc, dl, VT, V1);
2590 }
2591
2592 return SDValue();
2593}
2594
2595static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00002596 SDValue V1, unsigned TargetMask, SelectionDAG &DAG) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002597 switch(Opc) {
2598 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002599 case X86ISD::PSHUFD:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002600 case X86ISD::PSHUFHW:
2601 case X86ISD::PSHUFLW:
2602 return DAG.getNode(Opc, dl, VT, V1, DAG.getConstant(TargetMask, MVT::i8));
2603 }
2604
2605 return SDValue();
2606}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002607
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002608static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2609 SDValue V1, SDValue V2, unsigned TargetMask, SelectionDAG &DAG) {
2610 switch(Opc) {
2611 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002612 case X86ISD::PALIGN:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002613 case X86ISD::SHUFPD:
2614 case X86ISD::SHUFPS:
2615 return DAG.getNode(Opc, dl, VT, V1, V2,
2616 DAG.getConstant(TargetMask, MVT::i8));
2617 }
2618 return SDValue();
2619}
2620
2621static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2622 SDValue V1, SDValue V2, SelectionDAG &DAG) {
2623 switch(Opc) {
2624 default: llvm_unreachable("Unknown x86 shuffle node");
2625 case X86ISD::MOVLHPS:
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00002626 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002627 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002628 case X86ISD::MOVLPS:
2629 case X86ISD::MOVLPD:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002630 case X86ISD::MOVSS:
2631 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002632 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002633 case X86ISD::UNPCKLPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002634 case X86ISD::PUNPCKLWD:
2635 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002636 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002637 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002638 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002639 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002640 case X86ISD::PUNPCKHWD:
2641 case X86ISD::PUNPCKHBW:
2642 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002643 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002644 return DAG.getNode(Opc, dl, VT, V1, V2);
2645 }
2646 return SDValue();
2647}
2648
Dan Gohmand858e902010-04-17 15:26:15 +00002649SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002650 MachineFunction &MF = DAG.getMachineFunction();
2651 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2652 int ReturnAddrIndex = FuncInfo->getRAIndex();
2653
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002654 if (ReturnAddrIndex == 0) {
2655 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002656 uint64_t SlotSize = TD->getPointerSize();
David Greene3f2bf852009-11-12 20:49:22 +00002657 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00002658 false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002659 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002660 }
2661
Evan Cheng25ab6902006-09-08 06:48:29 +00002662 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002663}
2664
2665
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002666bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
2667 bool hasSymbolicDisplacement) {
2668 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00002669 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002670 return false;
2671
2672 // If we don't have a symbolic displacement - we don't have any extra
2673 // restrictions.
2674 if (!hasSymbolicDisplacement)
2675 return true;
2676
2677 // FIXME: Some tweaks might be needed for medium code model.
2678 if (M != CodeModel::Small && M != CodeModel::Kernel)
2679 return false;
2680
2681 // For small code model we assume that latest object is 16MB before end of 31
2682 // bits boundary. We may also accept pretty large negative constants knowing
2683 // that all objects are in the positive half of address space.
2684 if (M == CodeModel::Small && Offset < 16*1024*1024)
2685 return true;
2686
2687 // For kernel code model we know that all object resist in the negative half
2688 // of 32bits address space. We may not accept negative offsets, since they may
2689 // be just off and we may accept pretty large positive ones.
2690 if (M == CodeModel::Kernel && Offset > 0)
2691 return true;
2692
2693 return false;
2694}
2695
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002696/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
2697/// specific condition code, returning the condition code and the LHS/RHS of the
2698/// comparison to make.
2699static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2700 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00002701 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002702 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2703 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2704 // X > -1 -> X == 0, jump !sign.
2705 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002706 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002707 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2708 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002709 return X86::COND_S;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002710 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00002711 // X < 1 -> X <= 0
2712 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002713 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002714 }
Chris Lattnerf9570512006-09-13 03:22:10 +00002715 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002716
Evan Chengd9558e02006-01-06 00:43:03 +00002717 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002718 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002719 case ISD::SETEQ: return X86::COND_E;
2720 case ISD::SETGT: return X86::COND_G;
2721 case ISD::SETGE: return X86::COND_GE;
2722 case ISD::SETLT: return X86::COND_L;
2723 case ISD::SETLE: return X86::COND_LE;
2724 case ISD::SETNE: return X86::COND_NE;
2725 case ISD::SETULT: return X86::COND_B;
2726 case ISD::SETUGT: return X86::COND_A;
2727 case ISD::SETULE: return X86::COND_BE;
2728 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00002729 }
Chris Lattner4c78e022008-12-23 23:42:27 +00002730 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002731
Chris Lattner4c78e022008-12-23 23:42:27 +00002732 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00002733
Chris Lattner4c78e022008-12-23 23:42:27 +00002734 // If LHS is a foldable load, but RHS is not, flip the condition.
2735 if ((ISD::isNON_EXTLoad(LHS.getNode()) && LHS.hasOneUse()) &&
2736 !(ISD::isNON_EXTLoad(RHS.getNode()) && RHS.hasOneUse())) {
2737 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
2738 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00002739 }
2740
Chris Lattner4c78e022008-12-23 23:42:27 +00002741 switch (SetCCOpcode) {
2742 default: break;
2743 case ISD::SETOLT:
2744 case ISD::SETOLE:
2745 case ISD::SETUGT:
2746 case ISD::SETUGE:
2747 std::swap(LHS, RHS);
2748 break;
2749 }
2750
2751 // On a floating point condition, the flags are set as follows:
2752 // ZF PF CF op
2753 // 0 | 0 | 0 | X > Y
2754 // 0 | 0 | 1 | X < Y
2755 // 1 | 0 | 0 | X == Y
2756 // 1 | 1 | 1 | unordered
2757 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002758 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00002759 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002760 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00002761 case ISD::SETOLT: // flipped
2762 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002763 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00002764 case ISD::SETOLE: // flipped
2765 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002766 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002767 case ISD::SETUGT: // flipped
2768 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002769 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00002770 case ISD::SETUGE: // flipped
2771 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002772 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002773 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002774 case ISD::SETNE: return X86::COND_NE;
2775 case ISD::SETUO: return X86::COND_P;
2776 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00002777 case ISD::SETOEQ:
2778 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00002779 }
Evan Chengd9558e02006-01-06 00:43:03 +00002780}
2781
Evan Cheng4a460802006-01-11 00:33:36 +00002782/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2783/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00002784/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00002785static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00002786 switch (X86CC) {
2787 default:
2788 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00002789 case X86::COND_B:
2790 case X86::COND_BE:
2791 case X86::COND_E:
2792 case X86::COND_P:
2793 case X86::COND_A:
2794 case X86::COND_AE:
2795 case X86::COND_NE:
2796 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00002797 return true;
2798 }
2799}
2800
Evan Chengeb2f9692009-10-27 19:56:55 +00002801/// isFPImmLegal - Returns true if the target can instruction select the
2802/// specified FP immediate natively. If false, the legalizer will
2803/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00002804bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00002805 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
2806 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
2807 return true;
2808 }
2809 return false;
2810}
2811
Nate Begeman9008ca62009-04-27 18:41:29 +00002812/// isUndefOrInRange - Return true if Val is undef or if its value falls within
2813/// the specified range (L, H].
2814static bool isUndefOrInRange(int Val, int Low, int Hi) {
2815 return (Val < 0) || (Val >= Low && Val < Hi);
2816}
2817
2818/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
2819/// specified value.
2820static bool isUndefOrEqual(int Val, int CmpVal) {
2821 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00002822 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00002823 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00002824}
2825
Nate Begeman9008ca62009-04-27 18:41:29 +00002826/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
2827/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
2828/// the second operand.
Owen Andersone50ed302009-08-10 22:56:29 +00002829static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00002830 if (VT == MVT::v4f32 || VT == MVT::v4i32 )
Nate Begeman9008ca62009-04-27 18:41:29 +00002831 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002832 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00002833 return (Mask[0] < 2 && Mask[1] < 2);
2834 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002835}
2836
Nate Begeman9008ca62009-04-27 18:41:29 +00002837bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002838 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002839 N->getMask(M);
2840 return ::isPSHUFDMask(M, N->getValueType(0));
2841}
Evan Cheng0188ecb2006-03-22 18:59:22 +00002842
Nate Begeman9008ca62009-04-27 18:41:29 +00002843/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
2844/// is suitable for input to PSHUFHW.
Owen Andersone50ed302009-08-10 22:56:29 +00002845static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002846 if (VT != MVT::v8i16)
Evan Cheng0188ecb2006-03-22 18:59:22 +00002847 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002848
Nate Begeman9008ca62009-04-27 18:41:29 +00002849 // Lower quadword copied in order or undef.
2850 for (int i = 0; i != 4; ++i)
2851 if (Mask[i] >= 0 && Mask[i] != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00002852 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002853
Evan Cheng506d3df2006-03-29 23:07:14 +00002854 // Upper quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002855 for (int i = 4; i != 8; ++i)
2856 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Evan Cheng506d3df2006-03-29 23:07:14 +00002857 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002858
Evan Cheng506d3df2006-03-29 23:07:14 +00002859 return true;
2860}
2861
Nate Begeman9008ca62009-04-27 18:41:29 +00002862bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002863 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002864 N->getMask(M);
2865 return ::isPSHUFHWMask(M, N->getValueType(0));
2866}
Evan Cheng506d3df2006-03-29 23:07:14 +00002867
Nate Begeman9008ca62009-04-27 18:41:29 +00002868/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
2869/// is suitable for input to PSHUFLW.
Owen Andersone50ed302009-08-10 22:56:29 +00002870static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002871 if (VT != MVT::v8i16)
Evan Cheng506d3df2006-03-29 23:07:14 +00002872 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002873
Rafael Espindola15684b22009-04-24 12:40:33 +00002874 // Upper quadword copied in order.
Nate Begeman9008ca62009-04-27 18:41:29 +00002875 for (int i = 4; i != 8; ++i)
2876 if (Mask[i] >= 0 && Mask[i] != i)
Rafael Espindola15684b22009-04-24 12:40:33 +00002877 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002878
Rafael Espindola15684b22009-04-24 12:40:33 +00002879 // Lower quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002880 for (int i = 0; i != 4; ++i)
2881 if (Mask[i] >= 4)
Rafael Espindola15684b22009-04-24 12:40:33 +00002882 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002883
Rafael Espindola15684b22009-04-24 12:40:33 +00002884 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002885}
2886
Nate Begeman9008ca62009-04-27 18:41:29 +00002887bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002888 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002889 N->getMask(M);
2890 return ::isPSHUFLWMask(M, N->getValueType(0));
2891}
2892
Nate Begemana09008b2009-10-19 02:17:23 +00002893/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
2894/// is suitable for input to PALIGNR.
2895static bool isPALIGNRMask(const SmallVectorImpl<int> &Mask, EVT VT,
2896 bool hasSSSE3) {
2897 int i, e = VT.getVectorNumElements();
Michael J. Spencerec38de22010-10-10 22:04:20 +00002898
Nate Begemana09008b2009-10-19 02:17:23 +00002899 // Do not handle v2i64 / v2f64 shuffles with palignr.
2900 if (e < 4 || !hasSSSE3)
2901 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00002902
Nate Begemana09008b2009-10-19 02:17:23 +00002903 for (i = 0; i != e; ++i)
2904 if (Mask[i] >= 0)
2905 break;
Michael J. Spencerec38de22010-10-10 22:04:20 +00002906
Nate Begemana09008b2009-10-19 02:17:23 +00002907 // All undef, not a palignr.
2908 if (i == e)
2909 return false;
2910
2911 // Determine if it's ok to perform a palignr with only the LHS, since we
2912 // don't have access to the actual shuffle elements to see if RHS is undef.
2913 bool Unary = Mask[i] < (int)e;
2914 bool NeedsUnary = false;
2915
2916 int s = Mask[i] - i;
Michael J. Spencerec38de22010-10-10 22:04:20 +00002917
Nate Begemana09008b2009-10-19 02:17:23 +00002918 // Check the rest of the elements to see if they are consecutive.
2919 for (++i; i != e; ++i) {
2920 int m = Mask[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00002921 if (m < 0)
Nate Begemana09008b2009-10-19 02:17:23 +00002922 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00002923
Nate Begemana09008b2009-10-19 02:17:23 +00002924 Unary = Unary && (m < (int)e);
2925 NeedsUnary = NeedsUnary || (m < s);
2926
2927 if (NeedsUnary && !Unary)
2928 return false;
2929 if (Unary && m != ((s+i) & (e-1)))
2930 return false;
2931 if (!Unary && m != (s+i))
2932 return false;
2933 }
2934 return true;
2935}
2936
2937bool X86::isPALIGNRMask(ShuffleVectorSDNode *N) {
2938 SmallVector<int, 8> M;
2939 N->getMask(M);
2940 return ::isPALIGNRMask(M, N->getValueType(0), true);
2941}
2942
Evan Cheng14aed5e2006-03-24 01:18:28 +00002943/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2944/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Owen Andersone50ed302009-08-10 22:56:29 +00002945static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002946 int NumElems = VT.getVectorNumElements();
2947 if (NumElems != 2 && NumElems != 4)
2948 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002949
Nate Begeman9008ca62009-04-27 18:41:29 +00002950 int Half = NumElems / 2;
2951 for (int i = 0; i < Half; ++i)
2952 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002953 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002954 for (int i = Half; i < NumElems; ++i)
2955 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002956 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002957
Evan Cheng14aed5e2006-03-24 01:18:28 +00002958 return true;
2959}
2960
Nate Begeman9008ca62009-04-27 18:41:29 +00002961bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
2962 SmallVector<int, 8> M;
2963 N->getMask(M);
2964 return ::isSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002965}
2966
Evan Cheng213d2cf2007-05-17 18:45:50 +00002967/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00002968/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2969/// half elements to come from vector 1 (which would equal the dest.) and
2970/// the upper half to come from vector 2.
Owen Andersone50ed302009-08-10 22:56:29 +00002971static bool isCommutedSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002972 int NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00002973
2974 if (NumElems != 2 && NumElems != 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00002975 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002976
Nate Begeman9008ca62009-04-27 18:41:29 +00002977 int Half = NumElems / 2;
2978 for (int i = 0; i < Half; ++i)
2979 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002980 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002981 for (int i = Half; i < NumElems; ++i)
2982 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002983 return false;
2984 return true;
2985}
2986
Nate Begeman9008ca62009-04-27 18:41:29 +00002987static bool isCommutedSHUFP(ShuffleVectorSDNode *N) {
2988 SmallVector<int, 8> M;
2989 N->getMask(M);
2990 return isCommutedSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002991}
2992
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002993/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2994/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00002995bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
2996 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002997 return false;
2998
Evan Cheng2064a2b2006-03-28 06:50:32 +00002999 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman9008ca62009-04-27 18:41:29 +00003000 return isUndefOrEqual(N->getMaskElt(0), 6) &&
3001 isUndefOrEqual(N->getMaskElt(1), 7) &&
3002 isUndefOrEqual(N->getMaskElt(2), 2) &&
3003 isUndefOrEqual(N->getMaskElt(3), 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00003004}
3005
Nate Begeman0b10b912009-11-07 23:17:15 +00003006/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
3007/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
3008/// <2, 3, 2, 3>
3009bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
3010 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Michael J. Spencerec38de22010-10-10 22:04:20 +00003011
Nate Begeman0b10b912009-11-07 23:17:15 +00003012 if (NumElems != 4)
3013 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003014
Nate Begeman0b10b912009-11-07 23:17:15 +00003015 return isUndefOrEqual(N->getMaskElt(0), 2) &&
3016 isUndefOrEqual(N->getMaskElt(1), 3) &&
3017 isUndefOrEqual(N->getMaskElt(2), 2) &&
3018 isUndefOrEqual(N->getMaskElt(3), 3);
3019}
3020
Evan Cheng5ced1d82006-04-06 23:23:56 +00003021/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
3022/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman9008ca62009-04-27 18:41:29 +00003023bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
3024 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003025
Evan Cheng5ced1d82006-04-06 23:23:56 +00003026 if (NumElems != 2 && NumElems != 4)
3027 return false;
3028
Evan Chengc5cdff22006-04-07 21:53:05 +00003029 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003030 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003031 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003032
Evan Chengc5cdff22006-04-07 21:53:05 +00003033 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003034 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003035 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003036
3037 return true;
3038}
3039
Nate Begeman0b10b912009-11-07 23:17:15 +00003040/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
3041/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
3042bool X86::isMOVLHPSMask(ShuffleVectorSDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003043 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003044
Evan Cheng5ced1d82006-04-06 23:23:56 +00003045 if (NumElems != 2 && NumElems != 4)
3046 return false;
3047
Evan Chengc5cdff22006-04-07 21:53:05 +00003048 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003049 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003050 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003051
Nate Begeman9008ca62009-04-27 18:41:29 +00003052 for (unsigned i = 0; i < NumElems/2; ++i)
3053 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003054 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003055
3056 return true;
3057}
3058
Evan Cheng0038e592006-03-28 00:39:58 +00003059/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
3060/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Owen Andersone50ed302009-08-10 22:56:29 +00003061static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003062 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003063 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003064 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng0038e592006-03-28 00:39:58 +00003065 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003066
Nate Begeman9008ca62009-04-27 18:41:29 +00003067 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
3068 int BitI = Mask[i];
3069 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00003070 if (!isUndefOrEqual(BitI, j))
3071 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00003072 if (V2IsSplat) {
Mon P Wang7bcaefa2009-02-04 01:16:59 +00003073 if (!isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003074 return false;
3075 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00003076 if (!isUndefOrEqual(BitI1, j + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003077 return false;
3078 }
Evan Cheng0038e592006-03-28 00:39:58 +00003079 }
Evan Cheng0038e592006-03-28 00:39:58 +00003080 return true;
3081}
3082
Nate Begeman9008ca62009-04-27 18:41:29 +00003083bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3084 SmallVector<int, 8> M;
3085 N->getMask(M);
3086 return ::isUNPCKLMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003087}
3088
Evan Cheng4fcb9222006-03-28 02:43:26 +00003089/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3090/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Eric Christopherfd179292009-08-27 18:07:15 +00003091static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003092 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003093 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003094 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng4fcb9222006-03-28 02:43:26 +00003095 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003096
Nate Begeman9008ca62009-04-27 18:41:29 +00003097 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
3098 int BitI = Mask[i];
3099 int BitI1 = Mask[i+1];
Chris Lattner5a88b832007-02-25 07:10:00 +00003100 if (!isUndefOrEqual(BitI, j + NumElts/2))
Evan Chengc5cdff22006-04-07 21:53:05 +00003101 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00003102 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00003103 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003104 return false;
3105 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00003106 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003107 return false;
3108 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003109 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003110 return true;
3111}
3112
Nate Begeman9008ca62009-04-27 18:41:29 +00003113bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3114 SmallVector<int, 8> M;
3115 N->getMask(M);
3116 return ::isUNPCKHMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003117}
3118
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003119/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3120/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3121/// <0, 0, 1, 1>
Owen Andersone50ed302009-08-10 22:56:29 +00003122static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003123 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003124 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003125 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003126
Nate Begeman9008ca62009-04-27 18:41:29 +00003127 for (int i = 0, j = 0; i != NumElems; i += 2, ++j) {
3128 int BitI = Mask[i];
3129 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00003130 if (!isUndefOrEqual(BitI, j))
3131 return false;
3132 if (!isUndefOrEqual(BitI1, j))
3133 return false;
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003134 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003135 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003136}
3137
Nate Begeman9008ca62009-04-27 18:41:29 +00003138bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
3139 SmallVector<int, 8> M;
3140 N->getMask(M);
3141 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
3142}
3143
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003144/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3145/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3146/// <2, 2, 3, 3>
Owen Andersone50ed302009-08-10 22:56:29 +00003147static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003148 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003149 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
3150 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003151
Nate Begeman9008ca62009-04-27 18:41:29 +00003152 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
3153 int BitI = Mask[i];
3154 int BitI1 = Mask[i+1];
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003155 if (!isUndefOrEqual(BitI, j))
3156 return false;
3157 if (!isUndefOrEqual(BitI1, j))
3158 return false;
3159 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003160 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003161}
3162
Nate Begeman9008ca62009-04-27 18:41:29 +00003163bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
3164 SmallVector<int, 8> M;
3165 N->getMask(M);
3166 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
3167}
3168
Evan Cheng017dcc62006-04-21 01:05:10 +00003169/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3170/// specifies a shuffle of elements that is suitable for input to MOVSS,
3171/// MOVSD, and MOVD, i.e. setting the lowest element.
Owen Andersone50ed302009-08-10 22:56:29 +00003172static bool isMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003173 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003174 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003175
3176 int NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003177
Nate Begeman9008ca62009-04-27 18:41:29 +00003178 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003179 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003180
Nate Begeman9008ca62009-04-27 18:41:29 +00003181 for (int i = 1; i < NumElts; ++i)
3182 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003183 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003184
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003185 return true;
3186}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003187
Nate Begeman9008ca62009-04-27 18:41:29 +00003188bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
3189 SmallVector<int, 8> M;
3190 N->getMask(M);
3191 return ::isMOVLMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003192}
3193
Evan Cheng017dcc62006-04-21 01:05:10 +00003194/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
3195/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003196/// element of vector 2 and the other elements to come from vector 1 in order.
Owen Andersone50ed302009-08-10 22:56:29 +00003197static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003198 bool V2IsSplat = false, bool V2IsUndef = false) {
3199 int NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003200 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003201 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003202
Nate Begeman9008ca62009-04-27 18:41:29 +00003203 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003204 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003205
Nate Begeman9008ca62009-04-27 18:41:29 +00003206 for (int i = 1; i < NumOps; ++i)
3207 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3208 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3209 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003210 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003211
Evan Cheng39623da2006-04-20 08:58:49 +00003212 return true;
3213}
3214
Nate Begeman9008ca62009-04-27 18:41:29 +00003215static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00003216 bool V2IsUndef = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003217 SmallVector<int, 8> M;
3218 N->getMask(M);
3219 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00003220}
3221
Evan Chengd9539472006-04-14 21:59:03 +00003222/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3223/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003224bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N) {
3225 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003226 return false;
3227
3228 // Expect 1, 1, 3, 3
Rafael Espindola15684b22009-04-24 12:40:33 +00003229 for (unsigned i = 0; i < 2; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003230 int Elt = N->getMaskElt(i);
3231 if (Elt >= 0 && Elt != 1)
3232 return false;
Rafael Espindola15684b22009-04-24 12:40:33 +00003233 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003234
3235 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003236 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003237 int Elt = N->getMaskElt(i);
3238 if (Elt >= 0 && Elt != 3)
3239 return false;
3240 if (Elt == 3)
3241 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003242 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003243 // Don't use movshdup if it can be done with a shufps.
Nate Begeman9008ca62009-04-27 18:41:29 +00003244 // FIXME: verify that matching u, u, 3, 3 is what we want.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003245 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003246}
3247
3248/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3249/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003250bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N) {
3251 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003252 return false;
3253
3254 // Expect 0, 0, 2, 2
Nate Begeman9008ca62009-04-27 18:41:29 +00003255 for (unsigned i = 0; i < 2; ++i)
3256 if (N->getMaskElt(i) > 0)
3257 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003258
3259 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003260 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003261 int Elt = N->getMaskElt(i);
3262 if (Elt >= 0 && Elt != 2)
3263 return false;
3264 if (Elt == 2)
3265 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003266 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003267 // Don't use movsldup if it can be done with a shufps.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003268 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003269}
3270
Evan Cheng0b457f02008-09-25 20:50:48 +00003271/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3272/// specifies a shuffle of elements that is suitable for input to MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003273bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
3274 int e = N->getValueType(0).getVectorNumElements() / 2;
Eric Christopherfd179292009-08-27 18:07:15 +00003275
Nate Begeman9008ca62009-04-27 18:41:29 +00003276 for (int i = 0; i < e; ++i)
3277 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003278 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003279 for (int i = 0; i < e; ++i)
3280 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003281 return false;
3282 return true;
3283}
3284
Evan Cheng63d33002006-03-22 08:01:21 +00003285/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003286/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Evan Cheng63d33002006-03-22 08:01:21 +00003287unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003288 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3289 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
3290
Evan Chengb9df0ca2006-03-22 02:53:00 +00003291 unsigned Shift = (NumOperands == 4) ? 2 : 1;
3292 unsigned Mask = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00003293 for (int i = 0; i < NumOperands; ++i) {
3294 int Val = SVOp->getMaskElt(NumOperands-i-1);
3295 if (Val < 0) Val = 0;
Evan Cheng14aed5e2006-03-24 01:18:28 +00003296 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00003297 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00003298 if (i != NumOperands - 1)
3299 Mask <<= Shift;
3300 }
Evan Cheng63d33002006-03-22 08:01:21 +00003301 return Mask;
3302}
3303
Evan Cheng506d3df2006-03-29 23:07:14 +00003304/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003305/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003306unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003307 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003308 unsigned Mask = 0;
3309 // 8 nodes, but we only care about the last 4.
3310 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003311 int Val = SVOp->getMaskElt(i);
3312 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00003313 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00003314 if (i != 4)
3315 Mask <<= 2;
3316 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003317 return Mask;
3318}
3319
3320/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003321/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003322unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003323 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003324 unsigned Mask = 0;
3325 // 8 nodes, but we only care about the first 4.
3326 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003327 int Val = SVOp->getMaskElt(i);
3328 if (Val >= 0)
3329 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00003330 if (i != 0)
3331 Mask <<= 2;
3332 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003333 return Mask;
3334}
3335
Nate Begemana09008b2009-10-19 02:17:23 +00003336/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
3337/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
3338unsigned X86::getShufflePALIGNRImmediate(SDNode *N) {
3339 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3340 EVT VVT = N->getValueType(0);
3341 unsigned EltSize = VVT.getVectorElementType().getSizeInBits() >> 3;
3342 int Val = 0;
3343
3344 unsigned i, e;
3345 for (i = 0, e = VVT.getVectorNumElements(); i != e; ++i) {
3346 Val = SVOp->getMaskElt(i);
3347 if (Val >= 0)
3348 break;
3349 }
3350 return (Val - i) * EltSize;
3351}
3352
Evan Cheng37b73872009-07-30 08:33:02 +00003353/// isZeroNode - Returns true if Elt is a constant zero or a floating point
3354/// constant +0.0.
3355bool X86::isZeroNode(SDValue Elt) {
3356 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmane368b462010-06-18 14:22:04 +00003357 cast<ConstantSDNode>(Elt)->isNullValue()) ||
Evan Cheng37b73872009-07-30 08:33:02 +00003358 (isa<ConstantFPSDNode>(Elt) &&
3359 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
3360}
3361
Nate Begeman9008ca62009-04-27 18:41:29 +00003362/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
3363/// their permute mask.
3364static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
3365 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003366 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003367 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00003368 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00003369
Nate Begeman5a5ca152009-04-29 05:20:52 +00003370 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003371 int idx = SVOp->getMaskElt(i);
3372 if (idx < 0)
3373 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003374 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003375 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003376 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003377 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003378 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003379 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
3380 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003381}
3382
Evan Cheng779ccea2007-12-07 21:30:01 +00003383/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3384/// the two vector operands have swapped position.
Owen Andersone50ed302009-08-10 22:56:29 +00003385static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00003386 unsigned NumElems = VT.getVectorNumElements();
3387 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003388 int idx = Mask[i];
3389 if (idx < 0)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003390 continue;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003391 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003392 Mask[i] = idx + NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003393 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003394 Mask[i] = idx - NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003395 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003396}
3397
Evan Cheng533a0aa2006-04-19 20:35:22 +00003398/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
3399/// match movhlps. The lower half elements should come from upper half of
3400/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003401/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00003402static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
3403 if (Op->getValueType(0).getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00003404 return false;
3405 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003406 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003407 return false;
3408 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003409 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003410 return false;
3411 return true;
3412}
3413
Evan Cheng5ced1d82006-04-06 23:23:56 +00003414/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00003415/// is promoted to a vector. It also returns the LoadSDNode by reference if
3416/// required.
3417static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00003418 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
3419 return false;
3420 N = N->getOperand(0).getNode();
3421 if (!ISD::isNON_EXTLoad(N))
3422 return false;
3423 if (LD)
3424 *LD = cast<LoadSDNode>(N);
3425 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003426}
3427
Evan Cheng533a0aa2006-04-19 20:35:22 +00003428/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
3429/// match movlp{s|d}. The lower half elements should come from lower half of
3430/// V1 (and in order), and the upper half elements should come from the upper
3431/// half of V2 (and in order). And since V1 will become the source of the
3432/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003433static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
3434 ShuffleVectorSDNode *Op) {
Evan Cheng466685d2006-10-09 20:57:25 +00003435 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003436 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00003437 // Is V2 is a vector load, don't do this transformation. We will try to use
3438 // load folding shufps op.
3439 if (ISD::isNON_EXTLoad(V2))
3440 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003441
Nate Begeman5a5ca152009-04-29 05:20:52 +00003442 unsigned NumElems = Op->getValueType(0).getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003443
Evan Cheng533a0aa2006-04-19 20:35:22 +00003444 if (NumElems != 2 && NumElems != 4)
3445 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003446 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003447 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003448 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003449 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003450 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003451 return false;
3452 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003453}
3454
Evan Cheng39623da2006-04-20 08:58:49 +00003455/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
3456/// all the same.
3457static bool isSplatVector(SDNode *N) {
3458 if (N->getOpcode() != ISD::BUILD_VECTOR)
3459 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003460
Dan Gohman475871a2008-07-27 21:46:04 +00003461 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00003462 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
3463 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003464 return false;
3465 return true;
3466}
3467
Evan Cheng213d2cf2007-05-17 18:45:50 +00003468/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00003469/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00003470/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00003471static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00003472 SDValue V1 = N->getOperand(0);
3473 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003474 unsigned NumElems = N->getValueType(0).getVectorNumElements();
3475 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003476 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003477 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003478 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00003479 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
3480 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003481 if (Opc != ISD::BUILD_VECTOR ||
3482 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00003483 return false;
3484 } else if (Idx >= 0) {
3485 unsigned Opc = V1.getOpcode();
3486 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
3487 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003488 if (Opc != ISD::BUILD_VECTOR ||
3489 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00003490 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00003491 }
3492 }
3493 return true;
3494}
3495
3496/// getZeroVector - Returns a vector of specified type with all zero elements.
3497///
Owen Andersone50ed302009-08-10 22:56:29 +00003498static SDValue getZeroVector(EVT VT, bool HasSSE2, SelectionDAG &DAG,
Dale Johannesenace16102009-02-03 19:33:06 +00003499 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003500 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003501
Dale Johannesen0488fb62010-09-30 23:57:10 +00003502 // Always build SSE zero vectors as <4 x i32> bitcasted
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003503 // to their dest type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00003504 SDValue Vec;
Dale Johannesen0488fb62010-09-30 23:57:10 +00003505 if (VT.getSizeInBits() == 128) { // SSE
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003506 if (HasSSE2) { // SSE2
3507 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3508 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
3509 } else { // SSE1
3510 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
3511 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
3512 }
3513 } else if (VT.getSizeInBits() == 256) { // AVX
3514 // 256-bit logic and arithmetic instructions in AVX are
3515 // all floating-point, no support for integer ops. Default
3516 // to emitting fp zeroed vectors then.
Owen Anderson825b72b2009-08-11 20:47:22 +00003517 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003518 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
3519 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops, 8);
Evan Chengf0df0312008-05-15 08:39:06 +00003520 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003521 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00003522}
3523
Chris Lattner8a594482007-11-25 00:24:49 +00003524/// getOnesVector - Returns a vector of specified type with all bits set.
3525///
Owen Andersone50ed302009-08-10 22:56:29 +00003526static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003527 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003528
Chris Lattner8a594482007-11-25 00:24:49 +00003529 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
3530 // type. This ensures they get CSE'd.
Owen Anderson825b72b2009-08-11 20:47:22 +00003531 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00003532 SDValue Vec;
Dale Johannesen0488fb62010-09-30 23:57:10 +00003533 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003534 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00003535}
3536
3537
Evan Cheng39623da2006-04-20 08:58:49 +00003538/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
3539/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00003540static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003541 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003542 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003543
Evan Cheng39623da2006-04-20 08:58:49 +00003544 bool Changed = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003545 SmallVector<int, 8> MaskVec;
3546 SVOp->getMask(MaskVec);
Eric Christopherfd179292009-08-27 18:07:15 +00003547
Nate Begeman5a5ca152009-04-29 05:20:52 +00003548 for (unsigned i = 0; i != NumElems; ++i) {
3549 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003550 MaskVec[i] = NumElems;
3551 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00003552 }
Evan Cheng39623da2006-04-20 08:58:49 +00003553 }
Evan Cheng39623da2006-04-20 08:58:49 +00003554 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00003555 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
3556 SVOp->getOperand(1), &MaskVec[0]);
3557 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00003558}
3559
Evan Cheng017dcc62006-04-21 01:05:10 +00003560/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
3561/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00003562static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003563 SDValue V2) {
3564 unsigned NumElems = VT.getVectorNumElements();
3565 SmallVector<int, 8> Mask;
3566 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00003567 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003568 Mask.push_back(i);
3569 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00003570}
3571
Nate Begeman9008ca62009-04-27 18:41:29 +00003572/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003573static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003574 SDValue V2) {
3575 unsigned NumElems = VT.getVectorNumElements();
3576 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00003577 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003578 Mask.push_back(i);
3579 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00003580 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003581 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00003582}
3583
Nate Begeman9008ca62009-04-27 18:41:29 +00003584/// getUnpackhMask - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003585static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003586 SDValue V2) {
3587 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00003588 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00003589 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00003590 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003591 Mask.push_back(i + Half);
3592 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00003593 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003594 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00003595}
3596
Bruno Cardoso Lopesbb0a9482010-08-13 17:50:47 +00003597/// PromoteSplat - Promote a splat of v4i32, v8i16 or v16i8 to v4f32.
3598static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003599 EVT PVT = MVT::v4f32;
Owen Andersone50ed302009-08-10 22:56:29 +00003600 EVT VT = SV->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00003601 DebugLoc dl = SV->getDebugLoc();
3602 SDValue V1 = SV->getOperand(0);
3603 int NumElems = VT.getVectorNumElements();
3604 int EltNo = SV->getSplatIndex();
Rafael Espindola15684b22009-04-24 12:40:33 +00003605
Nate Begeman9008ca62009-04-27 18:41:29 +00003606 // unpack elements to the correct location
3607 while (NumElems > 4) {
3608 if (EltNo < NumElems/2) {
3609 V1 = getUnpackl(DAG, dl, VT, V1, V1);
3610 } else {
3611 V1 = getUnpackh(DAG, dl, VT, V1, V1);
3612 EltNo -= NumElems/2;
3613 }
3614 NumElems >>= 1;
3615 }
Eric Christopherfd179292009-08-27 18:07:15 +00003616
Nate Begeman9008ca62009-04-27 18:41:29 +00003617 // Perform the splat.
3618 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003619 V1 = DAG.getNode(ISD::BITCAST, dl, PVT, V1);
Nate Begeman9008ca62009-04-27 18:41:29 +00003620 V1 = DAG.getVectorShuffle(PVT, dl, V1, DAG.getUNDEF(PVT), &SplatMask[0]);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003621 return DAG.getNode(ISD::BITCAST, dl, VT, V1);
Evan Chengc575ca22006-04-17 20:43:08 +00003622}
3623
Evan Chengba05f722006-04-21 23:03:30 +00003624/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00003625/// vector of zero or undef vector. This produces a shuffle where the low
3626/// element of V2 is swizzled into the zero/undef vector, landing at element
3627/// 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 +00003628static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Chengf0df0312008-05-15 08:39:06 +00003629 bool isZero, bool HasSSE2,
3630 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003631 EVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00003632 SDValue V1 = isZero
Nate Begeman9008ca62009-04-27 18:41:29 +00003633 ? getZeroVector(VT, HasSSE2, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
3634 unsigned NumElems = VT.getVectorNumElements();
3635 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00003636 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003637 // If this is the insertion idx, put the low elt of V2 here.
3638 MaskVec.push_back(i == Idx ? NumElems : i);
3639 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00003640}
3641
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003642/// getShuffleScalarElt - Returns the scalar element that will make up the ith
3643/// element of the result of the vector shuffle.
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003644SDValue getShuffleScalarElt(SDNode *N, int Index, SelectionDAG &DAG,
3645 unsigned Depth) {
3646 if (Depth == 6)
3647 return SDValue(); // Limit search depth.
3648
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003649 SDValue V = SDValue(N, 0);
3650 EVT VT = V.getValueType();
3651 unsigned Opcode = V.getOpcode();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003652
3653 // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
3654 if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
3655 Index = SV->getMaskElt(Index);
3656
3657 if (Index < 0)
3658 return DAG.getUNDEF(VT.getVectorElementType());
3659
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003660 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003661 SDValue NewV = (Index < NumElems) ? SV->getOperand(0) : SV->getOperand(1);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003662 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG, Depth+1);
Evan Chengf26ffe92008-05-29 08:22:04 +00003663 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003664
3665 // Recurse into target specific vector shuffles to find scalars.
3666 if (isTargetShuffle(Opcode)) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003667 int NumElems = VT.getVectorNumElements();
3668 SmallVector<unsigned, 16> ShuffleMask;
3669 SDValue ImmN;
3670
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003671 switch(Opcode) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003672 case X86ISD::SHUFPS:
3673 case X86ISD::SHUFPD:
3674 ImmN = N->getOperand(N->getNumOperands()-1);
3675 DecodeSHUFPSMask(NumElems,
3676 cast<ConstantSDNode>(ImmN)->getZExtValue(),
3677 ShuffleMask);
3678 break;
3679 case X86ISD::PUNPCKHBW:
3680 case X86ISD::PUNPCKHWD:
3681 case X86ISD::PUNPCKHDQ:
3682 case X86ISD::PUNPCKHQDQ:
3683 DecodePUNPCKHMask(NumElems, ShuffleMask);
3684 break;
3685 case X86ISD::UNPCKHPS:
3686 case X86ISD::UNPCKHPD:
3687 DecodeUNPCKHPMask(NumElems, ShuffleMask);
3688 break;
3689 case X86ISD::PUNPCKLBW:
3690 case X86ISD::PUNPCKLWD:
3691 case X86ISD::PUNPCKLDQ:
3692 case X86ISD::PUNPCKLQDQ:
3693 DecodePUNPCKLMask(NumElems, ShuffleMask);
3694 break;
3695 case X86ISD::UNPCKLPS:
3696 case X86ISD::UNPCKLPD:
3697 DecodeUNPCKLPMask(NumElems, ShuffleMask);
3698 break;
3699 case X86ISD::MOVHLPS:
3700 DecodeMOVHLPSMask(NumElems, ShuffleMask);
3701 break;
3702 case X86ISD::MOVLHPS:
3703 DecodeMOVLHPSMask(NumElems, ShuffleMask);
3704 break;
3705 case X86ISD::PSHUFD:
3706 ImmN = N->getOperand(N->getNumOperands()-1);
3707 DecodePSHUFMask(NumElems,
3708 cast<ConstantSDNode>(ImmN)->getZExtValue(),
3709 ShuffleMask);
3710 break;
3711 case X86ISD::PSHUFHW:
3712 ImmN = N->getOperand(N->getNumOperands()-1);
3713 DecodePSHUFHWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
3714 ShuffleMask);
3715 break;
3716 case X86ISD::PSHUFLW:
3717 ImmN = N->getOperand(N->getNumOperands()-1);
3718 DecodePSHUFLWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
3719 ShuffleMask);
3720 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003721 case X86ISD::MOVSS:
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00003722 case X86ISD::MOVSD: {
3723 // The index 0 always comes from the first element of the second source,
3724 // this is why MOVSS and MOVSD are used in the first place. The other
3725 // elements come from the other positions of the first source vector.
3726 unsigned OpNum = (Index == 0) ? 1 : 0;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003727 return getShuffleScalarElt(V.getOperand(OpNum).getNode(), Index, DAG,
3728 Depth+1);
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00003729 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003730 default:
3731 assert("not implemented for target shuffle node");
3732 return SDValue();
3733 }
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003734
3735 Index = ShuffleMask[Index];
3736 if (Index < 0)
3737 return DAG.getUNDEF(VT.getVectorElementType());
3738
3739 SDValue NewV = (Index < NumElems) ? N->getOperand(0) : N->getOperand(1);
3740 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG,
3741 Depth+1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003742 }
3743
3744 // Actual nodes that may contain scalar elements
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003745 if (Opcode == ISD::BITCAST) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003746 V = V.getOperand(0);
3747 EVT SrcVT = V.getValueType();
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003748 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003749
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003750 if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003751 return SDValue();
3752 }
3753
3754 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
3755 return (Index == 0) ? V.getOperand(0)
3756 : DAG.getUNDEF(VT.getVectorElementType());
3757
3758 if (V.getOpcode() == ISD::BUILD_VECTOR)
3759 return V.getOperand(Index);
3760
3761 return SDValue();
3762}
3763
3764/// getNumOfConsecutiveZeros - Return the number of elements of a vector
3765/// shuffle operation which come from a consecutively from a zero. The
3766/// search can start in two diferent directions, from left or right.
3767static
3768unsigned getNumOfConsecutiveZeros(SDNode *N, int NumElems,
3769 bool ZerosFromLeft, SelectionDAG &DAG) {
3770 int i = 0;
3771
3772 while (i < NumElems) {
3773 unsigned Index = ZerosFromLeft ? i : NumElems-i-1;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003774 SDValue Elt = getShuffleScalarElt(N, Index, DAG, 0);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003775 if (!(Elt.getNode() &&
3776 (Elt.getOpcode() == ISD::UNDEF || X86::isZeroNode(Elt))))
3777 break;
3778 ++i;
3779 }
3780
3781 return i;
3782}
3783
3784/// isShuffleMaskConsecutive - Check if the shuffle mask indicies from MaskI to
3785/// MaskE correspond consecutively to elements from one of the vector operands,
3786/// starting from its index OpIdx. Also tell OpNum which source vector operand.
3787static
3788bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp, int MaskI, int MaskE,
3789 int OpIdx, int NumElems, unsigned &OpNum) {
3790 bool SeenV1 = false;
3791 bool SeenV2 = false;
3792
3793 for (int i = MaskI; i <= MaskE; ++i, ++OpIdx) {
3794 int Idx = SVOp->getMaskElt(i);
3795 // Ignore undef indicies
3796 if (Idx < 0)
3797 continue;
3798
3799 if (Idx < NumElems)
3800 SeenV1 = true;
3801 else
3802 SeenV2 = true;
3803
3804 // Only accept consecutive elements from the same vector
3805 if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
3806 return false;
3807 }
3808
3809 OpNum = SeenV1 ? 0 : 1;
3810 return true;
3811}
3812
3813/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
3814/// logical left shift of a vector.
3815static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
3816 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
3817 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
3818 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
3819 false /* check zeros from right */, DAG);
3820 unsigned OpSrc;
3821
3822 if (!NumZeros)
3823 return false;
3824
3825 // Considering the elements in the mask that are not consecutive zeros,
3826 // check if they consecutively come from only one of the source vectors.
3827 //
3828 // V1 = {X, A, B, C} 0
3829 // \ \ \ /
3830 // vector_shuffle V1, V2 <1, 2, 3, X>
3831 //
3832 if (!isShuffleMaskConsecutive(SVOp,
3833 0, // Mask Start Index
3834 NumElems-NumZeros-1, // Mask End Index
3835 NumZeros, // Where to start looking in the src vector
3836 NumElems, // Number of elements in vector
3837 OpSrc)) // Which source operand ?
3838 return false;
3839
3840 isLeft = false;
3841 ShAmt = NumZeros;
3842 ShVal = SVOp->getOperand(OpSrc);
3843 return true;
3844}
3845
3846/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
3847/// logical left shift of a vector.
3848static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
3849 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
3850 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
3851 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
3852 true /* check zeros from left */, DAG);
3853 unsigned OpSrc;
3854
3855 if (!NumZeros)
3856 return false;
3857
3858 // Considering the elements in the mask that are not consecutive zeros,
3859 // check if they consecutively come from only one of the source vectors.
3860 //
3861 // 0 { A, B, X, X } = V2
3862 // / \ / /
3863 // vector_shuffle V1, V2 <X, X, 4, 5>
3864 //
3865 if (!isShuffleMaskConsecutive(SVOp,
3866 NumZeros, // Mask Start Index
3867 NumElems-1, // Mask End Index
3868 0, // Where to start looking in the src vector
3869 NumElems, // Number of elements in vector
3870 OpSrc)) // Which source operand ?
3871 return false;
3872
3873 isLeft = true;
3874 ShAmt = NumZeros;
3875 ShVal = SVOp->getOperand(OpSrc);
3876 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00003877}
3878
3879/// isVectorShift - Returns true if the shuffle can be implemented as a
3880/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003881static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00003882 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003883 if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
3884 isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
3885 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00003886
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003887 return false;
Evan Chengf26ffe92008-05-29 08:22:04 +00003888}
3889
Evan Chengc78d3b42006-04-24 18:01:45 +00003890/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
3891///
Dan Gohman475871a2008-07-27 21:46:04 +00003892static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00003893 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00003894 SelectionDAG &DAG,
3895 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003896 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00003897 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00003898
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003899 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003900 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003901 bool First = true;
3902 for (unsigned i = 0; i < 16; ++i) {
3903 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
3904 if (ThisIsNonZero && First) {
3905 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003906 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00003907 else
Owen Anderson825b72b2009-08-11 20:47:22 +00003908 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00003909 First = false;
3910 }
3911
3912 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00003913 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003914 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
3915 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003916 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003917 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00003918 }
3919 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003920 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
3921 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
3922 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00003923 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003924 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00003925 } else
3926 ThisElt = LastElt;
3927
Gabor Greifba36cb52008-08-28 21:40:38 +00003928 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00003929 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00003930 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00003931 }
3932 }
3933
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003934 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00003935}
3936
Bill Wendlinga348c562007-03-22 18:42:45 +00003937/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00003938///
Dan Gohman475871a2008-07-27 21:46:04 +00003939static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00003940 unsigned NumNonZero, unsigned NumZero,
3941 SelectionDAG &DAG,
3942 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003943 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00003944 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00003945
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003946 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003947 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003948 bool First = true;
3949 for (unsigned i = 0; i < 8; ++i) {
3950 bool isNonZero = (NonZeros & (1 << i)) != 0;
3951 if (isNonZero) {
3952 if (First) {
3953 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003954 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00003955 else
Owen Anderson825b72b2009-08-11 20:47:22 +00003956 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00003957 First = false;
3958 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003959 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003960 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00003961 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00003962 }
3963 }
3964
3965 return V;
3966}
3967
Evan Chengf26ffe92008-05-29 08:22:04 +00003968/// getVShift - Return a vector logical shift node.
3969///
Owen Andersone50ed302009-08-10 22:56:29 +00003970static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00003971 unsigned NumBits, SelectionDAG &DAG,
3972 const TargetLowering &TLI, DebugLoc dl) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00003973 EVT ShVT = MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00003974 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003975 SrcOp = DAG.getNode(ISD::BITCAST, dl, ShVT, SrcOp);
3976 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00003977 DAG.getNode(Opc, dl, ShVT, SrcOp,
Gabor Greif327ef032008-08-28 23:19:51 +00003978 DAG.getConstant(NumBits, TLI.getShiftAmountTy())));
Evan Chengf26ffe92008-05-29 08:22:04 +00003979}
3980
Dan Gohman475871a2008-07-27 21:46:04 +00003981SDValue
Evan Chengc3630942009-12-09 21:00:30 +00003982X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00003983 SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00003984
Evan Chengc3630942009-12-09 21:00:30 +00003985 // Check if the scalar load can be widened into a vector load. And if
3986 // the address is "base + cst" see if the cst can be "absorbed" into
3987 // the shuffle mask.
3988 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
3989 SDValue Ptr = LD->getBasePtr();
3990 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
3991 return SDValue();
3992 EVT PVT = LD->getValueType(0);
3993 if (PVT != MVT::i32 && PVT != MVT::f32)
3994 return SDValue();
3995
3996 int FI = -1;
3997 int64_t Offset = 0;
3998 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
3999 FI = FINode->getIndex();
4000 Offset = 0;
4001 } else if (Ptr.getOpcode() == ISD::ADD &&
4002 isa<ConstantSDNode>(Ptr.getOperand(1)) &&
4003 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4004 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4005 Offset = Ptr.getConstantOperandVal(1);
4006 Ptr = Ptr.getOperand(0);
4007 } else {
4008 return SDValue();
4009 }
4010
4011 SDValue Chain = LD->getChain();
4012 // Make sure the stack object alignment is at least 16.
4013 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
4014 if (DAG.InferPtrAlignment(Ptr) < 16) {
4015 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00004016 // Can't change the alignment. FIXME: It's possible to compute
4017 // the exact stack offset and reference FI + adjust offset instead.
4018 // If someone *really* cares about this. That's the way to implement it.
4019 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004020 } else {
4021 MFI->setObjectAlignment(FI, 16);
4022 }
4023 }
4024
4025 // (Offset % 16) must be multiple of 4. Then address is then
4026 // Ptr + (Offset & ~15).
4027 if (Offset < 0)
4028 return SDValue();
4029 if ((Offset % 16) & 3)
4030 return SDValue();
4031 int64_t StartOffset = Offset & ~15;
4032 if (StartOffset)
4033 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
4034 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
4035
4036 int EltNo = (Offset - StartOffset) >> 2;
4037 int Mask[4] = { EltNo, EltNo, EltNo, EltNo };
4038 EVT VT = (PVT == MVT::i32) ? MVT::v4i32 : MVT::v4f32;
Chris Lattner51abfe42010-09-21 06:02:19 +00004039 SDValue V1 = DAG.getLoad(VT, dl, Chain, Ptr,
4040 LD->getPointerInfo().getWithOffset(StartOffset),
David Greene67c9d422010-02-15 16:53:33 +00004041 false, false, 0);
Evan Chengc3630942009-12-09 21:00:30 +00004042 // Canonicalize it to a v4i32 shuffle.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004043 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, V1);
4044 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Chengc3630942009-12-09 21:00:30 +00004045 DAG.getVectorShuffle(MVT::v4i32, dl, V1,
Chris Lattner51abfe42010-09-21 06:02:19 +00004046 DAG.getUNDEF(MVT::v4i32),&Mask[0]));
Evan Chengc3630942009-12-09 21:00:30 +00004047 }
4048
4049 return SDValue();
4050}
4051
Michael J. Spencerec38de22010-10-10 22:04:20 +00004052/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
4053/// vector of type 'VT', see if the elements can be replaced by a single large
Nate Begeman1449f292010-03-24 22:19:06 +00004054/// load which has the same value as a build_vector whose operands are 'elts'.
4055///
4056/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
Michael J. Spencerec38de22010-10-10 22:04:20 +00004057///
Nate Begeman1449f292010-03-24 22:19:06 +00004058/// FIXME: we'd also like to handle the case where the last elements are zero
4059/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
4060/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004061static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
Chris Lattner88641552010-09-22 00:34:38 +00004062 DebugLoc &DL, SelectionDAG &DAG) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00004063 EVT EltVT = VT.getVectorElementType();
4064 unsigned NumElems = Elts.size();
Michael J. Spencerec38de22010-10-10 22:04:20 +00004065
Nate Begemanfdea31a2010-03-24 20:49:50 +00004066 LoadSDNode *LDBase = NULL;
4067 unsigned LastLoadedElt = -1U;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004068
Nate Begeman1449f292010-03-24 22:19:06 +00004069 // For each element in the initializer, see if we've found a load or an undef.
Michael J. Spencerec38de22010-10-10 22:04:20 +00004070 // If we don't find an initial load element, or later load elements are
Nate Begeman1449f292010-03-24 22:19:06 +00004071 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004072 for (unsigned i = 0; i < NumElems; ++i) {
4073 SDValue Elt = Elts[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00004074
Nate Begemanfdea31a2010-03-24 20:49:50 +00004075 if (!Elt.getNode() ||
4076 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
4077 return SDValue();
4078 if (!LDBase) {
4079 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
4080 return SDValue();
4081 LDBase = cast<LoadSDNode>(Elt.getNode());
4082 LastLoadedElt = i;
4083 continue;
4084 }
4085 if (Elt.getOpcode() == ISD::UNDEF)
4086 continue;
4087
4088 LoadSDNode *LD = cast<LoadSDNode>(Elt);
4089 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
4090 return SDValue();
4091 LastLoadedElt = i;
4092 }
Nate Begeman1449f292010-03-24 22:19:06 +00004093
4094 // If we have found an entire vector of loads and undefs, then return a large
4095 // load of the entire vector width starting at the base pointer. If we found
4096 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004097 if (LastLoadedElt == NumElems - 1) {
4098 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
Chris Lattner88641552010-09-22 00:34:38 +00004099 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004100 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004101 LDBase->isVolatile(), LDBase->isNonTemporal(), 0);
Chris Lattner88641552010-09-22 00:34:38 +00004102 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004103 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004104 LDBase->isVolatile(), LDBase->isNonTemporal(),
4105 LDBase->getAlignment());
4106 } else if (NumElems == 4 && LastLoadedElt == 1) {
4107 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
4108 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
Chris Lattner88641552010-09-22 00:34:38 +00004109 SDValue ResNode = DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys,
4110 Ops, 2, MVT::i32,
4111 LDBase->getMemOperand());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004112 return DAG.getNode(ISD::BITCAST, DL, VT, ResNode);
Nate Begemanfdea31a2010-03-24 20:49:50 +00004113 }
4114 return SDValue();
4115}
4116
Evan Chengc3630942009-12-09 21:00:30 +00004117SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004118X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004119 DebugLoc dl = Op.getDebugLoc();
Chris Lattner6e80e442010-08-28 17:15:43 +00004120 // All zero's are handled with pxor in SSE2 and above, xorps in SSE1.
4121 // All one's are handled with pcmpeqd. In AVX, zero's are handled with
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004122 // vpxor in 128-bit and xor{pd,ps} in 256-bit, but no 256 version of pcmpeqd
4123 // is present, so AllOnes is ignored.
4124 if (ISD::isBuildVectorAllZeros(Op.getNode()) ||
4125 (Op.getValueType().getSizeInBits() != 256 &&
4126 ISD::isBuildVectorAllOnes(Op.getNode()))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004127 // Canonicalize this to <4 x i32> (SSE) to
Chris Lattner8a594482007-11-25 00:24:49 +00004128 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
4129 // eliminated on x86-32 hosts.
Dale Johannesen0488fb62010-09-30 23:57:10 +00004130 if (Op.getValueType() == MVT::v4i32)
Chris Lattner8a594482007-11-25 00:24:49 +00004131 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004132
Gabor Greifba36cb52008-08-28 21:40:38 +00004133 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00004134 return getOnesVector(Op.getValueType(), DAG, dl);
4135 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00004136 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004137
Owen Andersone50ed302009-08-10 22:56:29 +00004138 EVT VT = Op.getValueType();
4139 EVT ExtVT = VT.getVectorElementType();
4140 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004141
4142 unsigned NumElems = Op.getNumOperands();
4143 unsigned NumZero = 0;
4144 unsigned NumNonZero = 0;
4145 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004146 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00004147 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004148 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00004149 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00004150 if (Elt.getOpcode() == ISD::UNDEF)
4151 continue;
4152 Values.insert(Elt);
4153 if (Elt.getOpcode() != ISD::Constant &&
4154 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004155 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00004156 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00004157 NumZero++;
4158 else {
4159 NonZeros |= (1 << i);
4160 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004161 }
4162 }
4163
Chris Lattner97a2a562010-08-26 05:24:29 +00004164 // All undef vector. Return an UNDEF. All zero vectors were handled above.
4165 if (NumNonZero == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00004166 return DAG.getUNDEF(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004167
Chris Lattner67f453a2008-03-09 05:42:06 +00004168 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00004169 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004170 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00004171 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00004172
Chris Lattner62098042008-03-09 01:05:04 +00004173 // If this is an insertion of an i64 value on x86-32, and if the top bits of
4174 // the value are obviously zero, truncate the value to i32 and do the
4175 // insertion that way. Only do this if the value is non-constant or if the
4176 // value is a constant being inserted into element 0. It is cheaper to do
4177 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00004178 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00004179 (!IsAllConstants || Idx == 0)) {
4180 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004181 // Handle SSE only.
4182 assert(VT == MVT::v2i64 && "Expected an SSE value type!");
4183 EVT VecVT = MVT::v4i32;
4184 unsigned VecElts = 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00004185
Chris Lattner62098042008-03-09 01:05:04 +00004186 // Truncate the value (which may itself be a constant) to i32, and
4187 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00004188 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00004189 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00004190 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
4191 Subtarget->hasSSE2(), DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00004192
Chris Lattner62098042008-03-09 01:05:04 +00004193 // Now we have our 32-bit value zero extended in the low element of
4194 // a vector. If Idx != 0, swizzle it into place.
4195 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004196 SmallVector<int, 4> Mask;
4197 Mask.push_back(Idx);
4198 for (unsigned i = 1; i != VecElts; ++i)
4199 Mask.push_back(i);
4200 Item = DAG.getVectorShuffle(VecVT, dl, Item,
Eric Christopherfd179292009-08-27 18:07:15 +00004201 DAG.getUNDEF(Item.getValueType()),
Nate Begeman9008ca62009-04-27 18:41:29 +00004202 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00004203 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004204 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00004205 }
4206 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004207
Chris Lattner19f79692008-03-08 22:59:52 +00004208 // If we have a constant or non-constant insertion into the low element of
4209 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
4210 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00004211 // depending on what the source datatype is.
4212 if (Idx == 0) {
4213 if (NumZero == 0) {
4214 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Owen Anderson825b72b2009-08-11 20:47:22 +00004215 } else if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
4216 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Eli Friedman10415532009-06-06 06:05:10 +00004217 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
4218 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
4219 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget->hasSSE2(),
4220 DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00004221 } else if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
4222 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
Dale Johannesen0488fb62010-09-30 23:57:10 +00004223 assert(VT.getSizeInBits() == 128 && "Expected an SSE value type!");
4224 EVT MiddleVT = MVT::v4i32;
Eli Friedman10415532009-06-06 06:05:10 +00004225 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MiddleVT, Item);
4226 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
4227 Subtarget->hasSSE2(), DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004228 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Eli Friedman10415532009-06-06 06:05:10 +00004229 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004230 }
Evan Chengf26ffe92008-05-29 08:22:04 +00004231
4232 // Is it a vector logical left shift?
4233 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00004234 X86::isZeroNode(Op.getOperand(0)) &&
4235 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004236 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00004237 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00004238 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004239 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00004240 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004241 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004242
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004243 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00004244 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004245
Chris Lattner19f79692008-03-08 22:59:52 +00004246 // Otherwise, if this is a vector with i32 or f32 elements, and the element
4247 // is a non-constant being inserted into an element other than the low one,
4248 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
4249 // movd/movss) to move this into the low element, then shuffle it into
4250 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004251 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00004252 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00004253
Evan Cheng0db9fe62006-04-25 20:13:52 +00004254 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00004255 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
4256 Subtarget->hasSSE2(), DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00004257 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004258 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00004259 MaskVec.push_back(i == Idx ? 0 : 1);
4260 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004261 }
4262 }
4263
Chris Lattner67f453a2008-03-09 05:42:06 +00004264 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00004265 if (Values.size() == 1) {
4266 if (EVTBits == 32) {
4267 // Instead of a shuffle like this:
4268 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
4269 // Check if it's possible to issue this instead.
4270 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
4271 unsigned Idx = CountTrailingZeros_32(NonZeros);
4272 SDValue Item = Op.getOperand(Idx);
4273 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
4274 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
4275 }
Dan Gohman475871a2008-07-27 21:46:04 +00004276 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004277 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004278
Dan Gohmana3941172007-07-24 22:55:08 +00004279 // A vector full of immediates; various special cases are already
4280 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004281 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00004282 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00004283
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00004284 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00004285 if (EVTBits == 64) {
4286 if (NumNonZero == 1) {
4287 // One half is zero or undef.
4288 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00004289 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00004290 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00004291 return getShuffleVectorZeroOrUndef(V2, Idx, true,
4292 Subtarget->hasSSE2(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00004293 }
Dan Gohman475871a2008-07-27 21:46:04 +00004294 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00004295 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004296
4297 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00004298 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00004299 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00004300 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004301 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004302 }
4303
Bill Wendling826f36f2007-03-28 00:57:11 +00004304 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00004305 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Chris Lattner97a2a562010-08-26 05:24:29 +00004306 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004307 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004308 }
4309
4310 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00004311 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00004312 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004313 if (NumElems == 4 && NumZero > 0) {
4314 for (unsigned i = 0; i < 4; ++i) {
4315 bool isZero = !(NonZeros & (1 << i));
4316 if (isZero)
Dale Johannesenace16102009-02-03 19:33:06 +00004317 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004318 else
Dale Johannesenace16102009-02-03 19:33:06 +00004319 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004320 }
4321
4322 for (unsigned i = 0; i < 2; ++i) {
4323 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
4324 default: break;
4325 case 0:
4326 V[i] = V[i*2]; // Must be a zero vector.
4327 break;
4328 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00004329 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004330 break;
4331 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00004332 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004333 break;
4334 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00004335 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004336 break;
4337 }
4338 }
4339
Nate Begeman9008ca62009-04-27 18:41:29 +00004340 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004341 bool Reverse = (NonZeros & 0x3) == 2;
4342 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004343 MaskVec.push_back(Reverse ? 1-i : i);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004344 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
4345 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004346 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
4347 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004348 }
4349
Nate Begemanfdea31a2010-03-24 20:49:50 +00004350 if (Values.size() > 1 && VT.getSizeInBits() == 128) {
4351 // Check for a build vector of consecutive loads.
4352 for (unsigned i = 0; i < NumElems; ++i)
4353 V[i] = Op.getOperand(i);
Michael J. Spencerec38de22010-10-10 22:04:20 +00004354
Nate Begemanfdea31a2010-03-24 20:49:50 +00004355 // Check for elements which are consecutive loads.
4356 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
4357 if (LD.getNode())
4358 return LD;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004359
4360 // For SSE 4.1, use insertps to put the high elements into the low element.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004361 if (getSubtarget()->hasSSE41()) {
Chris Lattner24faf612010-08-28 17:59:08 +00004362 SDValue Result;
4363 if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
4364 Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
4365 else
4366 Result = DAG.getUNDEF(VT);
Michael J. Spencerec38de22010-10-10 22:04:20 +00004367
Chris Lattner24faf612010-08-28 17:59:08 +00004368 for (unsigned i = 1; i < NumElems; ++i) {
4369 if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
4370 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
Nate Begeman9008ca62009-04-27 18:41:29 +00004371 Op.getOperand(i), DAG.getIntPtrConstant(i));
Chris Lattner24faf612010-08-28 17:59:08 +00004372 }
4373 return Result;
Nate Begeman9008ca62009-04-27 18:41:29 +00004374 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00004375
Chris Lattner6e80e442010-08-28 17:15:43 +00004376 // Otherwise, expand into a number of unpckl*, start by extending each of
4377 // our (non-undef) elements to the full vector width with the element in the
4378 // bottom slot of the vector (which generates no code for SSE).
4379 for (unsigned i = 0; i < NumElems; ++i) {
4380 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
4381 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
4382 else
4383 V[i] = DAG.getUNDEF(VT);
4384 }
4385
4386 // Next, we iteratively mix elements, e.g. for v4f32:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004387 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
4388 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
4389 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Chris Lattner6e80e442010-08-28 17:15:43 +00004390 unsigned EltStride = NumElems >> 1;
4391 while (EltStride != 0) {
Chris Lattner3ddcc432010-08-28 17:28:30 +00004392 for (unsigned i = 0; i < EltStride; ++i) {
4393 // If V[i+EltStride] is undef and this is the first round of mixing,
4394 // then it is safe to just drop this shuffle: V[i] is already in the
4395 // right place, the one element (since it's the first round) being
4396 // inserted as undef can be dropped. This isn't safe for successive
4397 // rounds because they will permute elements within both vectors.
4398 if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
4399 EltStride == NumElems/2)
4400 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004401
Chris Lattner6e80e442010-08-28 17:15:43 +00004402 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
Chris Lattner3ddcc432010-08-28 17:28:30 +00004403 }
Chris Lattner6e80e442010-08-28 17:15:43 +00004404 EltStride >>= 1;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004405 }
4406 return V[0];
4407 }
Dan Gohman475871a2008-07-27 21:46:04 +00004408 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004409}
4410
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004411SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004412X86TargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const {
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004413 // We support concatenate two MMX registers and place them in a MMX
4414 // register. This is better than doing a stack convert.
4415 DebugLoc dl = Op.getDebugLoc();
4416 EVT ResVT = Op.getValueType();
4417 assert(Op.getNumOperands() == 2);
4418 assert(ResVT == MVT::v2i64 || ResVT == MVT::v4i32 ||
4419 ResVT == MVT::v8i16 || ResVT == MVT::v16i8);
4420 int Mask[2];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004421 SDValue InVec = DAG.getNode(ISD::BITCAST,dl, MVT::v1i64, Op.getOperand(0));
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004422 SDValue VecOp = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
4423 InVec = Op.getOperand(1);
4424 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4425 unsigned NumElts = ResVT.getVectorNumElements();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004426 VecOp = DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004427 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, ResVT, VecOp,
4428 InVec.getOperand(0), DAG.getIntPtrConstant(NumElts/2+1));
4429 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004430 InVec = DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, InVec);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004431 SDValue VecOp2 = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
4432 Mask[0] = 0; Mask[1] = 2;
4433 VecOp = DAG.getVectorShuffle(MVT::v2i64, dl, VecOp, VecOp2, Mask);
4434 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004435 return DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004436}
4437
Nate Begemanb9a47b82009-02-23 08:49:38 +00004438// v8i16 shuffles - Prefer shuffles in the following order:
4439// 1. [all] pshuflw, pshufhw, optional move
4440// 2. [ssse3] 1 x pshufb
4441// 3. [ssse3] 2 x pshufb + 1 x por
4442// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004443SDValue
4444X86TargetLowering::LowerVECTOR_SHUFFLEv8i16(SDValue Op,
4445 SelectionDAG &DAG) const {
4446 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00004447 SDValue V1 = SVOp->getOperand(0);
4448 SDValue V2 = SVOp->getOperand(1);
4449 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00004450 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00004451
Nate Begemanb9a47b82009-02-23 08:49:38 +00004452 // Determine if more than 1 of the words in each of the low and high quadwords
4453 // of the result come from the same quadword of one of the two inputs. Undef
4454 // mask values count as coming from any quadword, for better codegen.
4455 SmallVector<unsigned, 4> LoQuad(4);
4456 SmallVector<unsigned, 4> HiQuad(4);
4457 BitVector InputQuads(4);
4458 for (unsigned i = 0; i < 8; ++i) {
4459 SmallVectorImpl<unsigned> &Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00004460 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004461 MaskVals.push_back(EltIdx);
4462 if (EltIdx < 0) {
4463 ++Quad[0];
4464 ++Quad[1];
4465 ++Quad[2];
4466 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00004467 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004468 }
4469 ++Quad[EltIdx / 4];
4470 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00004471 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004472
Nate Begemanb9a47b82009-02-23 08:49:38 +00004473 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00004474 unsigned MaxQuad = 1;
4475 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004476 if (LoQuad[i] > MaxQuad) {
4477 BestLoQuad = i;
4478 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00004479 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004480 }
4481
Nate Begemanb9a47b82009-02-23 08:49:38 +00004482 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00004483 MaxQuad = 1;
4484 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004485 if (HiQuad[i] > MaxQuad) {
4486 BestHiQuad = i;
4487 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00004488 }
4489 }
4490
Nate Begemanb9a47b82009-02-23 08:49:38 +00004491 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00004492 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00004493 // single pshufb instruction is necessary. If There are more than 2 input
4494 // quads, disable the next transformation since it does not help SSSE3.
4495 bool V1Used = InputQuads[0] || InputQuads[1];
4496 bool V2Used = InputQuads[2] || InputQuads[3];
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004497 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004498 if (InputQuads.count() == 2 && V1Used && V2Used) {
4499 BestLoQuad = InputQuads.find_first();
4500 BestHiQuad = InputQuads.find_next(BestLoQuad);
4501 }
4502 if (InputQuads.count() > 2) {
4503 BestLoQuad = -1;
4504 BestHiQuad = -1;
4505 }
4506 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004507
Nate Begemanb9a47b82009-02-23 08:49:38 +00004508 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
4509 // the shuffle mask. If a quad is scored as -1, that means that it contains
4510 // words from all 4 input quadwords.
4511 SDValue NewV;
4512 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004513 SmallVector<int, 8> MaskV;
4514 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
4515 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
Eric Christopherfd179292009-08-27 18:07:15 +00004516 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004517 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V1),
4518 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V2), &MaskV[0]);
4519 NewV = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00004520
Nate Begemanb9a47b82009-02-23 08:49:38 +00004521 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
4522 // source words for the shuffle, to aid later transformations.
4523 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00004524 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00004525 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004526 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00004527 if (idx != (int)i)
4528 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004529 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00004530 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004531 AllWordsInNewV = false;
4532 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00004533 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004534
Nate Begemanb9a47b82009-02-23 08:49:38 +00004535 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
4536 if (AllWordsInNewV) {
4537 for (int i = 0; i != 8; ++i) {
4538 int idx = MaskVals[i];
4539 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00004540 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004541 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004542 if ((idx != i) && idx < 4)
4543 pshufhw = false;
4544 if ((idx != i) && idx > 3)
4545 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00004546 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00004547 V1 = NewV;
4548 V2Used = false;
4549 BestLoQuad = 0;
4550 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004551 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004552
Nate Begemanb9a47b82009-02-23 08:49:38 +00004553 // If we've eliminated the use of V2, and the new mask is a pshuflw or
4554 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00004555 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00004556 unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
4557 unsigned TargetMask = 0;
4558 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00004559 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00004560 TargetMask = pshufhw ? X86::getShufflePSHUFHWImmediate(NewV.getNode()):
4561 X86::getShufflePSHUFLWImmediate(NewV.getNode());
4562 V1 = NewV.getOperand(0);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00004563 return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
Evan Cheng14b32e12007-12-11 01:46:18 +00004564 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004565 }
Eric Christopherfd179292009-08-27 18:07:15 +00004566
Nate Begemanb9a47b82009-02-23 08:49:38 +00004567 // If we have SSSE3, and all words of the result are from 1 input vector,
4568 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
4569 // is present, fall back to case 4.
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004570 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004571 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00004572
Nate Begemanb9a47b82009-02-23 08:49:38 +00004573 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00004574 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00004575 // mask, and elements that come from V1 in the V2 mask, so that the two
4576 // results can be OR'd together.
4577 bool TwoInputs = V1Used && V2Used;
4578 for (unsigned i = 0; i != 8; ++i) {
4579 int EltIdx = MaskVals[i] * 2;
4580 if (TwoInputs && (EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004581 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4582 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004583 continue;
4584 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004585 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
4586 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004587 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004588 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004589 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00004590 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004591 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004592 if (!TwoInputs)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004593 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004594
Nate Begemanb9a47b82009-02-23 08:49:38 +00004595 // Calculate the shuffle mask for the second input, shuffle it, and
4596 // OR it with the first shuffled input.
4597 pshufbMask.clear();
4598 for (unsigned i = 0; i != 8; ++i) {
4599 int EltIdx = MaskVals[i] * 2;
4600 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004601 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4602 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004603 continue;
4604 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004605 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
4606 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004607 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004608 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00004609 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00004610 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004611 MVT::v16i8, &pshufbMask[0], 16));
4612 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004613 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004614 }
4615
4616 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
4617 // and update MaskVals with new element order.
4618 BitVector InOrder(8);
4619 if (BestLoQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004620 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004621 for (int i = 0; i != 4; ++i) {
4622 int idx = MaskVals[i];
4623 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004624 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004625 InOrder.set(i);
4626 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004627 MaskV.push_back(idx & 3);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004628 InOrder.set(i);
4629 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004630 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004631 }
4632 }
4633 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004634 MaskV.push_back(i);
Owen Anderson825b72b2009-08-11 20:47:22 +00004635 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00004636 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00004637
4638 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3())
4639 NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
4640 NewV.getOperand(0),
4641 X86::getShufflePSHUFLWImmediate(NewV.getNode()),
4642 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004643 }
Eric Christopherfd179292009-08-27 18:07:15 +00004644
Nate Begemanb9a47b82009-02-23 08:49:38 +00004645 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
4646 // and update MaskVals with the new element order.
4647 if (BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004648 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004649 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004650 MaskV.push_back(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004651 for (unsigned i = 4; i != 8; ++i) {
4652 int idx = MaskVals[i];
4653 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004654 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004655 InOrder.set(i);
4656 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004657 MaskV.push_back((idx & 3) + 4);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004658 InOrder.set(i);
4659 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004660 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004661 }
4662 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004663 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00004664 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00004665
4666 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3())
4667 NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
4668 NewV.getOperand(0),
4669 X86::getShufflePSHUFHWImmediate(NewV.getNode()),
4670 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004671 }
Eric Christopherfd179292009-08-27 18:07:15 +00004672
Nate Begemanb9a47b82009-02-23 08:49:38 +00004673 // In case BestHi & BestLo were both -1, which means each quadword has a word
4674 // from each of the four input quadwords, calculate the InOrder bitvector now
4675 // before falling through to the insert/extract cleanup.
4676 if (BestLoQuad == -1 && BestHiQuad == -1) {
4677 NewV = V1;
4678 for (int i = 0; i != 8; ++i)
4679 if (MaskVals[i] < 0 || MaskVals[i] == i)
4680 InOrder.set(i);
4681 }
Eric Christopherfd179292009-08-27 18:07:15 +00004682
Nate Begemanb9a47b82009-02-23 08:49:38 +00004683 // The other elements are put in the right place using pextrw and pinsrw.
4684 for (unsigned i = 0; i != 8; ++i) {
4685 if (InOrder[i])
4686 continue;
4687 int EltIdx = MaskVals[i];
4688 if (EltIdx < 0)
4689 continue;
4690 SDValue ExtOp = (EltIdx < 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00004691 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004692 DAG.getIntPtrConstant(EltIdx))
Owen Anderson825b72b2009-08-11 20:47:22 +00004693 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004694 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00004695 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004696 DAG.getIntPtrConstant(i));
4697 }
4698 return NewV;
4699}
4700
4701// v16i8 shuffles - Prefer shuffles in the following order:
4702// 1. [ssse3] 1 x pshufb
4703// 2. [ssse3] 2 x pshufb + 1 x por
4704// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
4705static
Nate Begeman9008ca62009-04-27 18:41:29 +00004706SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00004707 SelectionDAG &DAG,
4708 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004709 SDValue V1 = SVOp->getOperand(0);
4710 SDValue V2 = SVOp->getOperand(1);
4711 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00004712 SmallVector<int, 16> MaskVals;
Nate Begeman9008ca62009-04-27 18:41:29 +00004713 SVOp->getMask(MaskVals);
Eric Christopherfd179292009-08-27 18:07:15 +00004714
Nate Begemanb9a47b82009-02-23 08:49:38 +00004715 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00004716 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00004717 // present, fall back to case 3.
4718 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
4719 bool V1Only = true;
4720 bool V2Only = true;
4721 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004722 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00004723 if (EltIdx < 0)
4724 continue;
4725 if (EltIdx < 16)
4726 V2Only = false;
4727 else
4728 V1Only = false;
4729 }
Eric Christopherfd179292009-08-27 18:07:15 +00004730
Nate Begemanb9a47b82009-02-23 08:49:38 +00004731 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
4732 if (TLI.getSubtarget()->hasSSSE3()) {
4733 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00004734
Nate Begemanb9a47b82009-02-23 08:49:38 +00004735 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00004736 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00004737 //
4738 // Otherwise, we have elements from both input vectors, and must zero out
4739 // elements that come from V2 in the first mask, and V1 in the second mask
4740 // so that we can OR them together.
4741 bool TwoInputs = !(V1Only || V2Only);
4742 for (unsigned i = 0; i != 16; ++i) {
4743 int EltIdx = MaskVals[i];
4744 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004745 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004746 continue;
4747 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004748 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004749 }
4750 // If all the elements are from V2, assign it to V1 and return after
4751 // building the first pshufb.
4752 if (V2Only)
4753 V1 = V2;
Owen Anderson825b72b2009-08-11 20:47:22 +00004754 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00004755 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004756 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004757 if (!TwoInputs)
4758 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00004759
Nate Begemanb9a47b82009-02-23 08:49:38 +00004760 // Calculate the shuffle mask for the second input, shuffle it, and
4761 // OR it with the first shuffled input.
4762 pshufbMask.clear();
4763 for (unsigned i = 0; i != 16; ++i) {
4764 int EltIdx = MaskVals[i];
4765 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004766 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004767 continue;
4768 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004769 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004770 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004771 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00004772 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004773 MVT::v16i8, &pshufbMask[0], 16));
4774 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004775 }
Eric Christopherfd179292009-08-27 18:07:15 +00004776
Nate Begemanb9a47b82009-02-23 08:49:38 +00004777 // No SSSE3 - Calculate in place words and then fix all out of place words
4778 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
4779 // the 16 different words that comprise the two doublequadword input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004780 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
4781 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004782 SDValue NewV = V2Only ? V2 : V1;
4783 for (int i = 0; i != 8; ++i) {
4784 int Elt0 = MaskVals[i*2];
4785 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00004786
Nate Begemanb9a47b82009-02-23 08:49:38 +00004787 // This word of the result is all undef, skip it.
4788 if (Elt0 < 0 && Elt1 < 0)
4789 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004790
Nate Begemanb9a47b82009-02-23 08:49:38 +00004791 // This word of the result is already in the correct place, skip it.
4792 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
4793 continue;
4794 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
4795 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004796
Nate Begemanb9a47b82009-02-23 08:49:38 +00004797 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
4798 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
4799 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00004800
4801 // If Elt0 and Elt1 are defined, are consecutive, and can be load
4802 // using a single extract together, load it and store it.
4803 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004804 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00004805 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00004806 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00004807 DAG.getIntPtrConstant(i));
4808 continue;
4809 }
4810
Nate Begemanb9a47b82009-02-23 08:49:38 +00004811 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00004812 // source byte is not also odd, shift the extracted word left 8 bits
4813 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00004814 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004815 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004816 DAG.getIntPtrConstant(Elt1 / 2));
4817 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004818 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004819 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00004820 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004821 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
4822 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004823 }
4824 // If Elt0 is defined, extract it from the appropriate source. If the
4825 // source byte is not also even, shift the extracted word right 8 bits. If
4826 // Elt1 was also defined, OR the extracted values together before
4827 // inserting them in the result.
4828 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004829 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004830 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
4831 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004832 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004833 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00004834 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004835 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
4836 DAG.getConstant(0x00FF, MVT::i16));
4837 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00004838 : InsElt0;
4839 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004840 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004841 DAG.getIntPtrConstant(i));
4842 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004843 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00004844}
4845
Evan Cheng7a831ce2007-12-15 03:00:47 +00004846/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00004847/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00004848/// done when every pair / quad of shuffle mask elements point to elements in
4849/// the right sequence. e.g.
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00004850/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
Evan Cheng14b32e12007-12-11 01:46:18 +00004851static
Nate Begeman9008ca62009-04-27 18:41:29 +00004852SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00004853 SelectionDAG &DAG, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00004854 EVT VT = SVOp->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00004855 SDValue V1 = SVOp->getOperand(0);
4856 SDValue V2 = SVOp->getOperand(1);
4857 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00004858 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00004859 EVT NewVT;
Owen Anderson825b72b2009-08-11 20:47:22 +00004860 switch (VT.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004861 default: assert(false && "Unexpected!");
Owen Anderson825b72b2009-08-11 20:47:22 +00004862 case MVT::v4f32: NewVT = MVT::v2f64; break;
4863 case MVT::v4i32: NewVT = MVT::v2i64; break;
4864 case MVT::v8i16: NewVT = MVT::v4i32; break;
4865 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00004866 }
4867
Nate Begeman9008ca62009-04-27 18:41:29 +00004868 int Scale = NumElems / NewWidth;
4869 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00004870 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004871 int StartIdx = -1;
4872 for (int j = 0; j < Scale; ++j) {
4873 int EltIdx = SVOp->getMaskElt(i+j);
4874 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00004875 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00004876 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00004877 StartIdx = EltIdx - (EltIdx % Scale);
4878 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00004879 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00004880 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004881 if (StartIdx == -1)
4882 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00004883 else
Nate Begeman9008ca62009-04-27 18:41:29 +00004884 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004885 }
4886
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004887 V1 = DAG.getNode(ISD::BITCAST, dl, NewVT, V1);
4888 V2 = DAG.getNode(ISD::BITCAST, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00004889 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004890}
4891
Evan Chengd880b972008-05-09 21:53:03 +00004892/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00004893///
Owen Andersone50ed302009-08-10 22:56:29 +00004894static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00004895 SDValue SrcOp, SelectionDAG &DAG,
4896 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004897 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004898 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00004899 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00004900 LD = dyn_cast<LoadSDNode>(SrcOp);
4901 if (!LD) {
4902 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
4903 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00004904 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Duncan Sandscdfad362010-11-03 12:17:33 +00004905 if ((ExtVT != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00004906 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004907 SrcOp.getOperand(0).getOpcode() == ISD::BITCAST &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00004908 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004909 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00004910 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004911 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004912 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
4913 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
4914 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00004915 SrcOp.getOperand(0)
4916 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00004917 }
4918 }
4919 }
4920
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004921 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004922 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004923 DAG.getNode(ISD::BITCAST, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00004924 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00004925}
4926
Evan Chengace3c172008-07-22 21:13:36 +00004927/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
4928/// shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00004929static SDValue
Nate Begeman9008ca62009-04-27 18:41:29 +00004930LowerVECTOR_SHUFFLE_4wide(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
4931 SDValue V1 = SVOp->getOperand(0);
4932 SDValue V2 = SVOp->getOperand(1);
4933 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00004934 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00004935
Evan Chengace3c172008-07-22 21:13:36 +00004936 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00004937 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00004938 SmallVector<int, 8> Mask1(4U, -1);
4939 SmallVector<int, 8> PermMask;
4940 SVOp->getMask(PermMask);
4941
Evan Chengace3c172008-07-22 21:13:36 +00004942 unsigned NumHi = 0;
4943 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00004944 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004945 int Idx = PermMask[i];
4946 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00004947 Locs[i] = std::make_pair(-1, -1);
4948 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004949 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
4950 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00004951 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00004952 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004953 NumLo++;
4954 } else {
4955 Locs[i] = std::make_pair(1, NumHi);
4956 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00004957 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004958 NumHi++;
4959 }
4960 }
4961 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004962
Evan Chengace3c172008-07-22 21:13:36 +00004963 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004964 // If no more than two elements come from either vector. This can be
4965 // implemented with two shuffles. First shuffle gather the elements.
4966 // The second shuffle, which takes the first shuffle as both of its
4967 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00004968 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004969
Nate Begeman9008ca62009-04-27 18:41:29 +00004970 SmallVector<int, 8> Mask2(4U, -1);
Eric Christopherfd179292009-08-27 18:07:15 +00004971
Evan Chengace3c172008-07-22 21:13:36 +00004972 for (unsigned i = 0; i != 4; ++i) {
4973 if (Locs[i].first == -1)
4974 continue;
4975 else {
4976 unsigned Idx = (i < 2) ? 0 : 4;
4977 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00004978 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004979 }
4980 }
4981
Nate Begeman9008ca62009-04-27 18:41:29 +00004982 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004983 } else if (NumLo == 3 || NumHi == 3) {
4984 // Otherwise, we must have three elements from one vector, call it X, and
4985 // one element from the other, call it Y. First, use a shufps to build an
4986 // intermediate vector with the one element from Y and the element from X
4987 // that will be in the same half in the final destination (the indexes don't
4988 // matter). Then, use a shufps to build the final vector, taking the half
4989 // containing the element from Y from the intermediate, and the other half
4990 // from X.
4991 if (NumHi == 3) {
4992 // Normalize it so the 3 elements come from V1.
Nate Begeman9008ca62009-04-27 18:41:29 +00004993 CommuteVectorShuffleMask(PermMask, VT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004994 std::swap(V1, V2);
4995 }
4996
4997 // Find the element from V2.
4998 unsigned HiIndex;
4999 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005000 int Val = PermMask[HiIndex];
5001 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005002 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005003 if (Val >= 4)
5004 break;
5005 }
5006
Nate Begeman9008ca62009-04-27 18:41:29 +00005007 Mask1[0] = PermMask[HiIndex];
5008 Mask1[1] = -1;
5009 Mask1[2] = PermMask[HiIndex^1];
5010 Mask1[3] = -1;
5011 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005012
5013 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005014 Mask1[0] = PermMask[0];
5015 Mask1[1] = PermMask[1];
5016 Mask1[2] = HiIndex & 1 ? 6 : 4;
5017 Mask1[3] = HiIndex & 1 ? 4 : 6;
5018 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005019 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005020 Mask1[0] = HiIndex & 1 ? 2 : 0;
5021 Mask1[1] = HiIndex & 1 ? 0 : 2;
5022 Mask1[2] = PermMask[2];
5023 Mask1[3] = PermMask[3];
5024 if (Mask1[2] >= 0)
5025 Mask1[2] += 4;
5026 if (Mask1[3] >= 0)
5027 Mask1[3] += 4;
5028 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005029 }
Evan Chengace3c172008-07-22 21:13:36 +00005030 }
5031
5032 // Break it into (shuffle shuffle_hi, shuffle_lo).
5033 Locs.clear();
Nate Begeman9008ca62009-04-27 18:41:29 +00005034 SmallVector<int,8> LoMask(4U, -1);
5035 SmallVector<int,8> HiMask(4U, -1);
5036
5037 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00005038 unsigned MaskIdx = 0;
5039 unsigned LoIdx = 0;
5040 unsigned HiIdx = 2;
5041 for (unsigned i = 0; i != 4; ++i) {
5042 if (i == 2) {
5043 MaskPtr = &HiMask;
5044 MaskIdx = 1;
5045 LoIdx = 0;
5046 HiIdx = 2;
5047 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005048 int Idx = PermMask[i];
5049 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00005050 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00005051 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00005052 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00005053 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005054 LoIdx++;
5055 } else {
5056 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00005057 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005058 HiIdx++;
5059 }
5060 }
5061
Nate Begeman9008ca62009-04-27 18:41:29 +00005062 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
5063 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
5064 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00005065 for (unsigned i = 0; i != 4; ++i) {
5066 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005067 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00005068 } else {
5069 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00005070 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00005071 }
5072 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005073 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00005074}
5075
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005076static bool MayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005077 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005078 V = V.getOperand(0);
5079 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
5080 V = V.getOperand(0);
5081 if (MayFoldLoad(V))
5082 return true;
5083 return false;
5084}
5085
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005086// FIXME: the version above should always be used. Since there's
5087// a bug where several vector shuffles can't be folded because the
5088// DAG is not updated during lowering and a node claims to have two
5089// uses while it only has one, use this version, and let isel match
5090// another instruction if the load really happens to have more than
5091// one use. Remove this version after this bug get fixed.
Evan Cheng835580f2010-10-07 20:50:20 +00005092// rdar://8434668, PR8156
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005093static bool RelaxedMayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005094 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005095 V = V.getOperand(0);
5096 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
5097 V = V.getOperand(0);
5098 if (ISD::isNormalLoad(V.getNode()))
5099 return true;
5100 return false;
5101}
5102
5103/// CanFoldShuffleIntoVExtract - Check if the current shuffle is used by
5104/// a vector extract, and if both can be later optimized into a single load.
5105/// This is done in visitEXTRACT_VECTOR_ELT and the conditions are checked
5106/// here because otherwise a target specific shuffle node is going to be
5107/// emitted for this shuffle, and the optimization not done.
5108/// FIXME: This is probably not the best approach, but fix the problem
5109/// until the right path is decided.
5110static
5111bool CanXFormVExtractWithShuffleIntoLoad(SDValue V, SelectionDAG &DAG,
5112 const TargetLowering &TLI) {
5113 EVT VT = V.getValueType();
5114 ShuffleVectorSDNode *SVOp = dyn_cast<ShuffleVectorSDNode>(V);
5115
5116 // Be sure that the vector shuffle is present in a pattern like this:
5117 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), c) -> (f32 load $addr)
5118 if (!V.hasOneUse())
5119 return false;
5120
5121 SDNode *N = *V.getNode()->use_begin();
5122 if (N->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
5123 return false;
5124
5125 SDValue EltNo = N->getOperand(1);
5126 if (!isa<ConstantSDNode>(EltNo))
5127 return false;
5128
5129 // If the bit convert changed the number of elements, it is unsafe
5130 // to examine the mask.
5131 bool HasShuffleIntoBitcast = false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005132 if (V.getOpcode() == ISD::BITCAST) {
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005133 EVT SrcVT = V.getOperand(0).getValueType();
5134 if (SrcVT.getVectorNumElements() != VT.getVectorNumElements())
5135 return false;
5136 V = V.getOperand(0);
5137 HasShuffleIntoBitcast = true;
5138 }
5139
5140 // Select the input vector, guarding against out of range extract vector.
5141 unsigned NumElems = VT.getVectorNumElements();
5142 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
5143 int Idx = (Elt > NumElems) ? -1 : SVOp->getMaskElt(Elt);
5144 V = (Idx < (int)NumElems) ? V.getOperand(0) : V.getOperand(1);
5145
5146 // Skip one more bit_convert if necessary
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005147 if (V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005148 V = V.getOperand(0);
5149
5150 if (ISD::isNormalLoad(V.getNode())) {
5151 // Is the original load suitable?
5152 LoadSDNode *LN0 = cast<LoadSDNode>(V);
5153
5154 // FIXME: avoid the multi-use bug that is preventing lots of
5155 // of foldings to be detected, this is still wrong of course, but
5156 // give the temporary desired behavior, and if it happens that
5157 // the load has real more uses, during isel it will not fold, and
5158 // will generate poor code.
5159 if (!LN0 || LN0->isVolatile()) // || !LN0->hasOneUse()
5160 return false;
5161
5162 if (!HasShuffleIntoBitcast)
5163 return true;
5164
5165 // If there's a bitcast before the shuffle, check if the load type and
5166 // alignment is valid.
5167 unsigned Align = LN0->getAlignment();
5168 unsigned NewAlign =
5169 TLI.getTargetData()->getABITypeAlignment(
5170 VT.getTypeForEVT(*DAG.getContext()));
5171
5172 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
5173 return false;
5174 }
5175
5176 return true;
5177}
5178
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005179static
Evan Cheng835580f2010-10-07 20:50:20 +00005180SDValue getMOVDDup(SDValue &Op, DebugLoc &dl, SDValue V1, SelectionDAG &DAG) {
5181 EVT VT = Op.getValueType();
5182
5183 // Canonizalize to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005184 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1);
5185 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng835580f2010-10-07 20:50:20 +00005186 getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64,
5187 V1, DAG));
5188}
5189
5190static
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005191SDValue getMOVLowToHigh(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG,
5192 bool HasSSE2) {
5193 SDValue V1 = Op.getOperand(0);
5194 SDValue V2 = Op.getOperand(1);
5195 EVT VT = Op.getValueType();
5196
5197 assert(VT != MVT::v2i64 && "unsupported shuffle type");
5198
5199 if (HasSSE2 && VT == MVT::v2f64)
5200 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
5201
5202 // v4f32 or v4i32
5203 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V2, DAG);
5204}
5205
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00005206static
5207SDValue getMOVHighToLow(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG) {
5208 SDValue V1 = Op.getOperand(0);
5209 SDValue V2 = Op.getOperand(1);
5210 EVT VT = Op.getValueType();
5211
5212 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
5213 "unsupported shuffle type");
5214
5215 if (V2.getOpcode() == ISD::UNDEF)
5216 V2 = V1;
5217
5218 // v4i32 or v4f32
5219 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
5220}
5221
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005222static
5223SDValue getMOVLP(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG, bool HasSSE2) {
5224 SDValue V1 = Op.getOperand(0);
5225 SDValue V2 = Op.getOperand(1);
5226 EVT VT = Op.getValueType();
5227 unsigned NumElems = VT.getVectorNumElements();
5228
5229 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
5230 // operand of these instructions is only memory, so check if there's a
5231 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
5232 // same masks.
5233 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005234
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00005235 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005236 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005237 CanFoldLoad = true;
5238
5239 // When V1 is a load, it can be folded later into a store in isel, example:
5240 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
5241 // turns into:
5242 // (MOVLPSmr addr:$src1, VR128:$src2)
5243 // So, recognize this potential and also use MOVLPS or MOVLPD
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005244 if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005245 CanFoldLoad = true;
5246
5247 if (CanFoldLoad) {
5248 if (HasSSE2 && NumElems == 2)
5249 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
5250
5251 if (NumElems == 4)
5252 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
5253 }
5254
5255 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5256 // movl and movlp will both match v2i64, but v2i64 is never matched by
5257 // movl earlier because we make it strict to avoid messing with the movlp load
5258 // folding logic (see the code above getMOVLP call). Match it here then,
5259 // this is horrible, but will stay like this until we move all shuffle
5260 // matching to x86 specific nodes. Note that for the 1st condition all
5261 // types are matched with movsd.
5262 if ((HasSSE2 && NumElems == 2) || !X86::isMOVLMask(SVOp))
5263 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
5264 else if (HasSSE2)
5265 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
5266
5267
5268 assert(VT != MVT::v4i32 && "unsupported shuffle type");
5269
5270 // Invert the operand order and use SHUFPS to match it.
5271 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V2, V1,
5272 X86::getShuffleSHUFImmediate(SVOp), DAG);
5273}
5274
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005275static inline unsigned getUNPCKLOpcode(EVT VT) {
5276 switch(VT.getSimpleVT().SimpleTy) {
5277 case MVT::v4i32: return X86ISD::PUNPCKLDQ;
5278 case MVT::v2i64: return X86ISD::PUNPCKLQDQ;
5279 case MVT::v4f32: return X86ISD::UNPCKLPS;
5280 case MVT::v2f64: return X86ISD::UNPCKLPD;
5281 case MVT::v16i8: return X86ISD::PUNPCKLBW;
5282 case MVT::v8i16: return X86ISD::PUNPCKLWD;
5283 default:
5284 llvm_unreachable("Unknow type for unpckl");
5285 }
5286 return 0;
5287}
5288
5289static inline unsigned getUNPCKHOpcode(EVT VT) {
5290 switch(VT.getSimpleVT().SimpleTy) {
5291 case MVT::v4i32: return X86ISD::PUNPCKHDQ;
5292 case MVT::v2i64: return X86ISD::PUNPCKHQDQ;
5293 case MVT::v4f32: return X86ISD::UNPCKHPS;
5294 case MVT::v2f64: return X86ISD::UNPCKHPD;
5295 case MVT::v16i8: return X86ISD::PUNPCKHBW;
5296 case MVT::v8i16: return X86ISD::PUNPCKHWD;
5297 default:
5298 llvm_unreachable("Unknow type for unpckh");
5299 }
5300 return 0;
5301}
5302
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005303static
5304SDValue NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG,
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005305 const TargetLowering &TLI,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005306 const X86Subtarget *Subtarget) {
5307 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5308 EVT VT = Op.getValueType();
5309 DebugLoc dl = Op.getDebugLoc();
5310 SDValue V1 = Op.getOperand(0);
5311 SDValue V2 = Op.getOperand(1);
5312
5313 if (isZeroShuffle(SVOp))
5314 return getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
5315
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005316 // Handle splat operations
5317 if (SVOp->isSplat()) {
5318 // Special case, this is the only place now where it's
5319 // allowed to return a vector_shuffle operation without
5320 // using a target specific node, because *hopefully* it
5321 // will be optimized away by the dag combiner.
5322 if (VT.getVectorNumElements() <= 4 &&
5323 CanXFormVExtractWithShuffleIntoLoad(Op, DAG, TLI))
5324 return Op;
5325
5326 // Handle splats by matching through known masks
5327 if (VT.getVectorNumElements() <= 4)
5328 return SDValue();
5329
Evan Cheng835580f2010-10-07 20:50:20 +00005330 // Canonicalize all of the remaining to v4f32.
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005331 return PromoteSplat(SVOp, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005332 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005333
5334 // If the shuffle can be profitably rewritten as a narrower shuffle, then
5335 // do it!
5336 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
5337 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5338 if (NewOp.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005339 return DAG.getNode(ISD::BITCAST, dl, VT, NewOp);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005340 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
5341 // FIXME: Figure out a cleaner way to do this.
5342 // Try to make use of movq to zero out the top part.
5343 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
5344 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5345 if (NewOp.getNode()) {
5346 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
5347 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
5348 DAG, Subtarget, dl);
5349 }
5350 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
5351 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5352 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
5353 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
5354 DAG, Subtarget, dl);
5355 }
5356 }
5357 return SDValue();
5358}
5359
Dan Gohman475871a2008-07-27 21:46:04 +00005360SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005361X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00005362 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00005363 SDValue V1 = Op.getOperand(0);
5364 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00005365 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005366 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00005367 unsigned NumElems = VT.getVectorNumElements();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005368 bool isMMX = VT.getSizeInBits() == 64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005369 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
5370 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00005371 bool V1IsSplat = false;
5372 bool V2IsSplat = false;
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005373 bool HasSSE2 = Subtarget->hasSSE2() || Subtarget->hasAVX();
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00005374 bool HasSSE3 = Subtarget->hasSSE3() || Subtarget->hasAVX();
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00005375 bool HasSSSE3 = Subtarget->hasSSSE3() || Subtarget->hasAVX();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005376 MachineFunction &MF = DAG.getMachineFunction();
5377 bool OptForSize = MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005378
Dale Johannesen0488fb62010-09-30 23:57:10 +00005379 // Shuffle operations on MMX not supported.
5380 if (isMMX)
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00005381 return Op;
5382
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005383 // Vector shuffle lowering takes 3 steps:
5384 //
5385 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
5386 // narrowing and commutation of operands should be handled.
5387 // 2) Matching of shuffles with known shuffle masks to x86 target specific
5388 // shuffle nodes.
5389 // 3) Rewriting of unmatched masks into new generic shuffle operations,
5390 // so the shuffle can be broken into other shuffles and the legalizer can
5391 // try the lowering again.
5392 //
5393 // The general ideia is that no vector_shuffle operation should be left to
5394 // be matched during isel, all of them must be converted to a target specific
5395 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00005396
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005397 // Normalize the input vectors. Here splats, zeroed vectors, profitable
5398 // narrowing and commutation of operands should be handled. The actual code
5399 // doesn't include all of those, work in progress...
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005400 SDValue NewOp = NormalizeVectorShuffle(Op, DAG, *this, Subtarget);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005401 if (NewOp.getNode())
5402 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00005403
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005404 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
5405 // unpckh_undef). Only use pshufd if speed is more important than size.
5406 if (OptForSize && X86::isUNPCKL_v_undef_Mask(SVOp))
5407 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5408 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V1, DAG);
5409 if (OptForSize && X86::isUNPCKH_v_undef_Mask(SVOp))
5410 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5411 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00005412
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005413 if (X86::isMOVDDUPMask(SVOp) && HasSSE3 && V2IsUndef &&
Dale Johannesen0488fb62010-09-30 23:57:10 +00005414 RelaxedMayFoldVectorLoad(V1))
Evan Cheng835580f2010-10-07 20:50:20 +00005415 return getMOVDDup(Op, dl, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005416
Dale Johannesen0488fb62010-09-30 23:57:10 +00005417 if (X86::isMOVHLPS_v_undef_Mask(SVOp))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005418 return getMOVHighToLow(Op, dl, DAG);
5419
5420 // Use to match splats
5421 if (HasSSE2 && X86::isUNPCKHMask(SVOp) && V2IsUndef &&
5422 (VT == MVT::v2f64 || VT == MVT::v2i64))
5423 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
5424
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005425 if (X86::isPSHUFDMask(SVOp)) {
5426 // The actual implementation will match the mask in the if above and then
5427 // during isel it can match several different instructions, not only pshufd
5428 // as its name says, sad but true, emulate the behavior for now...
5429 if (X86::isMOVDDUPMask(SVOp) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
5430 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
5431
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005432 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
5433
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005434 if (HasSSE2 && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005435 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
5436
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005437 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005438 return getTargetShuffleNode(X86ISD::SHUFPD, dl, VT, V1, V1,
5439 TargetMask, DAG);
5440
5441 if (VT == MVT::v4f32)
5442 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V1, V1,
5443 TargetMask, DAG);
5444 }
Eric Christopherfd179292009-08-27 18:07:15 +00005445
Evan Chengf26ffe92008-05-29 08:22:04 +00005446 // Check if this can be converted into a logical shift.
5447 bool isLeft = false;
5448 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00005449 SDValue ShVal;
Nate Begeman9008ca62009-04-27 18:41:29 +00005450 bool isShift = getSubtarget()->hasSSE2() &&
Evan Chengc3630942009-12-09 21:00:30 +00005451 isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00005452 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00005453 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00005454 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00005455 EVT EltVT = VT.getVectorElementType();
5456 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00005457 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005458 }
Eric Christopherfd179292009-08-27 18:07:15 +00005459
Nate Begeman9008ca62009-04-27 18:41:29 +00005460 if (X86::isMOVLMask(SVOp)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005461 if (V1IsUndef)
5462 return V2;
Gabor Greifba36cb52008-08-28 21:40:38 +00005463 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00005464 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Dale Johannesen0488fb62010-09-30 23:57:10 +00005465 if (!X86::isMOVLPMask(SVOp)) {
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005466 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00005467 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
5468
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005469 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00005470 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
5471 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00005472 }
Eric Christopherfd179292009-08-27 18:07:15 +00005473
Nate Begeman9008ca62009-04-27 18:41:29 +00005474 // FIXME: fold these into legal mask.
Dale Johannesen0488fb62010-09-30 23:57:10 +00005475 if (X86::isMOVLHPSMask(SVOp) && !X86::isUNPCKLMask(SVOp))
5476 return getMOVLowToHigh(Op, dl, DAG, HasSSE2);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005477
Dale Johannesen0488fb62010-09-30 23:57:10 +00005478 if (X86::isMOVHLPSMask(SVOp))
5479 return getMOVHighToLow(Op, dl, DAG);
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00005480
Dale Johannesen0488fb62010-09-30 23:57:10 +00005481 if (X86::isMOVSHDUPMask(SVOp) && HasSSE3 && V2IsUndef && NumElems == 4)
5482 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00005483
Dale Johannesen0488fb62010-09-30 23:57:10 +00005484 if (X86::isMOVSLDUPMask(SVOp) && HasSSE3 && V2IsUndef && NumElems == 4)
5485 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00005486
Dale Johannesen0488fb62010-09-30 23:57:10 +00005487 if (X86::isMOVLPMask(SVOp))
5488 return getMOVLP(Op, dl, DAG, HasSSE2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005489
Nate Begeman9008ca62009-04-27 18:41:29 +00005490 if (ShouldXformToMOVHLPS(SVOp) ||
5491 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
5492 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005493
Evan Chengf26ffe92008-05-29 08:22:04 +00005494 if (isShift) {
5495 // No better options. Use a vshl / vsrl.
Dan Gohman8a55ce42009-09-23 21:02:20 +00005496 EVT EltVT = VT.getVectorElementType();
5497 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00005498 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005499 }
Eric Christopherfd179292009-08-27 18:07:15 +00005500
Evan Cheng9eca5e82006-10-25 21:49:50 +00005501 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00005502 // FIXME: This should also accept a bitcast of a splat? Be careful, not
5503 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00005504 V1IsSplat = isSplatVector(V1.getNode());
5505 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00005506
Chris Lattner8a594482007-11-25 00:24:49 +00005507 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00005508 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005509 Op = CommuteVectorShuffle(SVOp, DAG);
5510 SVOp = cast<ShuffleVectorSDNode>(Op);
5511 V1 = SVOp->getOperand(0);
5512 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00005513 std::swap(V1IsSplat, V2IsSplat);
5514 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00005515 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00005516 }
5517
Nate Begeman9008ca62009-04-27 18:41:29 +00005518 if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) {
5519 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00005520 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00005521 return V1;
5522 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
5523 // the instruction selector will not match, so get a canonical MOVL with
5524 // swapped operands to undo the commute.
5525 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00005526 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005527
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005528 if (X86::isUNPCKLMask(SVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005529 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005530
5531 if (X86::isUNPCKHMask(SVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005532 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V2, DAG);
Evan Chenge1113032006-10-04 18:33:38 +00005533
Evan Cheng9bbbb982006-10-25 20:48:19 +00005534 if (V2IsSplat) {
5535 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005536 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00005537 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00005538 SDValue NewMask = NormalizeMask(SVOp, DAG);
5539 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
5540 if (NSVOp != SVOp) {
5541 if (X86::isUNPCKLMask(NSVOp, true)) {
5542 return NewMask;
5543 } else if (X86::isUNPCKHMask(NSVOp, true)) {
5544 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005545 }
5546 }
5547 }
5548
Evan Cheng9eca5e82006-10-25 21:49:50 +00005549 if (Commuted) {
5550 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00005551 // FIXME: this seems wrong.
5552 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
5553 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005554
5555 if (X86::isUNPCKLMask(NewSVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005556 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V2, V1, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005557
5558 if (X86::isUNPCKHMask(NewSVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005559 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V2, V1, DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00005560 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005561
Nate Begeman9008ca62009-04-27 18:41:29 +00005562 // Normalize the node to match x86 shuffle ops if needed
Dale Johannesen0488fb62010-09-30 23:57:10 +00005563 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp))
Nate Begeman9008ca62009-04-27 18:41:29 +00005564 return CommuteVectorShuffle(SVOp, DAG);
5565
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00005566 // The checks below are all present in isShuffleMaskLegal, but they are
5567 // inlined here right now to enable us to directly emit target specific
5568 // nodes, and remove one by one until they don't return Op anymore.
5569 SmallVector<int, 16> M;
5570 SVOp->getMask(M);
5571
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00005572 if (isPALIGNRMask(M, VT, HasSSSE3))
5573 return getTargetShuffleNode(X86ISD::PALIGN, dl, VT, V1, V2,
5574 X86::getShufflePALIGNRImmediate(SVOp),
5575 DAG);
5576
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00005577 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
5578 SVOp->getSplatIndex() == 0 && V2IsUndef) {
5579 if (VT == MVT::v2f64)
5580 return getTargetShuffleNode(X86ISD::UNPCKLPD, dl, VT, V1, V1, DAG);
5581 if (VT == MVT::v2i64)
5582 return getTargetShuffleNode(X86ISD::PUNPCKLQDQ, dl, VT, V1, V1, DAG);
5583 }
5584
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00005585 if (isPSHUFHWMask(M, VT))
5586 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
5587 X86::getShufflePSHUFHWImmediate(SVOp),
5588 DAG);
5589
5590 if (isPSHUFLWMask(M, VT))
5591 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
5592 X86::getShufflePSHUFLWImmediate(SVOp),
5593 DAG);
5594
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00005595 if (isSHUFPMask(M, VT)) {
5596 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
5597 if (VT == MVT::v4f32 || VT == MVT::v4i32)
5598 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V1, V2,
5599 TargetMask, DAG);
5600 if (VT == MVT::v2f64 || VT == MVT::v2i64)
5601 return getTargetShuffleNode(X86ISD::SHUFPD, dl, VT, V1, V2,
5602 TargetMask, DAG);
5603 }
5604
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005605 if (X86::isUNPCKL_v_undef_Mask(SVOp))
5606 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5607 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V1, DAG);
5608 if (X86::isUNPCKH_v_undef_Mask(SVOp))
5609 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5610 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
5611
Evan Cheng14b32e12007-12-11 01:46:18 +00005612 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
Owen Anderson825b72b2009-08-11 20:47:22 +00005613 if (VT == MVT::v8i16) {
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005614 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00005615 if (NewOp.getNode())
Evan Cheng14b32e12007-12-11 01:46:18 +00005616 return NewOp;
5617 }
5618
Owen Anderson825b72b2009-08-11 20:47:22 +00005619 if (VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005620 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005621 if (NewOp.getNode())
5622 return NewOp;
5623 }
Eric Christopherfd179292009-08-27 18:07:15 +00005624
Dale Johannesen0488fb62010-09-30 23:57:10 +00005625 // Handle all 4 wide cases with a number of shuffles.
5626 if (NumElems == 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00005627 return LowerVECTOR_SHUFFLE_4wide(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005628
Dan Gohman475871a2008-07-27 21:46:04 +00005629 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005630}
5631
Dan Gohman475871a2008-07-27 21:46:04 +00005632SDValue
5633X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00005634 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005635 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005636 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005637 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005638 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005639 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00005640 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005641 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00005642 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005643 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00005644 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
5645 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
5646 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005647 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
5648 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005649 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005650 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00005651 Op.getOperand(0)),
5652 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00005653 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005654 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00005655 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005656 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00005657 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Owen Anderson825b72b2009-08-11 20:47:22 +00005658 } else if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00005659 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
5660 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00005661 // result has a single use which is a store or a bitcast to i32. And in
5662 // the case of a store, it's not worth it if the index is a constant 0,
5663 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00005664 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00005665 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00005666 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00005667 if ((User->getOpcode() != ISD::STORE ||
5668 (isa<ConstantSDNode>(Op.getOperand(1)) &&
5669 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005670 (User->getOpcode() != ISD::BITCAST ||
Owen Anderson825b72b2009-08-11 20:47:22 +00005671 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00005672 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00005673 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005674 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00005675 Op.getOperand(0)),
5676 Op.getOperand(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005677 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Extract);
Owen Anderson825b72b2009-08-11 20:47:22 +00005678 } else if (VT == MVT::i32) {
Mon P Wangf0fcdd82009-01-15 21:10:20 +00005679 // ExtractPS works with constant index.
5680 if (isa<ConstantSDNode>(Op.getOperand(1)))
5681 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00005682 }
Dan Gohman475871a2008-07-27 21:46:04 +00005683 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00005684}
5685
5686
Dan Gohman475871a2008-07-27 21:46:04 +00005687SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005688X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
5689 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005690 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00005691 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005692
Evan Cheng62a3f152008-03-24 21:52:23 +00005693 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005694 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00005695 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00005696 return Res;
5697 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00005698
Owen Andersone50ed302009-08-10 22:56:29 +00005699 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005700 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005701 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005702 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00005703 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005704 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00005705 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005706 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
5707 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005708 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005709 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00005710 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005711 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00005712 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00005713 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00005714 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00005715 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00005716 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00005717 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005718 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005719 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005720 if (Idx == 0)
5721 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00005722
Evan Cheng0db9fe62006-04-25 20:13:52 +00005723 // SHUFPS the element to the lowest double word, then movss.
Nate Begeman9008ca62009-04-27 18:41:29 +00005724 int Mask[4] = { Idx, -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00005725 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00005726 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00005727 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00005728 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00005729 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00005730 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00005731 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
5732 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
5733 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005734 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005735 if (Idx == 0)
5736 return Op;
5737
5738 // UNPCKHPD the element to the lowest double word, then movsd.
5739 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
5740 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00005741 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00005742 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00005743 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00005744 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00005745 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00005746 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005747 }
5748
Dan Gohman475871a2008-07-27 21:46:04 +00005749 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005750}
5751
Dan Gohman475871a2008-07-27 21:46:04 +00005752SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005753X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op,
5754 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005755 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00005756 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005757 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00005758
Dan Gohman475871a2008-07-27 21:46:04 +00005759 SDValue N0 = Op.getOperand(0);
5760 SDValue N1 = Op.getOperand(1);
5761 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00005762
Dan Gohman8a55ce42009-09-23 21:02:20 +00005763 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00005764 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00005765 unsigned Opc;
5766 if (VT == MVT::v8i16)
5767 Opc = X86ISD::PINSRW;
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00005768 else if (VT == MVT::v16i8)
5769 Opc = X86ISD::PINSRB;
5770 else
5771 Opc = X86ISD::PINSRB;
5772
Nate Begeman14d12ca2008-02-11 04:19:36 +00005773 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
5774 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00005775 if (N1.getValueType() != MVT::i32)
5776 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
5777 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005778 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00005779 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00005780 } else if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00005781 // Bits [7:6] of the constant are the source select. This will always be
5782 // zero here. The DAG Combiner may combine an extract_elt index into these
5783 // bits. For example (insert (extract, 3), 2) could be matched by putting
5784 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00005785 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00005786 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00005787 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00005788 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005789 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00005790 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00005791 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00005792 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00005793 } else if (EltVT == MVT::i32 && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00005794 // PINSR* works with constant index.
5795 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00005796 }
Dan Gohman475871a2008-07-27 21:46:04 +00005797 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00005798}
5799
Dan Gohman475871a2008-07-27 21:46:04 +00005800SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005801X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005802 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00005803 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00005804
5805 if (Subtarget->hasSSE41())
5806 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
5807
Dan Gohman8a55ce42009-09-23 21:02:20 +00005808 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00005809 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00005810
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005811 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00005812 SDValue N0 = Op.getOperand(0);
5813 SDValue N1 = Op.getOperand(1);
5814 SDValue N2 = Op.getOperand(2);
Evan Cheng794405e2007-12-12 07:55:34 +00005815
Dan Gohman8a55ce42009-09-23 21:02:20 +00005816 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00005817 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
5818 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00005819 if (N1.getValueType() != MVT::i32)
5820 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
5821 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005822 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesen0488fb62010-09-30 23:57:10 +00005823 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005824 }
Dan Gohman475871a2008-07-27 21:46:04 +00005825 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005826}
5827
Dan Gohman475871a2008-07-27 21:46:04 +00005828SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005829X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005830 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00005831
Chris Lattnerf172ecd2010-07-04 23:07:25 +00005832 if (Op.getValueType() == MVT::v1i64 &&
5833 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00005834 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00005835
Owen Anderson825b72b2009-08-11 20:47:22 +00005836 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Dale Johannesen0488fb62010-09-30 23:57:10 +00005837 assert(Op.getValueType().getSimpleVT().getSizeInBits() == 128 &&
5838 "Expected an SSE type!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005839 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(),
Dale Johannesen0488fb62010-09-30 23:57:10 +00005840 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005841}
5842
Bill Wendling056292f2008-09-16 21:48:12 +00005843// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
5844// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
5845// one of the above mentioned nodes. It has to be wrapped because otherwise
5846// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
5847// be used to form addressing mode. These wrapped nodes will be selected
5848// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00005849SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005850X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005851 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00005852
Chris Lattner41621a22009-06-26 19:22:52 +00005853 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
5854 // global base reg.
5855 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00005856 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005857 CodeModel::Model M = getTargetMachine().getCodeModel();
5858
Chris Lattner4f066492009-07-11 20:29:19 +00005859 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005860 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00005861 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00005862 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005863 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00005864 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005865 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00005866
Evan Cheng1606e8e2009-03-13 07:51:59 +00005867 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00005868 CP->getAlignment(),
5869 CP->getOffset(), OpFlag);
5870 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00005871 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00005872 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00005873 if (OpFlag) {
5874 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00005875 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00005876 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00005877 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005878 }
5879
5880 return Result;
5881}
5882
Dan Gohmand858e902010-04-17 15:26:15 +00005883SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00005884 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00005885
Chris Lattner18c59872009-06-27 04:16:01 +00005886 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
5887 // global base reg.
5888 unsigned char OpFlag = 0;
5889 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005890 CodeModel::Model M = getTargetMachine().getCodeModel();
5891
Chris Lattner4f066492009-07-11 20:29:19 +00005892 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005893 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00005894 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00005895 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005896 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00005897 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005898 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00005899
Chris Lattner18c59872009-06-27 04:16:01 +00005900 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
5901 OpFlag);
5902 DebugLoc DL = JT->getDebugLoc();
5903 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00005904
Chris Lattner18c59872009-06-27 04:16:01 +00005905 // With PIC, the address is actually $g + Offset.
Chris Lattner1e61e692010-11-15 02:46:57 +00005906 if (OpFlag)
Chris Lattner18c59872009-06-27 04:16:01 +00005907 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
5908 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00005909 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00005910 Result);
Eric Christopherfd179292009-08-27 18:07:15 +00005911
Chris Lattner18c59872009-06-27 04:16:01 +00005912 return Result;
5913}
5914
5915SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005916X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00005917 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00005918
Chris Lattner18c59872009-06-27 04:16:01 +00005919 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
5920 // global base reg.
5921 unsigned char OpFlag = 0;
5922 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005923 CodeModel::Model M = getTargetMachine().getCodeModel();
5924
Chris Lattner4f066492009-07-11 20:29:19 +00005925 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005926 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00005927 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00005928 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005929 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00005930 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005931 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00005932
Chris Lattner18c59872009-06-27 04:16:01 +00005933 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00005934
Chris Lattner18c59872009-06-27 04:16:01 +00005935 DebugLoc DL = Op.getDebugLoc();
5936 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00005937
5938
Chris Lattner18c59872009-06-27 04:16:01 +00005939 // With PIC, the address is actually $g + Offset.
5940 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00005941 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00005942 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
5943 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00005944 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00005945 Result);
5946 }
Eric Christopherfd179292009-08-27 18:07:15 +00005947
Chris Lattner18c59872009-06-27 04:16:01 +00005948 return Result;
5949}
5950
Dan Gohman475871a2008-07-27 21:46:04 +00005951SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005952X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00005953 // Create the TargetBlockAddressAddress node.
5954 unsigned char OpFlags =
5955 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00005956 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00005957 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Dan Gohman29cbade2009-11-20 23:18:13 +00005958 DebugLoc dl = Op.getDebugLoc();
5959 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(),
5960 /*isTarget=*/true, OpFlags);
5961
Dan Gohmanf705adb2009-10-30 01:28:02 +00005962 if (Subtarget->isPICStyleRIPRel() &&
5963 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00005964 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
5965 else
5966 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00005967
Dan Gohman29cbade2009-11-20 23:18:13 +00005968 // With PIC, the address is actually $g + Offset.
5969 if (isGlobalRelativeToPICBase(OpFlags)) {
5970 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
5971 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
5972 Result);
5973 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00005974
5975 return Result;
5976}
5977
5978SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00005979X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00005980 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00005981 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00005982 // Create the TargetGlobalAddress node, folding in the constant
5983 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00005984 unsigned char OpFlags =
5985 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005986 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00005987 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005988 if (OpFlags == X86II::MO_NO_FLAG &&
5989 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00005990 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00005991 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00005992 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00005993 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00005994 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00005995 }
Eric Christopherfd179292009-08-27 18:07:15 +00005996
Chris Lattner4f066492009-07-11 20:29:19 +00005997 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005998 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00005999 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
6000 else
6001 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00006002
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006003 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00006004 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00006005 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
6006 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006007 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006008 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006009
Chris Lattner36c25012009-07-10 07:34:39 +00006010 // For globals that require a load from a stub to get the address, emit the
6011 // load.
6012 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00006013 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00006014 MachinePointerInfo::getGOT(), false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006015
Dan Gohman6520e202008-10-18 02:06:02 +00006016 // If there was a non-zero offset that we didn't fold, create an explicit
6017 // addition for it.
6018 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00006019 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00006020 DAG.getConstant(Offset, getPointerTy()));
6021
Evan Cheng0db9fe62006-04-25 20:13:52 +00006022 return Result;
6023}
6024
Evan Chengda43bcf2008-09-24 00:05:32 +00006025SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006026X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00006027 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00006028 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006029 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00006030}
6031
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006032static SDValue
6033GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00006034 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Chris Lattnerb903bed2009-06-26 21:20:29 +00006035 unsigned char OperandFlags) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006036 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Owen Anderson825b72b2009-08-11 20:47:22 +00006037 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006038 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00006039 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006040 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00006041 GA->getOffset(),
6042 OperandFlags);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006043 if (InFlag) {
6044 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00006045 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006046 } else {
6047 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00006048 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006049 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006050
6051 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00006052 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006053
Rafael Espindola15f1b662009-04-24 12:59:40 +00006054 SDValue Flag = Chain.getValue(1);
6055 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006056}
6057
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006058// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00006059static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006060LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006061 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00006062 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00006063 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
6064 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006065 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006066 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006067 InFlag = Chain.getValue(1);
6068
Chris Lattnerb903bed2009-06-26 21:20:29 +00006069 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006070}
6071
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006072// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00006073static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006074LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006075 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00006076 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
6077 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006078}
6079
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006080// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
6081// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00006082static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006083 const EVT PtrVT, TLSModel::Model model,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00006084 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00006085 DebugLoc dl = GA->getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00006086
Chris Lattnerf93b90c2010-09-22 04:39:11 +00006087 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
6088 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
6089 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00006090
Michael J. Spencerec38de22010-10-10 22:04:20 +00006091 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Chris Lattnerf93b90c2010-09-22 04:39:11 +00006092 DAG.getIntPtrConstant(0),
6093 MachinePointerInfo(Ptr), false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00006094
Chris Lattnerb903bed2009-06-26 21:20:29 +00006095 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00006096 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
6097 // initialexec.
6098 unsigned WrapperKind = X86ISD::Wrapper;
6099 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00006100 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Chris Lattner18c59872009-06-27 04:16:01 +00006101 } else if (is64Bit) {
6102 assert(model == TLSModel::InitialExec);
6103 OperandFlags = X86II::MO_GOTTPOFF;
6104 WrapperKind = X86ISD::WrapperRIP;
6105 } else {
6106 assert(model == TLSModel::InitialExec);
6107 OperandFlags = X86II::MO_INDNTPOFF;
Chris Lattnerb903bed2009-06-26 21:20:29 +00006108 }
Eric Christopherfd179292009-08-27 18:07:15 +00006109
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006110 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
6111 // exec)
Michael J. Spencerec38de22010-10-10 22:04:20 +00006112 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Devang Patel0d881da2010-07-06 22:08:15 +00006113 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00006114 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00006115 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00006116
Rafael Espindola9a580232009-02-27 13:37:18 +00006117 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00006118 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00006119 MachinePointerInfo::getGOT(), false, false, 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00006120
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006121 // The address of the thread local variable is the add of the thread
6122 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00006123 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006124}
6125
Dan Gohman475871a2008-07-27 21:46:04 +00006126SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006127X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00006128
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006129 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00006130 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00006131
Eric Christopher30ef0e52010-06-03 04:07:48 +00006132 if (Subtarget->isTargetELF()) {
6133 // TODO: implement the "local dynamic" model
6134 // TODO: implement the "initial exec"model for pic executables
Michael J. Spencerec38de22010-10-10 22:04:20 +00006135
Eric Christopher30ef0e52010-06-03 04:07:48 +00006136 // If GV is an alias then use the aliasee for determining
6137 // thread-localness.
6138 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
6139 GV = GA->resolveAliasedGlobal(false);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006140
6141 TLSModel::Model model
Eric Christopher30ef0e52010-06-03 04:07:48 +00006142 = getTLSModel(GV, getTargetMachine().getRelocationModel());
Michael J. Spencerec38de22010-10-10 22:04:20 +00006143
Eric Christopher30ef0e52010-06-03 04:07:48 +00006144 switch (model) {
6145 case TLSModel::GeneralDynamic:
6146 case TLSModel::LocalDynamic: // not implemented
6147 if (Subtarget->is64Bit())
6148 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
6149 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Michael J. Spencerec38de22010-10-10 22:04:20 +00006150
Eric Christopher30ef0e52010-06-03 04:07:48 +00006151 case TLSModel::InitialExec:
6152 case TLSModel::LocalExec:
6153 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
6154 Subtarget->is64Bit());
6155 }
6156 } else if (Subtarget->isTargetDarwin()) {
6157 // Darwin only has one model of TLS. Lower to that.
6158 unsigned char OpFlag = 0;
6159 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
6160 X86ISD::WrapperRIP : X86ISD::Wrapper;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006161
Eric Christopher30ef0e52010-06-03 04:07:48 +00006162 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6163 // global base reg.
6164 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
6165 !Subtarget->is64Bit();
6166 if (PIC32)
6167 OpFlag = X86II::MO_TLVP_PIC_BASE;
6168 else
6169 OpFlag = X86II::MO_TLVP;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006170 DebugLoc DL = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00006171 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopherd8c05362010-12-09 06:25:53 +00006172 GA->getValueType(0),
Eric Christopher30ef0e52010-06-03 04:07:48 +00006173 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00006174 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006175
Eric Christopher30ef0e52010-06-03 04:07:48 +00006176 // With PIC32, the address is actually $g + Offset.
6177 if (PIC32)
6178 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
6179 DAG.getNode(X86ISD::GlobalBaseReg,
6180 DebugLoc(), getPointerTy()),
6181 Offset);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006182
Eric Christopher30ef0e52010-06-03 04:07:48 +00006183 // Lowering the machine isd will make sure everything is in the right
6184 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00006185 SDValue Chain = DAG.getEntryNode();
6186 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
6187 SDValue Args[] = { Chain, Offset };
6188 Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006189
Eric Christopher30ef0e52010-06-03 04:07:48 +00006190 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
6191 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
6192 MFI->setAdjustsStack(true);
Eric Christopher8bce7cc2010-12-09 00:27:58 +00006193
Eric Christopher30ef0e52010-06-03 04:07:48 +00006194 // And our return value (tls address) is in the standard call return value
6195 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00006196 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
6197 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy());
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006198 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00006199
Eric Christopher30ef0e52010-06-03 04:07:48 +00006200 assert(false &&
6201 "TLS not implemented for this target.");
Eric Christopherfd179292009-08-27 18:07:15 +00006202
Torok Edwinc23197a2009-07-14 16:55:14 +00006203 llvm_unreachable("Unreachable");
Chris Lattner5867de12009-04-01 22:14:45 +00006204 return SDValue();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006205}
6206
Evan Cheng0db9fe62006-04-25 20:13:52 +00006207
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006208/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michelfdc40a02009-02-17 22:15:04 +00006209/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00006210SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman4c1fa612008-03-03 22:22:09 +00006211 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00006212 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006213 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006214 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006215 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00006216 SDValue ShOpLo = Op.getOperand(0);
6217 SDValue ShOpHi = Op.getOperand(1);
6218 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00006219 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00006220 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00006221 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00006222
Dan Gohman475871a2008-07-27 21:46:04 +00006223 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006224 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00006225 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
6226 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006227 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00006228 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
6229 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006230 }
Evan Chenge3413162006-01-09 18:33:28 +00006231
Owen Anderson825b72b2009-08-11 20:47:22 +00006232 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
6233 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00006234 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00006235 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00006236
Dan Gohman475871a2008-07-27 21:46:04 +00006237 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00006238 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00006239 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
6240 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00006241
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006242 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00006243 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
6244 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006245 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00006246 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
6247 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006248 }
6249
Dan Gohman475871a2008-07-27 21:46:04 +00006250 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00006251 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006252}
Evan Chenga3195e82006-01-12 22:54:21 +00006253
Dan Gohmand858e902010-04-17 15:26:15 +00006254SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
6255 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006256 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00006257
Dale Johannesen0488fb62010-09-30 23:57:10 +00006258 if (SrcVT.isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00006259 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00006260
Owen Anderson825b72b2009-08-11 20:47:22 +00006261 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00006262 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00006263
Eli Friedman36df4992009-05-27 00:47:34 +00006264 // These are really Legal; return the operand so the caller accepts it as
6265 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00006266 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00006267 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00006268 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00006269 Subtarget->is64Bit()) {
6270 return Op;
6271 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006272
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006273 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006274 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006275 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00006276 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006277 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00006278 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00006279 StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006280 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006281 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00006282 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
6283}
Evan Cheng0db9fe62006-04-25 20:13:52 +00006284
Owen Andersone50ed302009-08-10 22:56:29 +00006285SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Michael J. Spencerec38de22010-10-10 22:04:20 +00006286 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00006287 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006288 // Build the FILD
Chris Lattner492a43e2010-09-22 01:28:21 +00006289 DebugLoc DL = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00006290 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00006291 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006292 if (useSSE)
Owen Anderson825b72b2009-08-11 20:47:22 +00006293 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
Chris Lattner5a88b832007-02-25 07:10:00 +00006294 else
Owen Anderson825b72b2009-08-11 20:47:22 +00006295 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006296
Chris Lattner492a43e2010-09-22 01:28:21 +00006297 unsigned ByteSize = SrcVT.getSizeInBits()/8;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006298
Chris Lattner492a43e2010-09-22 01:28:21 +00006299 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
6300 MachineMemOperand *MMO =
6301 DAG.getMachineFunction()
6302 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6303 MachineMemOperand::MOLoad, ByteSize, ByteSize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006304
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006305 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Chris Lattner492a43e2010-09-22 01:28:21 +00006306 SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
6307 X86ISD::FILD, DL,
6308 Tys, Ops, array_lengthof(Ops),
6309 SrcVT, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006310
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006311 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006312 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00006313 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006314
6315 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
6316 // shouldn't be necessary except that RFP cannot be live across
6317 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006318 MachineFunction &MF = DAG.getMachineFunction();
Bob Wilsoneafca4e2010-09-22 17:35:14 +00006319 unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
6320 int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006321 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00006322 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006323 SDValue Ops[] = {
6324 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
6325 };
Chris Lattner492a43e2010-09-22 01:28:21 +00006326 MachineMemOperand *MMO =
6327 DAG.getMachineFunction()
6328 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
Bob Wilsoneafca4e2010-09-22 17:35:14 +00006329 MachineMemOperand::MOStore, SSFISize, SSFISize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006330
Chris Lattner492a43e2010-09-22 01:28:21 +00006331 Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
6332 Ops, array_lengthof(Ops),
6333 Op.getValueType(), MMO);
6334 Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006335 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006336 false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006337 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006338
Evan Cheng0db9fe62006-04-25 20:13:52 +00006339 return Result;
6340}
6341
Bill Wendling8b8a6362009-01-17 03:56:04 +00006342// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00006343SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
6344 SelectionDAG &DAG) const {
Bill Wendling8b8a6362009-01-17 03:56:04 +00006345 // This algorithm is not obvious. Here it is in C code, more or less:
6346 /*
6347 double uint64_to_double( uint32_t hi, uint32_t lo ) {
6348 static const __m128i exp = { 0x4330000045300000ULL, 0 };
6349 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00006350
Bill Wendling8b8a6362009-01-17 03:56:04 +00006351 // Copy ints to xmm registers.
6352 __m128i xh = _mm_cvtsi32_si128( hi );
6353 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00006354
Bill Wendling8b8a6362009-01-17 03:56:04 +00006355 // Combine into low half of a single xmm register.
6356 __m128i x = _mm_unpacklo_epi32( xh, xl );
6357 __m128d d;
6358 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00006359
Bill Wendling8b8a6362009-01-17 03:56:04 +00006360 // Merge in appropriate exponents to give the integer bits the right
6361 // magnitude.
6362 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00006363
Bill Wendling8b8a6362009-01-17 03:56:04 +00006364 // Subtract away the biases to deal with the IEEE-754 double precision
6365 // implicit 1.
6366 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00006367
Bill Wendling8b8a6362009-01-17 03:56:04 +00006368 // All conversions up to here are exact. The correctly rounded result is
6369 // calculated using the current rounding mode using the following
6370 // horizontal add.
6371 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
6372 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
6373 // store doesn't really need to be here (except
6374 // maybe to zero the other double)
6375 return sd;
6376 }
6377 */
Dale Johannesen040225f2008-10-21 23:07:49 +00006378
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006379 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00006380 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00006381
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006382 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00006383 std::vector<Constant*> CV0;
Owen Andersoneed707b2009-07-24 23:12:02 +00006384 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x45300000)));
6385 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x43300000)));
6386 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
6387 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
Owen Andersonaf7ec972009-07-28 21:19:26 +00006388 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006389 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006390
Bill Wendling8b8a6362009-01-17 03:56:04 +00006391 std::vector<Constant*> CV1;
Owen Andersona90b3dc2009-07-15 21:51:10 +00006392 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006393 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
Owen Andersona90b3dc2009-07-15 21:51:10 +00006394 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006395 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Owen Andersonaf7ec972009-07-28 21:19:26 +00006396 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006397 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006398
Owen Anderson825b72b2009-08-11 20:47:22 +00006399 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6400 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00006401 Op.getOperand(0),
6402 DAG.getIntPtrConstant(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006403 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6404 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00006405 Op.getOperand(0),
6406 DAG.getIntPtrConstant(0)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006407 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
6408 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Chris Lattnere8639032010-09-21 06:22:23 +00006409 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006410 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00006411 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006412 SDValue XR2F = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Unpck2);
Owen Anderson825b72b2009-08-11 20:47:22 +00006413 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00006414 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006415 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00006416 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006417
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006418 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman9008ca62009-04-27 18:41:29 +00006419 int ShufMask[2] = { 1, -1 };
Owen Anderson825b72b2009-08-11 20:47:22 +00006420 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
6421 DAG.getUNDEF(MVT::v2f64), ShufMask);
6422 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
6423 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006424 DAG.getIntPtrConstant(0));
6425}
6426
Bill Wendling8b8a6362009-01-17 03:56:04 +00006427// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00006428SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
6429 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006430 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00006431 // FP constant to bias correct the final result.
6432 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00006433 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006434
6435 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00006436 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6437 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Bill Wendling8b8a6362009-01-17 03:56:04 +00006438 Op.getOperand(0),
6439 DAG.getIntPtrConstant(0)));
6440
Owen Anderson825b72b2009-08-11 20:47:22 +00006441 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006442 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00006443 DAG.getIntPtrConstant(0));
6444
6445 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00006446 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006447 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00006448 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006449 MVT::v2f64, Load)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006450 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00006451 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006452 MVT::v2f64, Bias)));
6453 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006454 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00006455 DAG.getIntPtrConstant(0));
6456
6457 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00006458 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006459
6460 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00006461 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00006462
Owen Anderson825b72b2009-08-11 20:47:22 +00006463 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006464 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00006465 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00006466 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006467 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00006468 }
6469
6470 // Handle final rounding.
6471 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00006472}
6473
Dan Gohmand858e902010-04-17 15:26:15 +00006474SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
6475 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00006476 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006477 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00006478
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006479 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00006480 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
6481 // the optimization here.
6482 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00006483 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00006484
Owen Andersone50ed302009-08-10 22:56:29 +00006485 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006486 EVT DstVT = Op.getValueType();
6487 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00006488 return LowerUINT_TO_FP_i64(Op, DAG);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006489 else if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00006490 return LowerUINT_TO_FP_i32(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00006491
6492 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00006493 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006494 if (SrcVT == MVT::i32) {
6495 SDValue WordOff = DAG.getConstant(4, getPointerTy());
6496 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
6497 getPointerTy(), StackSlot, WordOff);
6498 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006499 StackSlot, MachinePointerInfo(),
6500 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006501 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006502 OffsetSlot, MachinePointerInfo(),
6503 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006504 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
6505 return Fild;
6506 }
6507
6508 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
6509 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006510 StackSlot, MachinePointerInfo(),
6511 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006512 // For i64 source, we need to add the appropriate power of 2 if the input
6513 // was negative. This is the same as the optimization in
6514 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
6515 // we must be careful to do the computation in x87 extended precision, not
6516 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00006517 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
6518 MachineMemOperand *MMO =
6519 DAG.getMachineFunction()
6520 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6521 MachineMemOperand::MOLoad, 8, 8);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006522
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006523 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
6524 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Chris Lattner492a43e2010-09-22 01:28:21 +00006525 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, 3,
6526 MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006527
6528 APInt FF(32, 0x5F800000ULL);
6529
6530 // Check whether the sign bit is set.
6531 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
6532 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
6533 ISD::SETLT);
6534
6535 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
6536 SDValue FudgePtr = DAG.getConstantPool(
6537 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
6538 getPointerTy());
6539
6540 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
6541 SDValue Zero = DAG.getIntPtrConstant(0);
6542 SDValue Four = DAG.getIntPtrConstant(4);
6543 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
6544 Zero, Four);
6545 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
6546
6547 // Load the value out, extending it from f32 to f80.
6548 // FIXME: Avoid the extend by constructing the right constant pool?
Evan Chengbcc80172010-07-07 22:15:37 +00006549 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, MVT::f80, dl, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00006550 FudgePtr, MachinePointerInfo::getConstantPool(),
6551 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006552 // Extend everything to 80 bits to force it to be done on x87.
6553 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
6554 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00006555}
6556
Dan Gohman475871a2008-07-27 21:46:04 +00006557std::pair<SDValue,SDValue> X86TargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00006558FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) const {
Chris Lattner07290932010-09-22 01:05:16 +00006559 DebugLoc DL = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00006560
Owen Andersone50ed302009-08-10 22:56:29 +00006561 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00006562
6563 if (!IsSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006564 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
6565 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00006566 }
6567
Owen Anderson825b72b2009-08-11 20:47:22 +00006568 assert(DstTy.getSimpleVT() <= MVT::i64 &&
6569 DstTy.getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00006570 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00006571
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006572 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00006573 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00006574 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00006575 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00006576 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00006577 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00006578 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00006579 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006580
Evan Cheng87c89352007-10-15 20:11:21 +00006581 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
6582 // stack slot.
6583 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00006584 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00006585 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006586 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00006587
Michael J. Spencerec38de22010-10-10 22:04:20 +00006588
6589
Evan Cheng0db9fe62006-04-25 20:13:52 +00006590 unsigned Opc;
Owen Anderson825b72b2009-08-11 20:47:22 +00006591 switch (DstTy.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00006592 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
Owen Anderson825b72b2009-08-11 20:47:22 +00006593 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
6594 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
6595 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006596 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006597
Dan Gohman475871a2008-07-27 21:46:04 +00006598 SDValue Chain = DAG.getEntryNode();
6599 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00006600 EVT TheVT = Op.getOperand(0).getValueType();
6601 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006602 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00006603 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006604 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006605 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00006606 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00006607 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00006608 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00006609 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00006610
Chris Lattner492a43e2010-09-22 01:28:21 +00006611 MachineMemOperand *MMO =
6612 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6613 MachineMemOperand::MOLoad, MemSize, MemSize);
6614 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, 3,
6615 DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006616 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00006617 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006618 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
6619 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00006620
Chris Lattner07290932010-09-22 01:05:16 +00006621 MachineMemOperand *MMO =
6622 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6623 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006624
Evan Cheng0db9fe62006-04-25 20:13:52 +00006625 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00006626 SDValue Ops[] = { Chain, Value, StackSlot };
Chris Lattner07290932010-09-22 01:05:16 +00006627 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
6628 Ops, 3, DstTy, MMO);
Evan Chengd9558e02006-01-06 00:43:03 +00006629
Chris Lattner27a6c732007-11-24 07:07:01 +00006630 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006631}
6632
Dan Gohmand858e902010-04-17 15:26:15 +00006633SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
6634 SelectionDAG &DAG) const {
Dale Johannesen0488fb62010-09-30 23:57:10 +00006635 if (Op.getValueType().isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00006636 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00006637
Eli Friedman948e95a2009-05-23 09:59:16 +00006638 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00006639 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00006640 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
6641 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00006642
Chris Lattner27a6c732007-11-24 07:07:01 +00006643 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006644 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00006645 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00006646}
6647
Dan Gohmand858e902010-04-17 15:26:15 +00006648SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
6649 SelectionDAG &DAG) const {
Eli Friedman948e95a2009-05-23 09:59:16 +00006650 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
6651 SDValue FIST = Vals.first, StackSlot = Vals.second;
6652 assert(FIST.getNode() && "Unexpected failure");
6653
6654 // Load the result.
6655 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00006656 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00006657}
6658
Dan Gohmand858e902010-04-17 15:26:15 +00006659SDValue X86TargetLowering::LowerFABS(SDValue Op,
6660 SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00006661 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006662 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00006663 EVT VT = Op.getValueType();
6664 EVT EltVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00006665 if (VT.isVector())
6666 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006667 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00006668 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006669 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00006670 CV.push_back(C);
6671 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006672 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006673 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00006674 CV.push_back(C);
6675 CV.push_back(C);
6676 CV.push_back(C);
6677 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006678 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00006679 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006680 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006681 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00006682 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006683 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006684 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006685}
6686
Dan Gohmand858e902010-04-17 15:26:15 +00006687SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00006688 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006689 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00006690 EVT VT = Op.getValueType();
6691 EVT EltVT = VT;
Duncan Sandsda9ad382009-09-06 19:29:07 +00006692 if (VT.isVector())
Duncan Sands83ec4b62008-06-06 12:08:01 +00006693 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006694 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00006695 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006696 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00006697 CV.push_back(C);
6698 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006699 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006700 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00006701 CV.push_back(C);
6702 CV.push_back(C);
6703 CV.push_back(C);
6704 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006705 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00006706 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006707 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006708 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00006709 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006710 false, false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006711 if (VT.isVector()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006712 return DAG.getNode(ISD::BITCAST, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00006713 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006714 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00006715 Op.getOperand(0)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006716 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00006717 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00006718 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00006719 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006720}
6721
Dan Gohmand858e902010-04-17 15:26:15 +00006722SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00006723 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00006724 SDValue Op0 = Op.getOperand(0);
6725 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006726 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00006727 EVT VT = Op.getValueType();
6728 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00006729
6730 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00006731 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006732 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00006733 SrcVT = VT;
6734 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00006735 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00006736 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006737 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00006738 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00006739 }
6740
6741 // At this point the operands and the result should have the same
6742 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00006743
Evan Cheng68c47cb2007-01-05 07:55:56 +00006744 // First get the sign bit of second operand.
6745 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00006746 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006747 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
6748 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00006749 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006750 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
6751 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
6752 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
6753 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00006754 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00006755 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006756 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006757 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00006758 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006759 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006760 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00006761
6762 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00006763 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006764 // Op0 is MVT::f32, Op1 is MVT::f64.
6765 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
6766 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
6767 DAG.getConstant(32, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006768 SignBit = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, SignBit);
Owen Anderson825b72b2009-08-11 20:47:22 +00006769 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00006770 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00006771 }
6772
Evan Cheng73d6cf12007-01-05 21:37:56 +00006773 // Clear first operand sign bit.
6774 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00006775 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006776 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
6777 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00006778 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006779 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
6780 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
6781 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
6782 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00006783 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00006784 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006785 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006786 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00006787 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006788 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006789 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00006790
6791 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00006792 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00006793}
6794
Dan Gohman076aee32009-03-04 19:44:21 +00006795/// Emit nodes that will be selected as "test Op0,Op0", or something
6796/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00006797SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00006798 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00006799 DebugLoc dl = Op.getDebugLoc();
6800
Dan Gohman31125812009-03-07 01:58:32 +00006801 // CF and OF aren't always set the way we want. Determine which
6802 // of these we need.
6803 bool NeedCF = false;
6804 bool NeedOF = false;
6805 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006806 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00006807 case X86::COND_A: case X86::COND_AE:
6808 case X86::COND_B: case X86::COND_BE:
6809 NeedCF = true;
6810 break;
6811 case X86::COND_G: case X86::COND_GE:
6812 case X86::COND_L: case X86::COND_LE:
6813 case X86::COND_O: case X86::COND_NO:
6814 NeedOF = true;
6815 break;
Dan Gohman31125812009-03-07 01:58:32 +00006816 }
6817
Dan Gohman076aee32009-03-04 19:44:21 +00006818 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00006819 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
6820 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006821 if (Op.getResNo() != 0 || NeedOF || NeedCF)
6822 // Emit a CMP with 0, which is the TEST pattern.
6823 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
6824 DAG.getConstant(0, Op.getValueType()));
6825
6826 unsigned Opcode = 0;
6827 unsigned NumOperands = 0;
6828 switch (Op.getNode()->getOpcode()) {
6829 case ISD::ADD:
6830 // Due to an isel shortcoming, be conservative if this add is likely to be
6831 // selected as part of a load-modify-store instruction. When the root node
6832 // in a match is a store, isel doesn't know how to remap non-chain non-flag
6833 // uses of other nodes in the match, such as the ADD in this case. This
6834 // leads to the ADD being left around and reselected, with the result being
6835 // two adds in the output. Alas, even if none our users are stores, that
6836 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
6837 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
6838 // climbing the DAG back to the root, and it doesn't seem to be worth the
6839 // effort.
6840 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Dan Gohman076aee32009-03-04 19:44:21 +00006841 UE = Op.getNode()->use_end(); UI != UE; ++UI)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006842 if (UI->getOpcode() != ISD::CopyToReg && UI->getOpcode() != ISD::SETCC)
6843 goto default_case;
6844
6845 if (ConstantSDNode *C =
6846 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
6847 // An add of one will be selected as an INC.
6848 if (C->getAPIntValue() == 1) {
6849 Opcode = X86ISD::INC;
6850 NumOperands = 1;
6851 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00006852 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006853
6854 // An add of negative one (subtract of one) will be selected as a DEC.
6855 if (C->getAPIntValue().isAllOnesValue()) {
6856 Opcode = X86ISD::DEC;
6857 NumOperands = 1;
6858 break;
6859 }
Dan Gohman076aee32009-03-04 19:44:21 +00006860 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006861
6862 // Otherwise use a regular EFLAGS-setting add.
6863 Opcode = X86ISD::ADD;
6864 NumOperands = 2;
6865 break;
6866 case ISD::AND: {
6867 // If the primary and result isn't used, don't bother using X86ISD::AND,
6868 // because a TEST instruction will be better.
6869 bool NonFlagUse = false;
6870 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
6871 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
6872 SDNode *User = *UI;
6873 unsigned UOpNo = UI.getOperandNo();
6874 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
6875 // Look pass truncate.
6876 UOpNo = User->use_begin().getOperandNo();
6877 User = *User->use_begin();
6878 }
6879
6880 if (User->getOpcode() != ISD::BRCOND &&
6881 User->getOpcode() != ISD::SETCC &&
6882 (User->getOpcode() != ISD::SELECT || UOpNo != 0)) {
6883 NonFlagUse = true;
6884 break;
6885 }
Dan Gohman076aee32009-03-04 19:44:21 +00006886 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006887
6888 if (!NonFlagUse)
6889 break;
6890 }
6891 // FALL THROUGH
6892 case ISD::SUB:
6893 case ISD::OR:
6894 case ISD::XOR:
6895 // Due to the ISEL shortcoming noted above, be conservative if this op is
6896 // likely to be selected as part of a load-modify-store instruction.
6897 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
6898 UE = Op.getNode()->use_end(); UI != UE; ++UI)
6899 if (UI->getOpcode() == ISD::STORE)
6900 goto default_case;
6901
6902 // Otherwise use a regular EFLAGS-setting instruction.
6903 switch (Op.getNode()->getOpcode()) {
6904 default: llvm_unreachable("unexpected operator!");
6905 case ISD::SUB: Opcode = X86ISD::SUB; break;
6906 case ISD::OR: Opcode = X86ISD::OR; break;
6907 case ISD::XOR: Opcode = X86ISD::XOR; break;
6908 case ISD::AND: Opcode = X86ISD::AND; break;
6909 }
6910
6911 NumOperands = 2;
6912 break;
6913 case X86ISD::ADD:
6914 case X86ISD::SUB:
6915 case X86ISD::INC:
6916 case X86ISD::DEC:
6917 case X86ISD::OR:
6918 case X86ISD::XOR:
6919 case X86ISD::AND:
6920 return SDValue(Op.getNode(), 1);
6921 default:
6922 default_case:
6923 break;
Dan Gohman076aee32009-03-04 19:44:21 +00006924 }
6925
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006926 if (Opcode == 0)
6927 // Emit a CMP with 0, which is the TEST pattern.
6928 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
6929 DAG.getConstant(0, Op.getValueType()));
6930
6931 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
6932 SmallVector<SDValue, 4> Ops;
6933 for (unsigned i = 0; i != NumOperands; ++i)
6934 Ops.push_back(Op.getOperand(i));
6935
6936 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
6937 DAG.ReplaceAllUsesWith(Op, New);
6938 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00006939}
6940
6941/// Emit nodes that will be selected as "cmp Op0,Op1", or something
6942/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00006943SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00006944 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00006945 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
6946 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00006947 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00006948
6949 DebugLoc dl = Op0.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00006950 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00006951}
6952
Evan Chengd40d03e2010-01-06 19:38:29 +00006953/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
6954/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00006955SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
6956 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00006957 SDValue Op0 = And.getOperand(0);
6958 SDValue Op1 = And.getOperand(1);
6959 if (Op0.getOpcode() == ISD::TRUNCATE)
6960 Op0 = Op0.getOperand(0);
6961 if (Op1.getOpcode() == ISD::TRUNCATE)
6962 Op1 = Op1.getOperand(0);
6963
Evan Chengd40d03e2010-01-06 19:38:29 +00006964 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00006965 if (Op1.getOpcode() == ISD::SHL)
6966 std::swap(Op0, Op1);
6967 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00006968 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
6969 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00006970 // If we looked past a truncate, check that it's only truncating away
6971 // known zeros.
6972 unsigned BitWidth = Op0.getValueSizeInBits();
6973 unsigned AndBitWidth = And.getValueSizeInBits();
6974 if (BitWidth > AndBitWidth) {
6975 APInt Mask = APInt::getAllOnesValue(BitWidth), Zeros, Ones;
6976 DAG.ComputeMaskedBits(Op0, Mask, Zeros, Ones);
6977 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
6978 return SDValue();
6979 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00006980 LHS = Op1;
6981 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00006982 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00006983 } else if (Op1.getOpcode() == ISD::Constant) {
6984 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
6985 SDValue AndLHS = Op0;
Evan Chengd40d03e2010-01-06 19:38:29 +00006986 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
6987 LHS = AndLHS.getOperand(0);
6988 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00006989 }
Evan Chengd40d03e2010-01-06 19:38:29 +00006990 }
Evan Cheng0488db92007-09-25 01:57:46 +00006991
Evan Chengd40d03e2010-01-06 19:38:29 +00006992 if (LHS.getNode()) {
Evan Chenge5b51ac2010-04-17 06:13:15 +00006993 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00006994 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00006995 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00006996 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00006997 // Also promote i16 to i32 for performance / code size reason.
6998 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00006999 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00007000 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00007001
Evan Chengd40d03e2010-01-06 19:38:29 +00007002 // If the operand types disagree, extend the shift amount to match. Since
7003 // BT ignores high bits (like shifts) we can use anyextend.
7004 if (LHS.getValueType() != RHS.getValueType())
7005 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00007006
Evan Chengd40d03e2010-01-06 19:38:29 +00007007 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
7008 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
7009 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
7010 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00007011 }
7012
Evan Cheng54de3ea2010-01-05 06:52:31 +00007013 return SDValue();
7014}
7015
Dan Gohmand858e902010-04-17 15:26:15 +00007016SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng54de3ea2010-01-05 06:52:31 +00007017 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
7018 SDValue Op0 = Op.getOperand(0);
7019 SDValue Op1 = Op.getOperand(1);
7020 DebugLoc dl = Op.getDebugLoc();
7021 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
7022
7023 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00007024 // Lower (X & (1 << N)) == 0 to BT(X, N).
7025 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
7026 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Chris Lattner481eebc2010-12-19 21:23:48 +00007027 if (Op0.getOpcode() == ISD::AND && Op0.hasOneUse() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00007028 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane368b462010-06-18 14:22:04 +00007029 cast<ConstantSDNode>(Op1)->isNullValue() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00007030 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
7031 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
7032 if (NewSetCC.getNode())
7033 return NewSetCC;
7034 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00007035
Chris Lattner481eebc2010-12-19 21:23:48 +00007036 // Look for X == 0, X == 1, X != 0, or X != 1. We can simplify some forms of
7037 // these.
7038 if (Op1.getOpcode() == ISD::Constant &&
Evan Cheng2c755ba2010-02-27 07:36:59 +00007039 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
7040 cast<ConstantSDNode>(Op1)->isNullValue()) &&
7041 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
Chris Lattner481eebc2010-12-19 21:23:48 +00007042
7043 // If the input is a setcc, then reuse the input setcc or use a new one with
7044 // the inverted condition.
7045 if (Op0.getOpcode() == X86ISD::SETCC) {
7046 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
7047 bool Invert = (CC == ISD::SETNE) ^
7048 cast<ConstantSDNode>(Op1)->isNullValue();
7049 if (!Invert) return Op0;
7050
Evan Cheng2c755ba2010-02-27 07:36:59 +00007051 CCode = X86::GetOppositeBranchCondition(CCode);
Chris Lattner481eebc2010-12-19 21:23:48 +00007052 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
7053 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
7054 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00007055 }
7056
Evan Chenge5b51ac2010-04-17 06:13:15 +00007057 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00007058 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00007059 if (X86CC == X86::COND_INVALID)
7060 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00007061
Chris Lattnerc19d1c32010-12-19 22:08:31 +00007062 SDValue EFLAGS = EmitCmp(Op0, Op1, X86CC, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00007063 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattnerc19d1c32010-12-19 22:08:31 +00007064 DAG.getConstant(X86CC, MVT::i8), EFLAGS);
Evan Cheng0488db92007-09-25 01:57:46 +00007065}
7066
Dan Gohmand858e902010-04-17 15:26:15 +00007067SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00007068 SDValue Cond;
7069 SDValue Op0 = Op.getOperand(0);
7070 SDValue Op1 = Op.getOperand(1);
7071 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00007072 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00007073 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
7074 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007075 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00007076
7077 if (isFP) {
7078 unsigned SSECC = 8;
Owen Andersone50ed302009-08-10 22:56:29 +00007079 EVT VT0 = Op0.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00007080 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
7081 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00007082 bool Swap = false;
7083
7084 switch (SetCCOpcode) {
7085 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00007086 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00007087 case ISD::SETEQ: SSECC = 0; break;
Scott Michelfdc40a02009-02-17 22:15:04 +00007088 case ISD::SETOGT:
Nate Begeman30a0de92008-07-17 16:51:19 +00007089 case ISD::SETGT: Swap = true; // Fallthrough
7090 case ISD::SETLT:
7091 case ISD::SETOLT: SSECC = 1; break;
7092 case ISD::SETOGE:
7093 case ISD::SETGE: Swap = true; // Fallthrough
7094 case ISD::SETLE:
7095 case ISD::SETOLE: SSECC = 2; break;
7096 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00007097 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00007098 case ISD::SETNE: SSECC = 4; break;
7099 case ISD::SETULE: Swap = true;
7100 case ISD::SETUGE: SSECC = 5; break;
7101 case ISD::SETULT: Swap = true;
7102 case ISD::SETUGT: SSECC = 6; break;
7103 case ISD::SETO: SSECC = 7; break;
7104 }
7105 if (Swap)
7106 std::swap(Op0, Op1);
7107
Nate Begemanfb8ead02008-07-25 19:05:58 +00007108 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00007109 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00007110 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00007111 SDValue UNORD, EQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00007112 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
7113 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00007114 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00007115 }
7116 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00007117 SDValue ORD, NEQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00007118 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
7119 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00007120 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00007121 }
Torok Edwinc23197a2009-07-14 16:55:14 +00007122 llvm_unreachable("Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00007123 }
7124 // Handle all other FP comparisons here.
Owen Anderson825b72b2009-08-11 20:47:22 +00007125 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00007126 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007127
Nate Begeman30a0de92008-07-17 16:51:19 +00007128 // We are handling one of the integer comparisons here. Since SSE only has
7129 // GT and EQ comparisons for integer, swapping operands and multiple
7130 // operations may be required for some comparisons.
7131 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
7132 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00007133
Owen Anderson825b72b2009-08-11 20:47:22 +00007134 switch (VT.getSimpleVT().SimpleTy) {
Nate Begeman30a0de92008-07-17 16:51:19 +00007135 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007136 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007137 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007138 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
7139 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00007140 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007141
Nate Begeman30a0de92008-07-17 16:51:19 +00007142 switch (SetCCOpcode) {
7143 default: break;
7144 case ISD::SETNE: Invert = true;
7145 case ISD::SETEQ: Opc = EQOpc; break;
7146 case ISD::SETLT: Swap = true;
7147 case ISD::SETGT: Opc = GTOpc; break;
7148 case ISD::SETGE: Swap = true;
7149 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
7150 case ISD::SETULT: Swap = true;
7151 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
7152 case ISD::SETUGE: Swap = true;
7153 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
7154 }
7155 if (Swap)
7156 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007157
Nate Begeman30a0de92008-07-17 16:51:19 +00007158 // Since SSE has no unsigned integer comparisons, we need to flip the sign
7159 // bits of the inputs before performing those operations.
7160 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00007161 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00007162 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
7163 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00007164 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00007165 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
7166 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00007167 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
7168 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00007169 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007170
Dale Johannesenace16102009-02-03 19:33:06 +00007171 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00007172
7173 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00007174 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00007175 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00007176
Nate Begeman30a0de92008-07-17 16:51:19 +00007177 return Result;
7178}
Evan Cheng0488db92007-09-25 01:57:46 +00007179
Evan Cheng370e5342008-12-03 08:38:43 +00007180// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00007181static bool isX86LogicalCmp(SDValue Op) {
7182 unsigned Opc = Op.getNode()->getOpcode();
7183 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
7184 return true;
7185 if (Op.getResNo() == 1 &&
7186 (Opc == X86ISD::ADD ||
7187 Opc == X86ISD::SUB ||
Chris Lattner5b856542010-12-20 00:59:46 +00007188 Opc == X86ISD::ADC ||
7189 Opc == X86ISD::SBB ||
Dan Gohman076aee32009-03-04 19:44:21 +00007190 Opc == X86ISD::SMUL ||
7191 Opc == X86ISD::UMUL ||
7192 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00007193 Opc == X86ISD::DEC ||
7194 Opc == X86ISD::OR ||
7195 Opc == X86ISD::XOR ||
7196 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00007197 return true;
7198
Chris Lattner9637d5b2010-12-05 07:49:54 +00007199 if (Op.getResNo() == 2 && Opc == X86ISD::UMUL)
7200 return true;
7201
Dan Gohman076aee32009-03-04 19:44:21 +00007202 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00007203}
7204
Chris Lattnera2b56002010-12-05 01:23:24 +00007205static bool isZero(SDValue V) {
7206 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
7207 return C && C->isNullValue();
7208}
7209
Chris Lattner96908b12010-12-05 02:00:51 +00007210static bool isAllOnes(SDValue V) {
7211 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
7212 return C && C->isAllOnesValue();
7213}
7214
Dan Gohmand858e902010-04-17 15:26:15 +00007215SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00007216 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00007217 SDValue Cond = Op.getOperand(0);
Chris Lattnera2b56002010-12-05 01:23:24 +00007218 SDValue Op1 = Op.getOperand(1);
7219 SDValue Op2 = Op.getOperand(2);
7220 DebugLoc DL = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00007221 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00007222
Dan Gohman1a492952009-10-20 16:22:37 +00007223 if (Cond.getOpcode() == ISD::SETCC) {
7224 SDValue NewCond = LowerSETCC(Cond, DAG);
7225 if (NewCond.getNode())
7226 Cond = NewCond;
7227 }
Evan Cheng734503b2006-09-11 02:19:56 +00007228
Chris Lattnera2b56002010-12-05 01:23:24 +00007229 // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00007230 // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
Chris Lattnera2b56002010-12-05 01:23:24 +00007231 // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00007232 // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007233 if (Cond.getOpcode() == X86ISD::SETCC &&
Chris Lattner96908b12010-12-05 02:00:51 +00007234 Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
7235 isZero(Cond.getOperand(1).getOperand(1))) {
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007236 SDValue Cmp = Cond.getOperand(1);
Chris Lattnera2b56002010-12-05 01:23:24 +00007237
7238 unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
7239
Chris Lattner96908b12010-12-05 02:00:51 +00007240 if ((isAllOnes(Op1) || isAllOnes(Op2)) &&
7241 (CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
7242 SDValue Y = isAllOnes(Op2) ? Op1 : Op2;
Chris Lattnera2b56002010-12-05 01:23:24 +00007243
7244 SDValue CmpOp0 = Cmp.getOperand(0);
7245 Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32,
7246 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
7247
Chris Lattner96908b12010-12-05 02:00:51 +00007248 SDValue Res = // Res = 0 or -1.
Chris Lattnera2b56002010-12-05 01:23:24 +00007249 DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
7250 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
Chris Lattner96908b12010-12-05 02:00:51 +00007251
7252 if (isAllOnes(Op1) != (CondCode == X86::COND_E))
7253 Res = DAG.getNOT(DL, Res, Res.getValueType());
7254
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007255 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
Chris Lattnera2b56002010-12-05 01:23:24 +00007256 if (N2C == 0 || !N2C->isNullValue())
7257 Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
7258 return Res;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007259 }
7260 }
7261
Chris Lattnera2b56002010-12-05 01:23:24 +00007262 // Look past (and (setcc_carry (cmp ...)), 1).
Evan Chengad9c0a32009-12-15 00:53:42 +00007263 if (Cond.getOpcode() == ISD::AND &&
7264 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
7265 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00007266 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00007267 Cond = Cond.getOperand(0);
7268 }
7269
Evan Cheng3f41d662007-10-08 22:16:29 +00007270 // If condition flag is set by a X86ISD::CMP, then use it as the condition
7271 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00007272 if (Cond.getOpcode() == X86ISD::SETCC ||
7273 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00007274 CC = Cond.getOperand(0);
7275
Dan Gohman475871a2008-07-27 21:46:04 +00007276 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00007277 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00007278 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00007279
Evan Cheng3f41d662007-10-08 22:16:29 +00007280 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007281 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00007282 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00007283 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00007284
Chris Lattnerd1980a52009-03-12 06:52:53 +00007285 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
7286 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00007287 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00007288 addTest = false;
7289 }
7290 }
7291
7292 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007293 // Look pass the truncate.
7294 if (Cond.getOpcode() == ISD::TRUNCATE)
7295 Cond = Cond.getOperand(0);
7296
7297 // We know the result of AND is compared against zero. Try to match
7298 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00007299 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Chris Lattnera2b56002010-12-05 01:23:24 +00007300 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG);
Evan Chengd40d03e2010-01-06 19:38:29 +00007301 if (NewSetCC.getNode()) {
7302 CC = NewSetCC.getOperand(0);
7303 Cond = NewSetCC.getOperand(1);
7304 addTest = false;
7305 }
7306 }
7307 }
7308
7309 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007310 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00007311 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00007312 }
7313
Evan Cheng0488db92007-09-25 01:57:46 +00007314 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
7315 // condition is true.
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007316 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Flag);
7317 SDValue Ops[] = { Op2, Op1, CC, Cond };
Chris Lattnera2b56002010-12-05 01:23:24 +00007318 return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00007319}
7320
Evan Cheng370e5342008-12-03 08:38:43 +00007321// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
7322// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
7323// from the AND / OR.
7324static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
7325 Opc = Op.getOpcode();
7326 if (Opc != ISD::OR && Opc != ISD::AND)
7327 return false;
7328 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
7329 Op.getOperand(0).hasOneUse() &&
7330 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
7331 Op.getOperand(1).hasOneUse());
7332}
7333
Evan Cheng961d6d42009-02-02 08:19:07 +00007334// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
7335// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00007336static bool isXor1OfSetCC(SDValue Op) {
7337 if (Op.getOpcode() != ISD::XOR)
7338 return false;
7339 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
7340 if (N1C && N1C->getAPIntValue() == 1) {
7341 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
7342 Op.getOperand(0).hasOneUse();
7343 }
7344 return false;
7345}
7346
Dan Gohmand858e902010-04-17 15:26:15 +00007347SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00007348 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00007349 SDValue Chain = Op.getOperand(0);
7350 SDValue Cond = Op.getOperand(1);
7351 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007352 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00007353 SDValue CC;
Evan Cheng734503b2006-09-11 02:19:56 +00007354
Dan Gohman1a492952009-10-20 16:22:37 +00007355 if (Cond.getOpcode() == ISD::SETCC) {
7356 SDValue NewCond = LowerSETCC(Cond, DAG);
7357 if (NewCond.getNode())
7358 Cond = NewCond;
7359 }
Chris Lattnere55484e2008-12-25 05:34:37 +00007360#if 0
7361 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00007362 else if (Cond.getOpcode() == X86ISD::ADD ||
7363 Cond.getOpcode() == X86ISD::SUB ||
7364 Cond.getOpcode() == X86ISD::SMUL ||
7365 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00007366 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00007367#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00007368
Evan Chengad9c0a32009-12-15 00:53:42 +00007369 // Look pass (and (setcc_carry (cmp ...)), 1).
7370 if (Cond.getOpcode() == ISD::AND &&
7371 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
7372 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00007373 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00007374 Cond = Cond.getOperand(0);
7375 }
7376
Evan Cheng3f41d662007-10-08 22:16:29 +00007377 // If condition flag is set by a X86ISD::CMP, then use it as the condition
7378 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00007379 if (Cond.getOpcode() == X86ISD::SETCC ||
7380 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00007381 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007382
Dan Gohman475871a2008-07-27 21:46:04 +00007383 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00007384 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00007385 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00007386 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00007387 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00007388 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00007389 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00007390 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00007391 default: break;
7392 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00007393 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00007394 // These can only come from an arithmetic instruction with overflow,
7395 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00007396 Cond = Cond.getNode()->getOperand(1);
7397 addTest = false;
7398 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00007399 }
Evan Cheng0488db92007-09-25 01:57:46 +00007400 }
Evan Cheng370e5342008-12-03 08:38:43 +00007401 } else {
7402 unsigned CondOpc;
7403 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
7404 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00007405 if (CondOpc == ISD::OR) {
7406 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
7407 // two branches instead of an explicit OR instruction with a
7408 // separate test.
7409 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00007410 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00007411 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007412 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00007413 Chain, Dest, CC, Cmp);
7414 CC = Cond.getOperand(1).getOperand(0);
7415 Cond = Cmp;
7416 addTest = false;
7417 }
7418 } else { // ISD::AND
7419 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
7420 // two branches instead of an explicit AND instruction with a
7421 // separate test. However, we only do this if this block doesn't
7422 // have a fall-through edge, because this requires an explicit
7423 // jmp when the condition is false.
7424 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00007425 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00007426 Op.getNode()->hasOneUse()) {
7427 X86::CondCode CCode =
7428 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
7429 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007430 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00007431 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00007432 // Look for an unconditional branch following this conditional branch.
7433 // We need this because we need to reverse the successors in order
7434 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00007435 if (User->getOpcode() == ISD::BR) {
7436 SDValue FalseBB = User->getOperand(1);
7437 SDNode *NewBR =
7438 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00007439 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00007440 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00007441 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00007442
Dale Johannesene4d209d2009-02-03 20:21:25 +00007443 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00007444 Chain, Dest, CC, Cmp);
7445 X86::CondCode CCode =
7446 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
7447 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007448 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00007449 Cond = Cmp;
7450 addTest = false;
7451 }
7452 }
Dan Gohman279c22e2008-10-21 03:29:32 +00007453 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00007454 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
7455 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
7456 // It should be transformed during dag combiner except when the condition
7457 // is set by a arithmetics with overflow node.
7458 X86::CondCode CCode =
7459 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
7460 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007461 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00007462 Cond = Cond.getOperand(0).getOperand(1);
7463 addTest = false;
Dan Gohman279c22e2008-10-21 03:29:32 +00007464 }
Evan Cheng0488db92007-09-25 01:57:46 +00007465 }
7466
7467 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007468 // Look pass the truncate.
7469 if (Cond.getOpcode() == ISD::TRUNCATE)
7470 Cond = Cond.getOperand(0);
7471
7472 // We know the result of AND is compared against zero. Try to match
7473 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00007474 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007475 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
7476 if (NewSetCC.getNode()) {
7477 CC = NewSetCC.getOperand(0);
7478 Cond = NewSetCC.getOperand(1);
7479 addTest = false;
7480 }
7481 }
7482 }
7483
7484 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007485 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00007486 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00007487 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00007488 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00007489 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00007490}
7491
Anton Korobeynikove060b532007-04-17 19:34:00 +00007492
7493// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
7494// Calls to _alloca is needed to probe the stack when allocating more than 4k
7495// bytes in one go. Touching the stack at 4K increments is necessary to ensure
7496// that the guard pages used by the OS virtual memory manager are allocated in
7497// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00007498SDValue
7499X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00007500 SelectionDAG &DAG) const {
Duncan Sands1e1ca0b2010-10-21 16:02:12 +00007501 assert((Subtarget->isTargetCygMing() || Subtarget->isTargetWindows()) &&
Michael J. Spencere9c253e2010-10-21 01:41:01 +00007502 "This should be used only on Windows targets");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007503 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007504
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007505 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00007506 SDValue Chain = Op.getOperand(0);
7507 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007508 // FIXME: Ensure alignment here
7509
Dan Gohman475871a2008-07-27 21:46:04 +00007510 SDValue Flag;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007511
Owen Anderson825b72b2009-08-11 20:47:22 +00007512 EVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007513
Dale Johannesendd64c412009-02-04 00:33:20 +00007514 Chain = DAG.getCopyToReg(Chain, dl, X86::EAX, Size, Flag);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00007515 Flag = Chain.getValue(1);
7516
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00007517 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00007518
Michael J. Spencere9c253e2010-10-21 01:41:01 +00007519 Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag);
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00007520 Flag = Chain.getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007521
Dale Johannesendd64c412009-02-04 00:33:20 +00007522 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007523
Dan Gohman475871a2008-07-27 21:46:04 +00007524 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Dale Johannesene4d209d2009-02-03 20:21:25 +00007525 return DAG.getMergeValues(Ops1, 2, dl);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007526}
7527
Dan Gohmand858e902010-04-17 15:26:15 +00007528SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00007529 MachineFunction &MF = DAG.getMachineFunction();
7530 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
7531
Dan Gohman69de1932008-02-06 22:27:42 +00007532 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00007533 DebugLoc DL = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00007534
Anton Korobeynikove7beda12010-10-03 22:52:07 +00007535 if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00007536 // vastart just stores the address of the VarArgsFrameIndex slot into the
7537 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +00007538 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
7539 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00007540 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
7541 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007542 }
7543
7544 // __va_list_tag:
7545 // gp_offset (0 - 6 * 8)
7546 // fp_offset (48 - 48 + 8 * 16)
7547 // overflow_arg_area (point to parameters coming in memory).
7548 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00007549 SmallVector<SDValue, 8> MemOps;
7550 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00007551 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00007552 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00007553 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
7554 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007555 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007556 MemOps.push_back(Store);
7557
7558 // Store fp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00007559 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007560 FIN, DAG.getIntPtrConstant(4));
Chris Lattner8026a9d2010-09-21 17:50:43 +00007561 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00007562 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
7563 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007564 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007565 MemOps.push_back(Store);
7566
7567 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +00007568 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007569 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +00007570 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
7571 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00007572 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
7573 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +00007574 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007575 MemOps.push_back(Store);
7576
7577 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +00007578 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007579 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +00007580 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
7581 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00007582 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
7583 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007584 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +00007585 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00007586 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00007587}
7588
Dan Gohmand858e902010-04-17 15:26:15 +00007589SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman320afb82010-10-12 18:00:49 +00007590 assert(Subtarget->is64Bit() &&
7591 "LowerVAARG only handles 64-bit va_arg!");
7592 assert((Subtarget->isTargetLinux() ||
7593 Subtarget->isTargetDarwin()) &&
7594 "Unhandled target in LowerVAARG");
7595 assert(Op.getNode()->getNumOperands() == 4);
7596 SDValue Chain = Op.getOperand(0);
7597 SDValue SrcPtr = Op.getOperand(1);
7598 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
7599 unsigned Align = Op.getConstantOperandVal(3);
7600 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9018e832008-05-10 01:26:14 +00007601
Dan Gohman320afb82010-10-12 18:00:49 +00007602 EVT ArgVT = Op.getNode()->getValueType(0);
7603 const Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
7604 uint32_t ArgSize = getTargetData()->getTypeAllocSize(ArgTy);
7605 uint8_t ArgMode;
7606
7607 // Decide which area this value should be read from.
7608 // TODO: Implement the AMD64 ABI in its entirety. This simple
7609 // selection mechanism works only for the basic types.
7610 if (ArgVT == MVT::f80) {
7611 llvm_unreachable("va_arg for f80 not yet implemented");
7612 } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) {
7613 ArgMode = 2; // Argument passed in XMM register. Use fp_offset.
7614 } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) {
7615 ArgMode = 1; // Argument passed in GPR64 register(s). Use gp_offset.
7616 } else {
7617 llvm_unreachable("Unhandled argument type in LowerVAARG");
7618 }
7619
7620 if (ArgMode == 2) {
7621 // Sanity Check: Make sure using fp_offset makes sense.
Michael J. Spencer87b86652010-10-19 07:32:42 +00007622 assert(!UseSoftFloat &&
Eric Christopher52b45052010-10-12 19:44:17 +00007623 !(DAG.getMachineFunction()
7624 .getFunction()->hasFnAttr(Attribute::NoImplicitFloat)) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00007625 Subtarget->hasXMM());
Dan Gohman320afb82010-10-12 18:00:49 +00007626 }
7627
7628 // Insert VAARG_64 node into the DAG
7629 // VAARG_64 returns two values: Variable Argument Address, Chain
7630 SmallVector<SDValue, 11> InstOps;
7631 InstOps.push_back(Chain);
7632 InstOps.push_back(SrcPtr);
7633 InstOps.push_back(DAG.getConstant(ArgSize, MVT::i32));
7634 InstOps.push_back(DAG.getConstant(ArgMode, MVT::i8));
7635 InstOps.push_back(DAG.getConstant(Align, MVT::i32));
7636 SDVTList VTs = DAG.getVTList(getPointerTy(), MVT::Other);
7637 SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl,
7638 VTs, &InstOps[0], InstOps.size(),
7639 MVT::i64,
7640 MachinePointerInfo(SV),
7641 /*Align=*/0,
7642 /*Volatile=*/false,
7643 /*ReadMem=*/true,
7644 /*WriteMem=*/true);
7645 Chain = VAARG.getValue(1);
7646
7647 // Load the next argument and return it
7648 return DAG.getLoad(ArgVT, dl,
7649 Chain,
7650 VAARG,
7651 MachinePointerInfo(),
7652 false, false, 0);
Dan Gohman9018e832008-05-10 01:26:14 +00007653}
7654
Dan Gohmand858e902010-04-17 15:26:15 +00007655SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) const {
Evan Chengae642192007-03-02 23:16:35 +00007656 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00007657 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00007658 SDValue Chain = Op.getOperand(0);
7659 SDValue DstPtr = Op.getOperand(1);
7660 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00007661 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
7662 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Chris Lattnere72f2022010-09-21 05:40:29 +00007663 DebugLoc DL = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00007664
Chris Lattnere72f2022010-09-21 05:40:29 +00007665 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +00007666 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Michael J. Spencerec38de22010-10-10 22:04:20 +00007667 false,
Chris Lattnere72f2022010-09-21 05:40:29 +00007668 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +00007669}
7670
Dan Gohman475871a2008-07-27 21:46:04 +00007671SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007672X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007673 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007674 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007675 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00007676 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00007677 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00007678 case Intrinsic::x86_sse_comieq_ss:
7679 case Intrinsic::x86_sse_comilt_ss:
7680 case Intrinsic::x86_sse_comile_ss:
7681 case Intrinsic::x86_sse_comigt_ss:
7682 case Intrinsic::x86_sse_comige_ss:
7683 case Intrinsic::x86_sse_comineq_ss:
7684 case Intrinsic::x86_sse_ucomieq_ss:
7685 case Intrinsic::x86_sse_ucomilt_ss:
7686 case Intrinsic::x86_sse_ucomile_ss:
7687 case Intrinsic::x86_sse_ucomigt_ss:
7688 case Intrinsic::x86_sse_ucomige_ss:
7689 case Intrinsic::x86_sse_ucomineq_ss:
7690 case Intrinsic::x86_sse2_comieq_sd:
7691 case Intrinsic::x86_sse2_comilt_sd:
7692 case Intrinsic::x86_sse2_comile_sd:
7693 case Intrinsic::x86_sse2_comigt_sd:
7694 case Intrinsic::x86_sse2_comige_sd:
7695 case Intrinsic::x86_sse2_comineq_sd:
7696 case Intrinsic::x86_sse2_ucomieq_sd:
7697 case Intrinsic::x86_sse2_ucomilt_sd:
7698 case Intrinsic::x86_sse2_ucomile_sd:
7699 case Intrinsic::x86_sse2_ucomigt_sd:
7700 case Intrinsic::x86_sse2_ucomige_sd:
7701 case Intrinsic::x86_sse2_ucomineq_sd: {
7702 unsigned Opc = 0;
7703 ISD::CondCode CC = ISD::SETCC_INVALID;
7704 switch (IntNo) {
7705 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007706 case Intrinsic::x86_sse_comieq_ss:
7707 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007708 Opc = X86ISD::COMI;
7709 CC = ISD::SETEQ;
7710 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00007711 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007712 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007713 Opc = X86ISD::COMI;
7714 CC = ISD::SETLT;
7715 break;
7716 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007717 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007718 Opc = X86ISD::COMI;
7719 CC = ISD::SETLE;
7720 break;
7721 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007722 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007723 Opc = X86ISD::COMI;
7724 CC = ISD::SETGT;
7725 break;
7726 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007727 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007728 Opc = X86ISD::COMI;
7729 CC = ISD::SETGE;
7730 break;
7731 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007732 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007733 Opc = X86ISD::COMI;
7734 CC = ISD::SETNE;
7735 break;
7736 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007737 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007738 Opc = X86ISD::UCOMI;
7739 CC = ISD::SETEQ;
7740 break;
7741 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007742 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007743 Opc = X86ISD::UCOMI;
7744 CC = ISD::SETLT;
7745 break;
7746 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007747 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007748 Opc = X86ISD::UCOMI;
7749 CC = ISD::SETLE;
7750 break;
7751 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007752 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007753 Opc = X86ISD::UCOMI;
7754 CC = ISD::SETGT;
7755 break;
7756 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007757 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007758 Opc = X86ISD::UCOMI;
7759 CC = ISD::SETGE;
7760 break;
7761 case Intrinsic::x86_sse_ucomineq_ss:
7762 case Intrinsic::x86_sse2_ucomineq_sd:
7763 Opc = X86ISD::UCOMI;
7764 CC = ISD::SETNE;
7765 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00007766 }
Evan Cheng734503b2006-09-11 02:19:56 +00007767
Dan Gohman475871a2008-07-27 21:46:04 +00007768 SDValue LHS = Op.getOperand(1);
7769 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00007770 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00007771 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00007772 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
7773 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
7774 DAG.getConstant(X86CC, MVT::i8), Cond);
7775 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00007776 }
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007777 // ptest and testp intrinsics. The intrinsic these come from are designed to
7778 // return an integer value, not just an instruction so lower it to the ptest
7779 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00007780 case Intrinsic::x86_sse41_ptestz:
7781 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007782 case Intrinsic::x86_sse41_ptestnzc:
7783 case Intrinsic::x86_avx_ptestz_256:
7784 case Intrinsic::x86_avx_ptestc_256:
7785 case Intrinsic::x86_avx_ptestnzc_256:
7786 case Intrinsic::x86_avx_vtestz_ps:
7787 case Intrinsic::x86_avx_vtestc_ps:
7788 case Intrinsic::x86_avx_vtestnzc_ps:
7789 case Intrinsic::x86_avx_vtestz_pd:
7790 case Intrinsic::x86_avx_vtestc_pd:
7791 case Intrinsic::x86_avx_vtestnzc_pd:
7792 case Intrinsic::x86_avx_vtestz_ps_256:
7793 case Intrinsic::x86_avx_vtestc_ps_256:
7794 case Intrinsic::x86_avx_vtestnzc_ps_256:
7795 case Intrinsic::x86_avx_vtestz_pd_256:
7796 case Intrinsic::x86_avx_vtestc_pd_256:
7797 case Intrinsic::x86_avx_vtestnzc_pd_256: {
7798 bool IsTestPacked = false;
Eric Christopher71c67532009-07-29 00:28:05 +00007799 unsigned X86CC = 0;
7800 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00007801 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007802 case Intrinsic::x86_avx_vtestz_ps:
7803 case Intrinsic::x86_avx_vtestz_pd:
7804 case Intrinsic::x86_avx_vtestz_ps_256:
7805 case Intrinsic::x86_avx_vtestz_pd_256:
7806 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00007807 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007808 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +00007809 // ZF = 1
7810 X86CC = X86::COND_E;
7811 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007812 case Intrinsic::x86_avx_vtestc_ps:
7813 case Intrinsic::x86_avx_vtestc_pd:
7814 case Intrinsic::x86_avx_vtestc_ps_256:
7815 case Intrinsic::x86_avx_vtestc_pd_256:
7816 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00007817 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007818 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00007819 // CF = 1
7820 X86CC = X86::COND_B;
7821 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007822 case Intrinsic::x86_avx_vtestnzc_ps:
7823 case Intrinsic::x86_avx_vtestnzc_pd:
7824 case Intrinsic::x86_avx_vtestnzc_ps_256:
7825 case Intrinsic::x86_avx_vtestnzc_pd_256:
7826 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +00007827 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007828 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00007829 // ZF and CF = 0
7830 X86CC = X86::COND_A;
7831 break;
7832 }
Eric Christopherfd179292009-08-27 18:07:15 +00007833
Eric Christopher71c67532009-07-29 00:28:05 +00007834 SDValue LHS = Op.getOperand(1);
7835 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007836 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
7837 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00007838 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
7839 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
7840 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +00007841 }
Evan Cheng5759f972008-05-04 09:15:50 +00007842
7843 // Fix vector shift instructions where the last operand is a non-immediate
7844 // i32 value.
7845 case Intrinsic::x86_sse2_pslli_w:
7846 case Intrinsic::x86_sse2_pslli_d:
7847 case Intrinsic::x86_sse2_pslli_q:
7848 case Intrinsic::x86_sse2_psrli_w:
7849 case Intrinsic::x86_sse2_psrli_d:
7850 case Intrinsic::x86_sse2_psrli_q:
7851 case Intrinsic::x86_sse2_psrai_w:
7852 case Intrinsic::x86_sse2_psrai_d:
7853 case Intrinsic::x86_mmx_pslli_w:
7854 case Intrinsic::x86_mmx_pslli_d:
7855 case Intrinsic::x86_mmx_pslli_q:
7856 case Intrinsic::x86_mmx_psrli_w:
7857 case Intrinsic::x86_mmx_psrli_d:
7858 case Intrinsic::x86_mmx_psrli_q:
7859 case Intrinsic::x86_mmx_psrai_w:
7860 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00007861 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00007862 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00007863 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00007864
7865 unsigned NewIntNo = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00007866 EVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00007867 switch (IntNo) {
7868 case Intrinsic::x86_sse2_pslli_w:
7869 NewIntNo = Intrinsic::x86_sse2_psll_w;
7870 break;
7871 case Intrinsic::x86_sse2_pslli_d:
7872 NewIntNo = Intrinsic::x86_sse2_psll_d;
7873 break;
7874 case Intrinsic::x86_sse2_pslli_q:
7875 NewIntNo = Intrinsic::x86_sse2_psll_q;
7876 break;
7877 case Intrinsic::x86_sse2_psrli_w:
7878 NewIntNo = Intrinsic::x86_sse2_psrl_w;
7879 break;
7880 case Intrinsic::x86_sse2_psrli_d:
7881 NewIntNo = Intrinsic::x86_sse2_psrl_d;
7882 break;
7883 case Intrinsic::x86_sse2_psrli_q:
7884 NewIntNo = Intrinsic::x86_sse2_psrl_q;
7885 break;
7886 case Intrinsic::x86_sse2_psrai_w:
7887 NewIntNo = Intrinsic::x86_sse2_psra_w;
7888 break;
7889 case Intrinsic::x86_sse2_psrai_d:
7890 NewIntNo = Intrinsic::x86_sse2_psra_d;
7891 break;
7892 default: {
Owen Anderson825b72b2009-08-11 20:47:22 +00007893 ShAmtVT = MVT::v2i32;
Evan Cheng5759f972008-05-04 09:15:50 +00007894 switch (IntNo) {
7895 case Intrinsic::x86_mmx_pslli_w:
7896 NewIntNo = Intrinsic::x86_mmx_psll_w;
7897 break;
7898 case Intrinsic::x86_mmx_pslli_d:
7899 NewIntNo = Intrinsic::x86_mmx_psll_d;
7900 break;
7901 case Intrinsic::x86_mmx_pslli_q:
7902 NewIntNo = Intrinsic::x86_mmx_psll_q;
7903 break;
7904 case Intrinsic::x86_mmx_psrli_w:
7905 NewIntNo = Intrinsic::x86_mmx_psrl_w;
7906 break;
7907 case Intrinsic::x86_mmx_psrli_d:
7908 NewIntNo = Intrinsic::x86_mmx_psrl_d;
7909 break;
7910 case Intrinsic::x86_mmx_psrli_q:
7911 NewIntNo = Intrinsic::x86_mmx_psrl_q;
7912 break;
7913 case Intrinsic::x86_mmx_psrai_w:
7914 NewIntNo = Intrinsic::x86_mmx_psra_w;
7915 break;
7916 case Intrinsic::x86_mmx_psrai_d:
7917 NewIntNo = Intrinsic::x86_mmx_psra_d;
7918 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00007919 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng5759f972008-05-04 09:15:50 +00007920 }
7921 break;
7922 }
7923 }
Mon P Wangefa42202009-09-03 19:56:25 +00007924
7925 // The vector shift intrinsics with scalars uses 32b shift amounts but
7926 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
7927 // to be zero.
7928 SDValue ShOps[4];
7929 ShOps[0] = ShAmt;
7930 ShOps[1] = DAG.getConstant(0, MVT::i32);
7931 if (ShAmtVT == MVT::v4i32) {
7932 ShOps[2] = DAG.getUNDEF(MVT::i32);
7933 ShOps[3] = DAG.getUNDEF(MVT::i32);
7934 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 4);
7935 } else {
7936 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 2);
Dale Johannesen0488fb62010-09-30 23:57:10 +00007937// FIXME this must be lowered to get rid of the invalid type.
Mon P Wangefa42202009-09-03 19:56:25 +00007938 }
7939
Owen Andersone50ed302009-08-10 22:56:29 +00007940 EVT VT = Op.getValueType();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007941 ShAmt = DAG.getNode(ISD::BITCAST, dl, VT, ShAmt);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007942 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007943 DAG.getConstant(NewIntNo, MVT::i32),
Evan Cheng5759f972008-05-04 09:15:50 +00007944 Op.getOperand(1), ShAmt);
7945 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00007946 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007947}
Evan Cheng72261582005-12-20 06:22:03 +00007948
Dan Gohmand858e902010-04-17 15:26:15 +00007949SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
7950 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +00007951 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7952 MFI->setReturnAddressIsTaken(true);
7953
Bill Wendling64e87322009-01-16 19:25:27 +00007954 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007955 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00007956
7957 if (Depth > 0) {
7958 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
7959 SDValue Offset =
7960 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +00007961 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007962 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00007963 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007964 FrameAddr, Offset),
Chris Lattner51abfe42010-09-21 06:02:19 +00007965 MachinePointerInfo(), false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +00007966 }
7967
7968 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00007969 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00007970 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Chris Lattner51abfe42010-09-21 06:02:19 +00007971 RetAddrFI, MachinePointerInfo(), false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00007972}
7973
Dan Gohmand858e902010-04-17 15:26:15 +00007974SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +00007975 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7976 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00007977
Owen Andersone50ed302009-08-10 22:56:29 +00007978 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007979 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00007980 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
7981 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00007982 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00007983 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +00007984 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
7985 MachinePointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +00007986 false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00007987 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00007988}
7989
Dan Gohman475871a2008-07-27 21:46:04 +00007990SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00007991 SelectionDAG &DAG) const {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00007992 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007993}
7994
Dan Gohmand858e902010-04-17 15:26:15 +00007995SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007996 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00007997 SDValue Chain = Op.getOperand(0);
7998 SDValue Offset = Op.getOperand(1);
7999 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008000 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008001
Dan Gohmand8816272010-08-11 18:14:00 +00008002 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
8003 Subtarget->is64Bit() ? X86::RBP : X86::EBP,
8004 getPointerTy());
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008005 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008006
Dan Gohmand8816272010-08-11 18:14:00 +00008007 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Frame,
8008 DAG.getIntPtrConstant(TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008009 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +00008010 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
8011 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00008012 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008013 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008014
Dale Johannesene4d209d2009-02-03 20:21:25 +00008015 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008016 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008017 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008018}
8019
Dan Gohman475871a2008-07-27 21:46:04 +00008020SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008021 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00008022 SDValue Root = Op.getOperand(0);
8023 SDValue Trmp = Op.getOperand(1); // trampoline
8024 SDValue FPtr = Op.getOperand(2); // nested function
8025 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008026 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008027
Dan Gohman69de1932008-02-06 22:27:42 +00008028 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008029
8030 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00008031 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00008032
8033 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +00008034 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
8035 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +00008036
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00008037 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
8038 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00008039
8040 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
8041
8042 // Load the pointer to the nested function into R11.
8043 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00008044 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00008045 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008046 Addr, MachinePointerInfo(TrmpAddr),
8047 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008048
Owen Anderson825b72b2009-08-11 20:47:22 +00008049 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8050 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008051 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
8052 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +00008053 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00008054
8055 // Load the 'nest' parameter value into R10.
8056 // R10 is specified in X86CallingConv.td
8057 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +00008058 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8059 DAG.getConstant(10, MVT::i64));
8060 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008061 Addr, MachinePointerInfo(TrmpAddr, 10),
8062 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008063
Owen Anderson825b72b2009-08-11 20:47:22 +00008064 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8065 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008066 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
8067 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +00008068 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00008069
8070 // Jump to the nested function.
8071 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +00008072 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8073 DAG.getConstant(20, MVT::i64));
8074 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008075 Addr, MachinePointerInfo(TrmpAddr, 20),
8076 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008077
8078 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +00008079 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8080 DAG.getConstant(22, MVT::i64));
8081 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00008082 MachinePointerInfo(TrmpAddr, 22),
8083 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008084
Dan Gohman475871a2008-07-27 21:46:04 +00008085 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00008086 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008087 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008088 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00008089 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00008090 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00008091 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00008092 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008093
8094 switch (CC) {
8095 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00008096 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +00008097 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00008098 case CallingConv::X86_StdCall: {
8099 // Pass 'nest' parameter in ECX.
8100 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00008101 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008102
8103 // Check that ECX wasn't needed by an 'inreg' parameter.
8104 const FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00008105 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008106
Chris Lattner58d74912008-03-12 17:45:29 +00008107 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00008108 unsigned InRegCount = 0;
8109 unsigned Idx = 1;
8110
8111 for (FunctionType::param_iterator I = FTy->param_begin(),
8112 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00008113 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00008114 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00008115 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008116
8117 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +00008118 report_fatal_error("Nest register in use - reduce number of inreg"
8119 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +00008120 }
8121 }
8122 break;
8123 }
8124 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +00008125 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00008126 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00008127 // Pass 'nest' parameter in EAX.
8128 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00008129 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008130 break;
8131 }
8132
Dan Gohman475871a2008-07-27 21:46:04 +00008133 SDValue OutChains[4];
8134 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008135
Owen Anderson825b72b2009-08-11 20:47:22 +00008136 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8137 DAG.getConstant(10, MVT::i32));
8138 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008139
Chris Lattnera62fe662010-02-05 19:20:30 +00008140 // This is storing the opcode for MOV32ri.
8141 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00008142 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00008143 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008144 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008145 Trmp, MachinePointerInfo(TrmpAddr),
8146 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008147
Owen Anderson825b72b2009-08-11 20:47:22 +00008148 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8149 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008150 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
8151 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +00008152 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008153
Chris Lattnera62fe662010-02-05 19:20:30 +00008154 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +00008155 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8156 DAG.getConstant(5, MVT::i32));
8157 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00008158 MachinePointerInfo(TrmpAddr, 5),
8159 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008160
Owen Anderson825b72b2009-08-11 20:47:22 +00008161 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8162 DAG.getConstant(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008163 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
8164 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +00008165 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008166
Dan Gohman475871a2008-07-27 21:46:04 +00008167 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00008168 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008169 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008170 }
8171}
8172
Dan Gohmand858e902010-04-17 15:26:15 +00008173SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
8174 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008175 /*
8176 The rounding mode is in bits 11:10 of FPSR, and has the following
8177 settings:
8178 00 Round to nearest
8179 01 Round to -inf
8180 10 Round to +inf
8181 11 Round to 0
8182
8183 FLT_ROUNDS, on the other hand, expects the following:
8184 -1 Undefined
8185 0 Round to 0
8186 1 Round to nearest
8187 2 Round to +inf
8188 3 Round to -inf
8189
8190 To perform the conversion, we do:
8191 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
8192 */
8193
8194 MachineFunction &MF = DAG.getMachineFunction();
8195 const TargetMachine &TM = MF.getTarget();
8196 const TargetFrameInfo &TFI = *TM.getFrameInfo();
8197 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00008198 EVT VT = Op.getValueType();
Chris Lattner2156b792010-09-22 01:11:26 +00008199 DebugLoc DL = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008200
8201 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +00008202 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +00008203 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008204
Michael J. Spencerec38de22010-10-10 22:04:20 +00008205
Chris Lattner2156b792010-09-22 01:11:26 +00008206 MachineMemOperand *MMO =
8207 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8208 MachineMemOperand::MOStore, 2, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008209
Chris Lattner2156b792010-09-22 01:11:26 +00008210 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
8211 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
8212 DAG.getVTList(MVT::Other),
8213 Ops, 2, MVT::i16, MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008214
8215 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +00008216 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Chris Lattner51abfe42010-09-21 06:02:19 +00008217 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008218
8219 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00008220 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +00008221 DAG.getNode(ISD::SRL, DL, MVT::i16,
8222 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00008223 CWD, DAG.getConstant(0x800, MVT::i16)),
8224 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00008225 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +00008226 DAG.getNode(ISD::SRL, DL, MVT::i16,
8227 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00008228 CWD, DAG.getConstant(0x400, MVT::i16)),
8229 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008230
Dan Gohman475871a2008-07-27 21:46:04 +00008231 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +00008232 DAG.getNode(ISD::AND, DL, MVT::i16,
8233 DAG.getNode(ISD::ADD, DL, MVT::i16,
8234 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +00008235 DAG.getConstant(1, MVT::i16)),
8236 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008237
8238
Duncan Sands83ec4b62008-06-06 12:08:01 +00008239 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +00008240 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008241}
8242
Dan Gohmand858e902010-04-17 15:26:15 +00008243SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008244 EVT VT = Op.getValueType();
8245 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008246 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008247 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00008248
8249 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008250 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00008251 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +00008252 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00008253 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008254 }
Evan Cheng18efe262007-12-14 02:13:44 +00008255
Evan Cheng152804e2007-12-14 08:30:15 +00008256 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008257 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008258 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00008259
8260 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00008261 SDValue Ops[] = {
8262 Op,
8263 DAG.getConstant(NumBits+NumBits-1, OpVT),
8264 DAG.getConstant(X86::COND_E, MVT::i8),
8265 Op.getValue(1)
8266 };
8267 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00008268
8269 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +00008270 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +00008271
Owen Anderson825b72b2009-08-11 20:47:22 +00008272 if (VT == MVT::i8)
8273 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008274 return Op;
8275}
8276
Dan Gohmand858e902010-04-17 15:26:15 +00008277SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008278 EVT VT = Op.getValueType();
8279 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008280 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008281 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00008282
8283 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008284 if (VT == MVT::i8) {
8285 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00008286 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008287 }
Evan Cheng152804e2007-12-14 08:30:15 +00008288
8289 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008290 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008291 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00008292
8293 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00008294 SDValue Ops[] = {
8295 Op,
8296 DAG.getConstant(NumBits, OpVT),
8297 DAG.getConstant(X86::COND_E, MVT::i8),
8298 Op.getValue(1)
8299 };
8300 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00008301
Owen Anderson825b72b2009-08-11 20:47:22 +00008302 if (VT == MVT::i8)
8303 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008304 return Op;
8305}
8306
Dan Gohmand858e902010-04-17 15:26:15 +00008307SDValue X86TargetLowering::LowerMUL_V2I64(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008308 EVT VT = Op.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00008309 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008310 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00008311
Mon P Wangaf9b9522008-12-18 21:42:19 +00008312 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
8313 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
8314 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
8315 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
8316 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
8317 //
8318 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
8319 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
8320 // return AloBlo + AloBhi + AhiBlo;
8321
8322 SDValue A = Op.getOperand(0);
8323 SDValue B = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008324
Dale Johannesene4d209d2009-02-03 20:21:25 +00008325 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008326 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
8327 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008328 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008329 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
8330 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008331 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008332 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008333 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008334 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008335 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008336 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008337 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008338 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008339 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008340 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008341 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
8342 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008343 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008344 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
8345 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008346 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
8347 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +00008348 return Res;
8349}
8350
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008351SDValue X86TargetLowering::LowerSHL(SDValue Op, SelectionDAG &DAG) const {
8352 EVT VT = Op.getValueType();
8353 DebugLoc dl = Op.getDebugLoc();
8354 SDValue R = Op.getOperand(0);
8355
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008356 LLVMContext *Context = DAG.getContext();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008357
Nate Begeman51409212010-07-28 00:21:48 +00008358 assert(Subtarget->hasSSE41() && "Cannot lower SHL without SSE4.1 or later");
8359
8360 if (VT == MVT::v4i32) {
8361 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8362 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
8363 Op.getOperand(1), DAG.getConstant(23, MVT::i32));
8364
8365 ConstantInt *CI = ConstantInt::get(*Context, APInt(32, 0x3f800000U));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008366
Nate Begeman51409212010-07-28 00:21:48 +00008367 std::vector<Constant*> CV(4, CI);
8368 Constant *C = ConstantVector::get(CV);
8369 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8370 SDValue Addend = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008371 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +00008372 false, false, 16);
8373
8374 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Addend);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008375 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op);
Nate Begeman51409212010-07-28 00:21:48 +00008376 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
8377 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
8378 }
8379 if (VT == MVT::v16i8) {
8380 // a = a << 5;
8381 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8382 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
8383 Op.getOperand(1), DAG.getConstant(5, MVT::i32));
8384
8385 ConstantInt *CM1 = ConstantInt::get(*Context, APInt(8, 15));
8386 ConstantInt *CM2 = ConstantInt::get(*Context, APInt(8, 63));
8387
8388 std::vector<Constant*> CVM1(16, CM1);
8389 std::vector<Constant*> CVM2(16, CM2);
8390 Constant *C = ConstantVector::get(CVM1);
8391 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8392 SDValue M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008393 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +00008394 false, false, 16);
8395
8396 // r = pblendv(r, psllw(r & (char16)15, 4), a);
8397 M = DAG.getNode(ISD::AND, dl, VT, R, M);
8398 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8399 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
8400 DAG.getConstant(4, MVT::i32));
Nate Begeman672fb622010-12-20 22:04:24 +00008401 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT, R, M, Op);
Nate Begeman51409212010-07-28 00:21:48 +00008402 // a += a
8403 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008404
Nate Begeman51409212010-07-28 00:21:48 +00008405 C = ConstantVector::get(CVM2);
8406 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8407 M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008408 MachinePointerInfo::getConstantPool(),
Chris Lattner51abfe42010-09-21 06:02:19 +00008409 false, false, 16);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008410
Nate Begeman51409212010-07-28 00:21:48 +00008411 // r = pblendv(r, psllw(r & (char16)63, 2), a);
8412 M = DAG.getNode(ISD::AND, dl, VT, R, M);
8413 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8414 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
8415 DAG.getConstant(2, MVT::i32));
Nate Begeman672fb622010-12-20 22:04:24 +00008416 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT, R, M, Op);
Nate Begeman51409212010-07-28 00:21:48 +00008417 // a += a
8418 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008419
Nate Begeman51409212010-07-28 00:21:48 +00008420 // return pblendv(r, r+r, a);
Nate Begeman672fb622010-12-20 22:04:24 +00008421 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT,
Nate Begeman51409212010-07-28 00:21:48 +00008422 R, DAG.getNode(ISD::ADD, dl, VT, R, R), Op);
8423 return R;
8424 }
8425 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008426}
Mon P Wangaf9b9522008-12-18 21:42:19 +00008427
Dan Gohmand858e902010-04-17 15:26:15 +00008428SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
Bill Wendling74c37652008-12-09 22:08:41 +00008429 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
8430 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +00008431 // looks for this combo and may remove the "setcc" instruction if the "setcc"
8432 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +00008433 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +00008434 SDValue LHS = N->getOperand(0);
8435 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +00008436 unsigned BaseOp = 0;
8437 unsigned Cond = 0;
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008438 DebugLoc DL = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +00008439 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008440 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +00008441 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +00008442 // A subtract of one will be selected as a INC. Note that INC doesn't
8443 // set CF, so we can't do this for UADDO.
8444 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
8445 if (C->getAPIntValue() == 1) {
8446 BaseOp = X86ISD::INC;
8447 Cond = X86::COND_O;
8448 break;
8449 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008450 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +00008451 Cond = X86::COND_O;
8452 break;
8453 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008454 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +00008455 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00008456 break;
8457 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +00008458 // A subtract of one will be selected as a DEC. Note that DEC doesn't
8459 // set CF, so we can't do this for USUBO.
8460 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
8461 if (C->getAPIntValue() == 1) {
8462 BaseOp = X86ISD::DEC;
8463 Cond = X86::COND_O;
8464 break;
8465 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008466 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +00008467 Cond = X86::COND_O;
8468 break;
8469 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008470 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +00008471 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00008472 break;
8473 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00008474 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +00008475 Cond = X86::COND_O;
8476 break;
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008477 case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs
8478 SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0),
8479 MVT::i32);
8480 SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS);
8481
8482 SDValue SetCC =
8483 DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
8484 DAG.getConstant(X86::COND_O, MVT::i32),
8485 SDValue(Sum.getNode(), 2));
8486
8487 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
8488 return Sum;
8489 }
Bill Wendling74c37652008-12-09 22:08:41 +00008490 }
Bill Wendling3fafd932008-11-26 22:37:40 +00008491
Bill Wendling61edeb52008-12-02 01:06:39 +00008492 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008493 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008494 SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +00008495
Bill Wendling61edeb52008-12-02 01:06:39 +00008496 SDValue SetCC =
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008497 DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1),
8498 DAG.getConstant(Cond, MVT::i32),
8499 SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +00008500
Bill Wendling61edeb52008-12-02 01:06:39 +00008501 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
8502 return Sum;
Bill Wendling41ea7e72008-11-24 19:21:46 +00008503}
8504
Eric Christopher9a9d2752010-07-22 02:48:34 +00008505SDValue X86TargetLowering::LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG) const{
8506 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00008507
Eric Christopherb6729dc2010-08-04 23:03:04 +00008508 if (!Subtarget->hasSSE2()) {
Eric Christopherc0b2a202010-08-14 21:51:50 +00008509 SDValue Chain = Op.getOperand(0);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008510 SDValue Zero = DAG.getConstant(0,
Eric Christopherb6729dc2010-08-04 23:03:04 +00008511 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Eric Christopherc0b2a202010-08-14 21:51:50 +00008512 SDValue Ops[] = {
8513 DAG.getRegister(X86::ESP, MVT::i32), // Base
8514 DAG.getTargetConstant(1, MVT::i8), // Scale
8515 DAG.getRegister(0, MVT::i32), // Index
8516 DAG.getTargetConstant(0, MVT::i32), // Disp
8517 DAG.getRegister(0, MVT::i32), // Segment.
8518 Zero,
8519 Chain
8520 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00008521 SDNode *Res =
Eric Christopherc0b2a202010-08-14 21:51:50 +00008522 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
8523 array_lengthof(Ops));
8524 return SDValue(Res, 0);
Eric Christopherb6729dc2010-08-04 23:03:04 +00008525 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00008526
Eric Christopher9a9d2752010-07-22 02:48:34 +00008527 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
Chris Lattner132929a2010-08-14 17:26:09 +00008528 if (!isDev)
Eric Christopher9a9d2752010-07-22 02:48:34 +00008529 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008530
Chris Lattner132929a2010-08-14 17:26:09 +00008531 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
8532 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
8533 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
8534 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
Michael J. Spencerec38de22010-10-10 22:04:20 +00008535
Chris Lattner132929a2010-08-14 17:26:09 +00008536 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
8537 if (!Op1 && !Op2 && !Op3 && Op4)
8538 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008539
Chris Lattner132929a2010-08-14 17:26:09 +00008540 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
8541 if (Op1 && !Op2 && !Op3 && !Op4)
8542 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008543
8544 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
Chris Lattner132929a2010-08-14 17:26:09 +00008545 // (MFENCE)>;
8546 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
Eric Christopher9a9d2752010-07-22 02:48:34 +00008547}
8548
Dan Gohmand858e902010-04-17 15:26:15 +00008549SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008550 EVT T = Op.getValueType();
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008551 DebugLoc DL = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +00008552 unsigned Reg = 0;
8553 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00008554 switch(T.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00008555 default:
8556 assert(false && "Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +00008557 case MVT::i8: Reg = X86::AL; size = 1; break;
8558 case MVT::i16: Reg = X86::AX; size = 2; break;
8559 case MVT::i32: Reg = X86::EAX; size = 4; break;
8560 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +00008561 assert(Subtarget->is64Bit() && "Node not type legal!");
8562 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +00008563 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00008564 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008565 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +00008566 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +00008567 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +00008568 Op.getOperand(1),
8569 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +00008570 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +00008571 cpIn.getValue(1) };
Owen Anderson825b72b2009-08-11 20:47:22 +00008572 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008573 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
8574 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
8575 Ops, 5, T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +00008576 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008577 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +00008578 return cpOut;
8579}
8580
Duncan Sands1607f052008-12-01 11:39:25 +00008581SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008582 SelectionDAG &DAG) const {
Duncan Sands1607f052008-12-01 11:39:25 +00008583 assert(Subtarget->is64Bit() && "Result not type legalized?");
Owen Anderson825b72b2009-08-11 20:47:22 +00008584 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Duncan Sands1607f052008-12-01 11:39:25 +00008585 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008586 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +00008587 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00008588 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
8589 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +00008590 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +00008591 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
8592 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +00008593 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +00008594 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +00008595 rdx.getValue(1)
8596 };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008597 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008598}
8599
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008600SDValue X86TargetLowering::LowerBITCAST(SDValue Op,
Dale Johannesen7d07b482010-05-21 00:52:33 +00008601 SelectionDAG &DAG) const {
8602 EVT SrcVT = Op.getOperand(0).getValueType();
8603 EVT DstVT = Op.getValueType();
Chris Lattner2a786eb2010-12-19 20:19:20 +00008604 assert(Subtarget->is64Bit() && !Subtarget->hasSSE2() &&
8605 Subtarget->hasMMX() && "Unexpected custom BITCAST");
Michael J. Spencerec38de22010-10-10 22:04:20 +00008606 assert((DstVT == MVT::i64 ||
Dale Johannesen7d07b482010-05-21 00:52:33 +00008607 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008608 "Unexpected custom BITCAST");
Dale Johannesen7d07b482010-05-21 00:52:33 +00008609 // i64 <=> MMX conversions are Legal.
8610 if (SrcVT==MVT::i64 && DstVT.isVector())
8611 return Op;
8612 if (DstVT==MVT::i64 && SrcVT.isVector())
8613 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +00008614 // MMX <=> MMX conversions are Legal.
8615 if (SrcVT.isVector() && DstVT.isVector())
8616 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +00008617 // All other conversions need to be expanded.
8618 return SDValue();
8619}
Chris Lattner5b856542010-12-20 00:59:46 +00008620
Dan Gohmand858e902010-04-17 15:26:15 +00008621SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen71d1bf52008-09-29 22:25:26 +00008622 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +00008623 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008624 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008625 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +00008626 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008627 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008628 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +00008629 Node->getOperand(0),
8630 Node->getOperand(1), negOp,
8631 cast<AtomicSDNode>(Node)->getSrcValue(),
8632 cast<AtomicSDNode>(Node)->getAlignment());
Mon P Wang63307c32008-05-05 19:05:59 +00008633}
8634
Chris Lattner5b856542010-12-20 00:59:46 +00008635static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
8636 EVT VT = Op.getNode()->getValueType(0);
8637
8638 // Let legalize expand this if it isn't a legal type yet.
8639 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
8640 return SDValue();
8641
8642 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
8643
8644 unsigned Opc;
8645 bool ExtraOp = false;
8646 switch (Op.getOpcode()) {
8647 default: assert(0 && "Invalid code");
8648 case ISD::ADDC: Opc = X86ISD::ADD; break;
8649 case ISD::ADDE: Opc = X86ISD::ADC; ExtraOp = true; break;
8650 case ISD::SUBC: Opc = X86ISD::SUB; break;
8651 case ISD::SUBE: Opc = X86ISD::SBB; ExtraOp = true; break;
8652 }
8653
8654 if (!ExtraOp)
8655 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
8656 Op.getOperand(1));
8657 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
8658 Op.getOperand(1), Op.getOperand(2));
8659}
8660
Evan Cheng0db9fe62006-04-25 20:13:52 +00008661/// LowerOperation - Provide custom lowering hooks for some operations.
8662///
Dan Gohmand858e902010-04-17 15:26:15 +00008663SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00008664 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008665 default: llvm_unreachable("Should not custom lower this!");
Eric Christopher9a9d2752010-07-22 02:48:34 +00008666 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op,DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008667 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
8668 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008669 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00008670 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008671 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
8672 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
8673 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
8674 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
8675 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
8676 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00008677 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +00008678 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +00008679 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008680 case ISD::SHL_PARTS:
8681 case ISD::SRA_PARTS:
8682 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
8683 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00008684 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008685 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00008686 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008687 case ISD::FABS: return LowerFABS(Op, DAG);
8688 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008689 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00008690 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman30a0de92008-07-17 16:51:19 +00008691 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00008692 case ISD::SELECT: return LowerSELECT(Op, DAG);
8693 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008694 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008695 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +00008696 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +00008697 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008698 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00008699 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
8700 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008701 case ISD::FRAME_TO_ARGS_OFFSET:
8702 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008703 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008704 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008705 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +00008706 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +00008707 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
8708 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Mon P Wangaf9b9522008-12-18 21:42:19 +00008709 case ISD::MUL: return LowerMUL_V2I64(Op, DAG);
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008710 case ISD::SHL: return LowerSHL(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +00008711 case ISD::SADDO:
8712 case ISD::UADDO:
8713 case ISD::SSUBO:
8714 case ISD::USUBO:
8715 case ISD::SMULO:
8716 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +00008717 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008718 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Chris Lattner5b856542010-12-20 00:59:46 +00008719 case ISD::ADDC:
8720 case ISD::ADDE:
8721 case ISD::SUBC:
8722 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008723 }
Chris Lattner27a6c732007-11-24 07:07:01 +00008724}
8725
Duncan Sands1607f052008-12-01 11:39:25 +00008726void X86TargetLowering::
8727ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00008728 SelectionDAG &DAG, unsigned NewOp) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008729 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008730 DebugLoc dl = Node->getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00008731 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +00008732
8733 SDValue Chain = Node->getOperand(0);
8734 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00008735 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00008736 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00008737 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00008738 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +00008739 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +00008740 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +00008741 SDValue Result =
8742 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
8743 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +00008744 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +00008745 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00008746 Results.push_back(Result.getValue(2));
8747}
8748
Duncan Sands126d9072008-07-04 11:47:58 +00008749/// ReplaceNodeResults - Replace a node with an illegal result type
8750/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00008751void X86TargetLowering::ReplaceNodeResults(SDNode *N,
8752 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00008753 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +00008754 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +00008755 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +00008756 default:
Duncan Sands1607f052008-12-01 11:39:25 +00008757 assert(false && "Do not know how to custom type legalize this operation!");
8758 return;
Chris Lattner5b856542010-12-20 00:59:46 +00008759 case ISD::ADDC:
8760 case ISD::ADDE:
8761 case ISD::SUBC:
8762 case ISD::SUBE:
8763 // We don't want to expand or promote these.
8764 return;
Duncan Sands1607f052008-12-01 11:39:25 +00008765 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +00008766 std::pair<SDValue,SDValue> Vals =
8767 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +00008768 SDValue FIST = Vals.first, StackSlot = Vals.second;
8769 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +00008770 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +00008771 // Return a load from the stack slot.
Chris Lattner51abfe42010-09-21 06:02:19 +00008772 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
8773 MachinePointerInfo(), false, false, 0));
Duncan Sands1607f052008-12-01 11:39:25 +00008774 }
8775 return;
8776 }
8777 case ISD::READCYCLECOUNTER: {
Owen Anderson825b72b2009-08-11 20:47:22 +00008778 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Duncan Sands1607f052008-12-01 11:39:25 +00008779 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008780 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00008781 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +00008782 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00008783 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00008784 eax.getValue(2));
8785 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
8786 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +00008787 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00008788 Results.push_back(edx.getValue(1));
8789 return;
8790 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008791 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +00008792 EVT T = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008793 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
Duncan Sands1607f052008-12-01 11:39:25 +00008794 SDValue cpInL, cpInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00008795 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
8796 DAG.getConstant(0, MVT::i32));
8797 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
8798 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00008799 cpInL = DAG.getCopyToReg(N->getOperand(0), dl, X86::EAX, cpInL, SDValue());
8800 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl, X86::EDX, cpInH,
Duncan Sands1607f052008-12-01 11:39:25 +00008801 cpInL.getValue(1));
8802 SDValue swapInL, swapInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00008803 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
8804 DAG.getConstant(0, MVT::i32));
8805 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
8806 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00008807 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl, X86::EBX, swapInL,
Duncan Sands1607f052008-12-01 11:39:25 +00008808 cpInH.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00008809 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl, X86::ECX, swapInH,
Duncan Sands1607f052008-12-01 11:39:25 +00008810 swapInL.getValue(1));
8811 SDValue Ops[] = { swapInH.getValue(0),
8812 N->getOperand(1),
8813 swapInH.getValue(1) };
Owen Anderson825b72b2009-08-11 20:47:22 +00008814 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Andrew Trick1a2cf3b2010-10-11 19:02:04 +00008815 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
8816 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG8_DAG, dl, Tys,
8817 Ops, 3, T, MMO);
Dale Johannesendd64c412009-02-04 00:33:20 +00008818 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl, X86::EAX,
Owen Anderson825b72b2009-08-11 20:47:22 +00008819 MVT::i32, Result.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00008820 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl, X86::EDX,
Owen Anderson825b72b2009-08-11 20:47:22 +00008821 MVT::i32, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +00008822 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Owen Anderson825b72b2009-08-11 20:47:22 +00008823 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00008824 Results.push_back(cpOutH.getValue(1));
8825 return;
8826 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008827 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +00008828 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
8829 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008830 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +00008831 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
8832 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008833 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +00008834 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
8835 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008836 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +00008837 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
8838 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008839 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +00008840 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
8841 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008842 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +00008843 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
8844 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008845 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +00008846 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
8847 return;
Chris Lattner27a6c732007-11-24 07:07:01 +00008848 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00008849}
8850
Evan Cheng72261582005-12-20 06:22:03 +00008851const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
8852 switch (Opcode) {
8853 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +00008854 case X86ISD::BSF: return "X86ISD::BSF";
8855 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +00008856 case X86ISD::SHLD: return "X86ISD::SHLD";
8857 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00008858 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +00008859 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +00008860 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +00008861 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +00008862 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00008863 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00008864 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
8865 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
8866 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00008867 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00008868 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +00008869 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +00008870 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +00008871 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +00008872 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +00008873 case X86ISD::COMI: return "X86ISD::COMI";
8874 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +00008875 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +00008876 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Evan Cheng72261582005-12-20 06:22:03 +00008877 case X86ISD::CMOV: return "X86ISD::CMOV";
8878 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00008879 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00008880 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
8881 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +00008882 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00008883 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +00008884 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +00008885 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +00008886 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +00008887 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
8888 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +00008889 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +00008890 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Nate Begemanb65c1752010-12-17 22:55:37 +00008891 case X86ISD::PANDN: return "X86ISD::PANDN";
8892 case X86ISD::PSIGNB: return "X86ISD::PSIGNB";
8893 case X86ISD::PSIGNW: return "X86ISD::PSIGNW";
8894 case X86ISD::PSIGND: return "X86ISD::PSIGND";
Nate Begeman672fb622010-12-20 22:04:24 +00008895 case X86ISD::PBLENDVB: return "X86ISD::PBLENDVB";
Evan Cheng8ca29322006-11-10 21:43:37 +00008896 case X86ISD::FMAX: return "X86ISD::FMAX";
8897 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +00008898 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
8899 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00008900 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +00008901 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008902 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00008903 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008904 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +00008905 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
8906 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008907 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
8908 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
8909 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
8910 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
8911 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
8912 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +00008913 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
8914 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +00008915 case X86ISD::VSHL: return "X86ISD::VSHL";
8916 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +00008917 case X86ISD::CMPPD: return "X86ISD::CMPPD";
8918 case X86ISD::CMPPS: return "X86ISD::CMPPS";
8919 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
8920 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
8921 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
8922 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
8923 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
8924 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
8925 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
8926 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008927 case X86ISD::ADD: return "X86ISD::ADD";
8928 case X86ISD::SUB: return "X86ISD::SUB";
Chris Lattner5b856542010-12-20 00:59:46 +00008929 case X86ISD::ADC: return "X86ISD::ADC";
8930 case X86ISD::SBB: return "X86ISD::SBB";
Bill Wendlingd350e022008-12-12 21:15:41 +00008931 case X86ISD::SMUL: return "X86ISD::SMUL";
8932 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +00008933 case X86ISD::INC: return "X86ISD::INC";
8934 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +00008935 case X86ISD::OR: return "X86ISD::OR";
8936 case X86ISD::XOR: return "X86ISD::XOR";
8937 case X86ISD::AND: return "X86ISD::AND";
Evan Cheng73f24c92009-03-30 21:36:47 +00008938 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +00008939 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008940 case X86ISD::TESTP: return "X86ISD::TESTP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00008941 case X86ISD::PALIGN: return "X86ISD::PALIGN";
8942 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
8943 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
8944 case X86ISD::PSHUFHW_LD: return "X86ISD::PSHUFHW_LD";
8945 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
8946 case X86ISD::PSHUFLW_LD: return "X86ISD::PSHUFLW_LD";
8947 case X86ISD::SHUFPS: return "X86ISD::SHUFPS";
8948 case X86ISD::SHUFPD: return "X86ISD::SHUFPD";
8949 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00008950 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00008951 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00008952 case X86ISD::MOVHLPD: return "X86ISD::MOVHLPD";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00008953 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
8954 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00008955 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
8956 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
8957 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
8958 case X86ISD::MOVSHDUP_LD: return "X86ISD::MOVSHDUP_LD";
8959 case X86ISD::MOVSLDUP_LD: return "X86ISD::MOVSLDUP_LD";
8960 case X86ISD::MOVSD: return "X86ISD::MOVSD";
8961 case X86ISD::MOVSS: return "X86ISD::MOVSS";
8962 case X86ISD::UNPCKLPS: return "X86ISD::UNPCKLPS";
8963 case X86ISD::UNPCKLPD: return "X86ISD::UNPCKLPD";
8964 case X86ISD::UNPCKHPS: return "X86ISD::UNPCKHPS";
8965 case X86ISD::UNPCKHPD: return "X86ISD::UNPCKHPD";
8966 case X86ISD::PUNPCKLBW: return "X86ISD::PUNPCKLBW";
8967 case X86ISD::PUNPCKLWD: return "X86ISD::PUNPCKLWD";
8968 case X86ISD::PUNPCKLDQ: return "X86ISD::PUNPCKLDQ";
8969 case X86ISD::PUNPCKLQDQ: return "X86ISD::PUNPCKLQDQ";
8970 case X86ISD::PUNPCKHBW: return "X86ISD::PUNPCKHBW";
8971 case X86ISD::PUNPCKHWD: return "X86ISD::PUNPCKHWD";
8972 case X86ISD::PUNPCKHDQ: return "X86ISD::PUNPCKHDQ";
8973 case X86ISD::PUNPCKHQDQ: return "X86ISD::PUNPCKHQDQ";
Dan Gohmand6708ea2009-08-15 01:38:56 +00008974 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Dan Gohman320afb82010-10-12 18:00:49 +00008975 case X86ISD::VAARG_64: return "X86ISD::VAARG_64";
Michael J. Spencere9c253e2010-10-21 01:41:01 +00008976 case X86ISD::WIN_ALLOCA: return "X86ISD::WIN_ALLOCA";
Evan Cheng72261582005-12-20 06:22:03 +00008977 }
8978}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008979
Chris Lattnerc9addb72007-03-30 23:15:24 +00008980// isLegalAddressingMode - Return true if the addressing mode represented
8981// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +00008982bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerc9addb72007-03-30 23:15:24 +00008983 const Type *Ty) const {
8984 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00008985 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +00008986 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +00008987
Chris Lattnerc9addb72007-03-30 23:15:24 +00008988 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00008989 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +00008990 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00008991
Chris Lattnerc9addb72007-03-30 23:15:24 +00008992 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +00008993 unsigned GVFlags =
8994 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00008995
Chris Lattnerdfed4132009-07-10 07:38:24 +00008996 // If a reference to this global requires an extra load, we can't fold it.
8997 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +00008998 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00008999
Chris Lattnerdfed4132009-07-10 07:38:24 +00009000 // If BaseGV requires a register for the PIC base, we cannot also have a
9001 // BaseReg specified.
9002 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +00009003 return false;
Evan Cheng52787842007-08-01 23:46:47 +00009004
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009005 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +00009006 if ((M != CodeModel::Small || R != Reloc::Static) &&
9007 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009008 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +00009009 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009010
Chris Lattnerc9addb72007-03-30 23:15:24 +00009011 switch (AM.Scale) {
9012 case 0:
9013 case 1:
9014 case 2:
9015 case 4:
9016 case 8:
9017 // These scales always work.
9018 break;
9019 case 3:
9020 case 5:
9021 case 9:
9022 // These scales are formed with basereg+scalereg. Only accept if there is
9023 // no basereg yet.
9024 if (AM.HasBaseReg)
9025 return false;
9026 break;
9027 default: // Other stuff never works.
9028 return false;
9029 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009030
Chris Lattnerc9addb72007-03-30 23:15:24 +00009031 return true;
9032}
9033
9034
Evan Cheng2bd122c2007-10-26 01:56:11 +00009035bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00009036 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +00009037 return false;
Evan Chenge127a732007-10-29 07:57:50 +00009038 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
9039 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00009040 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +00009041 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00009042 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +00009043}
9044
Owen Andersone50ed302009-08-10 22:56:29 +00009045bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +00009046 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009047 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009048 unsigned NumBits1 = VT1.getSizeInBits();
9049 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00009050 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009051 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00009052 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009053}
Evan Cheng2bd122c2007-10-26 01:56:11 +00009054
Dan Gohman97121ba2009-04-08 00:15:30 +00009055bool X86TargetLowering::isZExtFree(const Type *Ty1, const Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00009056 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00009057 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00009058}
9059
Owen Andersone50ed302009-08-10 22:56:29 +00009060bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00009061 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00009062 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00009063}
9064
Owen Andersone50ed302009-08-10 22:56:29 +00009065bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +00009066 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +00009067 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +00009068}
9069
Evan Cheng60c07e12006-07-05 22:17:51 +00009070/// isShuffleMaskLegal - Targets can use this to indicate that they only
9071/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
9072/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
9073/// are assumed to be legal.
9074bool
Eric Christopherfd179292009-08-27 18:07:15 +00009075X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +00009076 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +00009077 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +00009078 if (VT.getSizeInBits() == 64)
Eric Christophercff6f852010-04-15 01:40:20 +00009079 return isPALIGNRMask(M, VT, Subtarget->hasSSSE3());
Nate Begeman9008ca62009-04-27 18:41:29 +00009080
Nate Begemana09008b2009-10-19 02:17:23 +00009081 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +00009082 return (VT.getVectorNumElements() == 2 ||
9083 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
9084 isMOVLMask(M, VT) ||
9085 isSHUFPMask(M, VT) ||
9086 isPSHUFDMask(M, VT) ||
9087 isPSHUFHWMask(M, VT) ||
9088 isPSHUFLWMask(M, VT) ||
Nate Begemana09008b2009-10-19 02:17:23 +00009089 isPALIGNRMask(M, VT, Subtarget->hasSSSE3()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +00009090 isUNPCKLMask(M, VT) ||
9091 isUNPCKHMask(M, VT) ||
9092 isUNPCKL_v_undef_Mask(M, VT) ||
9093 isUNPCKH_v_undef_Mask(M, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00009094}
9095
Dan Gohman7d8143f2008-04-09 20:09:42 +00009096bool
Nate Begeman5a5ca152009-04-29 05:20:52 +00009097X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +00009098 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00009099 unsigned NumElts = VT.getVectorNumElements();
9100 // FIXME: This collection of masks seems suspect.
9101 if (NumElts == 2)
9102 return true;
9103 if (NumElts == 4 && VT.getSizeInBits() == 128) {
9104 return (isMOVLMask(Mask, VT) ||
9105 isCommutedMOVLMask(Mask, VT, true) ||
9106 isSHUFPMask(Mask, VT) ||
9107 isCommutedSHUFPMask(Mask, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00009108 }
9109 return false;
9110}
9111
9112//===----------------------------------------------------------------------===//
9113// X86 Scheduler Hooks
9114//===----------------------------------------------------------------------===//
9115
Mon P Wang63307c32008-05-05 19:05:59 +00009116// private utility function
9117MachineBasicBlock *
9118X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
9119 MachineBasicBlock *MBB,
9120 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009121 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009122 unsigned LoadOpc,
9123 unsigned CXchgOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009124 unsigned notOpc,
9125 unsigned EAXreg,
9126 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009127 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00009128 // For the atomic bitwise operator, we generate
9129 // thisMBB:
9130 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00009131 // ld t1 = [bitinstr.addr]
9132 // op t2 = t1, [bitinstr.val]
9133 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00009134 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
9135 // bz newMBB
9136 // fallthrough -->nextMBB
9137 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9138 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009139 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00009140 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009141
Mon P Wang63307c32008-05-05 19:05:59 +00009142 /// First build the CFG
9143 MachineFunction *F = MBB->getParent();
9144 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009145 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9146 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9147 F->insert(MBBIter, newMBB);
9148 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009149
Dan Gohman14152b42010-07-06 20:24:04 +00009150 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9151 nextMBB->splice(nextMBB->begin(), thisMBB,
9152 llvm::next(MachineBasicBlock::iterator(bInstr)),
9153 thisMBB->end());
9154 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009155
Mon P Wang63307c32008-05-05 19:05:59 +00009156 // Update thisMBB to fall through to newMBB
9157 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009158
Mon P Wang63307c32008-05-05 19:05:59 +00009159 // newMBB jumps to itself and fall through to nextMBB
9160 newMBB->addSuccessor(nextMBB);
9161 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009162
Mon P Wang63307c32008-05-05 19:05:59 +00009163 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009164 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009165 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +00009166 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00009167 MachineOperand& destOper = bInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009168 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00009169 int numArgs = bInstr->getNumOperands() - 1;
9170 for (int i=0; i < numArgs; ++i)
9171 argOpers[i] = &bInstr->getOperand(i+1);
9172
9173 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009174 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009175 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00009176
Dale Johannesen140be2d2008-08-19 18:47:28 +00009177 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009178 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00009179 for (int i=0; i <= lastAddrIndx; ++i)
9180 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009181
Dale Johannesen140be2d2008-08-19 18:47:28 +00009182 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009183 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00009184 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009185 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009186 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009187 tt = t1;
9188
Dale Johannesen140be2d2008-08-19 18:47:28 +00009189 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +00009190 assert((argOpers[valArgIndx]->isReg() ||
9191 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00009192 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +00009193 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009194 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00009195 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009196 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009197 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +00009198 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009199
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009200 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +00009201 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009202
Dale Johannesene4d209d2009-02-03 20:21:25 +00009203 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +00009204 for (int i=0; i <= lastAddrIndx; ++i)
9205 (*MIB).addOperand(*argOpers[i]);
9206 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +00009207 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00009208 (*MIB).setMemRefs(bInstr->memoperands_begin(),
9209 bInstr->memoperands_end());
Mon P Wangf5952662008-07-17 04:54:06 +00009210
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009211 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +00009212 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +00009213
Mon P Wang63307c32008-05-05 19:05:59 +00009214 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009215 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00009216
Dan Gohman14152b42010-07-06 20:24:04 +00009217 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00009218 return nextMBB;
9219}
9220
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00009221// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +00009222MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009223X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
9224 MachineBasicBlock *MBB,
9225 unsigned regOpcL,
9226 unsigned regOpcH,
9227 unsigned immOpcL,
9228 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009229 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009230 // For the atomic bitwise operator, we generate
9231 // thisMBB (instructions are in pairs, except cmpxchg8b)
9232 // ld t1,t2 = [bitinstr.addr]
9233 // newMBB:
9234 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
9235 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +00009236 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009237 // mov ECX, EBX <- t5, t6
9238 // mov EAX, EDX <- t1, t2
9239 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
9240 // mov t3, t4 <- EAX, EDX
9241 // bz newMBB
9242 // result in out1, out2
9243 // fallthrough -->nextMBB
9244
9245 const TargetRegisterClass *RC = X86::GR32RegisterClass;
9246 const unsigned LoadOpc = X86::MOV32rm;
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009247 const unsigned NotOpc = X86::NOT32r;
9248 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9249 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
9250 MachineFunction::iterator MBBIter = MBB;
9251 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009252
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009253 /// First build the CFG
9254 MachineFunction *F = MBB->getParent();
9255 MachineBasicBlock *thisMBB = MBB;
9256 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9257 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9258 F->insert(MBBIter, newMBB);
9259 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009260
Dan Gohman14152b42010-07-06 20:24:04 +00009261 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9262 nextMBB->splice(nextMBB->begin(), thisMBB,
9263 llvm::next(MachineBasicBlock::iterator(bInstr)),
9264 thisMBB->end());
9265 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009266
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009267 // Update thisMBB to fall through to newMBB
9268 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009269
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009270 // newMBB jumps to itself and fall through to nextMBB
9271 newMBB->addSuccessor(nextMBB);
9272 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009273
Dale Johannesene4d209d2009-02-03 20:21:25 +00009274 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009275 // Insert instructions into newMBB based on incoming instruction
9276 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009277 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009278 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009279 MachineOperand& dest1Oper = bInstr->getOperand(0);
9280 MachineOperand& dest2Oper = bInstr->getOperand(1);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009281 MachineOperand* argOpers[2 + X86::AddrNumOperands];
9282 for (int i=0; i < 2 + X86::AddrNumOperands; ++i) {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009283 argOpers[i] = &bInstr->getOperand(i+2);
9284
Dan Gohman71ea4e52010-05-14 21:01:44 +00009285 // We use some of the operands multiple times, so conservatively just
9286 // clear any kill flags that might be present.
9287 if (argOpers[i]->isReg() && argOpers[i]->isUse())
9288 argOpers[i]->setIsKill(false);
9289 }
9290
Evan Chengad5b52f2010-01-08 19:14:57 +00009291 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009292 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +00009293
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009294 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009295 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009296 for (int i=0; i <= lastAddrIndx; ++i)
9297 (*MIB).addOperand(*argOpers[i]);
9298 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009299 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +00009300 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +00009301 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009302 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +00009303 MachineOperand newOp3 = *(argOpers[3]);
9304 if (newOp3.isImm())
9305 newOp3.setImm(newOp3.getImm()+4);
9306 else
9307 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009308 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +00009309 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009310
9311 // t3/4 are defined later, at the bottom of the loop
9312 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
9313 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009314 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009315 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009316 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009317 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
9318
Evan Cheng306b4ca2010-01-08 23:41:50 +00009319 // The subsequent operations should be using the destination registers of
9320 //the PHI instructions.
Scott Michelfdc40a02009-02-17 22:15:04 +00009321 if (invSrc) {
Evan Cheng306b4ca2010-01-08 23:41:50 +00009322 t1 = F->getRegInfo().createVirtualRegister(RC);
9323 t2 = F->getRegInfo().createVirtualRegister(RC);
9324 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t1).addReg(dest1Oper.getReg());
9325 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t2).addReg(dest2Oper.getReg());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009326 } else {
Evan Cheng306b4ca2010-01-08 23:41:50 +00009327 t1 = dest1Oper.getReg();
9328 t2 = dest2Oper.getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009329 }
9330
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009331 int valArgIndx = lastAddrIndx + 1;
9332 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +00009333 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009334 "invalid operand");
9335 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
9336 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009337 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009338 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009339 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009340 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +00009341 if (regOpcL != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00009342 MIB.addReg(t1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009343 (*MIB).addOperand(*argOpers[valArgIndx]);
9344 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00009345 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009346 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00009347 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009348 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009349 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009350 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009351 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +00009352 if (regOpcH != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00009353 MIB.addReg(t2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009354 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009355
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009356 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009357 MIB.addReg(t1);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009358 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009359 MIB.addReg(t2);
9360
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009361 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009362 MIB.addReg(t5);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009363 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009364 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +00009365
Dale Johannesene4d209d2009-02-03 20:21:25 +00009366 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009367 for (int i=0; i <= lastAddrIndx; ++i)
9368 (*MIB).addOperand(*argOpers[i]);
9369
9370 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00009371 (*MIB).setMemRefs(bInstr->memoperands_begin(),
9372 bInstr->memoperands_end());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009373
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009374 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009375 MIB.addReg(X86::EAX);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009376 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009377 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +00009378
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009379 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009380 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009381
Dan Gohman14152b42010-07-06 20:24:04 +00009382 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009383 return nextMBB;
9384}
9385
9386// private utility function
9387MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +00009388X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
9389 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009390 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00009391 // For the atomic min/max operator, we generate
9392 // thisMBB:
9393 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00009394 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +00009395 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +00009396 // cmp t1, t2
9397 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +00009398 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00009399 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
9400 // bz newMBB
9401 // fallthrough -->nextMBB
9402 //
9403 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9404 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009405 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00009406 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009407
Mon P Wang63307c32008-05-05 19:05:59 +00009408 /// First build the CFG
9409 MachineFunction *F = MBB->getParent();
9410 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009411 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9412 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9413 F->insert(MBBIter, newMBB);
9414 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009415
Dan Gohman14152b42010-07-06 20:24:04 +00009416 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9417 nextMBB->splice(nextMBB->begin(), thisMBB,
9418 llvm::next(MachineBasicBlock::iterator(mInstr)),
9419 thisMBB->end());
9420 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009421
Mon P Wang63307c32008-05-05 19:05:59 +00009422 // Update thisMBB to fall through to newMBB
9423 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009424
Mon P Wang63307c32008-05-05 19:05:59 +00009425 // newMBB jumps to newMBB and fall through to nextMBB
9426 newMBB->addSuccessor(nextMBB);
9427 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009428
Dale Johannesene4d209d2009-02-03 20:21:25 +00009429 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00009430 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009431 assert(mInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009432 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +00009433 MachineOperand& destOper = mInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009434 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00009435 int numArgs = mInstr->getNumOperands() - 1;
9436 for (int i=0; i < numArgs; ++i)
9437 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009438
Mon P Wang63307c32008-05-05 19:05:59 +00009439 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009440 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009441 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00009442
Mon P Wangab3e7472008-05-05 22:56:23 +00009443 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009444 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00009445 for (int i=0; i <= lastAddrIndx; ++i)
9446 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +00009447
Mon P Wang63307c32008-05-05 19:05:59 +00009448 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +00009449 assert((argOpers[valArgIndx]->isReg() ||
9450 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00009451 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +00009452
9453 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +00009454 if (argOpers[valArgIndx]->isReg())
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009455 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +00009456 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009457 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00009458 (*MIB).addOperand(*argOpers[valArgIndx]);
9459
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009460 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +00009461 MIB.addReg(t1);
9462
Dale Johannesene4d209d2009-02-03 20:21:25 +00009463 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +00009464 MIB.addReg(t1);
9465 MIB.addReg(t2);
9466
9467 // Generate movc
9468 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009469 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +00009470 MIB.addReg(t2);
9471 MIB.addReg(t1);
9472
9473 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +00009474 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +00009475 for (int i=0; i <= lastAddrIndx; ++i)
9476 (*MIB).addOperand(*argOpers[i]);
9477 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +00009478 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00009479 (*MIB).setMemRefs(mInstr->memoperands_begin(),
9480 mInstr->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +00009481
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009482 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +00009483 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +00009484
Mon P Wang63307c32008-05-05 19:05:59 +00009485 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009486 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00009487
Dan Gohman14152b42010-07-06 20:24:04 +00009488 mInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00009489 return nextMBB;
9490}
9491
Eric Christopherf83a5de2009-08-27 18:08:16 +00009492// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00009493// or XMM0_V32I8 in AVX all of this code can be replaced with that
9494// in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +00009495MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +00009496X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +00009497 unsigned numArgs, bool memArg) const {
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00009498 assert((Subtarget->hasSSE42() || Subtarget->hasAVX()) &&
9499 "Target must have SSE4.2 or AVX features enabled");
9500
Eric Christopherb120ab42009-08-18 22:50:32 +00009501 DebugLoc dl = MI->getDebugLoc();
9502 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Eric Christopherb120ab42009-08-18 22:50:32 +00009503 unsigned Opc;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00009504 if (!Subtarget->hasAVX()) {
9505 if (memArg)
9506 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
9507 else
9508 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
9509 } else {
9510 if (memArg)
9511 Opc = numArgs == 3 ? X86::VPCMPISTRM128rm : X86::VPCMPESTRM128rm;
9512 else
9513 Opc = numArgs == 3 ? X86::VPCMPISTRM128rr : X86::VPCMPESTRM128rr;
9514 }
Eric Christopherb120ab42009-08-18 22:50:32 +00009515
Eric Christopher41c902f2010-11-30 08:20:21 +00009516 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Eric Christopherb120ab42009-08-18 22:50:32 +00009517 for (unsigned i = 0; i < numArgs; ++i) {
9518 MachineOperand &Op = MI->getOperand(i+1);
Eric Christopherb120ab42009-08-18 22:50:32 +00009519 if (!(Op.isReg() && Op.isImplicit()))
9520 MIB.addOperand(Op);
9521 }
Eric Christopher41c902f2010-11-30 08:20:21 +00009522 BuildMI(*BB, MI, dl, TII->get(X86::MOVAPSrr), MI->getOperand(0).getReg())
Eric Christopherb120ab42009-08-18 22:50:32 +00009523 .addReg(X86::XMM0);
9524
Dan Gohman14152b42010-07-06 20:24:04 +00009525 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +00009526 return BB;
9527}
9528
9529MachineBasicBlock *
Eric Christopher228232b2010-11-30 07:20:12 +00009530X86TargetLowering::EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +00009531 DebugLoc dl = MI->getDebugLoc();
9532 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9533
9534 // Address into RAX/EAX, other two args into ECX, EDX.
9535 unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
9536 unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
9537 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg);
9538 for (int i = 0; i < X86::AddrNumOperands; ++i)
Eric Christopher82be2202010-11-30 08:10:28 +00009539 MIB.addOperand(MI->getOperand(i));
Eric Christopher228232b2010-11-30 07:20:12 +00009540
9541 unsigned ValOps = X86::AddrNumOperands;
9542 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
9543 .addReg(MI->getOperand(ValOps).getReg());
9544 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX)
9545 .addReg(MI->getOperand(ValOps+1).getReg());
9546
9547 // The instruction doesn't actually take any operands though.
9548 BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr));
9549
9550 MI->eraseFromParent(); // The pseudo is gone now.
9551 return BB;
9552}
9553
9554MachineBasicBlock *
9555X86TargetLowering::EmitMwait(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +00009556 DebugLoc dl = MI->getDebugLoc();
9557 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9558
9559 // First arg in ECX, the second in EAX.
9560 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
9561 .addReg(MI->getOperand(0).getReg());
9562 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EAX)
9563 .addReg(MI->getOperand(1).getReg());
9564
9565 // The instruction doesn't actually take any operands though.
9566 BuildMI(*BB, MI, dl, TII->get(X86::MWAITrr));
9567
9568 MI->eraseFromParent(); // The pseudo is gone now.
9569 return BB;
9570}
9571
9572MachineBasicBlock *
Dan Gohman320afb82010-10-12 18:00:49 +00009573X86TargetLowering::EmitVAARG64WithCustomInserter(
9574 MachineInstr *MI,
9575 MachineBasicBlock *MBB) const {
9576 // Emit va_arg instruction on X86-64.
9577
9578 // Operands to this pseudo-instruction:
9579 // 0 ) Output : destination address (reg)
9580 // 1-5) Input : va_list address (addr, i64mem)
9581 // 6 ) ArgSize : Size (in bytes) of vararg type
9582 // 7 ) ArgMode : 0=overflow only, 1=use gp_offset, 2=use fp_offset
9583 // 8 ) Align : Alignment of type
9584 // 9 ) EFLAGS (implicit-def)
9585
9586 assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
9587 assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands");
9588
9589 unsigned DestReg = MI->getOperand(0).getReg();
9590 MachineOperand &Base = MI->getOperand(1);
9591 MachineOperand &Scale = MI->getOperand(2);
9592 MachineOperand &Index = MI->getOperand(3);
9593 MachineOperand &Disp = MI->getOperand(4);
9594 MachineOperand &Segment = MI->getOperand(5);
9595 unsigned ArgSize = MI->getOperand(6).getImm();
9596 unsigned ArgMode = MI->getOperand(7).getImm();
9597 unsigned Align = MI->getOperand(8).getImm();
9598
9599 // Memory Reference
9600 assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand");
9601 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
9602 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
9603
9604 // Machine Information
9605 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9606 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
9607 const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64);
9608 const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32);
9609 DebugLoc DL = MI->getDebugLoc();
9610
9611 // struct va_list {
9612 // i32 gp_offset
9613 // i32 fp_offset
9614 // i64 overflow_area (address)
9615 // i64 reg_save_area (address)
9616 // }
9617 // sizeof(va_list) = 24
9618 // alignment(va_list) = 8
9619
9620 unsigned TotalNumIntRegs = 6;
9621 unsigned TotalNumXMMRegs = 8;
9622 bool UseGPOffset = (ArgMode == 1);
9623 bool UseFPOffset = (ArgMode == 2);
9624 unsigned MaxOffset = TotalNumIntRegs * 8 +
9625 (UseFPOffset ? TotalNumXMMRegs * 16 : 0);
9626
9627 /* Align ArgSize to a multiple of 8 */
9628 unsigned ArgSizeA8 = (ArgSize + 7) & ~7;
9629 bool NeedsAlign = (Align > 8);
9630
9631 MachineBasicBlock *thisMBB = MBB;
9632 MachineBasicBlock *overflowMBB;
9633 MachineBasicBlock *offsetMBB;
9634 MachineBasicBlock *endMBB;
9635
9636 unsigned OffsetDestReg = 0; // Argument address computed by offsetMBB
9637 unsigned OverflowDestReg = 0; // Argument address computed by overflowMBB
9638 unsigned OffsetReg = 0;
9639
9640 if (!UseGPOffset && !UseFPOffset) {
9641 // If we only pull from the overflow region, we don't create a branch.
9642 // We don't need to alter control flow.
9643 OffsetDestReg = 0; // unused
9644 OverflowDestReg = DestReg;
9645
9646 offsetMBB = NULL;
9647 overflowMBB = thisMBB;
9648 endMBB = thisMBB;
9649 } else {
9650 // First emit code to check if gp_offset (or fp_offset) is below the bound.
9651 // If so, pull the argument from reg_save_area. (branch to offsetMBB)
9652 // If not, pull from overflow_area. (branch to overflowMBB)
9653 //
9654 // thisMBB
9655 // | .
9656 // | .
9657 // offsetMBB overflowMBB
9658 // | .
9659 // | .
9660 // endMBB
9661
9662 // Registers for the PHI in endMBB
9663 OffsetDestReg = MRI.createVirtualRegister(AddrRegClass);
9664 OverflowDestReg = MRI.createVirtualRegister(AddrRegClass);
9665
9666 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
9667 MachineFunction *MF = MBB->getParent();
9668 overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB);
9669 offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB);
9670 endMBB = MF->CreateMachineBasicBlock(LLVM_BB);
9671
9672 MachineFunction::iterator MBBIter = MBB;
9673 ++MBBIter;
9674
9675 // Insert the new basic blocks
9676 MF->insert(MBBIter, offsetMBB);
9677 MF->insert(MBBIter, overflowMBB);
9678 MF->insert(MBBIter, endMBB);
9679
9680 // Transfer the remainder of MBB and its successor edges to endMBB.
9681 endMBB->splice(endMBB->begin(), thisMBB,
9682 llvm::next(MachineBasicBlock::iterator(MI)),
9683 thisMBB->end());
9684 endMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
9685
9686 // Make offsetMBB and overflowMBB successors of thisMBB
9687 thisMBB->addSuccessor(offsetMBB);
9688 thisMBB->addSuccessor(overflowMBB);
9689
9690 // endMBB is a successor of both offsetMBB and overflowMBB
9691 offsetMBB->addSuccessor(endMBB);
9692 overflowMBB->addSuccessor(endMBB);
9693
9694 // Load the offset value into a register
9695 OffsetReg = MRI.createVirtualRegister(OffsetRegClass);
9696 BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg)
9697 .addOperand(Base)
9698 .addOperand(Scale)
9699 .addOperand(Index)
9700 .addDisp(Disp, UseFPOffset ? 4 : 0)
9701 .addOperand(Segment)
9702 .setMemRefs(MMOBegin, MMOEnd);
9703
9704 // Check if there is enough room left to pull this argument.
9705 BuildMI(thisMBB, DL, TII->get(X86::CMP32ri))
9706 .addReg(OffsetReg)
9707 .addImm(MaxOffset + 8 - ArgSizeA8);
9708
9709 // Branch to "overflowMBB" if offset >= max
9710 // Fall through to "offsetMBB" otherwise
9711 BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE)))
9712 .addMBB(overflowMBB);
9713 }
9714
9715 // In offsetMBB, emit code to use the reg_save_area.
9716 if (offsetMBB) {
9717 assert(OffsetReg != 0);
9718
9719 // Read the reg_save_area address.
9720 unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass);
9721 BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg)
9722 .addOperand(Base)
9723 .addOperand(Scale)
9724 .addOperand(Index)
9725 .addDisp(Disp, 16)
9726 .addOperand(Segment)
9727 .setMemRefs(MMOBegin, MMOEnd);
9728
9729 // Zero-extend the offset
9730 unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass);
9731 BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64)
9732 .addImm(0)
9733 .addReg(OffsetReg)
9734 .addImm(X86::sub_32bit);
9735
9736 // Add the offset to the reg_save_area to get the final address.
9737 BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg)
9738 .addReg(OffsetReg64)
9739 .addReg(RegSaveReg);
9740
9741 // Compute the offset for the next argument
9742 unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass);
9743 BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg)
9744 .addReg(OffsetReg)
9745 .addImm(UseFPOffset ? 16 : 8);
9746
9747 // Store it back into the va_list.
9748 BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr))
9749 .addOperand(Base)
9750 .addOperand(Scale)
9751 .addOperand(Index)
9752 .addDisp(Disp, UseFPOffset ? 4 : 0)
9753 .addOperand(Segment)
9754 .addReg(NextOffsetReg)
9755 .setMemRefs(MMOBegin, MMOEnd);
9756
9757 // Jump to endMBB
9758 BuildMI(offsetMBB, DL, TII->get(X86::JMP_4))
9759 .addMBB(endMBB);
9760 }
9761
9762 //
9763 // Emit code to use overflow area
9764 //
9765
9766 // Load the overflow_area address into a register.
9767 unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass);
9768 BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg)
9769 .addOperand(Base)
9770 .addOperand(Scale)
9771 .addOperand(Index)
9772 .addDisp(Disp, 8)
9773 .addOperand(Segment)
9774 .setMemRefs(MMOBegin, MMOEnd);
9775
9776 // If we need to align it, do so. Otherwise, just copy the address
9777 // to OverflowDestReg.
9778 if (NeedsAlign) {
9779 // Align the overflow address
9780 assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2");
9781 unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass);
9782
9783 // aligned_addr = (addr + (align-1)) & ~(align-1)
9784 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg)
9785 .addReg(OverflowAddrReg)
9786 .addImm(Align-1);
9787
9788 BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg)
9789 .addReg(TmpReg)
9790 .addImm(~(uint64_t)(Align-1));
9791 } else {
9792 BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg)
9793 .addReg(OverflowAddrReg);
9794 }
9795
9796 // Compute the next overflow address after this argument.
9797 // (the overflow address should be kept 8-byte aligned)
9798 unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass);
9799 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg)
9800 .addReg(OverflowDestReg)
9801 .addImm(ArgSizeA8);
9802
9803 // Store the new overflow address.
9804 BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr))
9805 .addOperand(Base)
9806 .addOperand(Scale)
9807 .addOperand(Index)
9808 .addDisp(Disp, 8)
9809 .addOperand(Segment)
9810 .addReg(NextAddrReg)
9811 .setMemRefs(MMOBegin, MMOEnd);
9812
9813 // If we branched, emit the PHI to the front of endMBB.
9814 if (offsetMBB) {
9815 BuildMI(*endMBB, endMBB->begin(), DL,
9816 TII->get(X86::PHI), DestReg)
9817 .addReg(OffsetDestReg).addMBB(offsetMBB)
9818 .addReg(OverflowDestReg).addMBB(overflowMBB);
9819 }
9820
9821 // Erase the pseudo instruction
9822 MI->eraseFromParent();
9823
9824 return endMBB;
9825}
9826
9827MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +00009828X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
9829 MachineInstr *MI,
9830 MachineBasicBlock *MBB) const {
9831 // Emit code to save XMM registers to the stack. The ABI says that the
9832 // number of registers to save is given in %al, so it's theoretically
9833 // possible to do an indirect jump trick to avoid saving all of them,
9834 // however this code takes a simpler approach and just executes all
9835 // of the stores if %al is non-zero. It's less code, and it's probably
9836 // easier on the hardware branch predictor, and stores aren't all that
9837 // expensive anyway.
9838
9839 // Create the new basic blocks. One block contains all the XMM stores,
9840 // and one block is the final destination regardless of whether any
9841 // stores were performed.
9842 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
9843 MachineFunction *F = MBB->getParent();
9844 MachineFunction::iterator MBBIter = MBB;
9845 ++MBBIter;
9846 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
9847 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
9848 F->insert(MBBIter, XMMSaveMBB);
9849 F->insert(MBBIter, EndMBB);
9850
Dan Gohman14152b42010-07-06 20:24:04 +00009851 // Transfer the remainder of MBB and its successor edges to EndMBB.
9852 EndMBB->splice(EndMBB->begin(), MBB,
9853 llvm::next(MachineBasicBlock::iterator(MI)),
9854 MBB->end());
9855 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
9856
Dan Gohmand6708ea2009-08-15 01:38:56 +00009857 // The original block will now fall through to the XMM save block.
9858 MBB->addSuccessor(XMMSaveMBB);
9859 // The XMMSaveMBB will fall through to the end block.
9860 XMMSaveMBB->addSuccessor(EndMBB);
9861
9862 // Now add the instructions.
9863 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9864 DebugLoc DL = MI->getDebugLoc();
9865
9866 unsigned CountReg = MI->getOperand(0).getReg();
9867 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
9868 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
9869
9870 if (!Subtarget->isTargetWin64()) {
9871 // If %al is 0, branch around the XMM save block.
9872 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009873 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +00009874 MBB->addSuccessor(EndMBB);
9875 }
9876
9877 // In the XMM save block, save all the XMM argument registers.
9878 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
9879 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +00009880 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +00009881 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +00009882 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +00009883 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +00009884 /*Size=*/16, /*Align=*/16);
Dan Gohmand6708ea2009-08-15 01:38:56 +00009885 BuildMI(XMMSaveMBB, DL, TII->get(X86::MOVAPSmr))
9886 .addFrameIndex(RegSaveFrameIndex)
9887 .addImm(/*Scale=*/1)
9888 .addReg(/*IndexReg=*/0)
9889 .addImm(/*Disp=*/Offset)
9890 .addReg(/*Segment=*/0)
9891 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +00009892 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +00009893 }
9894
Dan Gohman14152b42010-07-06 20:24:04 +00009895 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +00009896
9897 return EndMBB;
9898}
Mon P Wang63307c32008-05-05 19:05:59 +00009899
Evan Cheng60c07e12006-07-05 22:17:51 +00009900MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +00009901X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00009902 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +00009903 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9904 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +00009905
Chris Lattner52600972009-09-02 05:57:00 +00009906 // To "insert" a SELECT_CC instruction, we actually have to insert the
9907 // diamond control-flow pattern. The incoming instruction knows the
9908 // destination vreg to set, the condition code register to branch on, the
9909 // true/false values to select between, and a branch opcode to use.
9910 const BasicBlock *LLVM_BB = BB->getBasicBlock();
9911 MachineFunction::iterator It = BB;
9912 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +00009913
Chris Lattner52600972009-09-02 05:57:00 +00009914 // thisMBB:
9915 // ...
9916 // TrueVal = ...
9917 // cmpTY ccX, r1, r2
9918 // bCC copy1MBB
9919 // fallthrough --> copy0MBB
9920 MachineBasicBlock *thisMBB = BB;
9921 MachineFunction *F = BB->getParent();
9922 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
9923 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +00009924 F->insert(It, copy0MBB);
9925 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +00009926
Bill Wendling730c07e2010-06-25 20:48:10 +00009927 // If the EFLAGS register isn't dead in the terminator, then claim that it's
9928 // live into the sink and copy blocks.
9929 const MachineFunction *MF = BB->getParent();
9930 const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo();
9931 BitVector ReservedRegs = TRI->getReservedRegs(*MF);
Bill Wendling730c07e2010-06-25 20:48:10 +00009932
Dan Gohman14152b42010-07-06 20:24:04 +00009933 for (unsigned I = 0, E = MI->getNumOperands(); I != E; ++I) {
9934 const MachineOperand &MO = MI->getOperand(I);
9935 if (!MO.isReg() || !MO.isUse() || MO.isKill()) continue;
Bill Wendling730c07e2010-06-25 20:48:10 +00009936 unsigned Reg = MO.getReg();
9937 if (Reg != X86::EFLAGS) continue;
9938 copy0MBB->addLiveIn(Reg);
9939 sinkMBB->addLiveIn(Reg);
9940 }
9941
Dan Gohman14152b42010-07-06 20:24:04 +00009942 // Transfer the remainder of BB and its successor edges to sinkMBB.
9943 sinkMBB->splice(sinkMBB->begin(), BB,
9944 llvm::next(MachineBasicBlock::iterator(MI)),
9945 BB->end());
9946 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
9947
9948 // Add the true and fallthrough blocks as its successors.
9949 BB->addSuccessor(copy0MBB);
9950 BB->addSuccessor(sinkMBB);
9951
9952 // Create the conditional branch instruction.
9953 unsigned Opc =
9954 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
9955 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
9956
Chris Lattner52600972009-09-02 05:57:00 +00009957 // copy0MBB:
9958 // %FalseValue = ...
9959 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +00009960 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +00009961
Chris Lattner52600972009-09-02 05:57:00 +00009962 // sinkMBB:
9963 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
9964 // ...
Dan Gohman14152b42010-07-06 20:24:04 +00009965 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
9966 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +00009967 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
9968 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
9969
Dan Gohman14152b42010-07-06 20:24:04 +00009970 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +00009971 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +00009972}
9973
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00009974MachineBasicBlock *
Michael J. Spencere9c253e2010-10-21 01:41:01 +00009975X86TargetLowering::EmitLoweredWinAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00009976 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00009977 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9978 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00009979
9980 // The lowering is pretty easy: we're just emitting the call to _alloca. The
9981 // non-trivial part is impdef of ESP.
9982 // FIXME: The code should be tweaked as soon as we'll try to do codegen for
9983 // mingw-w64.
9984
Michael J. Spencere9c253e2010-10-21 01:41:01 +00009985 const char *StackProbeSymbol =
9986 Subtarget->isTargetWindows() ? "_chkstk" : "_alloca";
9987
Dan Gohman14152b42010-07-06 20:24:04 +00009988 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
Michael J. Spencere9c253e2010-10-21 01:41:01 +00009989 .addExternalSymbol(StackProbeSymbol)
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00009990 .addReg(X86::EAX, RegState::Implicit)
9991 .addReg(X86::ESP, RegState::Implicit)
9992 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
Anton Korobeynikov9f7f83b2010-08-25 07:50:11 +00009993 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
9994 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00009995
Dan Gohman14152b42010-07-06 20:24:04 +00009996 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00009997 return BB;
9998}
Chris Lattner52600972009-09-02 05:57:00 +00009999
10000MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +000010001X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
10002 MachineBasicBlock *BB) const {
10003 // This is pretty easy. We're taking the value that we received from
10004 // our load from the relocation, sticking it in either RDI (x86-64)
10005 // or EAX and doing an indirect call. The return value will then
10006 // be in the normal return register.
Michael J. Spencerec38de22010-10-10 22:04:20 +000010007 const X86InstrInfo *TII
Eric Christopher54415362010-06-08 22:04:25 +000010008 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +000010009 DebugLoc DL = MI->getDebugLoc();
10010 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +000010011
10012 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +000010013 assert(MI->getOperand(3).isGlobal() && "This should be a global");
Michael J. Spencerec38de22010-10-10 22:04:20 +000010014
Eric Christopher30ef0e52010-06-03 04:07:48 +000010015 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +000010016 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
10017 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +000010018 .addReg(X86::RIP)
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);
Eric Christopher722d3152010-09-27 06:01:51 +000010023 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +000010024 addDirectMem(MIB, X86::RDI);
Eric Christopher61025492010-06-15 23:08:42 +000010025 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +000010026 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
10027 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +000010028 .addReg(0)
10029 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000010030 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher61025492010-06-15 23:08:42 +000010031 MI->getOperand(3).getTargetFlags())
10032 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000010033 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000010034 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010035 } else {
Dan Gohman14152b42010-07-06 20:24:04 +000010036 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
10037 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +000010038 .addReg(TII->getGlobalBaseReg(F))
10039 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000010040 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000010041 MI->getOperand(3).getTargetFlags())
10042 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000010043 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000010044 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010045 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000010046
Dan Gohman14152b42010-07-06 20:24:04 +000010047 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +000010048 return BB;
10049}
10050
10051MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +000010052X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010053 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +000010054 switch (MI->getOpcode()) {
10055 default: assert(false && "Unexpected instr type to insert");
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010056 case X86::WIN_ALLOCA:
10057 return EmitLoweredWinAlloca(MI, BB);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010058 case X86::TLSCall_32:
10059 case X86::TLSCall_64:
10060 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +000010061 case X86::CMOV_GR8:
Evan Cheng60c07e12006-07-05 22:17:51 +000010062 case X86::CMOV_FR32:
10063 case X86::CMOV_FR64:
10064 case X86::CMOV_V4F32:
10065 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +000010066 case X86::CMOV_V2I64:
Chris Lattner314a1132010-03-14 18:31:44 +000010067 case X86::CMOV_GR16:
10068 case X86::CMOV_GR32:
10069 case X86::CMOV_RFP32:
10070 case X86::CMOV_RFP64:
10071 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010072 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000010073
Dale Johannesen849f2142007-07-03 00:53:03 +000010074 case X86::FP32_TO_INT16_IN_MEM:
10075 case X86::FP32_TO_INT32_IN_MEM:
10076 case X86::FP32_TO_INT64_IN_MEM:
10077 case X86::FP64_TO_INT16_IN_MEM:
10078 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +000010079 case X86::FP64_TO_INT64_IN_MEM:
10080 case X86::FP80_TO_INT16_IN_MEM:
10081 case X86::FP80_TO_INT32_IN_MEM:
10082 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +000010083 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10084 DebugLoc DL = MI->getDebugLoc();
10085
Evan Cheng60c07e12006-07-05 22:17:51 +000010086 // Change the floating point control register to use "round towards zero"
10087 // mode when truncating to an integer value.
10088 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +000010089 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +000010090 addFrameReference(BuildMI(*BB, MI, DL,
10091 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010092
10093 // Load the old value of the high byte of the control word...
10094 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +000010095 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohman14152b42010-07-06 20:24:04 +000010096 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +000010097 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010098
10099 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +000010100 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000010101 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +000010102
10103 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +000010104 addFrameReference(BuildMI(*BB, MI, DL,
10105 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010106
10107 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +000010108 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000010109 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +000010110
10111 // Get the X86 opcode to use.
10112 unsigned Opc;
10113 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000010114 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +000010115 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
10116 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
10117 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
10118 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
10119 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
10120 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +000010121 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
10122 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
10123 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +000010124 }
10125
10126 X86AddressMode AM;
10127 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +000010128 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000010129 AM.BaseType = X86AddressMode::RegBase;
10130 AM.Base.Reg = Op.getReg();
10131 } else {
10132 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +000010133 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +000010134 }
10135 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +000010136 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000010137 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010138 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +000010139 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000010140 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010141 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +000010142 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000010143 AM.GV = Op.getGlobal();
10144 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +000010145 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010146 }
Dan Gohman14152b42010-07-06 20:24:04 +000010147 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010148 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +000010149
10150 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +000010151 addFrameReference(BuildMI(*BB, MI, DL,
10152 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010153
Dan Gohman14152b42010-07-06 20:24:04 +000010154 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +000010155 return BB;
10156 }
Eric Christopherb120ab42009-08-18 22:50:32 +000010157 // String/text processing lowering.
10158 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010159 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000010160 return EmitPCMP(MI, BB, 3, false /* in-mem */);
10161 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010162 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000010163 return EmitPCMP(MI, BB, 3, true /* in-mem */);
10164 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010165 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000010166 return EmitPCMP(MI, BB, 5, false /* in mem */);
10167 case X86::PCMPESTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010168 case X86::VPCMPESTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000010169 return EmitPCMP(MI, BB, 5, true /* in mem */);
10170
Eric Christopher228232b2010-11-30 07:20:12 +000010171 // Thread synchronization.
10172 case X86::MONITOR:
10173 return EmitMonitor(MI, BB);
10174 case X86::MWAIT:
10175 return EmitMwait(MI, BB);
10176
Eric Christopherb120ab42009-08-18 22:50:32 +000010177 // Atomic Lowering.
Mon P Wang63307c32008-05-05 19:05:59 +000010178 case X86::ATOMAND32:
10179 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010180 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010181 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010182 X86::NOT32r, X86::EAX,
10183 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000010184 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +000010185 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
10186 X86::OR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010187 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010188 X86::NOT32r, X86::EAX,
10189 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000010190 case X86::ATOMXOR32:
10191 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010192 X86::XOR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010193 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010194 X86::NOT32r, X86::EAX,
10195 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010196 case X86::ATOMNAND32:
10197 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010198 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010199 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010200 X86::NOT32r, X86::EAX,
10201 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +000010202 case X86::ATOMMIN32:
10203 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
10204 case X86::ATOMMAX32:
10205 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
10206 case X86::ATOMUMIN32:
10207 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
10208 case X86::ATOMUMAX32:
10209 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +000010210
10211 case X86::ATOMAND16:
10212 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
10213 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010214 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010215 X86::NOT16r, X86::AX,
10216 X86::GR16RegisterClass);
10217 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +000010218 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010219 X86::OR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010220 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010221 X86::NOT16r, X86::AX,
10222 X86::GR16RegisterClass);
10223 case X86::ATOMXOR16:
10224 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
10225 X86::XOR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010226 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010227 X86::NOT16r, X86::AX,
10228 X86::GR16RegisterClass);
10229 case X86::ATOMNAND16:
10230 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
10231 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010232 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010233 X86::NOT16r, X86::AX,
10234 X86::GR16RegisterClass, true);
10235 case X86::ATOMMIN16:
10236 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
10237 case X86::ATOMMAX16:
10238 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
10239 case X86::ATOMUMIN16:
10240 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
10241 case X86::ATOMUMAX16:
10242 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
10243
10244 case X86::ATOMAND8:
10245 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
10246 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010247 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010248 X86::NOT8r, X86::AL,
10249 X86::GR8RegisterClass);
10250 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +000010251 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010252 X86::OR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010253 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010254 X86::NOT8r, X86::AL,
10255 X86::GR8RegisterClass);
10256 case X86::ATOMXOR8:
10257 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
10258 X86::XOR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010259 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010260 X86::NOT8r, X86::AL,
10261 X86::GR8RegisterClass);
10262 case X86::ATOMNAND8:
10263 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
10264 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010265 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010266 X86::NOT8r, X86::AL,
10267 X86::GR8RegisterClass, true);
10268 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010269 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +000010270 case X86::ATOMAND64:
10271 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010272 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010273 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010274 X86::NOT64r, X86::RAX,
10275 X86::GR64RegisterClass);
10276 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +000010277 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
10278 X86::OR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010279 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010280 X86::NOT64r, X86::RAX,
10281 X86::GR64RegisterClass);
10282 case X86::ATOMXOR64:
10283 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010284 X86::XOR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010285 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010286 X86::NOT64r, X86::RAX,
10287 X86::GR64RegisterClass);
10288 case X86::ATOMNAND64:
10289 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
10290 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010291 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010292 X86::NOT64r, X86::RAX,
10293 X86::GR64RegisterClass, true);
10294 case X86::ATOMMIN64:
10295 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
10296 case X86::ATOMMAX64:
10297 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
10298 case X86::ATOMUMIN64:
10299 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
10300 case X86::ATOMUMAX64:
10301 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010302
10303 // This group does 64-bit operations on a 32-bit host.
10304 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010305 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010306 X86::AND32rr, X86::AND32rr,
10307 X86::AND32ri, X86::AND32ri,
10308 false);
10309 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010310 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010311 X86::OR32rr, X86::OR32rr,
10312 X86::OR32ri, X86::OR32ri,
10313 false);
10314 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010315 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010316 X86::XOR32rr, X86::XOR32rr,
10317 X86::XOR32ri, X86::XOR32ri,
10318 false);
10319 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010320 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010321 X86::AND32rr, X86::AND32rr,
10322 X86::AND32ri, X86::AND32ri,
10323 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010324 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010325 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010326 X86::ADD32rr, X86::ADC32rr,
10327 X86::ADD32ri, X86::ADC32ri,
10328 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010329 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010330 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010331 X86::SUB32rr, X86::SBB32rr,
10332 X86::SUB32ri, X86::SBB32ri,
10333 false);
Dale Johannesen880ae362008-10-03 22:25:52 +000010334 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010335 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +000010336 X86::MOV32rr, X86::MOV32rr,
10337 X86::MOV32ri, X86::MOV32ri,
10338 false);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010339 case X86::VASTART_SAVE_XMM_REGS:
10340 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Dan Gohman320afb82010-10-12 18:00:49 +000010341
10342 case X86::VAARG_64:
10343 return EmitVAARG64WithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000010344 }
10345}
10346
10347//===----------------------------------------------------------------------===//
10348// X86 Optimization Hooks
10349//===----------------------------------------------------------------------===//
10350
Dan Gohman475871a2008-07-27 21:46:04 +000010351void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +000010352 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000010353 APInt &KnownZero,
10354 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +000010355 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +000010356 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010357 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +000010358 assert((Opc >= ISD::BUILTIN_OP_END ||
10359 Opc == ISD::INTRINSIC_WO_CHAIN ||
10360 Opc == ISD::INTRINSIC_W_CHAIN ||
10361 Opc == ISD::INTRINSIC_VOID) &&
10362 "Should use MaskedValueIsZero if you don't know whether Op"
10363 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010364
Dan Gohmanf4f92f52008-02-13 23:07:24 +000010365 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010366 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +000010367 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +000010368 case X86ISD::ADD:
10369 case X86ISD::SUB:
Chris Lattner5b856542010-12-20 00:59:46 +000010370 case X86ISD::ADC:
10371 case X86ISD::SBB:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000010372 case X86ISD::SMUL:
10373 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +000010374 case X86ISD::INC:
10375 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +000010376 case X86ISD::OR:
10377 case X86ISD::XOR:
10378 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000010379 // These nodes' second result is a boolean.
10380 if (Op.getResNo() == 0)
10381 break;
10382 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010383 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000010384 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
10385 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +000010386 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010387 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010388}
Chris Lattner259e97c2006-01-31 19:43:35 +000010389
Owen Andersonbc146b02010-09-21 20:42:50 +000010390unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
10391 unsigned Depth) const {
10392 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
10393 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
10394 return Op.getValueType().getScalarType().getSizeInBits();
Michael J. Spencerec38de22010-10-10 22:04:20 +000010395
Owen Andersonbc146b02010-09-21 20:42:50 +000010396 // Fallback case.
10397 return 1;
10398}
10399
Evan Cheng206ee9d2006-07-07 08:33:52 +000010400/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +000010401/// node is a GlobalAddress + offset.
10402bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +000010403 const GlobalValue* &GA,
10404 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +000010405 if (N->getOpcode() == X86ISD::Wrapper) {
10406 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +000010407 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +000010408 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +000010409 return true;
10410 }
Evan Cheng206ee9d2006-07-07 08:33:52 +000010411 }
Evan Chengad4196b2008-05-12 19:56:52 +000010412 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +000010413}
10414
Evan Cheng206ee9d2006-07-07 08:33:52 +000010415/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
10416/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
10417/// if the load addresses are consecutive, non-overlapping, and in the right
Nate Begemanfdea31a2010-03-24 20:49:50 +000010418/// order.
Dan Gohman475871a2008-07-27 21:46:04 +000010419static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Mon P Wanga0fd0d52010-12-19 23:55:53 +000010420 TargetLowering::DAGCombinerInfo &DCI) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000010421 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000010422 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +000010423
Eli Friedman7a5e5552009-06-07 06:52:44 +000010424 if (VT.getSizeInBits() != 128)
10425 return SDValue();
10426
Mon P Wanga0fd0d52010-12-19 23:55:53 +000010427 // Don't create instructions with illegal types after legalize types has run.
10428 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10429 if (!DCI.isBeforeLegalize() && !TLI.isTypeLegal(VT.getVectorElementType()))
10430 return SDValue();
10431
Nate Begemanfdea31a2010-03-24 20:49:50 +000010432 SmallVector<SDValue, 16> Elts;
10433 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000010434 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +000010435
Nate Begemanfdea31a2010-03-24 20:49:50 +000010436 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000010437}
Evan Chengd880b972008-05-09 21:53:03 +000010438
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +000010439/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
10440/// generation and convert it from being a bunch of shuffles and extracts
10441/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010442static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
10443 const TargetLowering &TLI) {
10444 SDValue InputVector = N->getOperand(0);
10445
10446 // Only operate on vectors of 4 elements, where the alternative shuffling
10447 // gets to be more expensive.
10448 if (InputVector.getValueType() != MVT::v4i32)
10449 return SDValue();
10450
10451 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
10452 // single use which is a sign-extend or zero-extend, and all elements are
10453 // used.
10454 SmallVector<SDNode *, 4> Uses;
10455 unsigned ExtractedElements = 0;
10456 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
10457 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
10458 if (UI.getUse().getResNo() != InputVector.getResNo())
10459 return SDValue();
10460
10461 SDNode *Extract = *UI;
10462 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
10463 return SDValue();
10464
10465 if (Extract->getValueType(0) != MVT::i32)
10466 return SDValue();
10467 if (!Extract->hasOneUse())
10468 return SDValue();
10469 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
10470 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
10471 return SDValue();
10472 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
10473 return SDValue();
10474
10475 // Record which element was extracted.
10476 ExtractedElements |=
10477 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
10478
10479 Uses.push_back(Extract);
10480 }
10481
10482 // If not all the elements were used, this may not be worthwhile.
10483 if (ExtractedElements != 15)
10484 return SDValue();
10485
10486 // Ok, we've now decided to do the transformation.
10487 DebugLoc dl = InputVector.getDebugLoc();
10488
10489 // Store the value to a temporary stack slot.
10490 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000010491 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
10492 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010493
10494 // Replace each use (extract) with a load of the appropriate element.
10495 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
10496 UE = Uses.end(); UI != UE; ++UI) {
10497 SDNode *Extract = *UI;
10498
10499 // Compute the element's address.
10500 SDValue Idx = Extract->getOperand(1);
10501 unsigned EltSize =
10502 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
10503 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
10504 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
10505
Eric Christopher90eb4022010-07-22 00:26:08 +000010506 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, Idx.getValueType(),
Chris Lattner51abfe42010-09-21 06:02:19 +000010507 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010508
10509 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000010510 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000010511 ScalarAddr, MachinePointerInfo(),
10512 false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010513
10514 // Replace the exact with the load.
10515 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
10516 }
10517
10518 // The replacement was made in place; don't return anything.
10519 return SDValue();
10520}
10521
Chris Lattner83e6c992006-10-04 06:57:07 +000010522/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000010523static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner47b4ce82009-03-11 05:48:52 +000010524 const X86Subtarget *Subtarget) {
10525 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +000010526 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000010527 // Get the LHS/RHS of the select.
10528 SDValue LHS = N->getOperand(1);
10529 SDValue RHS = N->getOperand(2);
Eric Christopherfd179292009-08-27 18:07:15 +000010530
Dan Gohman670e5392009-09-21 18:03:22 +000010531 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000010532 // instructions match the semantics of the common C idiom x<y?x:y but not
10533 // x<=y?x:y, because of how they handle negative zero (which can be
10534 // ignored in unsafe-math mode).
Chris Lattner83e6c992006-10-04 06:57:07 +000010535 if (Subtarget->hasSSE2() &&
Owen Anderson825b72b2009-08-11 20:47:22 +000010536 (LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64) &&
Chris Lattner47b4ce82009-03-11 05:48:52 +000010537 Cond.getOpcode() == ISD::SETCC) {
10538 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010539
Chris Lattner47b4ce82009-03-11 05:48:52 +000010540 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000010541 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000010542 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
10543 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000010544 switch (CC) {
10545 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000010546 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000010547 // Converting this to a min would handle NaNs incorrectly, and swapping
10548 // the operands would cause it to handle comparisons between positive
10549 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000010550 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000010551 if (!UnsafeFPMath &&
10552 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
10553 break;
10554 std::swap(LHS, RHS);
10555 }
Dan Gohman670e5392009-09-21 18:03:22 +000010556 Opcode = X86ISD::FMIN;
10557 break;
10558 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000010559 // Converting this to a min would handle comparisons between positive
10560 // and negative zero incorrectly.
10561 if (!UnsafeFPMath &&
10562 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
10563 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010564 Opcode = X86ISD::FMIN;
10565 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000010566 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000010567 // Converting this to a min would handle both negative zeros and NaNs
10568 // incorrectly, but we can swap the operands to fix both.
10569 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000010570 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010571 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000010572 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010573 Opcode = X86ISD::FMIN;
10574 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010575
Dan Gohman670e5392009-09-21 18:03:22 +000010576 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000010577 // Converting this to a max would handle comparisons between positive
10578 // and negative zero incorrectly.
10579 if (!UnsafeFPMath &&
10580 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(LHS))
10581 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010582 Opcode = X86ISD::FMAX;
10583 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000010584 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000010585 // Converting this to a max would handle NaNs incorrectly, and swapping
10586 // the operands would cause it to handle comparisons between positive
10587 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000010588 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000010589 if (!UnsafeFPMath &&
10590 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
10591 break;
10592 std::swap(LHS, RHS);
10593 }
Dan Gohman670e5392009-09-21 18:03:22 +000010594 Opcode = X86ISD::FMAX;
10595 break;
10596 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000010597 // Converting this to a max would handle both negative zeros and NaNs
10598 // incorrectly, but we can swap the operands to fix both.
10599 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000010600 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010601 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010602 case ISD::SETGE:
10603 Opcode = X86ISD::FMAX;
10604 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000010605 }
Dan Gohman670e5392009-09-21 18:03:22 +000010606 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000010607 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
10608 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000010609 switch (CC) {
10610 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000010611 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000010612 // Converting this to a min would handle comparisons between positive
10613 // and negative zero incorrectly, and swapping the operands would
10614 // cause it to handle NaNs incorrectly.
10615 if (!UnsafeFPMath &&
10616 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000010617 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000010618 break;
10619 std::swap(LHS, RHS);
10620 }
Dan Gohman670e5392009-09-21 18:03:22 +000010621 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000010622 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010623 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000010624 // Converting this to a min would handle NaNs incorrectly.
10625 if (!UnsafeFPMath &&
10626 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
10627 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010628 Opcode = X86ISD::FMIN;
10629 break;
10630 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000010631 // Converting this to a min would handle both negative zeros and NaNs
10632 // incorrectly, but we can swap the operands to fix both.
10633 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000010634 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010635 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010636 case ISD::SETGE:
10637 Opcode = X86ISD::FMIN;
10638 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010639
Dan Gohman670e5392009-09-21 18:03:22 +000010640 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000010641 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000010642 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000010643 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010644 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000010645 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010646 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000010647 // Converting this to a max would handle comparisons between positive
10648 // and negative zero incorrectly, and swapping the operands would
10649 // cause it to handle NaNs incorrectly.
10650 if (!UnsafeFPMath &&
10651 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000010652 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000010653 break;
10654 std::swap(LHS, RHS);
10655 }
Dan Gohman670e5392009-09-21 18:03:22 +000010656 Opcode = X86ISD::FMAX;
10657 break;
10658 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000010659 // Converting this to a max would handle both negative zeros and NaNs
10660 // incorrectly, but we can swap the operands to fix both.
10661 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000010662 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010663 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000010664 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010665 Opcode = X86ISD::FMAX;
10666 break;
10667 }
Chris Lattner83e6c992006-10-04 06:57:07 +000010668 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010669
Chris Lattner47b4ce82009-03-11 05:48:52 +000010670 if (Opcode)
10671 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000010672 }
Eric Christopherfd179292009-08-27 18:07:15 +000010673
Chris Lattnerd1980a52009-03-12 06:52:53 +000010674 // If this is a select between two integer constants, try to do some
10675 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000010676 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
10677 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000010678 // Don't do this for crazy integer types.
10679 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
10680 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000010681 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000010682 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000010683
Chris Lattnercee56e72009-03-13 05:53:31 +000010684 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000010685 // Efficiently invertible.
10686 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
10687 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
10688 isa<ConstantSDNode>(Cond.getOperand(1))))) {
10689 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000010690 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000010691 }
Eric Christopherfd179292009-08-27 18:07:15 +000010692
Chris Lattnerd1980a52009-03-12 06:52:53 +000010693 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000010694 if (FalseC->getAPIntValue() == 0 &&
10695 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000010696 if (NeedsCondInvert) // Invert the condition if needed.
10697 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
10698 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000010699
Chris Lattnerd1980a52009-03-12 06:52:53 +000010700 // Zero extend the condition if needed.
10701 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000010702
Chris Lattnercee56e72009-03-13 05:53:31 +000010703 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000010704 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000010705 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000010706 }
Eric Christopherfd179292009-08-27 18:07:15 +000010707
Chris Lattner97a29a52009-03-13 05:22:11 +000010708 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000010709 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +000010710 if (NeedsCondInvert) // Invert the condition if needed.
10711 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
10712 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000010713
Chris Lattner97a29a52009-03-13 05:22:11 +000010714 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000010715 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
10716 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000010717 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000010718 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000010719 }
Eric Christopherfd179292009-08-27 18:07:15 +000010720
Chris Lattnercee56e72009-03-13 05:53:31 +000010721 // Optimize cases that will turn into an LEA instruction. This requires
10722 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000010723 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000010724 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000010725 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000010726
Chris Lattnercee56e72009-03-13 05:53:31 +000010727 bool isFastMultiplier = false;
10728 if (Diff < 10) {
10729 switch ((unsigned char)Diff) {
10730 default: break;
10731 case 1: // result = add base, cond
10732 case 2: // result = lea base( , cond*2)
10733 case 3: // result = lea base(cond, cond*2)
10734 case 4: // result = lea base( , cond*4)
10735 case 5: // result = lea base(cond, cond*4)
10736 case 8: // result = lea base( , cond*8)
10737 case 9: // result = lea base(cond, cond*8)
10738 isFastMultiplier = true;
10739 break;
10740 }
10741 }
Eric Christopherfd179292009-08-27 18:07:15 +000010742
Chris Lattnercee56e72009-03-13 05:53:31 +000010743 if (isFastMultiplier) {
10744 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
10745 if (NeedsCondInvert) // Invert the condition if needed.
10746 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
10747 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000010748
Chris Lattnercee56e72009-03-13 05:53:31 +000010749 // Zero extend the condition if needed.
10750 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
10751 Cond);
10752 // Scale the condition by the difference.
10753 if (Diff != 1)
10754 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
10755 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000010756
Chris Lattnercee56e72009-03-13 05:53:31 +000010757 // Add the base if non-zero.
10758 if (FalseC->getAPIntValue() != 0)
10759 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
10760 SDValue(FalseC, 0));
10761 return Cond;
10762 }
Eric Christopherfd179292009-08-27 18:07:15 +000010763 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000010764 }
10765 }
Eric Christopherfd179292009-08-27 18:07:15 +000010766
Dan Gohman475871a2008-07-27 21:46:04 +000010767 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000010768}
10769
Chris Lattnerd1980a52009-03-12 06:52:53 +000010770/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
10771static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
10772 TargetLowering::DAGCombinerInfo &DCI) {
10773 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +000010774
Chris Lattnerd1980a52009-03-12 06:52:53 +000010775 // If the flag operand isn't dead, don't touch this CMOV.
10776 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
10777 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +000010778
Chris Lattnerd1980a52009-03-12 06:52:53 +000010779 // If this is a select between two integer constants, try to do some
10780 // optimizations. Note that the operands are ordered the opposite of SELECT
10781 // operands.
10782 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
10783 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
10784 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
10785 // larger than FalseC (the false value).
10786 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
Eric Christopherfd179292009-08-27 18:07:15 +000010787
Chris Lattnerd1980a52009-03-12 06:52:53 +000010788 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
10789 CC = X86::GetOppositeBranchCondition(CC);
10790 std::swap(TrueC, FalseC);
10791 }
Eric Christopherfd179292009-08-27 18:07:15 +000010792
Chris Lattnerd1980a52009-03-12 06:52:53 +000010793 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000010794 // This is efficient for any integer data type (including i8/i16) and
10795 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000010796 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
10797 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +000010798 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
10799 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000010800
Chris Lattnerd1980a52009-03-12 06:52:53 +000010801 // Zero extend the condition if needed.
10802 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000010803
Chris Lattnerd1980a52009-03-12 06:52:53 +000010804 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
10805 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000010806 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000010807 if (N->getNumValues() == 2) // Dead flag value?
10808 return DCI.CombineTo(N, Cond, SDValue());
10809 return Cond;
10810 }
Eric Christopherfd179292009-08-27 18:07:15 +000010811
Chris Lattnercee56e72009-03-13 05:53:31 +000010812 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
10813 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000010814 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
10815 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +000010816 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
10817 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000010818
Chris Lattner97a29a52009-03-13 05:22:11 +000010819 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000010820 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
10821 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000010822 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
10823 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000010824
Chris Lattner97a29a52009-03-13 05:22:11 +000010825 if (N->getNumValues() == 2) // Dead flag value?
10826 return DCI.CombineTo(N, Cond, SDValue());
10827 return Cond;
10828 }
Eric Christopherfd179292009-08-27 18:07:15 +000010829
Chris Lattnercee56e72009-03-13 05:53:31 +000010830 // Optimize cases that will turn into an LEA instruction. This requires
10831 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000010832 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000010833 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000010834 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000010835
Chris Lattnercee56e72009-03-13 05:53:31 +000010836 bool isFastMultiplier = false;
10837 if (Diff < 10) {
10838 switch ((unsigned char)Diff) {
10839 default: break;
10840 case 1: // result = add base, cond
10841 case 2: // result = lea base( , cond*2)
10842 case 3: // result = lea base(cond, cond*2)
10843 case 4: // result = lea base( , cond*4)
10844 case 5: // result = lea base(cond, cond*4)
10845 case 8: // result = lea base( , cond*8)
10846 case 9: // result = lea base(cond, cond*8)
10847 isFastMultiplier = true;
10848 break;
10849 }
10850 }
Eric Christopherfd179292009-08-27 18:07:15 +000010851
Chris Lattnercee56e72009-03-13 05:53:31 +000010852 if (isFastMultiplier) {
10853 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
10854 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +000010855 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
10856 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000010857 // Zero extend the condition if needed.
10858 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
10859 Cond);
10860 // Scale the condition by the difference.
10861 if (Diff != 1)
10862 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
10863 DAG.getConstant(Diff, Cond.getValueType()));
10864
10865 // Add the base if non-zero.
10866 if (FalseC->getAPIntValue() != 0)
10867 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
10868 SDValue(FalseC, 0));
10869 if (N->getNumValues() == 2) // Dead flag value?
10870 return DCI.CombineTo(N, Cond, SDValue());
10871 return Cond;
10872 }
Eric Christopherfd179292009-08-27 18:07:15 +000010873 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000010874 }
10875 }
10876 return SDValue();
10877}
10878
10879
Evan Cheng0b0cd912009-03-28 05:57:29 +000010880/// PerformMulCombine - Optimize a single multiply with constant into two
10881/// in order to implement it with two cheaper instructions, e.g.
10882/// LEA + SHL, LEA + LEA.
10883static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
10884 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000010885 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
10886 return SDValue();
10887
Owen Andersone50ed302009-08-10 22:56:29 +000010888 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010889 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000010890 return SDValue();
10891
10892 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
10893 if (!C)
10894 return SDValue();
10895 uint64_t MulAmt = C->getZExtValue();
10896 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
10897 return SDValue();
10898
10899 uint64_t MulAmt1 = 0;
10900 uint64_t MulAmt2 = 0;
10901 if ((MulAmt % 9) == 0) {
10902 MulAmt1 = 9;
10903 MulAmt2 = MulAmt / 9;
10904 } else if ((MulAmt % 5) == 0) {
10905 MulAmt1 = 5;
10906 MulAmt2 = MulAmt / 5;
10907 } else if ((MulAmt % 3) == 0) {
10908 MulAmt1 = 3;
10909 MulAmt2 = MulAmt / 3;
10910 }
10911 if (MulAmt2 &&
10912 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
10913 DebugLoc DL = N->getDebugLoc();
10914
10915 if (isPowerOf2_64(MulAmt2) &&
10916 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
10917 // If second multiplifer is pow2, issue it first. We want the multiply by
10918 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
10919 // is an add.
10920 std::swap(MulAmt1, MulAmt2);
10921
10922 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000010923 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000010924 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000010925 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000010926 else
Evan Cheng73f24c92009-03-30 21:36:47 +000010927 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000010928 DAG.getConstant(MulAmt1, VT));
10929
Eric Christopherfd179292009-08-27 18:07:15 +000010930 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000010931 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000010932 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000010933 else
Evan Cheng73f24c92009-03-30 21:36:47 +000010934 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000010935 DAG.getConstant(MulAmt2, VT));
10936
10937 // Do not add new nodes to DAG combiner worklist.
10938 DCI.CombineTo(N, NewMul, false);
10939 }
10940 return SDValue();
10941}
10942
Evan Chengad9c0a32009-12-15 00:53:42 +000010943static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
10944 SDValue N0 = N->getOperand(0);
10945 SDValue N1 = N->getOperand(1);
10946 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
10947 EVT VT = N0.getValueType();
10948
10949 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
10950 // since the result of setcc_c is all zero's or all ones.
10951 if (N1C && N0.getOpcode() == ISD::AND &&
10952 N0.getOperand(1).getOpcode() == ISD::Constant) {
10953 SDValue N00 = N0.getOperand(0);
10954 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
10955 ((N00.getOpcode() == ISD::ANY_EXTEND ||
10956 N00.getOpcode() == ISD::ZERO_EXTEND) &&
10957 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
10958 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
10959 APInt ShAmt = N1C->getAPIntValue();
10960 Mask = Mask.shl(ShAmt);
10961 if (Mask != 0)
10962 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
10963 N00, DAG.getConstant(Mask, VT));
10964 }
10965 }
10966
10967 return SDValue();
10968}
Evan Cheng0b0cd912009-03-28 05:57:29 +000010969
Nate Begeman740ab032009-01-26 00:52:55 +000010970/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
10971/// when possible.
10972static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
10973 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +000010974 EVT VT = N->getValueType(0);
10975 if (!VT.isVector() && VT.isInteger() &&
10976 N->getOpcode() == ISD::SHL)
10977 return PerformSHLCombine(N, DAG);
10978
Nate Begeman740ab032009-01-26 00:52:55 +000010979 // On X86 with SSE2 support, we can transform this to a vector shift if
10980 // all elements are shifted by the same amount. We can't do this in legalize
10981 // because the a constant vector is typically transformed to a constant pool
10982 // so we have no knowledge of the shift amount.
Nate Begemanc2fd67f2009-01-26 03:15:31 +000010983 if (!Subtarget->hasSSE2())
10984 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000010985
Owen Anderson825b72b2009-08-11 20:47:22 +000010986 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
Nate Begemanc2fd67f2009-01-26 03:15:31 +000010987 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000010988
Mon P Wang3becd092009-01-28 08:12:05 +000010989 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +000010990 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +000010991 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +000010992 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +000010993 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
10994 unsigned NumElts = VT.getVectorNumElements();
10995 unsigned i = 0;
10996 for (; i != NumElts; ++i) {
10997 SDValue Arg = ShAmtOp.getOperand(i);
10998 if (Arg.getOpcode() == ISD::UNDEF) continue;
10999 BaseShAmt = Arg;
11000 break;
11001 }
11002 for (; i != NumElts; ++i) {
11003 SDValue Arg = ShAmtOp.getOperand(i);
11004 if (Arg.getOpcode() == ISD::UNDEF) continue;
11005 if (Arg != BaseShAmt) {
11006 return SDValue();
11007 }
11008 }
11009 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +000011010 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +000011011 SDValue InVec = ShAmtOp.getOperand(0);
11012 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
11013 unsigned NumElts = InVec.getValueType().getVectorNumElements();
11014 unsigned i = 0;
11015 for (; i != NumElts; ++i) {
11016 SDValue Arg = InVec.getOperand(i);
11017 if (Arg.getOpcode() == ISD::UNDEF) continue;
11018 BaseShAmt = Arg;
11019 break;
11020 }
11021 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
11022 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +000011023 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +000011024 if (C->getZExtValue() == SplatIdx)
11025 BaseShAmt = InVec.getOperand(1);
11026 }
11027 }
11028 if (BaseShAmt.getNode() == 0)
11029 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
11030 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +000011031 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011032 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000011033
Mon P Wangefa42202009-09-03 19:56:25 +000011034 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +000011035 if (EltVT.bitsGT(MVT::i32))
11036 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
11037 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +000011038 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +000011039
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011040 // The shift amount is identical so we can do a vector shift.
11041 SDValue ValOp = N->getOperand(0);
11042 switch (N->getOpcode()) {
11043 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000011044 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011045 break;
11046 case ISD::SHL:
Owen Anderson825b72b2009-08-11 20:47:22 +000011047 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011048 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011049 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011050 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011051 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011052 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011053 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011054 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011055 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011056 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011057 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011058 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011059 break;
11060 case ISD::SRA:
Owen Anderson825b72b2009-08-11 20:47:22 +000011061 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011062 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011063 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011064 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011065 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011066 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011067 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011068 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011069 break;
11070 case ISD::SRL:
Owen Anderson825b72b2009-08-11 20:47:22 +000011071 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011072 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011073 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011074 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011075 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011076 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011077 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011078 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011079 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011080 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011081 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011082 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011083 break;
Nate Begeman740ab032009-01-26 00:52:55 +000011084 }
11085 return SDValue();
11086}
11087
Nate Begemanb65c1752010-12-17 22:55:37 +000011088
11089static SDValue PerformAndCombine(SDNode *N, SelectionDAG &DAG,
11090 TargetLowering::DAGCombinerInfo &DCI,
11091 const X86Subtarget *Subtarget) {
11092 if (DCI.isBeforeLegalizeOps())
11093 return SDValue();
11094
11095 // Want to form PANDN nodes, in the hopes of then easily combining them with
11096 // OR and AND nodes to form PBLEND/PSIGN.
11097 EVT VT = N->getValueType(0);
11098 if (VT != MVT::v2i64)
11099 return SDValue();
11100
11101 SDValue N0 = N->getOperand(0);
11102 SDValue N1 = N->getOperand(1);
11103 DebugLoc DL = N->getDebugLoc();
11104
11105 // Check LHS for vnot
11106 if (N0.getOpcode() == ISD::XOR &&
11107 ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode()))
11108 return DAG.getNode(X86ISD::PANDN, DL, VT, N0.getOperand(0), N1);
11109
11110 // Check RHS for vnot
11111 if (N1.getOpcode() == ISD::XOR &&
11112 ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode()))
11113 return DAG.getNode(X86ISD::PANDN, DL, VT, N1.getOperand(0), N0);
11114
11115 return SDValue();
11116}
11117
Evan Cheng760d1942010-01-04 21:22:48 +000011118static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000011119 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000011120 const X86Subtarget *Subtarget) {
Evan Cheng39cfeec2010-04-28 02:25:18 +000011121 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000011122 return SDValue();
11123
Evan Cheng760d1942010-01-04 21:22:48 +000011124 EVT VT = N->getValueType(0);
Nate Begemanb65c1752010-12-17 22:55:37 +000011125 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64 && VT != MVT::v2i64)
Evan Cheng760d1942010-01-04 21:22:48 +000011126 return SDValue();
11127
Evan Cheng760d1942010-01-04 21:22:48 +000011128 SDValue N0 = N->getOperand(0);
11129 SDValue N1 = N->getOperand(1);
Nate Begemanb65c1752010-12-17 22:55:37 +000011130
11131 // look for psign/blend
11132 if (Subtarget->hasSSSE3()) {
11133 if (VT == MVT::v2i64) {
11134 // Canonicalize pandn to RHS
11135 if (N0.getOpcode() == X86ISD::PANDN)
11136 std::swap(N0, N1);
11137 // or (and (m, x), (pandn m, y))
11138 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::PANDN) {
11139 SDValue Mask = N1.getOperand(0);
11140 SDValue X = N1.getOperand(1);
11141 SDValue Y;
11142 if (N0.getOperand(0) == Mask)
11143 Y = N0.getOperand(1);
11144 if (N0.getOperand(1) == Mask)
11145 Y = N0.getOperand(0);
11146
11147 // Check to see if the mask appeared in both the AND and PANDN and
11148 if (!Y.getNode())
11149 return SDValue();
11150
11151 // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them.
11152 if (Mask.getOpcode() != ISD::BITCAST ||
11153 X.getOpcode() != ISD::BITCAST ||
11154 Y.getOpcode() != ISD::BITCAST)
11155 return SDValue();
11156
11157 // Look through mask bitcast.
11158 Mask = Mask.getOperand(0);
11159 EVT MaskVT = Mask.getValueType();
11160
11161 // Validate that the Mask operand is a vector sra node. The sra node
11162 // will be an intrinsic.
11163 if (Mask.getOpcode() != ISD::INTRINSIC_WO_CHAIN)
11164 return SDValue();
11165
11166 // FIXME: what to do for bytes, since there is a psignb/pblendvb, but
11167 // there is no psrai.b
11168 switch (cast<ConstantSDNode>(Mask.getOperand(0))->getZExtValue()) {
11169 case Intrinsic::x86_sse2_psrai_w:
11170 case Intrinsic::x86_sse2_psrai_d:
11171 break;
11172 default: return SDValue();
11173 }
11174
11175 // Check that the SRA is all signbits.
11176 SDValue SraC = Mask.getOperand(2);
11177 unsigned SraAmt = cast<ConstantSDNode>(SraC)->getZExtValue();
11178 unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits();
11179 if ((SraAmt + 1) != EltBits)
11180 return SDValue();
11181
11182 DebugLoc DL = N->getDebugLoc();
11183
11184 // Now we know we at least have a plendvb with the mask val. See if
11185 // we can form a psignb/w/d.
11186 // psign = x.type == y.type == mask.type && y = sub(0, x);
11187 X = X.getOperand(0);
11188 Y = Y.getOperand(0);
11189 if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X &&
11190 ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) &&
11191 X.getValueType() == MaskVT && X.getValueType() == Y.getValueType()){
11192 unsigned Opc = 0;
11193 switch (EltBits) {
11194 case 8: Opc = X86ISD::PSIGNB; break;
11195 case 16: Opc = X86ISD::PSIGNW; break;
11196 case 32: Opc = X86ISD::PSIGND; break;
11197 default: break;
11198 }
11199 if (Opc) {
11200 SDValue Sign = DAG.getNode(Opc, DL, MaskVT, X, Mask.getOperand(1));
11201 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Sign);
11202 }
11203 }
11204 // PBLENDVB only available on SSE 4.1
11205 if (!Subtarget->hasSSE41())
11206 return SDValue();
11207
Nate Begemanb65c1752010-12-17 22:55:37 +000011208 X = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, X);
11209 Y = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Y);
11210 Mask = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Mask);
Nate Begeman672fb622010-12-20 22:04:24 +000011211 Mask = DAG.getNode(X86ISD::PBLENDVB, DL, MVT::v16i8, X, Y, Mask);
Nate Begemanb65c1752010-12-17 22:55:37 +000011212 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Mask);
11213 }
11214 }
11215 }
11216
11217 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
Evan Cheng760d1942010-01-04 21:22:48 +000011218 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
11219 std::swap(N0, N1);
11220 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
11221 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000011222 if (!N0.hasOneUse() || !N1.hasOneUse())
11223 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000011224
11225 SDValue ShAmt0 = N0.getOperand(1);
11226 if (ShAmt0.getValueType() != MVT::i8)
11227 return SDValue();
11228 SDValue ShAmt1 = N1.getOperand(1);
11229 if (ShAmt1.getValueType() != MVT::i8)
11230 return SDValue();
11231 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
11232 ShAmt0 = ShAmt0.getOperand(0);
11233 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
11234 ShAmt1 = ShAmt1.getOperand(0);
11235
11236 DebugLoc DL = N->getDebugLoc();
11237 unsigned Opc = X86ISD::SHLD;
11238 SDValue Op0 = N0.getOperand(0);
11239 SDValue Op1 = N1.getOperand(0);
11240 if (ShAmt0.getOpcode() == ISD::SUB) {
11241 Opc = X86ISD::SHRD;
11242 std::swap(Op0, Op1);
11243 std::swap(ShAmt0, ShAmt1);
11244 }
11245
Evan Cheng8b1190a2010-04-28 01:18:01 +000011246 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000011247 if (ShAmt1.getOpcode() == ISD::SUB) {
11248 SDValue Sum = ShAmt1.getOperand(0);
11249 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000011250 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
11251 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
11252 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
11253 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000011254 return DAG.getNode(Opc, DL, VT,
11255 Op0, Op1,
11256 DAG.getNode(ISD::TRUNCATE, DL,
11257 MVT::i8, ShAmt0));
11258 }
11259 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
11260 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
11261 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000011262 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000011263 return DAG.getNode(Opc, DL, VT,
11264 N0.getOperand(0), N1.getOperand(0),
11265 DAG.getNode(ISD::TRUNCATE, DL,
11266 MVT::i8, ShAmt0));
11267 }
Nate Begemanb65c1752010-12-17 22:55:37 +000011268
Evan Cheng760d1942010-01-04 21:22:48 +000011269 return SDValue();
11270}
11271
Chris Lattner149a4e52008-02-22 02:09:43 +000011272/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000011273static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000011274 const X86Subtarget *Subtarget) {
Chris Lattner149a4e52008-02-22 02:09:43 +000011275 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
11276 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000011277 // A preferable solution to the general problem is to figure out the right
11278 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000011279
11280 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng7e2ff772008-05-08 00:57:18 +000011281 StoreSDNode *St = cast<StoreSDNode>(N);
Owen Andersone50ed302009-08-10 22:56:29 +000011282 EVT VT = St->getValue().getValueType();
Evan Cheng536e6672009-03-12 05:59:15 +000011283 if (VT.getSizeInBits() != 64)
11284 return SDValue();
11285
Devang Patel578efa92009-06-05 21:57:13 +000011286 const Function *F = DAG.getMachineFunction().getFunction();
11287 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Eric Christopherfd179292009-08-27 18:07:15 +000011288 bool F64IsLegal = !UseSoftFloat && !NoImplicitFloatOps
Devang Patel578efa92009-06-05 21:57:13 +000011289 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +000011290 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000011291 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000011292 isa<LoadSDNode>(St->getValue()) &&
11293 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
11294 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000011295 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000011296 LoadSDNode *Ld = 0;
11297 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000011298 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000011299 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000011300 // Must be a store of a load. We currently handle two cases: the load
11301 // is a direct child, and it's under an intervening TokenFactor. It is
11302 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000011303 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000011304 Ld = cast<LoadSDNode>(St->getChain());
11305 else if (St->getValue().hasOneUse() &&
11306 ChainVal->getOpcode() == ISD::TokenFactor) {
11307 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000011308 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000011309 TokenFactorIndex = i;
11310 Ld = cast<LoadSDNode>(St->getValue());
11311 } else
11312 Ops.push_back(ChainVal->getOperand(i));
11313 }
11314 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000011315
Evan Cheng536e6672009-03-12 05:59:15 +000011316 if (!Ld || !ISD::isNormalLoad(Ld))
11317 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000011318
Evan Cheng536e6672009-03-12 05:59:15 +000011319 // If this is not the MMX case, i.e. we are just turning i64 load/store
11320 // into f64 load/store, avoid the transformation if there are multiple
11321 // uses of the loaded value.
11322 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
11323 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000011324
Evan Cheng536e6672009-03-12 05:59:15 +000011325 DebugLoc LdDL = Ld->getDebugLoc();
11326 DebugLoc StDL = N->getDebugLoc();
11327 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
11328 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
11329 // pair instead.
11330 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000011331 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000011332 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
11333 Ld->getPointerInfo(), Ld->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000011334 Ld->isNonTemporal(), Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000011335 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000011336 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000011337 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000011338 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000011339 Ops.size());
11340 }
Evan Cheng536e6672009-03-12 05:59:15 +000011341 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000011342 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000011343 St->isVolatile(), St->isNonTemporal(),
11344 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000011345 }
Evan Cheng536e6672009-03-12 05:59:15 +000011346
11347 // Otherwise, lower to two pairs of 32-bit loads / stores.
11348 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000011349 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
11350 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000011351
Owen Anderson825b72b2009-08-11 20:47:22 +000011352 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000011353 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000011354 Ld->isVolatile(), Ld->isNonTemporal(),
11355 Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000011356 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000011357 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000011358 Ld->isVolatile(), Ld->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000011359 MinAlign(Ld->getAlignment(), 4));
11360
11361 SDValue NewChain = LoLd.getValue(1);
11362 if (TokenFactorIndex != -1) {
11363 Ops.push_back(LoLd);
11364 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000011365 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000011366 Ops.size());
11367 }
11368
11369 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000011370 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
11371 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000011372
11373 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000011374 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000011375 St->isVolatile(), St->isNonTemporal(),
11376 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000011377 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000011378 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000011379 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000011380 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000011381 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000011382 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000011383 }
Dan Gohman475871a2008-07-27 21:46:04 +000011384 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000011385}
11386
Chris Lattner6cf73262008-01-25 06:14:17 +000011387/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
11388/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000011389static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000011390 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
11391 // F[X]OR(0.0, x) -> x
11392 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000011393 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
11394 if (C->getValueAPF().isPosZero())
11395 return N->getOperand(1);
11396 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
11397 if (C->getValueAPF().isPosZero())
11398 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000011399 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000011400}
11401
11402/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000011403static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000011404 // FAND(0.0, x) -> 0.0
11405 // FAND(x, 0.0) -> 0.0
11406 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
11407 if (C->getValueAPF().isPosZero())
11408 return N->getOperand(0);
11409 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
11410 if (C->getValueAPF().isPosZero())
11411 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000011412 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000011413}
11414
Dan Gohmane5af2d32009-01-29 01:59:02 +000011415static SDValue PerformBTCombine(SDNode *N,
11416 SelectionDAG &DAG,
11417 TargetLowering::DAGCombinerInfo &DCI) {
11418 // BT ignores high bits in the bit index operand.
11419 SDValue Op1 = N->getOperand(1);
11420 if (Op1.hasOneUse()) {
11421 unsigned BitWidth = Op1.getValueSizeInBits();
11422 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
11423 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000011424 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
11425 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000011426 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000011427 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
11428 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
11429 DCI.CommitTargetLoweringOpt(TLO);
11430 }
11431 return SDValue();
11432}
Chris Lattner83e6c992006-10-04 06:57:07 +000011433
Eli Friedman7a5e5552009-06-07 06:52:44 +000011434static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
11435 SDValue Op = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011436 if (Op.getOpcode() == ISD::BITCAST)
Eli Friedman7a5e5552009-06-07 06:52:44 +000011437 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000011438 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000011439 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000011440 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000011441 OpVT.getVectorElementType().getSizeInBits()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011442 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Eli Friedman7a5e5552009-06-07 06:52:44 +000011443 }
11444 return SDValue();
11445}
11446
Evan Cheng2e489c42009-12-16 00:53:11 +000011447static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG) {
11448 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
11449 // (and (i32 x86isd::setcc_carry), 1)
11450 // This eliminates the zext. This transformation is necessary because
11451 // ISD::SETCC is always legalized to i8.
11452 DebugLoc dl = N->getDebugLoc();
11453 SDValue N0 = N->getOperand(0);
11454 EVT VT = N->getValueType(0);
11455 if (N0.getOpcode() == ISD::AND &&
11456 N0.hasOneUse() &&
11457 N0.getOperand(0).hasOneUse()) {
11458 SDValue N00 = N0.getOperand(0);
11459 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
11460 return SDValue();
11461 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
11462 if (!C || C->getZExtValue() != 1)
11463 return SDValue();
11464 return DAG.getNode(ISD::AND, dl, VT,
11465 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
11466 N00.getOperand(0), N00.getOperand(1)),
11467 DAG.getConstant(1, VT));
11468 }
11469
11470 return SDValue();
11471}
11472
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011473// Optimize RES = X86ISD::SETCC CONDCODE, EFLAG_INPUT
11474static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG &DAG) {
11475 unsigned X86CC = N->getConstantOperandVal(0);
11476 SDValue EFLAG = N->getOperand(1);
11477 DebugLoc DL = N->getDebugLoc();
11478
11479 // Materialize "setb reg" as "sbb reg,reg", since it can be extended without
11480 // a zext and produces an all-ones bit which is more useful than 0/1 in some
11481 // cases.
11482 if (X86CC == X86::COND_B)
11483 return DAG.getNode(ISD::AND, DL, MVT::i8,
11484 DAG.getNode(X86ISD::SETCC_CARRY, DL, MVT::i8,
11485 DAG.getConstant(X86CC, MVT::i8), EFLAG),
11486 DAG.getConstant(1, MVT::i8));
11487
11488 return SDValue();
11489}
Chris Lattner23a01992010-12-20 01:37:09 +000011490
11491// Optimize RES, EFLAGS = X86ISD::ADC LHS, RHS, EFLAGS
11492static SDValue PerformADCCombine(SDNode *N, SelectionDAG &DAG,
11493 X86TargetLowering::DAGCombinerInfo &DCI) {
11494 // If the LHS and RHS of the ADC node are zero, then it can't overflow and
11495 // the result is either zero or one (depending on the input carry bit).
11496 // Strength reduce this down to a "set on carry" aka SETCC_CARRY&1.
11497 if (X86::isZeroNode(N->getOperand(0)) &&
11498 X86::isZeroNode(N->getOperand(1)) &&
11499 // We don't have a good way to replace an EFLAGS use, so only do this when
11500 // dead right now.
11501 SDValue(N, 1).use_empty()) {
11502 DebugLoc DL = N->getDebugLoc();
11503 EVT VT = N->getValueType(0);
11504 SDValue CarryOut = DAG.getConstant(0, N->getValueType(1));
11505 SDValue Res1 = DAG.getNode(ISD::AND, DL, VT,
11506 DAG.getNode(X86ISD::SETCC_CARRY, DL, VT,
11507 DAG.getConstant(X86::COND_B,MVT::i8),
11508 N->getOperand(2)),
11509 DAG.getConstant(1, VT));
11510 return DCI.CombineTo(N, Res1, CarryOut);
11511 }
11512
11513 return SDValue();
11514}
11515
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011516
Dan Gohman475871a2008-07-27 21:46:04 +000011517SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000011518 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000011519 SelectionDAG &DAG = DCI.DAG;
11520 switch (N->getOpcode()) {
11521 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000011522 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011523 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, *this);
Chris Lattneraf723b92008-01-25 05:46:26 +000011524 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +000011525 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Chris Lattner23a01992010-12-20 01:37:09 +000011526 case X86ISD::ADC: return PerformADCCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000011527 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000011528 case ISD::SHL:
11529 case ISD::SRA:
11530 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Nate Begemanb65c1752010-12-17 22:55:37 +000011531 case ISD::AND: return PerformAndCombine(N, DAG, DCI, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000011532 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000011533 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +000011534 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000011535 case X86ISD::FOR: return PerformFORCombine(N, DAG);
11536 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000011537 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000011538 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Evan Cheng2e489c42009-12-16 00:53:11 +000011539 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG);
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011540 case X86ISD::SETCC: return PerformSETCCCombine(N, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000011541 case X86ISD::SHUFPS: // Handle all target specific shuffles
11542 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +000011543 case X86ISD::PALIGN:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000011544 case X86ISD::PUNPCKHBW:
11545 case X86ISD::PUNPCKHWD:
11546 case X86ISD::PUNPCKHDQ:
11547 case X86ISD::PUNPCKHQDQ:
11548 case X86ISD::UNPCKHPS:
11549 case X86ISD::UNPCKHPD:
11550 case X86ISD::PUNPCKLBW:
11551 case X86ISD::PUNPCKLWD:
11552 case X86ISD::PUNPCKLDQ:
11553 case X86ISD::PUNPCKLQDQ:
11554 case X86ISD::UNPCKLPS:
11555 case X86ISD::UNPCKLPD:
11556 case X86ISD::MOVHLPS:
11557 case X86ISD::MOVLHPS:
11558 case X86ISD::PSHUFD:
11559 case X86ISD::PSHUFHW:
11560 case X86ISD::PSHUFLW:
11561 case X86ISD::MOVSS:
11562 case X86ISD::MOVSD:
Mon P Wanga0fd0d52010-12-19 23:55:53 +000011563 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, DCI);
Evan Cheng206ee9d2006-07-07 08:33:52 +000011564 }
11565
Dan Gohman475871a2008-07-27 21:46:04 +000011566 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000011567}
11568
Evan Chenge5b51ac2010-04-17 06:13:15 +000011569/// isTypeDesirableForOp - Return true if the target has native support for
11570/// the specified value type and it is 'desirable' to use the type for the
11571/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
11572/// instruction encodings are longer and some i16 instructions are slow.
11573bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
11574 if (!isTypeLegal(VT))
11575 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000011576 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000011577 return true;
11578
11579 switch (Opc) {
11580 default:
11581 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000011582 case ISD::LOAD:
11583 case ISD::SIGN_EXTEND:
11584 case ISD::ZERO_EXTEND:
11585 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000011586 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000011587 case ISD::SRL:
11588 case ISD::SUB:
11589 case ISD::ADD:
11590 case ISD::MUL:
11591 case ISD::AND:
11592 case ISD::OR:
11593 case ISD::XOR:
11594 return false;
11595 }
11596}
11597
11598/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000011599/// beneficial for dag combiner to promote the specified node. If true, it
11600/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000011601bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000011602 EVT VT = Op.getValueType();
11603 if (VT != MVT::i16)
11604 return false;
11605
Evan Cheng4c26e932010-04-19 19:29:22 +000011606 bool Promote = false;
11607 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000011608 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000011609 default: break;
11610 case ISD::LOAD: {
11611 LoadSDNode *LD = cast<LoadSDNode>(Op);
11612 // If the non-extending load has a single use and it's not live out, then it
11613 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000011614 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
11615 Op.hasOneUse()*/) {
11616 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
11617 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
11618 // The only case where we'd want to promote LOAD (rather then it being
11619 // promoted as an operand is when it's only use is liveout.
11620 if (UI->getOpcode() != ISD::CopyToReg)
11621 return false;
11622 }
11623 }
Evan Cheng4c26e932010-04-19 19:29:22 +000011624 Promote = true;
11625 break;
11626 }
11627 case ISD::SIGN_EXTEND:
11628 case ISD::ZERO_EXTEND:
11629 case ISD::ANY_EXTEND:
11630 Promote = true;
11631 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000011632 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000011633 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000011634 SDValue N0 = Op.getOperand(0);
11635 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000011636 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000011637 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000011638 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000011639 break;
11640 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000011641 case ISD::ADD:
11642 case ISD::MUL:
11643 case ISD::AND:
11644 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000011645 case ISD::XOR:
11646 Commute = true;
11647 // fallthrough
11648 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000011649 SDValue N0 = Op.getOperand(0);
11650 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000011651 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000011652 return false;
11653 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000011654 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000011655 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000011656 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000011657 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000011658 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000011659 }
11660 }
11661
11662 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000011663 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000011664}
11665
Evan Cheng60c07e12006-07-05 22:17:51 +000011666//===----------------------------------------------------------------------===//
11667// X86 Inline Assembly Support
11668//===----------------------------------------------------------------------===//
11669
Chris Lattnerb8105652009-07-20 17:51:36 +000011670static bool LowerToBSwap(CallInst *CI) {
11671 // FIXME: this should verify that we are targetting a 486 or better. If not,
11672 // we will turn this bswap into something that will be lowered to logical ops
11673 // instead of emitting the bswap asm. For now, we don't support 486 or lower
11674 // so don't worry about this.
Eric Christopherfd179292009-08-27 18:07:15 +000011675
Chris Lattnerb8105652009-07-20 17:51:36 +000011676 // Verify this is a simple bswap.
Gabor Greife1c2b9c2010-06-30 13:03:37 +000011677 if (CI->getNumArgOperands() != 1 ||
Gabor Greif1cfe44a2010-06-26 11:51:52 +000011678 CI->getType() != CI->getArgOperand(0)->getType() ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000011679 !CI->getType()->isIntegerTy())
Chris Lattnerb8105652009-07-20 17:51:36 +000011680 return false;
Eric Christopherfd179292009-08-27 18:07:15 +000011681
Chris Lattnerb8105652009-07-20 17:51:36 +000011682 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
11683 if (!Ty || Ty->getBitWidth() % 16 != 0)
11684 return false;
Eric Christopherfd179292009-08-27 18:07:15 +000011685
Chris Lattnerb8105652009-07-20 17:51:36 +000011686 // Okay, we can do this xform, do so now.
11687 const Type *Tys[] = { Ty };
11688 Module *M = CI->getParent()->getParent()->getParent();
11689 Constant *Int = Intrinsic::getDeclaration(M, Intrinsic::bswap, Tys, 1);
Eric Christopherfd179292009-08-27 18:07:15 +000011690
Gabor Greif1cfe44a2010-06-26 11:51:52 +000011691 Value *Op = CI->getArgOperand(0);
Chris Lattnerb8105652009-07-20 17:51:36 +000011692 Op = CallInst::Create(Int, Op, CI->getName(), CI);
Eric Christopherfd179292009-08-27 18:07:15 +000011693
Chris Lattnerb8105652009-07-20 17:51:36 +000011694 CI->replaceAllUsesWith(Op);
11695 CI->eraseFromParent();
11696 return true;
11697}
11698
11699bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
11700 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
John Thompson44ab89e2010-10-29 17:29:13 +000011701 InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
Chris Lattnerb8105652009-07-20 17:51:36 +000011702
11703 std::string AsmStr = IA->getAsmString();
11704
11705 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000011706 SmallVector<StringRef, 4> AsmPieces;
Peter Collingbourne98361182010-11-13 19:54:23 +000011707 SplitString(AsmStr, AsmPieces, ";\n");
Chris Lattnerb8105652009-07-20 17:51:36 +000011708
11709 switch (AsmPieces.size()) {
11710 default: return false;
11711 case 1:
11712 AsmStr = AsmPieces[0];
11713 AsmPieces.clear();
11714 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
11715
11716 // bswap $0
11717 if (AsmPieces.size() == 2 &&
11718 (AsmPieces[0] == "bswap" ||
11719 AsmPieces[0] == "bswapq" ||
11720 AsmPieces[0] == "bswapl") &&
11721 (AsmPieces[1] == "$0" ||
11722 AsmPieces[1] == "${0:q}")) {
11723 // No need to check constraints, nothing other than the equivalent of
11724 // "=r,0" would be valid here.
11725 return LowerToBSwap(CI);
11726 }
11727 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000011728 if (CI->getType()->isIntegerTy(16) &&
Chris Lattnerb8105652009-07-20 17:51:36 +000011729 AsmPieces.size() == 3 &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000011730 (AsmPieces[0] == "rorw" || AsmPieces[0] == "rolw") &&
Chris Lattnerb8105652009-07-20 17:51:36 +000011731 AsmPieces[1] == "$$8," &&
11732 AsmPieces[2] == "${0:w}" &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000011733 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
11734 AsmPieces.clear();
Benjamin Kramer018cbd52010-03-12 13:54:59 +000011735 const std::string &Constraints = IA->getConstraintString();
11736 SplitString(StringRef(Constraints).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000011737 std::sort(AsmPieces.begin(), AsmPieces.end());
11738 if (AsmPieces.size() == 4 &&
11739 AsmPieces[0] == "~{cc}" &&
11740 AsmPieces[1] == "~{dirflag}" &&
11741 AsmPieces[2] == "~{flags}" &&
11742 AsmPieces[3] == "~{fpsr}") {
11743 return LowerToBSwap(CI);
11744 }
Chris Lattnerb8105652009-07-20 17:51:36 +000011745 }
11746 break;
11747 case 3:
Peter Collingbourne948cf022010-11-13 19:54:30 +000011748 if (CI->getType()->isIntegerTy(32) &&
11749 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
11750 SmallVector<StringRef, 4> Words;
11751 SplitString(AsmPieces[0], Words, " \t,");
11752 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
11753 Words[2] == "${0:w}") {
11754 Words.clear();
11755 SplitString(AsmPieces[1], Words, " \t,");
11756 if (Words.size() == 3 && Words[0] == "rorl" && Words[1] == "$$16" &&
11757 Words[2] == "$0") {
11758 Words.clear();
11759 SplitString(AsmPieces[2], Words, " \t,");
11760 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
11761 Words[2] == "${0:w}") {
11762 AsmPieces.clear();
11763 const std::string &Constraints = IA->getConstraintString();
11764 SplitString(StringRef(Constraints).substr(5), AsmPieces, ",");
11765 std::sort(AsmPieces.begin(), AsmPieces.end());
11766 if (AsmPieces.size() == 4 &&
11767 AsmPieces[0] == "~{cc}" &&
11768 AsmPieces[1] == "~{dirflag}" &&
11769 AsmPieces[2] == "~{flags}" &&
11770 AsmPieces[3] == "~{fpsr}") {
11771 return LowerToBSwap(CI);
11772 }
11773 }
11774 }
11775 }
11776 }
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000011777 if (CI->getType()->isIntegerTy(64) &&
Owen Anderson1d0be152009-08-13 21:58:54 +000011778 Constraints.size() >= 2 &&
Chris Lattnerb8105652009-07-20 17:51:36 +000011779 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
11780 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
11781 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
Benjamin Kramerd4f19592010-01-11 18:03:24 +000011782 SmallVector<StringRef, 4> Words;
Chris Lattnerb8105652009-07-20 17:51:36 +000011783 SplitString(AsmPieces[0], Words, " \t");
11784 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
11785 Words.clear();
11786 SplitString(AsmPieces[1], Words, " \t");
11787 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
11788 Words.clear();
11789 SplitString(AsmPieces[2], Words, " \t,");
11790 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
11791 Words[2] == "%edx") {
11792 return LowerToBSwap(CI);
11793 }
11794 }
11795 }
11796 }
11797 break;
11798 }
11799 return false;
11800}
11801
11802
11803
Chris Lattnerf4dff842006-07-11 02:54:03 +000011804/// getConstraintType - Given a constraint letter, return the type of
11805/// constraint it is for this target.
11806X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000011807X86TargetLowering::getConstraintType(const std::string &Constraint) const {
11808 if (Constraint.size() == 1) {
11809 switch (Constraint[0]) {
Chris Lattner4234f572007-03-25 02:14:49 +000011810 case 'R':
Chris Lattner4234f572007-03-25 02:14:49 +000011811 case 'q':
11812 case 'Q':
John Thompson44ab89e2010-10-29 17:29:13 +000011813 case 'f':
11814 case 't':
11815 case 'u':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000011816 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +000011817 case 'x':
Chris Lattner4234f572007-03-25 02:14:49 +000011818 case 'Y':
11819 return C_RegisterClass;
John Thompson44ab89e2010-10-29 17:29:13 +000011820 case 'a':
11821 case 'b':
11822 case 'c':
11823 case 'd':
11824 case 'S':
11825 case 'D':
11826 case 'A':
11827 return C_Register;
11828 case 'I':
11829 case 'J':
11830 case 'K':
11831 case 'L':
11832 case 'M':
11833 case 'N':
11834 case 'G':
11835 case 'C':
Dale Johannesen78e3e522009-02-12 20:58:09 +000011836 case 'e':
11837 case 'Z':
11838 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000011839 default:
11840 break;
11841 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000011842 }
Chris Lattner4234f572007-03-25 02:14:49 +000011843 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000011844}
11845
John Thompson44ab89e2010-10-29 17:29:13 +000011846/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +000011847/// This object must already have been set up with the operand type
11848/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +000011849TargetLowering::ConstraintWeight
11850 X86TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +000011851 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +000011852 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +000011853 Value *CallOperandVal = info.CallOperandVal;
11854 // If we don't have a value, we can't do a match,
11855 // but allow it at the lowest weight.
11856 if (CallOperandVal == NULL)
John Thompson44ab89e2010-10-29 17:29:13 +000011857 return CW_Default;
11858 const Type *type = CallOperandVal->getType();
John Thompsoneac6e1d2010-09-13 18:15:37 +000011859 // Look at the constraint type.
11860 switch (*constraint) {
11861 default:
John Thompson44ab89e2010-10-29 17:29:13 +000011862 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
11863 case 'R':
11864 case 'q':
11865 case 'Q':
11866 case 'a':
11867 case 'b':
11868 case 'c':
11869 case 'd':
11870 case 'S':
11871 case 'D':
11872 case 'A':
11873 if (CallOperandVal->getType()->isIntegerTy())
11874 weight = CW_SpecificReg;
11875 break;
11876 case 'f':
11877 case 't':
11878 case 'u':
11879 if (type->isFloatingPointTy())
11880 weight = CW_SpecificReg;
11881 break;
11882 case 'y':
Chris Lattner2a786eb2010-12-19 20:19:20 +000011883 if (type->isX86_MMXTy() && Subtarget->hasMMX())
John Thompson44ab89e2010-10-29 17:29:13 +000011884 weight = CW_SpecificReg;
11885 break;
11886 case 'x':
11887 case 'Y':
Nate Begeman2ea8ee72010-12-10 00:26:57 +000011888 if ((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasXMM())
John Thompson44ab89e2010-10-29 17:29:13 +000011889 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +000011890 break;
11891 case 'I':
11892 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
11893 if (C->getZExtValue() <= 31)
John Thompson44ab89e2010-10-29 17:29:13 +000011894 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +000011895 }
11896 break;
John Thompson44ab89e2010-10-29 17:29:13 +000011897 case 'J':
11898 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
11899 if (C->getZExtValue() <= 63)
11900 weight = CW_Constant;
11901 }
11902 break;
11903 case 'K':
11904 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
11905 if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f))
11906 weight = CW_Constant;
11907 }
11908 break;
11909 case 'L':
11910 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
11911 if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff))
11912 weight = CW_Constant;
11913 }
11914 break;
11915 case 'M':
11916 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
11917 if (C->getZExtValue() <= 3)
11918 weight = CW_Constant;
11919 }
11920 break;
11921 case 'N':
11922 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
11923 if (C->getZExtValue() <= 0xff)
11924 weight = CW_Constant;
11925 }
11926 break;
11927 case 'G':
11928 case 'C':
11929 if (dyn_cast<ConstantFP>(CallOperandVal)) {
11930 weight = CW_Constant;
11931 }
11932 break;
11933 case 'e':
11934 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
11935 if ((C->getSExtValue() >= -0x80000000LL) &&
11936 (C->getSExtValue() <= 0x7fffffffLL))
11937 weight = CW_Constant;
11938 }
11939 break;
11940 case 'Z':
11941 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
11942 if (C->getZExtValue() <= 0xffffffff)
11943 weight = CW_Constant;
11944 }
11945 break;
John Thompsoneac6e1d2010-09-13 18:15:37 +000011946 }
11947 return weight;
11948}
11949
Dale Johannesenba2a0b92008-01-29 02:21:21 +000011950/// LowerXConstraint - try to replace an X constraint, which matches anything,
11951/// with another that has more specific requirements based on the type of the
11952/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000011953const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000011954LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000011955 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
11956 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000011957 if (ConstraintVT.isFloatingPoint()) {
Nate Begeman2ea8ee72010-12-10 00:26:57 +000011958 if (Subtarget->hasXMMInt())
Chris Lattner5e764232008-04-26 23:02:14 +000011959 return "Y";
Nate Begeman2ea8ee72010-12-10 00:26:57 +000011960 if (Subtarget->hasXMM())
Chris Lattner5e764232008-04-26 23:02:14 +000011961 return "x";
11962 }
Scott Michelfdc40a02009-02-17 22:15:04 +000011963
Chris Lattner5e764232008-04-26 23:02:14 +000011964 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000011965}
11966
Chris Lattner48884cd2007-08-25 00:47:38 +000011967/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
11968/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000011969void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattner48884cd2007-08-25 00:47:38 +000011970 char Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000011971 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000011972 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000011973 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000011974
Chris Lattner22aaf1d2006-10-31 20:13:11 +000011975 switch (Constraint) {
11976 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000011977 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000011978 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000011979 if (C->getZExtValue() <= 31) {
11980 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000011981 break;
11982 }
Devang Patel84f7fd22007-03-17 00:13:28 +000011983 }
Chris Lattner48884cd2007-08-25 00:47:38 +000011984 return;
Evan Cheng364091e2008-09-22 23:57:37 +000011985 case 'J':
11986 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000011987 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000011988 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
11989 break;
11990 }
11991 }
11992 return;
11993 case 'K':
11994 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000011995 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +000011996 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
11997 break;
11998 }
11999 }
12000 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000012001 case 'N':
12002 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000012003 if (C->getZExtValue() <= 255) {
12004 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000012005 break;
12006 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000012007 }
Chris Lattner48884cd2007-08-25 00:47:38 +000012008 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000012009 case 'e': {
12010 // 32-bit signed value
12011 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000012012 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
12013 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000012014 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000012015 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000012016 break;
12017 }
12018 // FIXME gcc accepts some relocatable values here too, but only in certain
12019 // memory models; it's complicated.
12020 }
12021 return;
12022 }
12023 case 'Z': {
12024 // 32-bit unsigned value
12025 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000012026 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
12027 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000012028 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
12029 break;
12030 }
12031 }
12032 // FIXME gcc accepts some relocatable values here too, but only in certain
12033 // memory models; it's complicated.
12034 return;
12035 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000012036 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012037 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000012038 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000012039 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000012040 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000012041 break;
12042 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012043
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000012044 // In any sort of PIC mode addresses need to be computed at runtime by
12045 // adding in a register or some sort of table lookup. These can't
12046 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000012047 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000012048 return;
12049
Chris Lattnerdc43a882007-05-03 16:52:29 +000012050 // If we are in non-pic codegen mode, we allow the address of a global (with
12051 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000012052 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000012053 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000012054
Chris Lattner49921962009-05-08 18:23:14 +000012055 // Match either (GA), (GA+C), (GA+C1+C2), etc.
12056 while (1) {
12057 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
12058 Offset += GA->getOffset();
12059 break;
12060 } else if (Op.getOpcode() == ISD::ADD) {
12061 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
12062 Offset += C->getZExtValue();
12063 Op = Op.getOperand(0);
12064 continue;
12065 }
12066 } else if (Op.getOpcode() == ISD::SUB) {
12067 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
12068 Offset += -C->getZExtValue();
12069 Op = Op.getOperand(0);
12070 continue;
12071 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000012072 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000012073
Chris Lattner49921962009-05-08 18:23:14 +000012074 // Otherwise, this isn't something we can handle, reject it.
12075 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000012076 }
Eric Christopherfd179292009-08-27 18:07:15 +000012077
Dan Gohman46510a72010-04-15 01:51:59 +000012078 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000012079 // If we require an extra load to get this address, as in PIC mode, we
12080 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000012081 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
12082 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000012083 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000012084
Devang Patel0d881da2010-07-06 22:08:15 +000012085 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
12086 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000012087 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012088 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000012089 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012090
Gabor Greifba36cb52008-08-28 21:40:38 +000012091 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000012092 Ops.push_back(Result);
12093 return;
12094 }
Dale Johannesen1784d162010-06-25 21:55:36 +000012095 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012096}
12097
Chris Lattner259e97c2006-01-31 19:43:35 +000012098std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +000012099getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000012100 EVT VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +000012101 if (Constraint.size() == 1) {
12102 // FIXME: not handling fp-stack yet!
Chris Lattner259e97c2006-01-31 19:43:35 +000012103 switch (Constraint[0]) { // GCC X86 Constraint Letters
Chris Lattnerf4dff842006-07-11 02:54:03 +000012104 default: break; // Unknown constraint letter
Evan Cheng47e9fab2009-07-17 22:13:25 +000012105 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
12106 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000012107 if (VT == MVT::i32)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012108 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX,
12109 X86::ESI, X86::EDI, X86::R8D, X86::R9D,
12110 X86::R10D,X86::R11D,X86::R12D,
12111 X86::R13D,X86::R14D,X86::R15D,
12112 X86::EBP, X86::ESP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012113 else if (VT == MVT::i16)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012114 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX,
12115 X86::SI, X86::DI, X86::R8W,X86::R9W,
12116 X86::R10W,X86::R11W,X86::R12W,
12117 X86::R13W,X86::R14W,X86::R15W,
12118 X86::BP, X86::SP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012119 else if (VT == MVT::i8)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012120 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL,
12121 X86::SIL, X86::DIL, X86::R8B,X86::R9B,
12122 X86::R10B,X86::R11B,X86::R12B,
12123 X86::R13B,X86::R14B,X86::R15B,
12124 X86::BPL, X86::SPL, 0);
12125
Owen Anderson825b72b2009-08-11 20:47:22 +000012126 else if (VT == MVT::i64)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012127 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX,
12128 X86::RSI, X86::RDI, X86::R8, X86::R9,
12129 X86::R10, X86::R11, X86::R12,
12130 X86::R13, X86::R14, X86::R15,
12131 X86::RBP, X86::RSP, 0);
12132
12133 break;
12134 }
Eric Christopherfd179292009-08-27 18:07:15 +000012135 // 32-bit fallthrough
Chris Lattner259e97c2006-01-31 19:43:35 +000012136 case 'Q': // Q_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +000012137 if (VT == MVT::i32)
Chris Lattner80a7ecc2006-05-06 00:29:37 +000012138 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012139 else if (VT == MVT::i16)
Chris Lattner80a7ecc2006-05-06 00:29:37 +000012140 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012141 else if (VT == MVT::i8)
Evan Cheng12914382007-08-13 23:27:11 +000012142 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012143 else if (VT == MVT::i64)
Chris Lattner03e6c702007-11-04 06:51:12 +000012144 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
12145 break;
Chris Lattner259e97c2006-01-31 19:43:35 +000012146 }
12147 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012148
Chris Lattner1efa40f2006-02-22 00:56:39 +000012149 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +000012150}
Chris Lattnerf76d1802006-07-31 23:26:50 +000012151
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012152std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000012153X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000012154 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000012155 // First, see if this is a constraint that directly corresponds to an LLVM
12156 // register class.
12157 if (Constraint.size() == 1) {
12158 // GCC Constraint Letters
12159 switch (Constraint[0]) {
12160 default: break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000012161 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000012162 case 'l': // INDEX_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +000012163 if (VT == MVT::i8)
Chris Lattner0f65cad2007-04-09 05:49:22 +000012164 return std::make_pair(0U, X86::GR8RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012165 if (VT == MVT::i16)
Chris Lattner1fa71982008-10-17 18:15:05 +000012166 return std::make_pair(0U, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012167 if (VT == MVT::i32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +000012168 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +000012169 return std::make_pair(0U, X86::GR64RegisterClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000012170 case 'R': // LEGACY_REGS
12171 if (VT == MVT::i8)
12172 return std::make_pair(0U, X86::GR8_NOREXRegisterClass);
12173 if (VT == MVT::i16)
12174 return std::make_pair(0U, X86::GR16_NOREXRegisterClass);
12175 if (VT == MVT::i32 || !Subtarget->is64Bit())
12176 return std::make_pair(0U, X86::GR32_NOREXRegisterClass);
12177 return std::make_pair(0U, X86::GR64_NOREXRegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000012178 case 'f': // FP Stack registers.
12179 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
12180 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000012181 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000012182 return std::make_pair(0U, X86::RFP32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012183 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000012184 return std::make_pair(0U, X86::RFP64RegisterClass);
12185 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000012186 case 'y': // MMX_REGS if MMX allowed.
12187 if (!Subtarget->hasMMX()) break;
12188 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000012189 case 'Y': // SSE_REGS if SSE2 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012190 if (!Subtarget->hasXMMInt()) break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000012191 // FALL THROUGH.
12192 case 'x': // SSE_REGS if SSE1 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012193 if (!Subtarget->hasXMM()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000012194
Owen Anderson825b72b2009-08-11 20:47:22 +000012195 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000012196 default: break;
12197 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000012198 case MVT::f32:
12199 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +000012200 return std::make_pair(0U, X86::FR32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012201 case MVT::f64:
12202 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +000012203 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000012204 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000012205 case MVT::v16i8:
12206 case MVT::v8i16:
12207 case MVT::v4i32:
12208 case MVT::v2i64:
12209 case MVT::v4f32:
12210 case MVT::v2f64:
Chris Lattner0f65cad2007-04-09 05:49:22 +000012211 return std::make_pair(0U, X86::VR128RegisterClass);
12212 }
Chris Lattnerad043e82007-04-09 05:11:28 +000012213 break;
12214 }
12215 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012216
Chris Lattnerf76d1802006-07-31 23:26:50 +000012217 // Use the default implementation in TargetLowering to convert the register
12218 // constraint into a member of a register class.
12219 std::pair<unsigned, const TargetRegisterClass*> Res;
12220 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000012221
12222 // Not found as a standard register?
12223 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000012224 // Map st(0) -> st(7) -> ST0
12225 if (Constraint.size() == 7 && Constraint[0] == '{' &&
12226 tolower(Constraint[1]) == 's' &&
12227 tolower(Constraint[2]) == 't' &&
12228 Constraint[3] == '(' &&
12229 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
12230 Constraint[5] == ')' &&
12231 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000012232
Chris Lattner56d77c72009-09-13 22:41:48 +000012233 Res.first = X86::ST0+Constraint[4]-'0';
12234 Res.second = X86::RFP80RegisterClass;
12235 return Res;
12236 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000012237
Chris Lattner56d77c72009-09-13 22:41:48 +000012238 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000012239 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000012240 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +000012241 Res.second = X86::RFP80RegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000012242 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000012243 }
Chris Lattner56d77c72009-09-13 22:41:48 +000012244
12245 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000012246 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000012247 Res.first = X86::EFLAGS;
12248 Res.second = X86::CCRRegisterClass;
12249 return Res;
12250 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000012251
Dale Johannesen330169f2008-11-13 21:52:36 +000012252 // 'A' means EAX + EDX.
12253 if (Constraint == "A") {
12254 Res.first = X86::EAX;
Dan Gohman68a31c22009-07-30 17:02:08 +000012255 Res.second = X86::GR32_ADRegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000012256 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000012257 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000012258 return Res;
12259 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012260
Chris Lattnerf76d1802006-07-31 23:26:50 +000012261 // Otherwise, check to see if this is a register class of the wrong value
12262 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
12263 // turn into {ax},{dx}.
12264 if (Res.second->hasType(VT))
12265 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012266
Chris Lattnerf76d1802006-07-31 23:26:50 +000012267 // All of the single-register GCC register classes map their values onto
12268 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
12269 // really want an 8-bit or 32-bit register, map to the appropriate register
12270 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +000012271 if (Res.second == X86::GR16RegisterClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000012272 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000012273 unsigned DestReg = 0;
12274 switch (Res.first) {
12275 default: break;
12276 case X86::AX: DestReg = X86::AL; break;
12277 case X86::DX: DestReg = X86::DL; break;
12278 case X86::CX: DestReg = X86::CL; break;
12279 case X86::BX: DestReg = X86::BL; break;
12280 }
12281 if (DestReg) {
12282 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000012283 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000012284 }
Owen Anderson825b72b2009-08-11 20:47:22 +000012285 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000012286 unsigned DestReg = 0;
12287 switch (Res.first) {
12288 default: break;
12289 case X86::AX: DestReg = X86::EAX; break;
12290 case X86::DX: DestReg = X86::EDX; break;
12291 case X86::CX: DestReg = X86::ECX; break;
12292 case X86::BX: DestReg = X86::EBX; break;
12293 case X86::SI: DestReg = X86::ESI; break;
12294 case X86::DI: DestReg = X86::EDI; break;
12295 case X86::BP: DestReg = X86::EBP; break;
12296 case X86::SP: DestReg = X86::ESP; break;
12297 }
12298 if (DestReg) {
12299 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000012300 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000012301 }
Owen Anderson825b72b2009-08-11 20:47:22 +000012302 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000012303 unsigned DestReg = 0;
12304 switch (Res.first) {
12305 default: break;
12306 case X86::AX: DestReg = X86::RAX; break;
12307 case X86::DX: DestReg = X86::RDX; break;
12308 case X86::CX: DestReg = X86::RCX; break;
12309 case X86::BX: DestReg = X86::RBX; break;
12310 case X86::SI: DestReg = X86::RSI; break;
12311 case X86::DI: DestReg = X86::RDI; break;
12312 case X86::BP: DestReg = X86::RBP; break;
12313 case X86::SP: DestReg = X86::RSP; break;
12314 }
12315 if (DestReg) {
12316 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000012317 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000012318 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000012319 }
Chris Lattner6ba50a92008-08-26 06:19:02 +000012320 } else if (Res.second == X86::FR32RegisterClass ||
12321 Res.second == X86::FR64RegisterClass ||
12322 Res.second == X86::VR128RegisterClass) {
12323 // Handle references to XMM physical registers that got mapped into the
12324 // wrong class. This can happen with constraints like {xmm0} where the
12325 // target independent register mapper will just pick the first match it can
12326 // find, ignoring the required type.
Owen Anderson825b72b2009-08-11 20:47:22 +000012327 if (VT == MVT::f32)
Chris Lattner6ba50a92008-08-26 06:19:02 +000012328 Res.second = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +000012329 else if (VT == MVT::f64)
Chris Lattner6ba50a92008-08-26 06:19:02 +000012330 Res.second = X86::FR64RegisterClass;
12331 else if (X86::VR128RegisterClass->hasType(VT))
12332 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000012333 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012334
Chris Lattnerf76d1802006-07-31 23:26:50 +000012335 return Res;
12336}