blob: 300f8d40dfae68156e1a9f8f4c9e4525a9c6294a [file] [log] [blame]
Arnold Schwaighofer92226dd2007-10-12 21:53:12 +00001//===-- X86ISelLowering.cpp - X86 DAG Lowering Implementation -------------===//
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that X86 uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
Evan Chengb1712452010-01-27 06:25:16 +000015#define DEBUG_TYPE "x86-isel"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000016#include "X86.h"
Evan Cheng0cc39452006-01-16 21:21:29 +000017#include "X86InstrBuilder.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000018#include "X86ISelLowering.h"
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000019#include "X86ShuffleDecode.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000020#include "X86TargetMachine.h"
Chris Lattner8c6ed052009-09-16 01:46:41 +000021#include "X86TargetObjectFile.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000022#include "llvm/CallingConv.h"
Evan Cheng223547a2006-01-31 22:28:30 +000023#include "llvm/Constants.h"
Evan Cheng347d5f72006-04-28 21:29:37 +000024#include "llvm/DerivedTypes.h"
Chris Lattnerb903bed2009-06-26 21:20:29 +000025#include "llvm/GlobalAlias.h"
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000026#include "llvm/GlobalVariable.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000027#include "llvm/Function.h"
Chris Lattnerb8105652009-07-20 17:51:36 +000028#include "llvm/Instructions.h"
Evan Cheng6be2c582006-04-05 23:38:46 +000029#include "llvm/Intrinsics.h"
Owen Andersona90b3dc2009-07-15 21:51:10 +000030#include "llvm/LLVMContext.h"
Evan Cheng55d42002011-01-08 01:24:27 +000031#include "llvm/CodeGen/IntrinsicLowering.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000032#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng4a460802006-01-11 00:33:36 +000033#include "llvm/CodeGen/MachineFunction.h"
34#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner5e1df8d2010-01-25 23:38:14 +000035#include "llvm/CodeGen/MachineJumpTableInfo.h"
Evan Chenga844bde2008-02-02 04:07:54 +000036#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000037#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman69de1932008-02-06 22:27:42 +000038#include "llvm/CodeGen/PseudoSourceValue.h"
Chris Lattner589c6f62010-01-26 06:28:43 +000039#include "llvm/MC/MCAsmInfo.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000040#include "llvm/MC/MCContext.h"
Daniel Dunbar4e815f82010-03-15 23:51:06 +000041#include "llvm/MC/MCExpr.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000042#include "llvm/MC/MCSymbol.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000043#include "llvm/ADT/BitVector.h"
Evan Cheng14b32e12007-12-11 01:46:18 +000044#include "llvm/ADT/SmallSet.h"
Evan Chengb1712452010-01-27 06:25:16 +000045#include "llvm/ADT/Statistic.h"
Chris Lattner1a60aa72006-10-31 19:42:44 +000046#include "llvm/ADT/StringExtras.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000047#include "llvm/ADT/VectorExtras.h"
Mon P Wang3c81d352008-11-23 04:37:22 +000048#include "llvm/Support/CommandLine.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000049#include "llvm/Support/Debug.h"
Bill Wendlingec041eb2010-03-12 19:20:40 +000050#include "llvm/Support/Dwarf.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000051#include "llvm/Support/ErrorHandling.h"
52#include "llvm/Support/MathExtras.h"
Torok Edwindac237e2009-07-08 20:53:28 +000053#include "llvm/Support/raw_ostream.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000054using namespace llvm;
Bill Wendlingec041eb2010-03-12 19:20:40 +000055using namespace dwarf;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000056
Evan Chengb1712452010-01-27 06:25:16 +000057STATISTIC(NumTailCalls, "Number of tail calls");
58
Evan Cheng10e86422008-04-25 19:11:04 +000059// Forward declarations.
Owen Andersone50ed302009-08-10 22:56:29 +000060static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +000061 SDValue V2);
Evan Cheng10e86422008-04-25 19:11:04 +000062
Chris Lattnerf0144122009-07-28 03:13:23 +000063static TargetLoweringObjectFile *createTLOF(X86TargetMachine &TM) {
Michael J. Spencerec38de22010-10-10 22:04:20 +000064
Eric Christopher62f35a22010-07-05 19:26:33 +000065 bool is64Bit = TM.getSubtarget<X86Subtarget>().is64Bit();
Michael J. Spencerec38de22010-10-10 22:04:20 +000066
Eric Christopher62f35a22010-07-05 19:26:33 +000067 if (TM.getSubtarget<X86Subtarget>().isTargetDarwin()) {
Chris Lattnere019ec12010-12-19 20:07:10 +000068 if (is64Bit)
69 return new X8664_MachoTargetObjectFile();
Anton Korobeynikov293d5922010-02-21 20:28:15 +000070 return new TargetLoweringObjectFileMachO();
Michael J. Spencerec38de22010-10-10 22:04:20 +000071 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000072
Chris Lattnere019ec12010-12-19 20:07:10 +000073 if (TM.getSubtarget<X86Subtarget>().isTargetELF() ){
74 if (is64Bit)
75 return new X8664_ELFTargetObjectFile(TM);
76 return new X8632_ELFTargetObjectFile(TM);
77 }
78 if (TM.getSubtarget<X86Subtarget>().isTargetCOFF())
79 return new TargetLoweringObjectFileCOFF();
Eric Christopher62f35a22010-07-05 19:26:33 +000080 llvm_unreachable("unknown subtarget type");
Chris Lattnerf0144122009-07-28 03:13:23 +000081}
82
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +000083X86TargetLowering::X86TargetLowering(X86TargetMachine &TM)
Chris Lattnerf0144122009-07-28 03:13:23 +000084 : TargetLowering(TM, createTLOF(TM)) {
Evan Cheng559806f2006-01-27 08:10:46 +000085 Subtarget = &TM.getSubtarget<X86Subtarget>();
Nate Begeman2ea8ee72010-12-10 00:26:57 +000086 X86ScalarSSEf64 = Subtarget->hasXMMInt();
87 X86ScalarSSEf32 = Subtarget->hasXMM();
Evan Cheng25ab6902006-09-08 06:48:29 +000088 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000089
Anton Korobeynikov2365f512007-07-14 14:06:15 +000090 RegInfo = TM.getRegisterInfo();
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000091 TD = getTargetData();
Anton Korobeynikov2365f512007-07-14 14:06:15 +000092
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000093 // Set up the TargetLowering object.
Chris Lattnera34b3cf2010-12-19 20:03:11 +000094 static MVT IntVTs[] = { MVT::i8, MVT::i16, MVT::i32, MVT::i64 };
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000095
96 // X86 is weird, it always uses i8 for shift amounts and setcc results.
Owen Anderson825b72b2009-08-11 20:47:22 +000097 setShiftAmountType(MVT::i8);
Duncan Sands03228082008-11-23 15:47:28 +000098 setBooleanContents(ZeroOrOneBooleanContent);
Evan Cheng211ffa12010-05-19 20:19:50 +000099 setSchedulingPreference(Sched::RegPressure);
Evan Cheng25ab6902006-09-08 06:48:29 +0000100 setStackPointerRegisterToSaveRestore(X86StackPtr);
Evan Cheng714554d2006-03-16 21:47:42 +0000101
Michael J. Spencer92bf38c2010-10-10 23:11:06 +0000102 if (Subtarget->isTargetWindows() && !Subtarget->isTargetCygMing()) {
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000103 // Setup Windows compiler runtime calls.
104 setLibcallName(RTLIB::SDIV_I64, "_alldiv");
Michael J. Spencer335b8062010-10-11 05:29:15 +0000105 setLibcallName(RTLIB::UDIV_I64, "_aulldiv");
106 setLibcallName(RTLIB::FPTOUINT_F64_I64, "_ftol2");
Michael J. Spencer94f7eeb2010-10-19 07:32:52 +0000107 setLibcallName(RTLIB::FPTOUINT_F32_I64, "_ftol2");
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000108 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::X86_StdCall);
Michael J. Spencer335b8062010-10-11 05:29:15 +0000109 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::X86_StdCall);
Michael J. Spencer6dad10e2010-10-27 18:52:38 +0000110 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I64, CallingConv::C);
111 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I64, CallingConv::C);
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000112 }
113
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000114 if (Subtarget->isTargetDarwin()) {
Evan Chengdf57fa02006-03-17 20:31:41 +0000115 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000116 setUseUnderscoreSetJmp(false);
117 setUseUnderscoreLongJmp(false);
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000118 } else if (Subtarget->isTargetMingw()) {
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000119 // MS runtime is weird: it exports _setjmp, but longjmp!
120 setUseUnderscoreSetJmp(true);
121 setUseUnderscoreLongJmp(false);
122 } else {
123 setUseUnderscoreSetJmp(true);
124 setUseUnderscoreLongJmp(true);
125 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000126
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000127 // Set up the register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000128 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
Dan Gohman71edb242010-04-30 18:30:26 +0000129 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000130 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
Evan Cheng25ab6902006-09-08 06:48:29 +0000131 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000132 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000133
Owen Anderson825b72b2009-08-11 20:47:22 +0000134 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Evan Chengc5484282006-10-04 00:56:09 +0000135
Scott Michelfdc40a02009-02-17 22:15:04 +0000136 // We don't accept any truncstore of integer registers.
Owen Anderson825b72b2009-08-11 20:47:22 +0000137 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000138 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000139 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000140 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000141 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
142 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
Evan Cheng7f042682008-10-15 02:05:31 +0000143
144 // SETOEQ and SETUNE require checking two conditions.
Owen Anderson825b72b2009-08-11 20:47:22 +0000145 setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand);
146 setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand);
147 setCondCodeAction(ISD::SETOEQ, MVT::f80, Expand);
148 setCondCodeAction(ISD::SETUNE, MVT::f32, Expand);
149 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
150 setCondCodeAction(ISD::SETUNE, MVT::f80, Expand);
Chris Lattnerddf89562008-01-17 19:59:44 +0000151
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000152 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
153 // operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000154 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
155 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
156 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +0000157
Evan Cheng25ab6902006-09-08 06:48:29 +0000158 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000159 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
160 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
Eli Friedman948e95a2009-05-23 09:59:16 +0000161 } else if (!UseSoftFloat) {
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000162 // We have an algorithm for SSE2->double, and we turn this into a
163 // 64-bit FILD followed by conditional FADD for other targets.
164 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Eli Friedman948e95a2009-05-23 09:59:16 +0000165 // We have an algorithm for SSE2, and we turn this into a 64-bit
166 // FILD for other targets.
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000167 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000168 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000169
170 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
171 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000172 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
173 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000174
Devang Patel6a784892009-06-05 18:48:29 +0000175 if (!UseSoftFloat) {
Bill Wendling105be5a2009-03-13 08:41:47 +0000176 // SSE has no i16 to fp conversion, only i32
177 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000178 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000179 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000180 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000181 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000182 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
183 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000184 }
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000185 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000186 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
187 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Promote);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000188 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000189
Dale Johannesen73328d12007-09-19 23:55:34 +0000190 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
191 // are Legal, f80 is custom lowered.
Owen Anderson825b72b2009-08-11 20:47:22 +0000192 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
193 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Evan Cheng6dab0532006-01-30 08:02:57 +0000194
Evan Cheng02568ff2006-01-30 22:13:22 +0000195 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
196 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000197 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
198 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
Evan Cheng02568ff2006-01-30 22:13:22 +0000199
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000200 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000201 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000202 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000203 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000204 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000205 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
206 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000207 }
208
209 // Handle FP_TO_UINT by promoting the destination to a larger signed
210 // conversion.
Owen Anderson825b72b2009-08-11 20:47:22 +0000211 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
212 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
213 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000214
Evan Cheng25ab6902006-09-08 06:48:29 +0000215 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000216 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
217 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Eli Friedman948e95a2009-05-23 09:59:16 +0000218 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000219 if (X86ScalarSSEf32 && !Subtarget->hasSSE3())
Evan Cheng25ab6902006-09-08 06:48:29 +0000220 // Expand FP_TO_UINT into a select.
221 // FIXME: We would like to use a Custom expander here eventually to do
222 // the optimal thing for SSE vs. the default expansion in the legalizer.
Owen Anderson825b72b2009-08-11 20:47:22 +0000223 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000224 else
Eli Friedman948e95a2009-05-23 09:59:16 +0000225 // With SSE3 we can use fisttpll to convert to a signed i64; without
226 // SSE, we're stuck with a fistpll.
Owen Anderson825b72b2009-08-11 20:47:22 +0000227 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000228 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000229
Chris Lattner399610a2006-12-05 18:22:22 +0000230 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Michael J. Spencerec38de22010-10-10 22:04:20 +0000231 if (!X86ScalarSSEf64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000232 setOperationAction(ISD::BITCAST , MVT::f32 , Expand);
233 setOperationAction(ISD::BITCAST , MVT::i32 , Expand);
Dale Johannesene39859a2010-05-21 18:40:15 +0000234 if (Subtarget->is64Bit()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000235 setOperationAction(ISD::BITCAST , MVT::f64 , Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000236 // Without SSE, i64->f64 goes through memory.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000237 setOperationAction(ISD::BITCAST , MVT::i64 , Expand);
Dale Johannesen7d07b482010-05-21 00:52:33 +0000238 }
Chris Lattnerf3597a12006-12-05 18:45:06 +0000239 }
Chris Lattner21f66852005-12-23 05:15:23 +0000240
Dan Gohmanb00ee212008-02-18 19:34:53 +0000241 // Scalar integer divide and remainder are lowered to use operations that
242 // produce two results, to match the available instructions. This exposes
243 // the two-result form to trivial CSE, which is able to combine x/y and x%y
244 // into a single instruction.
245 //
246 // Scalar integer multiply-high is also lowered to use two-result
247 // operations, to match the available instructions. However, plain multiply
248 // (low) operations are left as Legal, as there are single-result
249 // instructions for this in x86. Using the two-result multiply instructions
250 // when both high and low results are needed must be arranged by dagcombine.
Chris Lattnere019ec12010-12-19 20:07:10 +0000251 for (unsigned i = 0, e = 4; i != e; ++i) {
252 MVT VT = IntVTs[i];
253 setOperationAction(ISD::MULHS, VT, Expand);
254 setOperationAction(ISD::MULHU, VT, Expand);
255 setOperationAction(ISD::SDIV, VT, Expand);
256 setOperationAction(ISD::UDIV, VT, Expand);
257 setOperationAction(ISD::SREM, VT, Expand);
258 setOperationAction(ISD::UREM, VT, Expand);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000259
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000260 // Add/Sub overflow ops with MVT::Glues are lowered to EFLAGS dependences.
Chris Lattnerd8ff7ec2010-12-20 01:03:27 +0000261 setOperationAction(ISD::ADDC, VT, Custom);
262 setOperationAction(ISD::ADDE, VT, Custom);
263 setOperationAction(ISD::SUBC, VT, Custom);
264 setOperationAction(ISD::SUBE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000265 }
Dan Gohmana37c9f72007-09-25 18:23:27 +0000266
Owen Anderson825b72b2009-08-11 20:47:22 +0000267 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
268 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
269 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
270 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000271 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000272 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
273 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
274 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
275 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
276 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
277 setOperationAction(ISD::FREM , MVT::f32 , Expand);
278 setOperationAction(ISD::FREM , MVT::f64 , Expand);
279 setOperationAction(ISD::FREM , MVT::f80 , Expand);
280 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000281
Owen Anderson825b72b2009-08-11 20:47:22 +0000282 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
283 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000284 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
285 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000286 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
287 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000288 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000289 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
290 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000291 }
292
Benjamin Kramer1292c222010-12-04 20:32:23 +0000293 if (Subtarget->hasPOPCNT()) {
294 setOperationAction(ISD::CTPOP , MVT::i8 , Promote);
295 } else {
296 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
297 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
298 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
299 if (Subtarget->is64Bit())
300 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
301 }
302
Owen Anderson825b72b2009-08-11 20:47:22 +0000303 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
304 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000305
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000306 // These should be promoted to a larger select which is supported.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000307 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000308 // X86 wants to expand cmov itself.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000309 setOperationAction(ISD::SELECT , MVT::i8 , Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000310 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000311 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
312 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
313 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
314 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
315 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000316 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000317 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
318 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
319 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
320 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000321 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000322 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
323 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000324 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000325 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000326
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000327 // Darwin ABI issue.
Owen Anderson825b72b2009-08-11 20:47:22 +0000328 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
329 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
330 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
331 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000332 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000333 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
334 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000335 setOperationAction(ISD::BlockAddress , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000336 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000337 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
338 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
339 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
340 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000341 setOperationAction(ISD::BlockAddress , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000342 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000343 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Owen Anderson825b72b2009-08-11 20:47:22 +0000344 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
345 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
346 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000347 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000348 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
349 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
350 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000351 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000352
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000353 if (Subtarget->hasXMM())
Owen Anderson825b72b2009-08-11 20:47:22 +0000354 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Cheng27b7db52008-03-08 00:58:38 +0000355
Eric Christopher9a9d2752010-07-22 02:48:34 +0000356 // We may not have a libcall for MEMBARRIER so we should lower this.
357 setOperationAction(ISD::MEMBARRIER , MVT::Other, Custom);
Michael J. Spencerec38de22010-10-10 22:04:20 +0000358
Jim Grosbachf1ab49e2010-06-23 16:25:07 +0000359 // On X86 and X86-64, atomic operations are lowered to locked instructions.
360 // Locked instructions, in turn, have implicit fence semantics (all memory
361 // operations are flushed before issuing the locked instruction, and they
362 // are not buffered), so we can fold away the common pattern of
363 // fence-atomic-fence.
364 setShouldFoldAtomicFences(true);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000365
Mon P Wang63307c32008-05-05 19:05:59 +0000366 // Expand certain atomics
Chris Lattnere019ec12010-12-19 20:07:10 +0000367 for (unsigned i = 0, e = 4; i != e; ++i) {
368 MVT VT = IntVTs[i];
369 setOperationAction(ISD::ATOMIC_CMP_SWAP, VT, Custom);
370 setOperationAction(ISD::ATOMIC_LOAD_SUB, VT, Custom);
371 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000372
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000373 if (!Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000374 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
375 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
376 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
377 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
378 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
379 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
380 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000381 }
382
Evan Cheng3c992d22006-03-07 02:02:57 +0000383 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000384 if (!Subtarget->isTargetDarwin() &&
385 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000386 !Subtarget->isTargetCygMing()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000387 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Dan Gohman44066042008-07-01 00:05:16 +0000388 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000389
Owen Anderson825b72b2009-08-11 20:47:22 +0000390 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
391 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
392 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
393 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000394 if (Subtarget->is64Bit()) {
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000395 setExceptionPointerRegister(X86::RAX);
396 setExceptionSelectorRegister(X86::RDX);
397 } else {
398 setExceptionPointerRegister(X86::EAX);
399 setExceptionSelectorRegister(X86::EDX);
400 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000401 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
402 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000403
Owen Anderson825b72b2009-08-11 20:47:22 +0000404 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000405
Owen Anderson825b72b2009-08-11 20:47:22 +0000406 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000407
Nate Begemanacc398c2006-01-25 18:21:52 +0000408 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson825b72b2009-08-11 20:47:22 +0000409 setOperationAction(ISD::VASTART , MVT::Other, Custom);
410 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000411 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000412 setOperationAction(ISD::VAARG , MVT::Other, Custom);
413 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000414 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000415 setOperationAction(ISD::VAARG , MVT::Other, Expand);
416 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000417 }
Evan Chengae642192007-03-02 23:16:35 +0000418
Owen Anderson825b72b2009-08-11 20:47:22 +0000419 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
420 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000421 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000422 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Michael J. Spencere9c253e2010-10-21 01:41:01 +0000423 if (Subtarget->isTargetCygMing() || Subtarget->isTargetWindows())
Owen Anderson825b72b2009-08-11 20:47:22 +0000424 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +0000425 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000426 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000427
Evan Chengc7ce29b2009-02-13 22:36:38 +0000428 if (!UseSoftFloat && X86ScalarSSEf64) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000429 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000430 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000431 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
432 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000433
Evan Cheng223547a2006-01-31 22:28:30 +0000434 // Use ANDPD to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000435 setOperationAction(ISD::FABS , MVT::f64, Custom);
436 setOperationAction(ISD::FABS , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000437
438 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000439 setOperationAction(ISD::FNEG , MVT::f64, Custom);
440 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000441
Evan Cheng68c47cb2007-01-05 07:55:56 +0000442 // Use ANDPD and ORPD to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000443 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
444 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000445
Evan Chengd25e9e82006-02-02 00:28:23 +0000446 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000447 setOperationAction(ISD::FSIN , MVT::f64, Expand);
448 setOperationAction(ISD::FCOS , MVT::f64, Expand);
449 setOperationAction(ISD::FSIN , MVT::f32, Expand);
450 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000451
Chris Lattnera54aa942006-01-29 06:26:08 +0000452 // Expand FP immediates into loads from the stack, except for the special
453 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000454 addLegalFPImmediate(APFloat(+0.0)); // xorpd
455 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Evan Chengc7ce29b2009-02-13 22:36:38 +0000456 } else if (!UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000457 // Use SSE for f32, x87 for f64.
458 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000459 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
460 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000461
462 // Use ANDPS to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000463 setOperationAction(ISD::FABS , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000464
465 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000466 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000467
Owen Anderson825b72b2009-08-11 20:47:22 +0000468 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000469
470 // Use ANDPS and ORPS to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000471 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
472 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000473
474 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000475 setOperationAction(ISD::FSIN , MVT::f32, Expand);
476 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000477
Nate Begemane1795842008-02-14 08:57:00 +0000478 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000479 addLegalFPImmediate(APFloat(+0.0f)); // xorps
480 addLegalFPImmediate(APFloat(+0.0)); // FLD0
481 addLegalFPImmediate(APFloat(+1.0)); // FLD1
482 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
483 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
484
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000485 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000486 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
487 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000488 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000489 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000490 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000491 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000492 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
493 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000494
Owen Anderson825b72b2009-08-11 20:47:22 +0000495 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
496 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
497 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
498 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000499
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000500 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000501 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
502 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000503 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000504 addLegalFPImmediate(APFloat(+0.0)); // FLD0
505 addLegalFPImmediate(APFloat(+1.0)); // FLD1
506 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
507 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000508 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
509 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
510 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
511 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000512 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000513
Dale Johannesen59a58732007-08-05 18:49:15 +0000514 // Long double always uses X87.
Evan Cheng92722532009-03-26 23:06:32 +0000515 if (!UseSoftFloat) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000516 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
517 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
518 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000519 {
Benjamin Kramer98383962010-12-04 14:22:24 +0000520 APFloat TmpFlt = APFloat::getZero(APFloat::x87DoubleExtended);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000521 addLegalFPImmediate(TmpFlt); // FLD0
522 TmpFlt.changeSign();
523 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
Benjamin Kramer98383962010-12-04 14:22:24 +0000524
525 bool ignored;
Evan Chengc7ce29b2009-02-13 22:36:38 +0000526 APFloat TmpFlt2(+1.0);
527 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
528 &ignored);
529 addLegalFPImmediate(TmpFlt2); // FLD1
530 TmpFlt2.changeSign();
531 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
532 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000533
Evan Chengc7ce29b2009-02-13 22:36:38 +0000534 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000535 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
536 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000537 }
Dale Johannesen2f429012007-09-26 21:10:55 +0000538 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000539
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000540 // Always use a library call for pow.
Owen Anderson825b72b2009-08-11 20:47:22 +0000541 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
542 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
543 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000544
Owen Anderson825b72b2009-08-11 20:47:22 +0000545 setOperationAction(ISD::FLOG, MVT::f80, Expand);
546 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
547 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
548 setOperationAction(ISD::FEXP, MVT::f80, Expand);
549 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000550
Mon P Wangf007a8b2008-11-06 05:31:54 +0000551 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000552 // (for widening) or expand (for scalarization). Then we will selectively
553 // turn on ones that can be effectively codegen'd.
Owen Anderson825b72b2009-08-11 20:47:22 +0000554 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
555 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
556 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
557 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
558 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
559 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
560 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
561 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
562 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
563 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
564 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
565 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
566 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
567 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
568 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
569 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
570 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000571 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
David Greenecfe33c42011-01-26 19:13:22 +0000572 setOperationAction(ISD::EXTRACT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
573 setOperationAction(ISD::INSERT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000574 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
575 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
576 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
577 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
578 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
579 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
580 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
581 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
582 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
583 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
584 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
585 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
586 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
587 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
588 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
589 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
590 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
591 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
592 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
593 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
594 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
595 setOperationAction(ISD::VSETCC, (MVT::SimpleValueType)VT, Expand);
596 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
597 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
598 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
599 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
600 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
601 setOperationAction(ISD::FP_TO_UINT, (MVT::SimpleValueType)VT, Expand);
602 setOperationAction(ISD::FP_TO_SINT, (MVT::SimpleValueType)VT, Expand);
603 setOperationAction(ISD::UINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
604 setOperationAction(ISD::SINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
Dan Gohman87862e72009-12-11 21:31:27 +0000605 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,Expand);
Dan Gohman2e141d72009-12-14 23:40:38 +0000606 setOperationAction(ISD::TRUNCATE, (MVT::SimpleValueType)VT, Expand);
607 setOperationAction(ISD::SIGN_EXTEND, (MVT::SimpleValueType)VT, Expand);
608 setOperationAction(ISD::ZERO_EXTEND, (MVT::SimpleValueType)VT, Expand);
609 setOperationAction(ISD::ANY_EXTEND, (MVT::SimpleValueType)VT, Expand);
610 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
611 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
612 setTruncStoreAction((MVT::SimpleValueType)VT,
613 (MVT::SimpleValueType)InnerVT, Expand);
614 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
615 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
616 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000617 }
618
Evan Chengc7ce29b2009-02-13 22:36:38 +0000619 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
620 // with -msoft-float, disable use of MMX as well.
Chris Lattner2a786eb2010-12-19 20:19:20 +0000621 if (!UseSoftFloat && Subtarget->hasMMX()) {
Dale Johannesene93d99c2010-10-20 21:32:10 +0000622 addRegisterClass(MVT::x86mmx, X86::VR64RegisterClass);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000623 // No operations on x86mmx supported, everything uses intrinsics.
Evan Cheng470a6ad2006-02-22 02:26:30 +0000624 }
625
Dale Johannesen0488fb62010-09-30 23:57:10 +0000626 // MMX-sized vectors (other than x86mmx) are expected to be expanded
627 // into smaller operations.
628 setOperationAction(ISD::MULHS, MVT::v8i8, Expand);
629 setOperationAction(ISD::MULHS, MVT::v4i16, Expand);
630 setOperationAction(ISD::MULHS, MVT::v2i32, Expand);
631 setOperationAction(ISD::MULHS, MVT::v1i64, Expand);
632 setOperationAction(ISD::AND, MVT::v8i8, Expand);
633 setOperationAction(ISD::AND, MVT::v4i16, Expand);
634 setOperationAction(ISD::AND, MVT::v2i32, Expand);
635 setOperationAction(ISD::AND, MVT::v1i64, Expand);
636 setOperationAction(ISD::OR, MVT::v8i8, Expand);
637 setOperationAction(ISD::OR, MVT::v4i16, Expand);
638 setOperationAction(ISD::OR, MVT::v2i32, Expand);
639 setOperationAction(ISD::OR, MVT::v1i64, Expand);
640 setOperationAction(ISD::XOR, MVT::v8i8, Expand);
641 setOperationAction(ISD::XOR, MVT::v4i16, Expand);
642 setOperationAction(ISD::XOR, MVT::v2i32, Expand);
643 setOperationAction(ISD::XOR, MVT::v1i64, Expand);
644 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Expand);
645 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Expand);
646 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Expand);
647 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Expand);
648 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v1i64, Expand);
649 setOperationAction(ISD::SELECT, MVT::v8i8, Expand);
650 setOperationAction(ISD::SELECT, MVT::v4i16, Expand);
651 setOperationAction(ISD::SELECT, MVT::v2i32, Expand);
652 setOperationAction(ISD::SELECT, MVT::v1i64, Expand);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000653 setOperationAction(ISD::BITCAST, MVT::v8i8, Expand);
654 setOperationAction(ISD::BITCAST, MVT::v4i16, Expand);
655 setOperationAction(ISD::BITCAST, MVT::v2i32, Expand);
656 setOperationAction(ISD::BITCAST, MVT::v1i64, Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000657
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000658 if (!UseSoftFloat && Subtarget->hasXMM()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000659 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000660
Owen Anderson825b72b2009-08-11 20:47:22 +0000661 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
662 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
663 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
664 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
665 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
666 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
667 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
668 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
669 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
670 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
671 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
672 setOperationAction(ISD::VSETCC, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000673 }
674
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000675 if (!UseSoftFloat && Subtarget->hasXMMInt()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000676 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000677
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000678 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
679 // registers cannot be used even for integer operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000680 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
681 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
682 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
683 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000684
Owen Anderson825b72b2009-08-11 20:47:22 +0000685 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
686 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
687 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
688 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
689 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
690 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
691 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
692 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
693 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
694 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
695 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
696 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
697 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
698 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
699 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
700 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000701
Owen Anderson825b72b2009-08-11 20:47:22 +0000702 setOperationAction(ISD::VSETCC, MVT::v2f64, Custom);
703 setOperationAction(ISD::VSETCC, MVT::v16i8, Custom);
704 setOperationAction(ISD::VSETCC, MVT::v8i16, Custom);
705 setOperationAction(ISD::VSETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000706
Owen Anderson825b72b2009-08-11 20:47:22 +0000707 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
708 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
709 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
710 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
711 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000712
Mon P Wangeb38ebf2010-01-24 00:05:03 +0000713 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2f64, Custom);
714 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2i64, Custom);
715 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i8, Custom);
716 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i16, Custom);
717 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i32, Custom);
718
Evan Cheng2c3ae372006-04-12 21:21:57 +0000719 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Owen Anderson825b72b2009-08-11 20:47:22 +0000720 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
721 EVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000722 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000723 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000724 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000725 // Do not attempt to custom lower non-128-bit vectors
726 if (!VT.is128BitVector())
727 continue;
Owen Anderson825b72b2009-08-11 20:47:22 +0000728 setOperationAction(ISD::BUILD_VECTOR,
729 VT.getSimpleVT().SimpleTy, Custom);
730 setOperationAction(ISD::VECTOR_SHUFFLE,
731 VT.getSimpleVT().SimpleTy, Custom);
732 setOperationAction(ISD::EXTRACT_VECTOR_ELT,
733 VT.getSimpleVT().SimpleTy, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000734 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000735
Owen Anderson825b72b2009-08-11 20:47:22 +0000736 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
737 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
738 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
739 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
740 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
741 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000742
Nate Begemancdd1eec2008-02-12 22:51:28 +0000743 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000744 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
745 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000746 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000747
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000748 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Owen Anderson825b72b2009-08-11 20:47:22 +0000749 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; i++) {
750 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
Owen Andersone50ed302009-08-10 22:56:29 +0000751 EVT VT = SVT;
David Greene9b9838d2009-06-29 16:47:10 +0000752
753 // Do not attempt to promote non-128-bit vectors
Chris Lattner32b4b5a2010-07-05 05:53:14 +0000754 if (!VT.is128BitVector())
David Greene9b9838d2009-06-29 16:47:10 +0000755 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +0000756
Owen Andersond6662ad2009-08-10 20:46:15 +0000757 setOperationAction(ISD::AND, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000758 AddPromotedToType (ISD::AND, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000759 setOperationAction(ISD::OR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000760 AddPromotedToType (ISD::OR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000761 setOperationAction(ISD::XOR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000762 AddPromotedToType (ISD::XOR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000763 setOperationAction(ISD::LOAD, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000764 AddPromotedToType (ISD::LOAD, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000765 setOperationAction(ISD::SELECT, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000766 AddPromotedToType (ISD::SELECT, SVT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000767 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000768
Owen Anderson825b72b2009-08-11 20:47:22 +0000769 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000770
Evan Cheng2c3ae372006-04-12 21:21:57 +0000771 // Custom lower v2i64 and v2f64 selects.
Owen Anderson825b72b2009-08-11 20:47:22 +0000772 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
773 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
774 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
775 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000776
Owen Anderson825b72b2009-08-11 20:47:22 +0000777 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
778 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000779 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000780
Nate Begeman14d12ca2008-02-11 04:19:36 +0000781 if (Subtarget->hasSSE41()) {
Dale Johannesen54feef22010-05-27 20:12:41 +0000782 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
783 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
784 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
785 setOperationAction(ISD::FRINT, MVT::f32, Legal);
786 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
787 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
788 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
789 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
790 setOperationAction(ISD::FRINT, MVT::f64, Legal);
791 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
792
Nate Begeman14d12ca2008-02-11 04:19:36 +0000793 // FIXME: Do we need to handle scalar-to-vector here?
Owen Anderson825b72b2009-08-11 20:47:22 +0000794 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000795
Nate Begemanbdcb5af2010-07-27 22:37:06 +0000796 // Can turn SHL into an integer multiply.
797 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
Nate Begeman51409212010-07-28 00:21:48 +0000798 setOperationAction(ISD::SHL, MVT::v16i8, Custom);
Nate Begemanbdcb5af2010-07-27 22:37:06 +0000799
Nate Begeman14d12ca2008-02-11 04:19:36 +0000800 // i8 and i16 vectors are custom , because the source register and source
801 // source memory operand types are not the same width. f32 vectors are
802 // custom since the immediate controlling the insert encodes additional
803 // information.
Owen Anderson825b72b2009-08-11 20:47:22 +0000804 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
805 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
806 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
807 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000808
Owen Anderson825b72b2009-08-11 20:47:22 +0000809 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
810 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
811 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
812 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000813
814 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000815 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
816 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000817 }
818 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000819
Chris Lattnera34b3cf2010-12-19 20:03:11 +0000820 if (Subtarget->hasSSE42())
Owen Anderson825b72b2009-08-11 20:47:22 +0000821 setOperationAction(ISD::VSETCC, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000822
David Greene9b9838d2009-06-29 16:47:10 +0000823 if (!UseSoftFloat && Subtarget->hasAVX()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000824 addRegisterClass(MVT::v8f32, X86::VR256RegisterClass);
825 addRegisterClass(MVT::v4f64, X86::VR256RegisterClass);
826 addRegisterClass(MVT::v8i32, X86::VR256RegisterClass);
827 addRegisterClass(MVT::v4i64, X86::VR256RegisterClass);
Bruno Cardoso Lopes405f11b2010-08-10 01:43:16 +0000828 addRegisterClass(MVT::v32i8, X86::VR256RegisterClass);
David Greened94c1012009-06-29 22:50:51 +0000829
Owen Anderson825b72b2009-08-11 20:47:22 +0000830 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
831 setOperationAction(ISD::LOAD, MVT::v8i32, Legal);
832 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
833 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
David Greene54d8eba2011-01-27 22:38:56 +0000834
Owen Anderson825b72b2009-08-11 20:47:22 +0000835 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
836 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
837 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
838 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
839 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
840 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000841
Owen Anderson825b72b2009-08-11 20:47:22 +0000842 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
843 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
844 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
845 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
846 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
847 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000848
David Greene54d8eba2011-01-27 22:38:56 +0000849 // Custom lower build_vector, vector_shuffle, scalar_to_vector,
850 // insert_vector_elt extract_subvector and extract_vector_elt for
851 // 256-bit types.
852 for (unsigned i = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
853 i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE;
854 ++i) {
855 MVT::SimpleValueType VT = (MVT::SimpleValueType)i;
856 // Do not attempt to custom lower non-256-bit vectors
857 if (!isPowerOf2_32(MVT(VT).getVectorNumElements())
858 || (MVT(VT).getSizeInBits() < 256))
David Greene9b9838d2009-06-29 16:47:10 +0000859 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000860 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
861 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
David Greene54d8eba2011-01-27 22:38:56 +0000862 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000863 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
David Greene54d8eba2011-01-27 22:38:56 +0000864 setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000865 }
David Greene54d8eba2011-01-27 22:38:56 +0000866 // Custom-lower insert_subvector and extract_subvector based on
867 // the result type.
868 for (unsigned i = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
869 i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE;
870 ++i) {
871 MVT::SimpleValueType VT = (MVT::SimpleValueType)i;
872 // Do not attempt to custom lower non-256-bit vectors
873 if (!isPowerOf2_32(MVT(VT).getVectorNumElements()))
David Greene9b9838d2009-06-29 16:47:10 +0000874 continue;
David Greene54d8eba2011-01-27 22:38:56 +0000875
876 if (MVT(VT).getSizeInBits() == 128) {
877 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000878 }
David Greene54d8eba2011-01-27 22:38:56 +0000879 else if (MVT(VT).getSizeInBits() == 256) {
880 setOperationAction(ISD::INSERT_SUBVECTOR, VT, Custom);
881 }
David Greene9b9838d2009-06-29 16:47:10 +0000882 }
883
David Greene54d8eba2011-01-27 22:38:56 +0000884 // Promote v32i8, v16i16, v8i32 select, and, or, xor to v4i64.
885 // Don't promote loads because we need them for VPERM vector index versions.
886
887 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
888 VT != (unsigned)MVT::LAST_VECTOR_VALUETYPE;
889 VT++) {
890 if (!isPowerOf2_32(MVT((MVT::SimpleValueType)VT).getVectorNumElements())
891 || (MVT((MVT::SimpleValueType)VT).getSizeInBits() < 256))
892 continue;
893 setOperationAction(ISD::AND, (MVT::SimpleValueType)VT, Promote);
894 AddPromotedToType (ISD::AND, (MVT::SimpleValueType)VT, MVT::v4i64);
895 setOperationAction(ISD::OR, (MVT::SimpleValueType)VT, Promote);
896 AddPromotedToType (ISD::OR, (MVT::SimpleValueType)VT, MVT::v4i64);
897 setOperationAction(ISD::XOR, (MVT::SimpleValueType)VT, Promote);
898 AddPromotedToType (ISD::XOR, (MVT::SimpleValueType)VT, MVT::v4i64);
899 //setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Promote);
900 //AddPromotedToType (ISD::LOAD, (MVT::SimpleValueType)VT, MVT::v4i64);
901 setOperationAction(ISD::SELECT, (MVT::SimpleValueType)VT, Promote);
902 AddPromotedToType (ISD::SELECT, (MVT::SimpleValueType)VT, MVT::v4i64);
903 }
David Greene9b9838d2009-06-29 16:47:10 +0000904 }
905
Evan Cheng6be2c582006-04-05 23:38:46 +0000906 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000907 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +0000908
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000909
Eli Friedman962f5492010-06-02 19:35:46 +0000910 // Only custom-lower 64-bit SADDO and friends on 64-bit because we don't
911 // handle type legalization for these operations here.
Dan Gohman71c62a22010-06-02 19:13:40 +0000912 //
Eli Friedman962f5492010-06-02 19:35:46 +0000913 // FIXME: We really should do custom legalization for addition and
914 // subtraction on x86-32 once PR3203 is fixed. We really can't do much better
915 // than generic legalization for 64-bit multiplication-with-overflow, though.
Chris Lattnera34b3cf2010-12-19 20:03:11 +0000916 for (unsigned i = 0, e = 3+Subtarget->is64Bit(); i != e; ++i) {
917 // Add/Sub/Mul with overflow operations are custom lowered.
918 MVT VT = IntVTs[i];
919 setOperationAction(ISD::SADDO, VT, Custom);
920 setOperationAction(ISD::UADDO, VT, Custom);
921 setOperationAction(ISD::SSUBO, VT, Custom);
922 setOperationAction(ISD::USUBO, VT, Custom);
923 setOperationAction(ISD::SMULO, VT, Custom);
924 setOperationAction(ISD::UMULO, VT, Custom);
Eli Friedmana993f0a2010-06-02 00:27:18 +0000925 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000926
Chris Lattnera34b3cf2010-12-19 20:03:11 +0000927 // There are no 8-bit 3-address imul/mul instructions
928 setOperationAction(ISD::SMULO, MVT::i8, Expand);
929 setOperationAction(ISD::UMULO, MVT::i8, Expand);
Bill Wendling41ea7e72008-11-24 19:21:46 +0000930
Evan Chengd54f2d52009-03-31 19:38:51 +0000931 if (!Subtarget->is64Bit()) {
932 // These libcalls are not available in 32-bit.
933 setLibcallName(RTLIB::SHL_I128, 0);
934 setLibcallName(RTLIB::SRL_I128, 0);
935 setLibcallName(RTLIB::SRA_I128, 0);
936 }
937
Evan Cheng206ee9d2006-07-07 08:33:52 +0000938 // We have target-specific dag combine patterns for the following nodes:
939 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Dan Gohman1bbf72b2010-03-15 23:23:03 +0000940 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Evan Chengd880b972008-05-09 21:53:03 +0000941 setTargetDAGCombine(ISD::BUILD_VECTOR);
Chris Lattner83e6c992006-10-04 06:57:07 +0000942 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +0000943 setTargetDAGCombine(ISD::SHL);
944 setTargetDAGCombine(ISD::SRA);
945 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +0000946 setTargetDAGCombine(ISD::OR);
Nate Begemanb65c1752010-12-17 22:55:37 +0000947 setTargetDAGCombine(ISD::AND);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +0000948 setTargetDAGCombine(ISD::ADD);
949 setTargetDAGCombine(ISD::SUB);
Chris Lattner149a4e52008-02-22 02:09:43 +0000950 setTargetDAGCombine(ISD::STORE);
Evan Cheng2e489c42009-12-16 00:53:11 +0000951 setTargetDAGCombine(ISD::ZERO_EXTEND);
Evan Cheng0b0cd912009-03-28 05:57:29 +0000952 if (Subtarget->is64Bit())
953 setTargetDAGCombine(ISD::MUL);
Evan Cheng206ee9d2006-07-07 08:33:52 +0000954
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000955 computeRegisterProperties();
956
Evan Cheng05219282011-01-06 06:52:41 +0000957 // On Darwin, -Os means optimize for size without hurting performance,
958 // do not reduce the limit.
Dan Gohman87060f52008-06-30 21:00:56 +0000959 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +0000960 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 16 : 8;
Evan Cheng255f20f2010-04-01 06:04:33 +0000961 maxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +0000962 maxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
963 maxStoresPerMemmove = 8; // For @llvm.memmove -> sequence of stores
964 maxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Evan Chengfb8075d2008-02-28 00:43:03 +0000965 setPrefLoopAlignment(16);
Evan Cheng6ebf7bc2009-05-13 21:42:09 +0000966 benefitFromCodePlacementOpt = true;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000967}
968
Scott Michel5b8f82e2008-03-10 15:42:14 +0000969
Owen Anderson825b72b2009-08-11 20:47:22 +0000970MVT::SimpleValueType X86TargetLowering::getSetCCResultType(EVT VT) const {
971 return MVT::i8;
Scott Michel5b8f82e2008-03-10 15:42:14 +0000972}
973
974
Evan Cheng29286502008-01-23 23:17:41 +0000975/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
976/// the desired ByVal argument alignment.
977static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
978 if (MaxAlign == 16)
979 return;
980 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
981 if (VTy->getBitWidth() == 128)
982 MaxAlign = 16;
Evan Cheng29286502008-01-23 23:17:41 +0000983 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
984 unsigned EltAlign = 0;
985 getMaxByValAlign(ATy->getElementType(), EltAlign);
986 if (EltAlign > MaxAlign)
987 MaxAlign = EltAlign;
988 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
989 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
990 unsigned EltAlign = 0;
991 getMaxByValAlign(STy->getElementType(i), EltAlign);
992 if (EltAlign > MaxAlign)
993 MaxAlign = EltAlign;
994 if (MaxAlign == 16)
995 break;
996 }
997 }
998 return;
999}
1000
1001/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1002/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001003/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1004/// are at 4-byte boundaries.
Evan Cheng29286502008-01-23 23:17:41 +00001005unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001006 if (Subtarget->is64Bit()) {
1007 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001008 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001009 if (TyAlign > 8)
1010 return TyAlign;
1011 return 8;
1012 }
1013
Evan Cheng29286502008-01-23 23:17:41 +00001014 unsigned Align = 4;
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001015 if (Subtarget->hasXMM())
Dale Johannesen0c191872008-02-08 19:48:20 +00001016 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001017 return Align;
1018}
Chris Lattner2b02a442007-02-25 08:29:00 +00001019
Evan Chengf0df0312008-05-15 08:39:06 +00001020/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengc3b0c342010-04-08 07:37:57 +00001021/// and store operations as a result of memset, memcpy, and memmove
1022/// lowering. If DstAlign is zero that means it's safe to destination
1023/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1024/// means there isn't a need to check it against alignment requirement,
1025/// probably because the source does not need to be loaded. If
1026/// 'NonScalarIntSafe' is true, that means it's safe to return a
1027/// non-scalar-integer type, e.g. empty string source, constant, or loaded
1028/// from memory. 'MemcpyStrSrc' indicates whether the memcpy source is
1029/// constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001030/// It returns EVT::Other if the type should be determined using generic
1031/// target-independent logic.
Owen Andersone50ed302009-08-10 22:56:29 +00001032EVT
Evan Cheng255f20f2010-04-01 06:04:33 +00001033X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1034 unsigned DstAlign, unsigned SrcAlign,
Evan Chengf28f8bc2010-04-02 19:36:14 +00001035 bool NonScalarIntSafe,
Evan Chengc3b0c342010-04-08 07:37:57 +00001036 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00001037 MachineFunction &MF) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001038 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
1039 // linux. This is because the stack realignment code can't handle certain
1040 // cases like PR2962. This should be removed when PR2962 is fixed.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001041 const Function *F = MF.getFunction();
Evan Chenga5e13622011-01-07 19:35:30 +00001042 if (NonScalarIntSafe &&
1043 !F->hasFnAttr(Attribute::NoImplicitFloat)) {
Evan Cheng255f20f2010-04-01 06:04:33 +00001044 if (Size >= 16 &&
Evan Chenga5e13622011-01-07 19:35:30 +00001045 (Subtarget->isUnalignedMemAccessFast() ||
1046 ((DstAlign == 0 || DstAlign >= 16) &&
1047 (SrcAlign == 0 || SrcAlign >= 16))) &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001048 Subtarget->getStackAlignment() >= 16) {
1049 if (Subtarget->hasSSE2())
1050 return MVT::v4i32;
Evan Chengf28f8bc2010-04-02 19:36:14 +00001051 if (Subtarget->hasSSE1())
Evan Cheng255f20f2010-04-01 06:04:33 +00001052 return MVT::v4f32;
Evan Chengc3b0c342010-04-08 07:37:57 +00001053 } else if (!MemcpyStrSrc && Size >= 8 &&
Evan Cheng3ea97552010-04-01 20:27:45 +00001054 !Subtarget->is64Bit() &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001055 Subtarget->getStackAlignment() >= 8 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001056 Subtarget->hasXMMInt()) {
Evan Chengc3b0c342010-04-08 07:37:57 +00001057 // Do not use f64 to lower memcpy if source is string constant. It's
1058 // better to use i32 to avoid the loads.
Evan Cheng255f20f2010-04-01 06:04:33 +00001059 return MVT::f64;
Evan Chengc3b0c342010-04-08 07:37:57 +00001060 }
Chris Lattner4002a1b2008-10-28 05:49:35 +00001061 }
Evan Chengf0df0312008-05-15 08:39:06 +00001062 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001063 return MVT::i64;
1064 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001065}
1066
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001067/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1068/// current function. The returned value is a member of the
1069/// MachineJumpTableInfo::JTEntryKind enum.
1070unsigned X86TargetLowering::getJumpTableEncoding() const {
1071 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1072 // symbol.
1073 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1074 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001075 return MachineJumpTableInfo::EK_Custom32;
Michael J. Spencerec38de22010-10-10 22:04:20 +00001076
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001077 // Otherwise, use the normal jump table encoding heuristics.
1078 return TargetLowering::getJumpTableEncoding();
1079}
1080
Chris Lattnerc64daab2010-01-26 05:02:42 +00001081const MCExpr *
1082X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1083 const MachineBasicBlock *MBB,
1084 unsigned uid,MCContext &Ctx) const{
1085 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1086 Subtarget->isPICStyleGOT());
1087 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1088 // entries.
Daniel Dunbar4e815f82010-03-15 23:51:06 +00001089 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1090 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001091}
1092
Evan Chengcc415862007-11-09 01:32:10 +00001093/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1094/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001095SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001096 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001097 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001098 // This doesn't have DebugLoc associated with it, but is not really the
1099 // same as a Register.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001100 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001101 return Table;
1102}
1103
Chris Lattner589c6f62010-01-26 06:28:43 +00001104/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1105/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1106/// MCExpr.
1107const MCExpr *X86TargetLowering::
1108getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1109 MCContext &Ctx) const {
1110 // X86-64 uses RIP relative addressing based on the jump table label.
1111 if (Subtarget->isPICStyleRIPRel())
1112 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1113
1114 // Otherwise, the reference is relative to the PIC base.
Chris Lattner142b5312010-11-14 22:48:15 +00001115 return MCSymbolRefExpr::Create(MF->getPICBaseSymbol(), Ctx);
Chris Lattner589c6f62010-01-26 06:28:43 +00001116}
1117
Bill Wendlingb4202b82009-07-01 18:50:55 +00001118/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +00001119unsigned X86TargetLowering::getFunctionAlignment(const Function *F) const {
Dan Gohman25103a22009-08-18 00:20:06 +00001120 return F->hasFnAttr(Attribute::OptimizeForSize) ? 0 : 4;
Bill Wendling20c568f2009-06-30 22:38:32 +00001121}
1122
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001123// FIXME: Why this routine is here? Move to RegInfo!
Evan Chengdee81012010-07-26 21:50:05 +00001124std::pair<const TargetRegisterClass*, uint8_t>
1125X86TargetLowering::findRepresentativeClass(EVT VT) const{
1126 const TargetRegisterClass *RRC = 0;
1127 uint8_t Cost = 1;
1128 switch (VT.getSimpleVT().SimpleTy) {
1129 default:
1130 return TargetLowering::findRepresentativeClass(VT);
1131 case MVT::i8: case MVT::i16: case MVT::i32: case MVT::i64:
1132 RRC = (Subtarget->is64Bit()
1133 ? X86::GR64RegisterClass : X86::GR32RegisterClass);
1134 break;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001135 case MVT::x86mmx:
Evan Chengdee81012010-07-26 21:50:05 +00001136 RRC = X86::VR64RegisterClass;
1137 break;
1138 case MVT::f32: case MVT::f64:
1139 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1140 case MVT::v4f32: case MVT::v2f64:
1141 case MVT::v32i8: case MVT::v8i32: case MVT::v4i64: case MVT::v8f32:
1142 case MVT::v4f64:
1143 RRC = X86::VR128RegisterClass;
1144 break;
1145 }
1146 return std::make_pair(RRC, Cost);
1147}
1148
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001149// FIXME: Why this routine is here? Move to RegInfo!
Evan Cheng70017e42010-07-24 00:39:05 +00001150unsigned
1151X86TargetLowering::getRegPressureLimit(const TargetRegisterClass *RC,
1152 MachineFunction &MF) const {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001153 const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
Anton Korobeynikovd0c38172010-11-18 21:19:35 +00001154
1155 unsigned FPDiff = TFI->hasFP(MF) ? 1 : 0;
Evan Cheng70017e42010-07-24 00:39:05 +00001156 switch (RC->getID()) {
1157 default:
1158 return 0;
1159 case X86::GR32RegClassID:
1160 return 4 - FPDiff;
1161 case X86::GR64RegClassID:
1162 return 8 - FPDiff;
1163 case X86::VR128RegClassID:
1164 return Subtarget->is64Bit() ? 10 : 4;
1165 case X86::VR64RegClassID:
1166 return 4;
1167 }
1168}
1169
Eric Christopherf7a0c7b2010-07-06 05:18:56 +00001170bool X86TargetLowering::getStackCookieLocation(unsigned &AddressSpace,
1171 unsigned &Offset) const {
1172 if (!Subtarget->isTargetLinux())
1173 return false;
1174
1175 if (Subtarget->is64Bit()) {
1176 // %fs:0x28, unless we're using a Kernel code model, in which case it's %gs:
1177 Offset = 0x28;
1178 if (getTargetMachine().getCodeModel() == CodeModel::Kernel)
1179 AddressSpace = 256;
1180 else
1181 AddressSpace = 257;
1182 } else {
1183 // %gs:0x14 on i386
1184 Offset = 0x14;
1185 AddressSpace = 256;
1186 }
1187 return true;
1188}
1189
1190
Chris Lattner2b02a442007-02-25 08:29:00 +00001191//===----------------------------------------------------------------------===//
1192// Return Value Calling Convention Implementation
1193//===----------------------------------------------------------------------===//
1194
Chris Lattner59ed56b2007-02-28 04:55:35 +00001195#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001196
Michael J. Spencerec38de22010-10-10 22:04:20 +00001197bool
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001198X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv, bool isVarArg,
Dan Gohman84023e02010-07-10 09:00:22 +00001199 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001200 LLVMContext &Context) const {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001201 SmallVector<CCValAssign, 16> RVLocs;
1202 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001203 RVLocs, Context);
Dan Gohman84023e02010-07-10 09:00:22 +00001204 return CCInfo.CheckReturn(Outs, RetCC_X86);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001205}
1206
Dan Gohman98ca4f22009-08-05 01:29:28 +00001207SDValue
1208X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001209 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001210 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001211 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001212 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001213 MachineFunction &MF = DAG.getMachineFunction();
1214 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001215
Chris Lattner9774c912007-02-27 05:28:59 +00001216 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001217 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1218 RVLocs, *DAG.getContext());
1219 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001220
Evan Chengdcea1632010-02-04 02:40:39 +00001221 // Add the regs to the liveout set for the function.
1222 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1223 for (unsigned i = 0; i != RVLocs.size(); ++i)
1224 if (RVLocs[i].isRegLoc() && !MRI.isLiveOut(RVLocs[i].getLocReg()))
1225 MRI.addLiveOut(RVLocs[i].getLocReg());
Scott Michelfdc40a02009-02-17 22:15:04 +00001226
Dan Gohman475871a2008-07-27 21:46:04 +00001227 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001228
Dan Gohman475871a2008-07-27 21:46:04 +00001229 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001230 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1231 // Operand #1 = Bytes To Pop
Dan Gohman1e93df62010-04-17 14:41:14 +00001232 RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1233 MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001234
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001235 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001236 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1237 CCValAssign &VA = RVLocs[i];
1238 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohmanc9403652010-07-07 15:54:55 +00001239 SDValue ValToCopy = OutVals[i];
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001240 EVT ValVT = ValToCopy.getValueType();
1241
Dale Johannesenc4510512010-09-24 19:05:48 +00001242 // If this is x86-64, and we disabled SSE, we can't return FP values,
1243 // or SSE or MMX vectors.
1244 if ((ValVT == MVT::f32 || ValVT == MVT::f64 ||
1245 VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001246 (Subtarget->is64Bit() && !Subtarget->hasXMM())) {
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001247 report_fatal_error("SSE register return with SSE disabled");
1248 }
1249 // Likewise we can't return F64 values with SSE1 only. gcc does so, but
1250 // llvm-gcc has never done it right and no one has noticed, so this
1251 // should be OK for now.
1252 if (ValVT == MVT::f64 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001253 (Subtarget->is64Bit() && !Subtarget->hasXMMInt()))
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001254 report_fatal_error("SSE2 register return with SSE2 disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001255
Chris Lattner447ff682008-03-11 03:23:40 +00001256 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1257 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001258 if (VA.getLocReg() == X86::ST0 ||
1259 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001260 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1261 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001262 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001263 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001264 RetOps.push_back(ValToCopy);
1265 // Don't emit a copytoreg.
1266 continue;
1267 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001268
Evan Cheng242b38b2009-02-23 09:03:22 +00001269 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1270 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001271 if (Subtarget->is64Bit()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001272 if (ValVT == MVT::x86mmx) {
Chris Lattner97a2a562010-08-26 05:24:29 +00001273 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001274 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ValToCopy);
Eric Christopher90eb4022010-07-22 00:26:08 +00001275 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
1276 ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001277 // If we don't have SSE2 available, convert to v4f32 so the generated
1278 // register is legal.
1279 if (!Subtarget->hasSSE2())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001280 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32,ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001281 }
Evan Cheng242b38b2009-02-23 09:03:22 +00001282 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001283 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00001284
Dale Johannesendd64c412009-02-04 00:33:20 +00001285 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001286 Flag = Chain.getValue(1);
1287 }
Dan Gohman61a92132008-04-21 23:59:07 +00001288
1289 // The x86-64 ABI for returning structs by value requires that we copy
1290 // the sret argument into %rax for the return. We saved the argument into
1291 // a virtual register in the entry block, so now we copy the value out
1292 // and into %rax.
1293 if (Subtarget->is64Bit() &&
1294 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1295 MachineFunction &MF = DAG.getMachineFunction();
1296 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1297 unsigned Reg = FuncInfo->getSRetReturnReg();
Michael J. Spencerec38de22010-10-10 22:04:20 +00001298 assert(Reg &&
Zhongxing Xuc2798a12010-05-26 08:10:02 +00001299 "SRetReturnReg should have been set in LowerFormalArguments().");
Dale Johannesendd64c412009-02-04 00:33:20 +00001300 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001301
Dale Johannesendd64c412009-02-04 00:33:20 +00001302 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001303 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001304
1305 // RAX now acts like a return value.
Evan Chengdcea1632010-02-04 02:40:39 +00001306 MRI.addLiveOut(X86::RAX);
Dan Gohman61a92132008-04-21 23:59:07 +00001307 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001308
Chris Lattner447ff682008-03-11 03:23:40 +00001309 RetOps[0] = Chain; // Update chain.
1310
1311 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001312 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001313 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001314
1315 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001316 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001317}
1318
Evan Cheng3d2125c2010-11-30 23:55:39 +00001319bool X86TargetLowering::isUsedByReturnOnly(SDNode *N) const {
1320 if (N->getNumValues() != 1)
1321 return false;
1322 if (!N->hasNUsesOfValue(1, 0))
1323 return false;
1324
1325 SDNode *Copy = *N->use_begin();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001326 if (Copy->getOpcode() != ISD::CopyToReg &&
1327 Copy->getOpcode() != ISD::FP_EXTEND)
Evan Cheng3d2125c2010-11-30 23:55:39 +00001328 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001329
1330 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001331 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001332 UI != UE; ++UI) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001333 if (UI->getOpcode() != X86ISD::RET_FLAG)
1334 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001335 HasRet = true;
1336 }
Evan Cheng3d2125c2010-11-30 23:55:39 +00001337
Evan Cheng1bf891a2010-12-01 22:59:46 +00001338 return HasRet;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001339}
1340
Dan Gohman98ca4f22009-08-05 01:29:28 +00001341/// LowerCallResult - Lower the result values of a call into the
1342/// appropriate copies out of appropriate physical registers.
1343///
1344SDValue
1345X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001346 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001347 const SmallVectorImpl<ISD::InputArg> &Ins,
1348 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001349 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001350
Chris Lattnere32bbf62007-02-28 07:09:55 +00001351 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001352 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001353 bool Is64Bit = Subtarget->is64Bit();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001354 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +00001355 RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001356 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001357
Chris Lattner3085e152007-02-25 08:59:22 +00001358 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001359 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001360 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001361 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001362
Torok Edwin3f142c32009-02-01 18:15:56 +00001363 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001364 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001365 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasXMM())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001366 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001367 }
1368
Evan Cheng79fb3b42009-02-20 20:43:02 +00001369 SDValue Val;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001370
1371 // If this is a call to a function that returns an fp value on the floating
1372 // point stack, we must guarantee the the value is popped from the stack, so
1373 // a CopyFromReg is not good enough - the copy instruction may be eliminated
1374 // if the return value is not used. We use the FpGET_ST0 instructions
1375 // instead.
1376 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1) {
1377 // If we prefer to use the value in xmm registers, copy it out as f80 and
1378 // use a truncate to move it from fp stack reg to xmm reg.
1379 if (isScalarFPTypeInSSEReg(VA.getValVT())) CopyVT = MVT::f80;
1380 bool isST0 = VA.getLocReg() == X86::ST0;
1381 unsigned Opc = 0;
1382 if (CopyVT == MVT::f32) Opc = isST0 ? X86::FpGET_ST0_32:X86::FpGET_ST1_32;
1383 if (CopyVT == MVT::f64) Opc = isST0 ? X86::FpGET_ST0_64:X86::FpGET_ST1_64;
1384 if (CopyVT == MVT::f80) Opc = isST0 ? X86::FpGET_ST0_80:X86::FpGET_ST1_80;
1385 SDValue Ops[] = { Chain, InFlag };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001386 Chain = SDValue(DAG.getMachineNode(Opc, dl, CopyVT, MVT::Other, MVT::Glue,
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001387 Ops, 2), 1);
1388 Val = Chain.getValue(0);
1389
1390 // Round the f80 to the right size, which also moves it to the appropriate
1391 // xmm register.
1392 if (CopyVT != VA.getValVT())
1393 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
1394 // This truncation won't change the value.
1395 DAG.getIntPtrConstant(1));
1396 } else if (Is64Bit && CopyVT.isVector() && CopyVT.getSizeInBits() == 64) {
Evan Cheng242b38b2009-02-23 09:03:22 +00001397 // For x86-64, MMX values are returned in XMM0 / XMM1 except for v1i64.
1398 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
1399 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001400 MVT::v2i64, InFlag).getValue(1);
Evan Cheng242b38b2009-02-23 09:03:22 +00001401 Val = Chain.getValue(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00001402 Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64,
1403 Val, DAG.getConstant(0, MVT::i64));
Evan Cheng242b38b2009-02-23 09:03:22 +00001404 } else {
1405 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001406 MVT::i64, InFlag).getValue(1);
Evan Cheng242b38b2009-02-23 09:03:22 +00001407 Val = Chain.getValue(0);
1408 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001409 Val = DAG.getNode(ISD::BITCAST, dl, CopyVT, Val);
Evan Cheng79fb3b42009-02-20 20:43:02 +00001410 } else {
1411 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1412 CopyVT, InFlag).getValue(1);
1413 Val = Chain.getValue(0);
1414 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001415 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001416 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001417 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001418
Dan Gohman98ca4f22009-08-05 01:29:28 +00001419 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001420}
1421
1422
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001423//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001424// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001425//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001426// StdCall calling convention seems to be standard for many Windows' API
1427// routines and around. It differs from C calling convention just a little:
1428// callee should clean up the stack, not caller. Symbols should be also
1429// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001430// For info on fast calling convention see Fast Calling Convention (tail call)
1431// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001432
Dan Gohman98ca4f22009-08-05 01:29:28 +00001433/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001434/// semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001435static bool CallIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
1436 if (Outs.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001437 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001438
Dan Gohman98ca4f22009-08-05 01:29:28 +00001439 return Outs[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001440}
1441
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001442/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001443/// return semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001444static bool
1445ArgsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
1446 if (Ins.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001447 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001448
Dan Gohman98ca4f22009-08-05 01:29:28 +00001449 return Ins[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001450}
1451
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001452/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1453/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001454/// the specific parameter attribute. The copy will be passed as a byval
1455/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001456static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001457CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001458 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1459 DebugLoc dl) {
Chris Lattnere72f2022010-09-21 05:40:29 +00001460 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Michael J. Spencerec38de22010-10-10 22:04:20 +00001461
Dale Johannesendd64c412009-02-04 00:33:20 +00001462 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Mon P Wang20adc9d2010-04-04 03:10:48 +00001463 /*isVolatile*/false, /*AlwaysInline=*/true,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001464 MachinePointerInfo(), MachinePointerInfo());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001465}
1466
Chris Lattner29689432010-03-11 00:22:57 +00001467/// IsTailCallConvention - Return true if the calling convention is one that
1468/// supports tail call optimization.
1469static bool IsTailCallConvention(CallingConv::ID CC) {
1470 return (CC == CallingConv::Fast || CC == CallingConv::GHC);
1471}
1472
Evan Cheng0c439eb2010-01-27 00:07:07 +00001473/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1474/// a tailcall target by changing its ABI.
1475static bool FuncIsMadeTailCallSafe(CallingConv::ID CC) {
Chris Lattner29689432010-03-11 00:22:57 +00001476 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001477}
1478
Dan Gohman98ca4f22009-08-05 01:29:28 +00001479SDValue
1480X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001481 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001482 const SmallVectorImpl<ISD::InputArg> &Ins,
1483 DebugLoc dl, SelectionDAG &DAG,
1484 const CCValAssign &VA,
1485 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00001486 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00001487 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001488 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Evan Cheng0c439eb2010-01-27 00:07:07 +00001489 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001490 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001491 EVT ValVT;
1492
1493 // If value is passed by pointer we have address passed instead of the value
1494 // itself.
1495 if (VA.getLocInfo() == CCValAssign::Indirect)
1496 ValVT = VA.getLocVT();
1497 else
1498 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001499
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001500 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001501 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001502 // In case of tail call optimization mark all arguments mutable. Since they
1503 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001504 if (Flags.isByVal()) {
1505 int FI = MFI->CreateFixedObject(Flags.getByValSize(),
Evan Chenged2ae132010-07-03 00:40:23 +00001506 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001507 return DAG.getFrameIndex(FI, getPointerTy());
1508 } else {
1509 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00001510 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001511 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1512 return DAG.getLoad(ValVT, dl, Chain, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001513 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00001514 false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001515 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001516}
1517
Dan Gohman475871a2008-07-27 21:46:04 +00001518SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001519X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001520 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001521 bool isVarArg,
1522 const SmallVectorImpl<ISD::InputArg> &Ins,
1523 DebugLoc dl,
1524 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001525 SmallVectorImpl<SDValue> &InVals)
1526 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00001527 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001528 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001529
Gordon Henriksen86737662008-01-05 16:56:59 +00001530 const Function* Fn = MF.getFunction();
1531 if (Fn->hasExternalLinkage() &&
1532 Subtarget->isTargetCygMing() &&
1533 Fn->getName() == "main")
1534 FuncInfo->setForceFramePointer(true);
1535
Evan Cheng1bc78042006-04-26 01:20:17 +00001536 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001537 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001538 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001539
Chris Lattner29689432010-03-11 00:22:57 +00001540 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1541 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001542
Chris Lattner638402b2007-02-28 07:00:42 +00001543 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001544 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001545 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1546 ArgLocs, *DAG.getContext());
Duncan Sands45907662010-10-31 13:21:44 +00001547 CCInfo.AnalyzeFormalArguments(Ins, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001548
Chris Lattnerf39f7712007-02-28 05:46:49 +00001549 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001550 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001551 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1552 CCValAssign &VA = ArgLocs[i];
1553 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1554 // places.
1555 assert(VA.getValNo() != LastVal &&
1556 "Don't support value assigned to multiple locs yet");
1557 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001558
Chris Lattnerf39f7712007-02-28 05:46:49 +00001559 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001560 EVT RegVT = VA.getLocVT();
Devang Patel8a84e442009-01-05 17:31:22 +00001561 TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001562 if (RegVT == MVT::i32)
Chris Lattnerf39f7712007-02-28 05:46:49 +00001563 RC = X86::GR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001564 else if (Is64Bit && RegVT == MVT::i64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001565 RC = X86::GR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001566 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001567 RC = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001568 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001569 RC = X86::FR64RegisterClass;
Bruno Cardoso Lopesac098352010-08-05 23:35:51 +00001570 else if (RegVT.isVector() && RegVT.getSizeInBits() == 256)
1571 RC = X86::VR256RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001572 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001573 RC = X86::VR128RegisterClass;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001574 else if (RegVT == MVT::x86mmx)
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001575 RC = X86::VR64RegisterClass;
1576 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001577 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001578
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001579 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001580 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001581
Chris Lattnerf39f7712007-02-28 05:46:49 +00001582 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1583 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1584 // right size.
1585 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001586 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001587 DAG.getValueType(VA.getValVT()));
1588 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001589 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001590 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001591 else if (VA.getLocInfo() == CCValAssign::BCvt)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001592 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001593
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001594 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001595 // Handle MMX values passed in XMM regs.
1596 if (RegVT.isVector()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001597 ArgValue = DAG.getNode(X86ISD::MOVDQ2Q, dl, VA.getValVT(),
1598 ArgValue);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001599 } else
1600 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001601 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001602 } else {
1603 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001604 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001605 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001606
1607 // If value is passed via pointer - do a load.
1608 if (VA.getLocInfo() == CCValAssign::Indirect)
Chris Lattner51abfe42010-09-21 06:02:19 +00001609 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue,
1610 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001611
Dan Gohman98ca4f22009-08-05 01:29:28 +00001612 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001613 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001614
Dan Gohman61a92132008-04-21 23:59:07 +00001615 // The x86-64 ABI for returning structs by value requires that we copy
1616 // the sret argument into %rax for the return. Save the argument into
1617 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001618 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001619 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1620 unsigned Reg = FuncInfo->getSRetReturnReg();
1621 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001622 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001623 FuncInfo->setSRetReturnReg(Reg);
1624 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001625 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001626 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001627 }
1628
Chris Lattnerf39f7712007-02-28 05:46:49 +00001629 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00001630 // Align stack specially for tail calls.
1631 if (FuncIsMadeTailCallSafe(CallConv))
Gordon Henriksenae636f82008-01-03 16:47:34 +00001632 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001633
Evan Cheng1bc78042006-04-26 01:20:17 +00001634 // If the function takes variable number of arguments, make a frame index for
1635 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001636 if (isVarArg) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001637 if (!IsWin64 && (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
1638 CallConv != CallingConv::X86_ThisCall))) {
Jakob Stoklund Olesenb2eeed72010-07-29 17:42:27 +00001639 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,true));
Gordon Henriksen86737662008-01-05 16:56:59 +00001640 }
1641 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001642 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1643
1644 // FIXME: We should really autogenerate these arrays
1645 static const unsigned GPR64ArgRegsWin64[] = {
1646 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001647 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001648 static const unsigned GPR64ArgRegs64Bit[] = {
1649 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1650 };
1651 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001652 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1653 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1654 };
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001655 const unsigned *GPR64ArgRegs;
1656 unsigned NumXMMRegs = 0;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001657
1658 if (IsWin64) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001659 // The XMM registers which might contain var arg parameters are shadowed
1660 // in their paired GPR. So we only need to save the GPR to their home
1661 // slots.
1662 TotalNumIntRegs = 4;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001663 GPR64ArgRegs = GPR64ArgRegsWin64;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001664 } else {
1665 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1666 GPR64ArgRegs = GPR64ArgRegs64Bit;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001667
1668 NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs64Bit, TotalNumXMMRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001669 }
1670 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1671 TotalNumIntRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001672
Devang Patel578efa92009-06-05 21:57:13 +00001673 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001674 assert(!(NumXMMRegs && !Subtarget->hasXMM()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001675 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001676 assert(!(NumXMMRegs && UseSoftFloat && NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00001677 "SSE register cannot be used when SSE is disabled!");
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001678 if (UseSoftFloat || NoImplicitFloatOps || !Subtarget->hasXMM())
Torok Edwin3f142c32009-02-01 18:15:56 +00001679 // Kernel mode asks for SSE to be disabled, so don't push them
1680 // on the stack.
1681 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001682
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001683 if (IsWin64) {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001684 const TargetFrameLowering &TFI = *getTargetMachine().getFrameLowering();
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001685 // Get to the caller-allocated home save location. Add 8 to account
1686 // for the return address.
1687 int HomeOffset = TFI.getOffsetOfLocalArea() + 8;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001688 FuncInfo->setRegSaveFrameIndex(
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001689 MFI->CreateFixedObject(1, NumIntRegs * 8 + HomeOffset, false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001690 FuncInfo->setVarArgsFrameIndex(FuncInfo->getRegSaveFrameIndex());
1691 } else {
1692 // For X86-64, if there are vararg parameters that are passed via
1693 // registers, then we must store them to their spots on the stack so they
1694 // may be loaded by deferencing the result of va_next.
1695 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
1696 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
1697 FuncInfo->setRegSaveFrameIndex(
1698 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
Dan Gohman1e93df62010-04-17 14:41:14 +00001699 false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001700 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001701
Gordon Henriksen86737662008-01-05 16:56:59 +00001702 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001703 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00001704 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
1705 getPointerTy());
1706 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001707 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00001708 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
1709 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00001710 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
1711 X86::GR64RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00001712 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00001713 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001714 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001715 MachinePointerInfo::getFixedStack(
1716 FuncInfo->getRegSaveFrameIndex(), Offset),
1717 false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001718 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001719 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001720 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001721
Dan Gohmanface41a2009-08-16 21:24:25 +00001722 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
1723 // Now store the XMM (fp + vector) parameter registers.
1724 SmallVector<SDValue, 11> SaveXMMOps;
1725 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001726
Dan Gohmanface41a2009-08-16 21:24:25 +00001727 unsigned AL = MF.addLiveIn(X86::AL, X86::GR8RegisterClass);
1728 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
1729 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001730
Dan Gohman1e93df62010-04-17 14:41:14 +00001731 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1732 FuncInfo->getRegSaveFrameIndex()));
1733 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1734 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00001735
Dan Gohmanface41a2009-08-16 21:24:25 +00001736 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001737 unsigned VReg = MF.addLiveIn(XMMArgRegs64Bit[NumXMMRegs],
Dan Gohmanface41a2009-08-16 21:24:25 +00001738 X86::VR128RegisterClass);
1739 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
1740 SaveXMMOps.push_back(Val);
1741 }
1742 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
1743 MVT::Other,
1744 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00001745 }
Dan Gohmanface41a2009-08-16 21:24:25 +00001746
1747 if (!MemOps.empty())
1748 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1749 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001750 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001751 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001752
Gordon Henriksen86737662008-01-05 16:56:59 +00001753 // Some CCs need callee pop.
Dan Gohman4d3d6e12010-05-27 18:43:40 +00001754 if (Subtarget->IsCalleePop(isVarArg, CallConv)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001755 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001756 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00001757 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001758 // If this is an sret function, the return should pop the hidden pointer.
Chris Lattner29689432010-03-11 00:22:57 +00001759 if (!Is64Bit && !IsTailCallConvention(CallConv) && ArgsAreStructReturn(Ins))
Dan Gohman1e93df62010-04-17 14:41:14 +00001760 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001761 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001762
Gordon Henriksen86737662008-01-05 16:56:59 +00001763 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001764 // RegSaveFrameIndex is X86-64 only.
1765 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Anton Korobeynikovded05e32010-05-16 09:08:45 +00001766 if (CallConv == CallingConv::X86_FastCall ||
1767 CallConv == CallingConv::X86_ThisCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00001768 // fastcc functions can't have varargs.
1769 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00001770 }
Evan Cheng25caf632006-05-23 21:06:34 +00001771
Dan Gohman98ca4f22009-08-05 01:29:28 +00001772 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001773}
1774
Dan Gohman475871a2008-07-27 21:46:04 +00001775SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001776X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
1777 SDValue StackPtr, SDValue Arg,
1778 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00001779 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001780 ISD::ArgFlagsTy Flags) const {
Anton Korobeynikovc7c62bb2010-09-02 22:31:32 +00001781 const unsigned FirstStackArgOffset = (Subtarget->isTargetWin64() ? 32 : 0);
1782 unsigned LocMemOffset = FirstStackArgOffset + VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00001783 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00001784 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001785 if (Flags.isByVal())
Dale Johannesendd64c412009-02-04 00:33:20 +00001786 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001787
1788 return DAG.getStore(Chain, dl, Arg, PtrOff,
1789 MachinePointerInfo::getStack(LocMemOffset),
David Greene67c9d422010-02-15 16:53:33 +00001790 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00001791}
1792
Bill Wendling64e87322009-01-16 19:25:27 +00001793/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001794/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00001795SDValue
1796X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00001797 SDValue &OutRetAddr, SDValue Chain,
1798 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00001799 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001800 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00001801 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001802 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00001803
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001804 // Load the "old" Return address.
Chris Lattner51abfe42010-09-21 06:02:19 +00001805 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, MachinePointerInfo(),
1806 false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001807 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001808}
1809
1810/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1811/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00001812static SDValue
1813EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00001814 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00001815 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001816 // Store the return address to the appropriate stack slot.
1817 if (!FPDiff) return Chain;
1818 // Calculate the new stack slot for the return address.
1819 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00001820 int NewReturnAddrFI =
Evan Chenged2ae132010-07-03 00:40:23 +00001821 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false);
Owen Anderson825b72b2009-08-11 20:47:22 +00001822 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00001823 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001824 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00001825 MachinePointerInfo::getFixedStack(NewReturnAddrFI),
David Greene67c9d422010-02-15 16:53:33 +00001826 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001827 return Chain;
1828}
1829
Dan Gohman98ca4f22009-08-05 01:29:28 +00001830SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001831X86TargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001832 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001833 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001834 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001835 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001836 const SmallVectorImpl<ISD::InputArg> &Ins,
1837 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001838 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001839 MachineFunction &MF = DAG.getMachineFunction();
1840 bool Is64Bit = Subtarget->is64Bit();
1841 bool IsStructRet = CallIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00001842 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001843
Evan Cheng5f941932010-02-05 02:21:12 +00001844 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00001845 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00001846 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1847 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001848 Outs, OutVals, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00001849
1850 // Sibcalls are automatically detected tailcalls which do not require
1851 // ABI changes.
Dan Gohman1797ed52010-02-08 20:27:50 +00001852 if (!GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00001853 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00001854
1855 if (isTailCall)
1856 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00001857 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00001858
Chris Lattner29689432010-03-11 00:22:57 +00001859 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1860 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001861
Chris Lattner638402b2007-02-28 07:00:42 +00001862 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00001863 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001864 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1865 ArgLocs, *DAG.getContext());
Duncan Sands45907662010-10-31 13:21:44 +00001866 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001867
Chris Lattner423c5f42007-02-28 05:31:48 +00001868 // Get a count of how many bytes are to be pushed on the stack.
1869 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00001870 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00001871 // This is a sibcall. The memory operands are available in caller's
1872 // own caller's stack.
1873 NumBytes = 0;
Chris Lattner29689432010-03-11 00:22:57 +00001874 else if (GuaranteedTailCallOpt && IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00001875 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001876
Gordon Henriksen86737662008-01-05 16:56:59 +00001877 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00001878 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001879 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00001880 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00001881 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1882 FPDiff = NumBytesCallerPushed - NumBytes;
1883
1884 // Set the delta of movement of the returnaddr stackslot.
1885 // But only set if delta is greater than previous delta.
1886 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1887 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1888 }
1889
Evan Chengf22f9b32010-02-06 03:28:46 +00001890 if (!IsSibcall)
1891 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001892
Dan Gohman475871a2008-07-27 21:46:04 +00001893 SDValue RetAddrFrIdx;
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001894 // Load return adress for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00001895 if (isTailCall && FPDiff)
1896 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
1897 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00001898
Dan Gohman475871a2008-07-27 21:46:04 +00001899 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
1900 SmallVector<SDValue, 8> MemOpChains;
1901 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00001902
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001903 // Walk the register/memloc assignments, inserting copies/loads. In the case
1904 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00001905 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1906 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001907 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00001908 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00001909 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00001910 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00001911
Chris Lattner423c5f42007-02-28 05:31:48 +00001912 // Promote the value if needed.
1913 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001914 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00001915 case CCValAssign::Full: break;
1916 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001917 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001918 break;
1919 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001920 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001921 break;
1922 case CCValAssign::AExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001923 if (RegVT.isVector() && RegVT.getSizeInBits() == 128) {
1924 // Special case: passing MMX values in XMM registers.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001925 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00001926 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
1927 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001928 } else
1929 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
1930 break;
1931 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001932 Arg = DAG.getNode(ISD::BITCAST, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001933 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001934 case CCValAssign::Indirect: {
1935 // Store the argument.
1936 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00001937 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001938 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00001939 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00001940 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001941 Arg = SpillSlot;
1942 break;
1943 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00001944 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001945
Chris Lattner423c5f42007-02-28 05:31:48 +00001946 if (VA.isRegLoc()) {
1947 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00001948 if (isVarArg && Subtarget->isTargetWin64()) {
1949 // Win64 ABI requires argument XMM reg to be copied to the corresponding
1950 // shadow reg if callee is a varargs function.
1951 unsigned ShadowReg = 0;
1952 switch (VA.getLocReg()) {
1953 case X86::XMM0: ShadowReg = X86::RCX; break;
1954 case X86::XMM1: ShadowReg = X86::RDX; break;
1955 case X86::XMM2: ShadowReg = X86::R8; break;
1956 case X86::XMM3: ShadowReg = X86::R9; break;
1957 }
1958 if (ShadowReg)
1959 RegsToPass.push_back(std::make_pair(ShadowReg, Arg));
1960 }
Evan Chengf22f9b32010-02-06 03:28:46 +00001961 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00001962 assert(VA.isMemLoc());
1963 if (StackPtr.getNode() == 0)
1964 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
1965 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1966 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001967 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001968 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001969
Evan Cheng32fe1032006-05-25 00:59:30 +00001970 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001971 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001972 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001973
Evan Cheng347d5f72006-04-28 21:29:37 +00001974 // Build a sequence of copy-to-reg nodes chained together with token chain
1975 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001976 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001977 // Tail call byval lowering might overwrite argument registers so in case of
1978 // tail call optimization the copies to registers are lowered later.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001979 if (!isTailCall)
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001980 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001981 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00001982 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001983 InFlag = Chain.getValue(1);
1984 }
Gordon Henriksen86737662008-01-05 16:56:59 +00001985
Chris Lattner88e1fd52009-07-09 04:24:46 +00001986 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00001987 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1988 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001989 if (!isTailCall) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00001990 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
1991 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001992 DebugLoc(), getPointerTy()),
Chris Lattnerb133a0a2009-07-09 02:55:47 +00001993 InFlag);
1994 InFlag = Chain.getValue(1);
1995 } else {
1996 // If we are tail calling and generating PIC/GOT style code load the
1997 // address of the callee into ECX. The value in ecx is used as target of
1998 // the tail jump. This is done to circumvent the ebx/callee-saved problem
1999 // for tail calls on PIC/GOT architectures. Normally we would just put the
2000 // address of GOT into ebx and then call target@PLT. But for tail calls
2001 // ebx would be restored (since ebx is callee saved) before jumping to the
2002 // target@PLT.
2003
2004 // Note: The actual moving to ECX is done further down.
2005 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
2006 if (G && !G->getGlobal()->hasHiddenVisibility() &&
2007 !G->getGlobal()->hasProtectedVisibility())
2008 Callee = LowerGlobalAddress(Callee, DAG);
2009 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00002010 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002011 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002012 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002013
Nate Begemanc8ea6732010-07-21 20:49:52 +00002014 if (Is64Bit && isVarArg && !Subtarget->isTargetWin64()) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002015 // From AMD64 ABI document:
2016 // For calls that may call functions that use varargs or stdargs
2017 // (prototype-less calls or calls to functions containing ellipsis (...) in
2018 // the declaration) %al is used as hidden argument to specify the number
2019 // of SSE registers used. The contents of %al do not need to match exactly
2020 // the number of registers, but must be an ubound on the number of SSE
2021 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002022
Gordon Henriksen86737662008-01-05 16:56:59 +00002023 // Count the number of XMM registers allocated.
2024 static const unsigned XMMArgRegs[] = {
2025 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2026 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2027 };
2028 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00002029 assert((Subtarget->hasXMM() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00002030 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00002031
Dale Johannesendd64c412009-02-04 00:33:20 +00002032 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Owen Anderson825b72b2009-08-11 20:47:22 +00002033 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002034 InFlag = Chain.getValue(1);
2035 }
2036
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002037
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002038 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002039 if (isTailCall) {
2040 // Force all the incoming stack arguments to be loaded from the stack
2041 // before any new outgoing arguments are stored to the stack, because the
2042 // outgoing stack slots may alias the incoming argument stack slots, and
2043 // the alias isn't otherwise explicit. This is slightly more conservative
2044 // than necessary, because it means that each store effectively depends
2045 // on every argument instead of just those arguments it would clobber.
2046 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2047
Dan Gohman475871a2008-07-27 21:46:04 +00002048 SmallVector<SDValue, 8> MemOpChains2;
2049 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002050 int FI = 0;
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002051 // Do not flag preceeding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00002052 InFlag = SDValue();
Dan Gohman1797ed52010-02-08 20:27:50 +00002053 if (GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002054 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2055 CCValAssign &VA = ArgLocs[i];
2056 if (VA.isRegLoc())
2057 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002058 assert(VA.isMemLoc());
Dan Gohmanc9403652010-07-07 15:54:55 +00002059 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002060 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002061 // Create frame index.
2062 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002063 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002064 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002065 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002066
Duncan Sands276dcbd2008-03-21 09:14:45 +00002067 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002068 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002069 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002070 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00002071 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00002072 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002073 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002074
Dan Gohman98ca4f22009-08-05 01:29:28 +00002075 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2076 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002077 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002078 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002079 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002080 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002081 DAG.getStore(ArgChain, dl, Arg, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002082 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002083 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002084 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002085 }
2086 }
2087
2088 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002089 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002090 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002091
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002092 // Copy arguments to their registers.
2093 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002094 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002095 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002096 InFlag = Chain.getValue(1);
2097 }
Dan Gohman475871a2008-07-27 21:46:04 +00002098 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002099
Gordon Henriksen86737662008-01-05 16:56:59 +00002100 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002101 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00002102 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002103 }
2104
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002105 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2106 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2107 // In the 64-bit large code model, we have to make all calls
2108 // through a register, since the call instruction's 32-bit
2109 // pc-relative offset may not be large enough to hold the whole
2110 // address.
2111 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002112 // If the callee is a GlobalAddress node (quite common, every direct call
2113 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2114 // it.
2115
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002116 // We should use extra load for direct calls to dllimported functions in
2117 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002118 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002119 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002120 unsigned char OpFlags = 0;
Eric Christopherfd179292009-08-27 18:07:15 +00002121
Chris Lattner48a7d022009-07-09 05:02:21 +00002122 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2123 // external symbols most go through the PLT in PIC mode. If the symbol
2124 // has hidden or protected visibility, or if it is static or local, then
2125 // we don't need to use the PLT - we can directly call it.
2126 if (Subtarget->isTargetELF() &&
2127 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002128 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002129 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002130 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner80945782010-09-27 06:34:01 +00002131 (GV->isDeclaration() || GV->isWeakForLinker()) &&
2132 Subtarget->getDarwinVers() < 9) {
Chris Lattner74e726e2009-07-09 05:27:35 +00002133 // PC-relative references to external symbols should go through $stub,
2134 // unless we're building with the leopard linker or later, which
2135 // automatically synthesizes these stubs.
2136 OpFlags = X86II::MO_DARWIN_STUB;
2137 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002138
Devang Patel0d881da2010-07-06 22:08:15 +00002139 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002140 G->getOffset(), OpFlags);
2141 }
Bill Wendling056292f2008-09-16 21:48:12 +00002142 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002143 unsigned char OpFlags = 0;
2144
Evan Cheng1bf891a2010-12-01 22:59:46 +00002145 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to
2146 // external symbols should go through the PLT.
2147 if (Subtarget->isTargetELF() &&
2148 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
2149 OpFlags = X86II::MO_PLT;
2150 } else if (Subtarget->isPICStyleStubAny() &&
2151 Subtarget->getDarwinVers() < 9) {
2152 // PC-relative references to external symbols should go through $stub,
2153 // unless we're building with the leopard linker or later, which
2154 // automatically synthesizes these stubs.
2155 OpFlags = X86II::MO_DARWIN_STUB;
Chris Lattner74e726e2009-07-09 05:27:35 +00002156 }
Eric Christopherfd179292009-08-27 18:07:15 +00002157
Chris Lattner48a7d022009-07-09 05:02:21 +00002158 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2159 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002160 }
2161
Chris Lattnerd96d0722007-02-25 06:40:16 +00002162 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002163 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002164 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002165
Evan Chengf22f9b32010-02-06 03:28:46 +00002166 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002167 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2168 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002169 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002170 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002171
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002172 Ops.push_back(Chain);
2173 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002174
Dan Gohman98ca4f22009-08-05 01:29:28 +00002175 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002176 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002177
Gordon Henriksen86737662008-01-05 16:56:59 +00002178 // Add argument registers to the end of the list so that they are known live
2179 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002180 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2181 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2182 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002183
Evan Cheng586ccac2008-03-18 23:36:35 +00002184 // Add an implicit use GOT pointer in EBX.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002185 if (!isTailCall && Subtarget->isPICStyleGOT())
Evan Cheng586ccac2008-03-18 23:36:35 +00002186 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
2187
Anton Korobeynikov3a1e54a2010-08-17 21:06:07 +00002188 // Add an implicit use of AL for non-Windows x86 64-bit vararg functions.
2189 if (Is64Bit && isVarArg && !Subtarget->isTargetWin64())
Owen Anderson825b72b2009-08-11 20:47:22 +00002190 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
Evan Cheng586ccac2008-03-18 23:36:35 +00002191
Gabor Greifba36cb52008-08-28 21:40:38 +00002192 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002193 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002194
Dan Gohman98ca4f22009-08-05 01:29:28 +00002195 if (isTailCall) {
Dale Johannesen88004c22010-06-05 00:30:45 +00002196 // We used to do:
2197 //// If this is the first return lowered for this function, add the regs
2198 //// to the liveout set for the function.
2199 // This isn't right, although it's probably harmless on x86; liveouts
2200 // should be computed from returns not tail calls. Consider a void
2201 // function making a tail call to a function returning int.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002202 return DAG.getNode(X86ISD::TC_RETURN, dl,
2203 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002204 }
2205
Dale Johannesenace16102009-02-03 19:33:06 +00002206 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002207 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002208
Chris Lattner2d297092006-05-23 18:50:38 +00002209 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002210 unsigned NumBytesForCalleeToPush;
Dan Gohman4d3d6e12010-05-27 18:43:40 +00002211 if (Subtarget->IsCalleePop(isVarArg, CallConv))
Gordon Henriksen86737662008-01-05 16:56:59 +00002212 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Chris Lattner29689432010-03-11 00:22:57 +00002213 else if (!Is64Bit && !IsTailCallConvention(CallConv) && IsStructRet)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002214 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002215 // pops the hidden struct pointer, so we have to push it back.
2216 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002217 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002218 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002219 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002220
Gordon Henriksenae636f82008-01-03 16:47:34 +00002221 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002222 if (!IsSibcall) {
2223 Chain = DAG.getCALLSEQ_END(Chain,
2224 DAG.getIntPtrConstant(NumBytes, true),
2225 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2226 true),
2227 InFlag);
2228 InFlag = Chain.getValue(1);
2229 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002230
Chris Lattner3085e152007-02-25 08:59:22 +00002231 // Handle result values, copying them out of physregs into vregs that we
2232 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002233 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2234 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002235}
2236
Evan Cheng25ab6902006-09-08 06:48:29 +00002237
2238//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002239// Fast Calling Convention (tail call) implementation
2240//===----------------------------------------------------------------------===//
2241
2242// Like std call, callee cleans arguments, convention except that ECX is
2243// reserved for storing the tail called function address. Only 2 registers are
2244// free for argument passing (inreg). Tail call optimization is performed
2245// provided:
2246// * tailcallopt is enabled
2247// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002248// On X86_64 architecture with GOT-style position independent code only local
2249// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002250// To keep the stack aligned according to platform abi the function
2251// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2252// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002253// If a tail called function callee has more arguments than the caller the
2254// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002255// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002256// original REtADDR, but before the saved framepointer or the spilled registers
2257// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2258// stack layout:
2259// arg1
2260// arg2
2261// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002262// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002263// move area ]
2264// (possible EBP)
2265// ESI
2266// EDI
2267// local1 ..
2268
2269/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2270/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002271unsigned
2272X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2273 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002274 MachineFunction &MF = DAG.getMachineFunction();
2275 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002276 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002277 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002278 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002279 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002280 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002281 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2282 // Number smaller than 12 so just add the difference.
2283 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2284 } else {
2285 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002286 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002287 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002288 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002289 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002290}
2291
Evan Cheng5f941932010-02-05 02:21:12 +00002292/// MatchingStackOffset - Return true if the given stack call argument is
2293/// already available in the same position (relatively) of the caller's
2294/// incoming argument stack.
2295static
2296bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2297 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2298 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002299 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2300 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002301 if (Arg.getOpcode() == ISD::CopyFromReg) {
2302 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00002303 if (!TargetRegisterInfo::isVirtualRegister(VR))
Evan Cheng5f941932010-02-05 02:21:12 +00002304 return false;
2305 MachineInstr *Def = MRI->getVRegDef(VR);
2306 if (!Def)
2307 return false;
2308 if (!Flags.isByVal()) {
2309 if (!TII->isLoadFromStackSlot(Def, FI))
2310 return false;
2311 } else {
2312 unsigned Opcode = Def->getOpcode();
2313 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2314 Def->getOperand(1).isFI()) {
2315 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002316 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002317 } else
2318 return false;
2319 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002320 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2321 if (Flags.isByVal())
2322 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002323 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002324 // define @foo(%struct.X* %A) {
2325 // tail call @bar(%struct.X* byval %A)
2326 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002327 return false;
2328 SDValue Ptr = Ld->getBasePtr();
2329 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2330 if (!FINode)
2331 return false;
2332 FI = FINode->getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002333 } else
2334 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002335
Evan Cheng4cae1332010-03-05 08:38:04 +00002336 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002337 if (!MFI->isFixedObjectIndex(FI))
2338 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002339 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002340}
2341
Dan Gohman98ca4f22009-08-05 01:29:28 +00002342/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2343/// for tail call optimization. Targets which want to do tail call
2344/// optimization should implement this function.
2345bool
2346X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002347 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002348 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002349 bool isCalleeStructRet,
2350 bool isCallerStructRet,
Evan Chengb1712452010-01-27 06:25:16 +00002351 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002352 const SmallVectorImpl<SDValue> &OutVals,
Evan Chengb1712452010-01-27 06:25:16 +00002353 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002354 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002355 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002356 CalleeCC != CallingConv::C)
2357 return false;
2358
Evan Cheng7096ae42010-01-29 06:45:59 +00002359 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002360 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002361 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Cheng13617962010-04-30 01:12:32 +00002362 CallingConv::ID CallerCC = CallerF->getCallingConv();
2363 bool CCMatch = CallerCC == CalleeCC;
2364
Dan Gohman1797ed52010-02-08 20:27:50 +00002365 if (GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002366 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002367 return true;
2368 return false;
2369 }
2370
Dale Johannesen2f05cc02010-05-28 23:24:28 +00002371 // Look for obvious safe cases to perform tail call optimization that do not
2372 // require ABI changes. This is what gcc calls sibcall.
Evan Chengb2c92902010-02-02 02:22:50 +00002373
Evan Cheng2c12cb42010-03-26 16:26:03 +00002374 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2375 // emit a special epilogue.
2376 if (RegInfo->needsStackRealignment(MF))
2377 return false;
2378
Eric Christopher90eb4022010-07-22 00:26:08 +00002379 // Do not sibcall optimize vararg calls unless the call site is not passing
2380 // any arguments.
Evan Cheng3c262ee2010-03-26 02:13:13 +00002381 if (isVarArg && !Outs.empty())
Evan Cheng843bd692010-01-31 06:44:49 +00002382 return false;
2383
Evan Chenga375d472010-03-15 18:54:48 +00002384 // Also avoid sibcall optimization if either caller or callee uses struct
2385 // return semantics.
2386 if (isCalleeStructRet || isCallerStructRet)
2387 return false;
2388
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002389 // If the call result is in ST0 / ST1, it needs to be popped off the x87 stack.
2390 // Therefore if it's not used by the call it is not safe to optimize this into
2391 // a sibcall.
2392 bool Unused = false;
2393 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2394 if (!Ins[i].Used) {
2395 Unused = true;
2396 break;
2397 }
2398 }
2399 if (Unused) {
2400 SmallVector<CCValAssign, 16> RVLocs;
2401 CCState CCInfo(CalleeCC, false, getTargetMachine(),
2402 RVLocs, *DAG.getContext());
2403 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002404 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002405 CCValAssign &VA = RVLocs[i];
2406 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2407 return false;
2408 }
2409 }
2410
Evan Cheng13617962010-04-30 01:12:32 +00002411 // If the calling conventions do not match, then we'd better make sure the
2412 // results are returned in the same way as what the caller expects.
2413 if (!CCMatch) {
2414 SmallVector<CCValAssign, 16> RVLocs1;
2415 CCState CCInfo1(CalleeCC, false, getTargetMachine(),
2416 RVLocs1, *DAG.getContext());
2417 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2418
2419 SmallVector<CCValAssign, 16> RVLocs2;
2420 CCState CCInfo2(CallerCC, false, getTargetMachine(),
2421 RVLocs2, *DAG.getContext());
2422 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2423
2424 if (RVLocs1.size() != RVLocs2.size())
2425 return false;
2426 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2427 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2428 return false;
2429 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2430 return false;
2431 if (RVLocs1[i].isRegLoc()) {
2432 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2433 return false;
2434 } else {
2435 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2436 return false;
2437 }
2438 }
2439 }
2440
Evan Chenga6bff982010-01-30 01:22:00 +00002441 // If the callee takes no arguments then go on to check the results of the
2442 // call.
2443 if (!Outs.empty()) {
2444 // Check if stack adjustment is needed. For now, do not do this if any
2445 // argument is passed on the stack.
2446 SmallVector<CCValAssign, 16> ArgLocs;
2447 CCState CCInfo(CalleeCC, isVarArg, getTargetMachine(),
2448 ArgLocs, *DAG.getContext());
Duncan Sands45907662010-10-31 13:21:44 +00002449 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Evan Chengb2c92902010-02-02 02:22:50 +00002450 if (CCInfo.getNextStackOffset()) {
2451 MachineFunction &MF = DAG.getMachineFunction();
2452 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2453 return false;
Evan Chengb2c92902010-02-02 02:22:50 +00002454
2455 // Check if the arguments are already laid out in the right way as
2456 // the caller's fixed stack objects.
2457 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002458 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2459 const X86InstrInfo *TII =
2460 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002461 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2462 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00002463 SDValue Arg = OutVals[i];
Evan Chengb2c92902010-02-02 02:22:50 +00002464 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002465 if (VA.getLocInfo() == CCValAssign::Indirect)
2466 return false;
2467 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002468 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2469 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002470 return false;
2471 }
2472 }
2473 }
Evan Cheng9c044672010-05-29 01:35:22 +00002474
2475 // If the tailcall address may be in a register, then make sure it's
2476 // possible to register allocate for it. In 32-bit, the call address can
2477 // only target EAX, EDX, or ECX since the tail call must be scheduled after
Evan Chengdedd9742010-07-14 06:44:01 +00002478 // callee-saved registers are restored. These happen to be the same
2479 // registers used to pass 'inreg' arguments so watch out for those.
2480 if (!Subtarget->is64Bit() &&
2481 !isa<GlobalAddressSDNode>(Callee) &&
Evan Cheng9c044672010-05-29 01:35:22 +00002482 !isa<ExternalSymbolSDNode>(Callee)) {
Evan Cheng9c044672010-05-29 01:35:22 +00002483 unsigned NumInRegs = 0;
2484 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2485 CCValAssign &VA = ArgLocs[i];
Evan Chengdedd9742010-07-14 06:44:01 +00002486 if (!VA.isRegLoc())
2487 continue;
2488 unsigned Reg = VA.getLocReg();
2489 switch (Reg) {
2490 default: break;
2491 case X86::EAX: case X86::EDX: case X86::ECX:
2492 if (++NumInRegs == 3)
Evan Cheng9c044672010-05-29 01:35:22 +00002493 return false;
Evan Chengdedd9742010-07-14 06:44:01 +00002494 break;
Evan Cheng9c044672010-05-29 01:35:22 +00002495 }
2496 }
2497 }
Evan Chenga6bff982010-01-30 01:22:00 +00002498 }
Evan Chengb1712452010-01-27 06:25:16 +00002499
Dale Johannesend155d7e2010-10-25 22:17:05 +00002500 // An stdcall caller is expected to clean up its arguments; the callee
Dale Johannesen0e034562010-11-12 00:43:18 +00002501 // isn't going to do that.
Dale Johannesend155d7e2010-10-25 22:17:05 +00002502 if (!CCMatch && CallerCC==CallingConv::X86_StdCall)
2503 return false;
2504
Evan Cheng86809cc2010-02-03 03:28:02 +00002505 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002506}
2507
Dan Gohman3df24e62008-09-03 23:12:08 +00002508FastISel *
Dan Gohmana4160c32010-07-07 16:29:44 +00002509X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
2510 return X86::createFastISel(funcInfo);
Dan Gohmand9f3c482008-08-19 21:32:53 +00002511}
2512
2513
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002514//===----------------------------------------------------------------------===//
2515// Other Lowering Hooks
2516//===----------------------------------------------------------------------===//
2517
Bruno Cardoso Lopese654b562010-09-01 00:51:36 +00002518static bool MayFoldLoad(SDValue Op) {
2519 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
2520}
2521
2522static bool MayFoldIntoStore(SDValue Op) {
2523 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
2524}
2525
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002526static bool isTargetShuffle(unsigned Opcode) {
2527 switch(Opcode) {
2528 default: return false;
2529 case X86ISD::PSHUFD:
2530 case X86ISD::PSHUFHW:
2531 case X86ISD::PSHUFLW:
2532 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002533 case X86ISD::PALIGN:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002534 case X86ISD::SHUFPS:
2535 case X86ISD::MOVLHPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002536 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002537 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002538 case X86ISD::MOVLPS:
2539 case X86ISD::MOVLPD:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002540 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002541 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002542 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002543 case X86ISD::MOVSS:
2544 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002545 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002546 case X86ISD::UNPCKLPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002547 case X86ISD::PUNPCKLWD:
2548 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002549 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002550 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002551 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002552 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002553 case X86ISD::PUNPCKHWD:
2554 case X86ISD::PUNPCKHBW:
2555 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002556 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002557 return true;
2558 }
2559 return false;
2560}
2561
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002562static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002563 SDValue V1, SelectionDAG &DAG) {
2564 switch(Opc) {
2565 default: llvm_unreachable("Unknown x86 shuffle node");
2566 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002567 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002568 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002569 return DAG.getNode(Opc, dl, VT, V1);
2570 }
2571
2572 return SDValue();
2573}
2574
2575static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00002576 SDValue V1, unsigned TargetMask, SelectionDAG &DAG) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002577 switch(Opc) {
2578 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002579 case X86ISD::PSHUFD:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002580 case X86ISD::PSHUFHW:
2581 case X86ISD::PSHUFLW:
2582 return DAG.getNode(Opc, dl, VT, V1, DAG.getConstant(TargetMask, MVT::i8));
2583 }
2584
2585 return SDValue();
2586}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002587
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002588static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2589 SDValue V1, SDValue V2, unsigned TargetMask, SelectionDAG &DAG) {
2590 switch(Opc) {
2591 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002592 case X86ISD::PALIGN:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002593 case X86ISD::SHUFPD:
2594 case X86ISD::SHUFPS:
2595 return DAG.getNode(Opc, dl, VT, V1, V2,
2596 DAG.getConstant(TargetMask, MVT::i8));
2597 }
2598 return SDValue();
2599}
2600
2601static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2602 SDValue V1, SDValue V2, SelectionDAG &DAG) {
2603 switch(Opc) {
2604 default: llvm_unreachable("Unknown x86 shuffle node");
2605 case X86ISD::MOVLHPS:
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00002606 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002607 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002608 case X86ISD::MOVLPS:
2609 case X86ISD::MOVLPD:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002610 case X86ISD::MOVSS:
2611 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002612 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002613 case X86ISD::UNPCKLPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002614 case X86ISD::PUNPCKLWD:
2615 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002616 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002617 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002618 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002619 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002620 case X86ISD::PUNPCKHWD:
2621 case X86ISD::PUNPCKHBW:
2622 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002623 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002624 return DAG.getNode(Opc, dl, VT, V1, V2);
2625 }
2626 return SDValue();
2627}
2628
Dan Gohmand858e902010-04-17 15:26:15 +00002629SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002630 MachineFunction &MF = DAG.getMachineFunction();
2631 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2632 int ReturnAddrIndex = FuncInfo->getRAIndex();
2633
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002634 if (ReturnAddrIndex == 0) {
2635 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002636 uint64_t SlotSize = TD->getPointerSize();
David Greene3f2bf852009-11-12 20:49:22 +00002637 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00002638 false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002639 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002640 }
2641
Evan Cheng25ab6902006-09-08 06:48:29 +00002642 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002643}
2644
2645
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002646bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
2647 bool hasSymbolicDisplacement) {
2648 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00002649 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002650 return false;
2651
2652 // If we don't have a symbolic displacement - we don't have any extra
2653 // restrictions.
2654 if (!hasSymbolicDisplacement)
2655 return true;
2656
2657 // FIXME: Some tweaks might be needed for medium code model.
2658 if (M != CodeModel::Small && M != CodeModel::Kernel)
2659 return false;
2660
2661 // For small code model we assume that latest object is 16MB before end of 31
2662 // bits boundary. We may also accept pretty large negative constants knowing
2663 // that all objects are in the positive half of address space.
2664 if (M == CodeModel::Small && Offset < 16*1024*1024)
2665 return true;
2666
2667 // For kernel code model we know that all object resist in the negative half
2668 // of 32bits address space. We may not accept negative offsets, since they may
2669 // be just off and we may accept pretty large positive ones.
2670 if (M == CodeModel::Kernel && Offset > 0)
2671 return true;
2672
2673 return false;
2674}
2675
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002676/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
2677/// specific condition code, returning the condition code and the LHS/RHS of the
2678/// comparison to make.
2679static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2680 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00002681 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002682 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2683 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2684 // X > -1 -> X == 0, jump !sign.
2685 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002686 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002687 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2688 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002689 return X86::COND_S;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002690 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00002691 // X < 1 -> X <= 0
2692 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002693 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002694 }
Chris Lattnerf9570512006-09-13 03:22:10 +00002695 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002696
Evan Chengd9558e02006-01-06 00:43:03 +00002697 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002698 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002699 case ISD::SETEQ: return X86::COND_E;
2700 case ISD::SETGT: return X86::COND_G;
2701 case ISD::SETGE: return X86::COND_GE;
2702 case ISD::SETLT: return X86::COND_L;
2703 case ISD::SETLE: return X86::COND_LE;
2704 case ISD::SETNE: return X86::COND_NE;
2705 case ISD::SETULT: return X86::COND_B;
2706 case ISD::SETUGT: return X86::COND_A;
2707 case ISD::SETULE: return X86::COND_BE;
2708 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00002709 }
Chris Lattner4c78e022008-12-23 23:42:27 +00002710 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002711
Chris Lattner4c78e022008-12-23 23:42:27 +00002712 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00002713
Chris Lattner4c78e022008-12-23 23:42:27 +00002714 // If LHS is a foldable load, but RHS is not, flip the condition.
2715 if ((ISD::isNON_EXTLoad(LHS.getNode()) && LHS.hasOneUse()) &&
2716 !(ISD::isNON_EXTLoad(RHS.getNode()) && RHS.hasOneUse())) {
2717 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
2718 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00002719 }
2720
Chris Lattner4c78e022008-12-23 23:42:27 +00002721 switch (SetCCOpcode) {
2722 default: break;
2723 case ISD::SETOLT:
2724 case ISD::SETOLE:
2725 case ISD::SETUGT:
2726 case ISD::SETUGE:
2727 std::swap(LHS, RHS);
2728 break;
2729 }
2730
2731 // On a floating point condition, the flags are set as follows:
2732 // ZF PF CF op
2733 // 0 | 0 | 0 | X > Y
2734 // 0 | 0 | 1 | X < Y
2735 // 1 | 0 | 0 | X == Y
2736 // 1 | 1 | 1 | unordered
2737 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002738 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00002739 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002740 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00002741 case ISD::SETOLT: // flipped
2742 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002743 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00002744 case ISD::SETOLE: // flipped
2745 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002746 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002747 case ISD::SETUGT: // flipped
2748 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002749 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00002750 case ISD::SETUGE: // flipped
2751 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002752 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002753 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002754 case ISD::SETNE: return X86::COND_NE;
2755 case ISD::SETUO: return X86::COND_P;
2756 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00002757 case ISD::SETOEQ:
2758 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00002759 }
Evan Chengd9558e02006-01-06 00:43:03 +00002760}
2761
Evan Cheng4a460802006-01-11 00:33:36 +00002762/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2763/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00002764/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00002765static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00002766 switch (X86CC) {
2767 default:
2768 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00002769 case X86::COND_B:
2770 case X86::COND_BE:
2771 case X86::COND_E:
2772 case X86::COND_P:
2773 case X86::COND_A:
2774 case X86::COND_AE:
2775 case X86::COND_NE:
2776 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00002777 return true;
2778 }
2779}
2780
Evan Chengeb2f9692009-10-27 19:56:55 +00002781/// isFPImmLegal - Returns true if the target can instruction select the
2782/// specified FP immediate natively. If false, the legalizer will
2783/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00002784bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00002785 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
2786 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
2787 return true;
2788 }
2789 return false;
2790}
2791
Nate Begeman9008ca62009-04-27 18:41:29 +00002792/// isUndefOrInRange - Return true if Val is undef or if its value falls within
2793/// the specified range (L, H].
2794static bool isUndefOrInRange(int Val, int Low, int Hi) {
2795 return (Val < 0) || (Val >= Low && Val < Hi);
2796}
2797
2798/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
2799/// specified value.
2800static bool isUndefOrEqual(int Val, int CmpVal) {
2801 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00002802 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00002803 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00002804}
2805
Nate Begeman9008ca62009-04-27 18:41:29 +00002806/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
2807/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
2808/// the second operand.
Owen Andersone50ed302009-08-10 22:56:29 +00002809static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00002810 if (VT == MVT::v4f32 || VT == MVT::v4i32 )
Nate Begeman9008ca62009-04-27 18:41:29 +00002811 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002812 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00002813 return (Mask[0] < 2 && Mask[1] < 2);
2814 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002815}
2816
Nate Begeman9008ca62009-04-27 18:41:29 +00002817bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002818 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002819 N->getMask(M);
2820 return ::isPSHUFDMask(M, N->getValueType(0));
2821}
Evan Cheng0188ecb2006-03-22 18:59:22 +00002822
Nate Begeman9008ca62009-04-27 18:41:29 +00002823/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
2824/// is suitable for input to PSHUFHW.
Owen Andersone50ed302009-08-10 22:56:29 +00002825static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002826 if (VT != MVT::v8i16)
Evan Cheng0188ecb2006-03-22 18:59:22 +00002827 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002828
Nate Begeman9008ca62009-04-27 18:41:29 +00002829 // Lower quadword copied in order or undef.
2830 for (int i = 0; i != 4; ++i)
2831 if (Mask[i] >= 0 && Mask[i] != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00002832 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002833
Evan Cheng506d3df2006-03-29 23:07:14 +00002834 // Upper quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002835 for (int i = 4; i != 8; ++i)
2836 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Evan Cheng506d3df2006-03-29 23:07:14 +00002837 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002838
Evan Cheng506d3df2006-03-29 23:07:14 +00002839 return true;
2840}
2841
Nate Begeman9008ca62009-04-27 18:41:29 +00002842bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002843 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002844 N->getMask(M);
2845 return ::isPSHUFHWMask(M, N->getValueType(0));
2846}
Evan Cheng506d3df2006-03-29 23:07:14 +00002847
Nate Begeman9008ca62009-04-27 18:41:29 +00002848/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
2849/// is suitable for input to PSHUFLW.
Owen Andersone50ed302009-08-10 22:56:29 +00002850static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002851 if (VT != MVT::v8i16)
Evan Cheng506d3df2006-03-29 23:07:14 +00002852 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002853
Rafael Espindola15684b22009-04-24 12:40:33 +00002854 // Upper quadword copied in order.
Nate Begeman9008ca62009-04-27 18:41:29 +00002855 for (int i = 4; i != 8; ++i)
2856 if (Mask[i] >= 0 && Mask[i] != i)
Rafael Espindola15684b22009-04-24 12:40:33 +00002857 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002858
Rafael Espindola15684b22009-04-24 12:40:33 +00002859 // Lower quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002860 for (int i = 0; i != 4; ++i)
2861 if (Mask[i] >= 4)
Rafael Espindola15684b22009-04-24 12:40:33 +00002862 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002863
Rafael Espindola15684b22009-04-24 12:40:33 +00002864 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002865}
2866
Nate Begeman9008ca62009-04-27 18:41:29 +00002867bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002868 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002869 N->getMask(M);
2870 return ::isPSHUFLWMask(M, N->getValueType(0));
2871}
2872
Nate Begemana09008b2009-10-19 02:17:23 +00002873/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
2874/// is suitable for input to PALIGNR.
2875static bool isPALIGNRMask(const SmallVectorImpl<int> &Mask, EVT VT,
2876 bool hasSSSE3) {
2877 int i, e = VT.getVectorNumElements();
Michael J. Spencerec38de22010-10-10 22:04:20 +00002878
Nate Begemana09008b2009-10-19 02:17:23 +00002879 // Do not handle v2i64 / v2f64 shuffles with palignr.
2880 if (e < 4 || !hasSSSE3)
2881 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00002882
Nate Begemana09008b2009-10-19 02:17:23 +00002883 for (i = 0; i != e; ++i)
2884 if (Mask[i] >= 0)
2885 break;
Michael J. Spencerec38de22010-10-10 22:04:20 +00002886
Nate Begemana09008b2009-10-19 02:17:23 +00002887 // All undef, not a palignr.
2888 if (i == e)
2889 return false;
2890
2891 // Determine if it's ok to perform a palignr with only the LHS, since we
2892 // don't have access to the actual shuffle elements to see if RHS is undef.
2893 bool Unary = Mask[i] < (int)e;
2894 bool NeedsUnary = false;
2895
2896 int s = Mask[i] - i;
Michael J. Spencerec38de22010-10-10 22:04:20 +00002897
Nate Begemana09008b2009-10-19 02:17:23 +00002898 // Check the rest of the elements to see if they are consecutive.
2899 for (++i; i != e; ++i) {
2900 int m = Mask[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00002901 if (m < 0)
Nate Begemana09008b2009-10-19 02:17:23 +00002902 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00002903
Nate Begemana09008b2009-10-19 02:17:23 +00002904 Unary = Unary && (m < (int)e);
2905 NeedsUnary = NeedsUnary || (m < s);
2906
2907 if (NeedsUnary && !Unary)
2908 return false;
2909 if (Unary && m != ((s+i) & (e-1)))
2910 return false;
2911 if (!Unary && m != (s+i))
2912 return false;
2913 }
2914 return true;
2915}
2916
2917bool X86::isPALIGNRMask(ShuffleVectorSDNode *N) {
2918 SmallVector<int, 8> M;
2919 N->getMask(M);
2920 return ::isPALIGNRMask(M, N->getValueType(0), true);
2921}
2922
Evan Cheng14aed5e2006-03-24 01:18:28 +00002923/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2924/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Owen Andersone50ed302009-08-10 22:56:29 +00002925static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002926 int NumElems = VT.getVectorNumElements();
2927 if (NumElems != 2 && NumElems != 4)
2928 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002929
Nate Begeman9008ca62009-04-27 18:41:29 +00002930 int Half = NumElems / 2;
2931 for (int i = 0; i < Half; ++i)
2932 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002933 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002934 for (int i = Half; i < NumElems; ++i)
2935 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002936 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002937
Evan Cheng14aed5e2006-03-24 01:18:28 +00002938 return true;
2939}
2940
Nate Begeman9008ca62009-04-27 18:41:29 +00002941bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
2942 SmallVector<int, 8> M;
2943 N->getMask(M);
2944 return ::isSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002945}
2946
Evan Cheng213d2cf2007-05-17 18:45:50 +00002947/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00002948/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2949/// half elements to come from vector 1 (which would equal the dest.) and
2950/// the upper half to come from vector 2.
Owen Andersone50ed302009-08-10 22:56:29 +00002951static bool isCommutedSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002952 int NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00002953
2954 if (NumElems != 2 && NumElems != 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00002955 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002956
Nate Begeman9008ca62009-04-27 18:41:29 +00002957 int Half = NumElems / 2;
2958 for (int i = 0; i < Half; ++i)
2959 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002960 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002961 for (int i = Half; i < NumElems; ++i)
2962 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002963 return false;
2964 return true;
2965}
2966
Nate Begeman9008ca62009-04-27 18:41:29 +00002967static bool isCommutedSHUFP(ShuffleVectorSDNode *N) {
2968 SmallVector<int, 8> M;
2969 N->getMask(M);
2970 return isCommutedSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002971}
2972
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002973/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2974/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00002975bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
2976 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002977 return false;
2978
Evan Cheng2064a2b2006-03-28 06:50:32 +00002979 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman9008ca62009-04-27 18:41:29 +00002980 return isUndefOrEqual(N->getMaskElt(0), 6) &&
2981 isUndefOrEqual(N->getMaskElt(1), 7) &&
2982 isUndefOrEqual(N->getMaskElt(2), 2) &&
2983 isUndefOrEqual(N->getMaskElt(3), 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00002984}
2985
Nate Begeman0b10b912009-11-07 23:17:15 +00002986/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2987/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2988/// <2, 3, 2, 3>
2989bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
2990 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Michael J. Spencerec38de22010-10-10 22:04:20 +00002991
Nate Begeman0b10b912009-11-07 23:17:15 +00002992 if (NumElems != 4)
2993 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00002994
Nate Begeman0b10b912009-11-07 23:17:15 +00002995 return isUndefOrEqual(N->getMaskElt(0), 2) &&
2996 isUndefOrEqual(N->getMaskElt(1), 3) &&
2997 isUndefOrEqual(N->getMaskElt(2), 2) &&
2998 isUndefOrEqual(N->getMaskElt(3), 3);
2999}
3000
Evan Cheng5ced1d82006-04-06 23:23:56 +00003001/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
3002/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman9008ca62009-04-27 18:41:29 +00003003bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
3004 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003005
Evan Cheng5ced1d82006-04-06 23:23:56 +00003006 if (NumElems != 2 && NumElems != 4)
3007 return false;
3008
Evan Chengc5cdff22006-04-07 21:53:05 +00003009 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003010 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003011 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003012
Evan Chengc5cdff22006-04-07 21:53:05 +00003013 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003014 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003015 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003016
3017 return true;
3018}
3019
Nate Begeman0b10b912009-11-07 23:17:15 +00003020/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
3021/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
3022bool X86::isMOVLHPSMask(ShuffleVectorSDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003023 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003024
Evan Cheng5ced1d82006-04-06 23:23:56 +00003025 if (NumElems != 2 && NumElems != 4)
3026 return false;
3027
Evan Chengc5cdff22006-04-07 21:53:05 +00003028 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003029 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003030 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003031
Nate Begeman9008ca62009-04-27 18:41:29 +00003032 for (unsigned i = 0; i < NumElems/2; ++i)
3033 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003034 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003035
3036 return true;
3037}
3038
Evan Cheng0038e592006-03-28 00:39:58 +00003039/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
3040/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Owen Andersone50ed302009-08-10 22:56:29 +00003041static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003042 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003043 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003044 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng0038e592006-03-28 00:39:58 +00003045 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003046
Nate Begeman9008ca62009-04-27 18:41:29 +00003047 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
3048 int BitI = Mask[i];
3049 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00003050 if (!isUndefOrEqual(BitI, j))
3051 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00003052 if (V2IsSplat) {
Mon P Wang7bcaefa2009-02-04 01:16:59 +00003053 if (!isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003054 return false;
3055 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00003056 if (!isUndefOrEqual(BitI1, j + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003057 return false;
3058 }
Evan Cheng0038e592006-03-28 00:39:58 +00003059 }
Evan Cheng0038e592006-03-28 00:39:58 +00003060 return true;
3061}
3062
Nate Begeman9008ca62009-04-27 18:41:29 +00003063bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3064 SmallVector<int, 8> M;
3065 N->getMask(M);
3066 return ::isUNPCKLMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003067}
3068
Evan Cheng4fcb9222006-03-28 02:43:26 +00003069/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3070/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Eric Christopherfd179292009-08-27 18:07:15 +00003071static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003072 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003073 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003074 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng4fcb9222006-03-28 02:43:26 +00003075 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003076
Nate Begeman9008ca62009-04-27 18:41:29 +00003077 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
3078 int BitI = Mask[i];
3079 int BitI1 = Mask[i+1];
Chris Lattner5a88b832007-02-25 07:10:00 +00003080 if (!isUndefOrEqual(BitI, j + NumElts/2))
Evan Chengc5cdff22006-04-07 21:53:05 +00003081 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00003082 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00003083 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003084 return false;
3085 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00003086 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003087 return false;
3088 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003089 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003090 return true;
3091}
3092
Nate Begeman9008ca62009-04-27 18:41:29 +00003093bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3094 SmallVector<int, 8> M;
3095 N->getMask(M);
3096 return ::isUNPCKHMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003097}
3098
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003099/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3100/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3101/// <0, 0, 1, 1>
Owen Andersone50ed302009-08-10 22:56:29 +00003102static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003103 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003104 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003105 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003106
Nate Begeman9008ca62009-04-27 18:41:29 +00003107 for (int i = 0, j = 0; i != NumElems; i += 2, ++j) {
3108 int BitI = Mask[i];
3109 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00003110 if (!isUndefOrEqual(BitI, j))
3111 return false;
3112 if (!isUndefOrEqual(BitI1, j))
3113 return false;
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003114 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003115 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003116}
3117
Nate Begeman9008ca62009-04-27 18:41:29 +00003118bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
3119 SmallVector<int, 8> M;
3120 N->getMask(M);
3121 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
3122}
3123
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003124/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3125/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3126/// <2, 2, 3, 3>
Owen Andersone50ed302009-08-10 22:56:29 +00003127static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003128 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003129 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
3130 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003131
Nate Begeman9008ca62009-04-27 18:41:29 +00003132 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
3133 int BitI = Mask[i];
3134 int BitI1 = Mask[i+1];
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003135 if (!isUndefOrEqual(BitI, j))
3136 return false;
3137 if (!isUndefOrEqual(BitI1, j))
3138 return false;
3139 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003140 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003141}
3142
Nate Begeman9008ca62009-04-27 18:41:29 +00003143bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
3144 SmallVector<int, 8> M;
3145 N->getMask(M);
3146 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
3147}
3148
Evan Cheng017dcc62006-04-21 01:05:10 +00003149/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3150/// specifies a shuffle of elements that is suitable for input to MOVSS,
3151/// MOVSD, and MOVD, i.e. setting the lowest element.
Owen Andersone50ed302009-08-10 22:56:29 +00003152static bool isMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003153 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003154 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003155
3156 int NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003157
Nate Begeman9008ca62009-04-27 18:41:29 +00003158 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003159 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003160
Nate Begeman9008ca62009-04-27 18:41:29 +00003161 for (int i = 1; i < NumElts; ++i)
3162 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003163 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003164
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003165 return true;
3166}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003167
Nate Begeman9008ca62009-04-27 18:41:29 +00003168bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
3169 SmallVector<int, 8> M;
3170 N->getMask(M);
3171 return ::isMOVLMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003172}
3173
Evan Cheng017dcc62006-04-21 01:05:10 +00003174/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
3175/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003176/// element of vector 2 and the other elements to come from vector 1 in order.
Owen Andersone50ed302009-08-10 22:56:29 +00003177static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003178 bool V2IsSplat = false, bool V2IsUndef = false) {
3179 int NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003180 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003181 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003182
Nate Begeman9008ca62009-04-27 18:41:29 +00003183 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003184 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003185
Nate Begeman9008ca62009-04-27 18:41:29 +00003186 for (int i = 1; i < NumOps; ++i)
3187 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3188 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3189 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003190 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003191
Evan Cheng39623da2006-04-20 08:58:49 +00003192 return true;
3193}
3194
Nate Begeman9008ca62009-04-27 18:41:29 +00003195static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00003196 bool V2IsUndef = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003197 SmallVector<int, 8> M;
3198 N->getMask(M);
3199 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00003200}
3201
Evan Chengd9539472006-04-14 21:59:03 +00003202/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3203/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003204bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N) {
3205 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003206 return false;
3207
3208 // Expect 1, 1, 3, 3
Rafael Espindola15684b22009-04-24 12:40:33 +00003209 for (unsigned i = 0; i < 2; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003210 int Elt = N->getMaskElt(i);
3211 if (Elt >= 0 && Elt != 1)
3212 return false;
Rafael Espindola15684b22009-04-24 12:40:33 +00003213 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003214
3215 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003216 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003217 int Elt = N->getMaskElt(i);
3218 if (Elt >= 0 && Elt != 3)
3219 return false;
3220 if (Elt == 3)
3221 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003222 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003223 // Don't use movshdup if it can be done with a shufps.
Nate Begeman9008ca62009-04-27 18:41:29 +00003224 // FIXME: verify that matching u, u, 3, 3 is what we want.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003225 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003226}
3227
3228/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3229/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003230bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N) {
3231 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003232 return false;
3233
3234 // Expect 0, 0, 2, 2
Nate Begeman9008ca62009-04-27 18:41:29 +00003235 for (unsigned i = 0; i < 2; ++i)
3236 if (N->getMaskElt(i) > 0)
3237 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003238
3239 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003240 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003241 int Elt = N->getMaskElt(i);
3242 if (Elt >= 0 && Elt != 2)
3243 return false;
3244 if (Elt == 2)
3245 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003246 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003247 // Don't use movsldup if it can be done with a shufps.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003248 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003249}
3250
Evan Cheng0b457f02008-09-25 20:50:48 +00003251/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3252/// specifies a shuffle of elements that is suitable for input to MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003253bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
3254 int e = N->getValueType(0).getVectorNumElements() / 2;
Eric Christopherfd179292009-08-27 18:07:15 +00003255
Nate Begeman9008ca62009-04-27 18:41:29 +00003256 for (int i = 0; i < e; ++i)
3257 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003258 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003259 for (int i = 0; i < e; ++i)
3260 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003261 return false;
3262 return true;
3263}
3264
Evan Cheng63d33002006-03-22 08:01:21 +00003265/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003266/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Evan Cheng63d33002006-03-22 08:01:21 +00003267unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003268 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3269 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
3270
Evan Chengb9df0ca2006-03-22 02:53:00 +00003271 unsigned Shift = (NumOperands == 4) ? 2 : 1;
3272 unsigned Mask = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00003273 for (int i = 0; i < NumOperands; ++i) {
3274 int Val = SVOp->getMaskElt(NumOperands-i-1);
3275 if (Val < 0) Val = 0;
Evan Cheng14aed5e2006-03-24 01:18:28 +00003276 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00003277 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00003278 if (i != NumOperands - 1)
3279 Mask <<= Shift;
3280 }
Evan Cheng63d33002006-03-22 08:01:21 +00003281 return Mask;
3282}
3283
Evan Cheng506d3df2006-03-29 23:07:14 +00003284/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003285/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003286unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003287 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003288 unsigned Mask = 0;
3289 // 8 nodes, but we only care about the last 4.
3290 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003291 int Val = SVOp->getMaskElt(i);
3292 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00003293 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00003294 if (i != 4)
3295 Mask <<= 2;
3296 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003297 return Mask;
3298}
3299
3300/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003301/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003302unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003303 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003304 unsigned Mask = 0;
3305 // 8 nodes, but we only care about the first 4.
3306 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003307 int Val = SVOp->getMaskElt(i);
3308 if (Val >= 0)
3309 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00003310 if (i != 0)
3311 Mask <<= 2;
3312 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003313 return Mask;
3314}
3315
Nate Begemana09008b2009-10-19 02:17:23 +00003316/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
3317/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
3318unsigned X86::getShufflePALIGNRImmediate(SDNode *N) {
3319 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3320 EVT VVT = N->getValueType(0);
3321 unsigned EltSize = VVT.getVectorElementType().getSizeInBits() >> 3;
3322 int Val = 0;
3323
3324 unsigned i, e;
3325 for (i = 0, e = VVT.getVectorNumElements(); i != e; ++i) {
3326 Val = SVOp->getMaskElt(i);
3327 if (Val >= 0)
3328 break;
3329 }
3330 return (Val - i) * EltSize;
3331}
3332
Evan Cheng37b73872009-07-30 08:33:02 +00003333/// isZeroNode - Returns true if Elt is a constant zero or a floating point
3334/// constant +0.0.
3335bool X86::isZeroNode(SDValue Elt) {
3336 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmane368b462010-06-18 14:22:04 +00003337 cast<ConstantSDNode>(Elt)->isNullValue()) ||
Evan Cheng37b73872009-07-30 08:33:02 +00003338 (isa<ConstantFPSDNode>(Elt) &&
3339 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
3340}
3341
Nate Begeman9008ca62009-04-27 18:41:29 +00003342/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
3343/// their permute mask.
3344static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
3345 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003346 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003347 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00003348 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00003349
Nate Begeman5a5ca152009-04-29 05:20:52 +00003350 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003351 int idx = SVOp->getMaskElt(i);
3352 if (idx < 0)
3353 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003354 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003355 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003356 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003357 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003358 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003359 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
3360 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003361}
3362
Evan Cheng779ccea2007-12-07 21:30:01 +00003363/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3364/// the two vector operands have swapped position.
Owen Andersone50ed302009-08-10 22:56:29 +00003365static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00003366 unsigned NumElems = VT.getVectorNumElements();
3367 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003368 int idx = Mask[i];
3369 if (idx < 0)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003370 continue;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003371 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003372 Mask[i] = idx + NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003373 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003374 Mask[i] = idx - NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003375 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003376}
3377
Evan Cheng533a0aa2006-04-19 20:35:22 +00003378/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
3379/// match movhlps. The lower half elements should come from upper half of
3380/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003381/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00003382static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
3383 if (Op->getValueType(0).getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00003384 return false;
3385 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003386 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003387 return false;
3388 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003389 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003390 return false;
3391 return true;
3392}
3393
Evan Cheng5ced1d82006-04-06 23:23:56 +00003394/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00003395/// is promoted to a vector. It also returns the LoadSDNode by reference if
3396/// required.
3397static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00003398 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
3399 return false;
3400 N = N->getOperand(0).getNode();
3401 if (!ISD::isNON_EXTLoad(N))
3402 return false;
3403 if (LD)
3404 *LD = cast<LoadSDNode>(N);
3405 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003406}
3407
Evan Cheng533a0aa2006-04-19 20:35:22 +00003408/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
3409/// match movlp{s|d}. The lower half elements should come from lower half of
3410/// V1 (and in order), and the upper half elements should come from the upper
3411/// half of V2 (and in order). And since V1 will become the source of the
3412/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003413static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
3414 ShuffleVectorSDNode *Op) {
Evan Cheng466685d2006-10-09 20:57:25 +00003415 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003416 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00003417 // Is V2 is a vector load, don't do this transformation. We will try to use
3418 // load folding shufps op.
3419 if (ISD::isNON_EXTLoad(V2))
3420 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003421
Nate Begeman5a5ca152009-04-29 05:20:52 +00003422 unsigned NumElems = Op->getValueType(0).getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003423
Evan Cheng533a0aa2006-04-19 20:35:22 +00003424 if (NumElems != 2 && NumElems != 4)
3425 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003426 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003427 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003428 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003429 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003430 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003431 return false;
3432 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003433}
3434
Evan Cheng39623da2006-04-20 08:58:49 +00003435/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
3436/// all the same.
3437static bool isSplatVector(SDNode *N) {
3438 if (N->getOpcode() != ISD::BUILD_VECTOR)
3439 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003440
Dan Gohman475871a2008-07-27 21:46:04 +00003441 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00003442 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
3443 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003444 return false;
3445 return true;
3446}
3447
Evan Cheng213d2cf2007-05-17 18:45:50 +00003448/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00003449/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00003450/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00003451static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00003452 SDValue V1 = N->getOperand(0);
3453 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003454 unsigned NumElems = N->getValueType(0).getVectorNumElements();
3455 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003456 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003457 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003458 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00003459 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
3460 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003461 if (Opc != ISD::BUILD_VECTOR ||
3462 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00003463 return false;
3464 } else if (Idx >= 0) {
3465 unsigned Opc = V1.getOpcode();
3466 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
3467 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003468 if (Opc != ISD::BUILD_VECTOR ||
3469 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00003470 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00003471 }
3472 }
3473 return true;
3474}
3475
3476/// getZeroVector - Returns a vector of specified type with all zero elements.
3477///
Owen Andersone50ed302009-08-10 22:56:29 +00003478static SDValue getZeroVector(EVT VT, bool HasSSE2, SelectionDAG &DAG,
Dale Johannesenace16102009-02-03 19:33:06 +00003479 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003480 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003481
Dale Johannesen0488fb62010-09-30 23:57:10 +00003482 // Always build SSE zero vectors as <4 x i32> bitcasted
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003483 // to their dest type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00003484 SDValue Vec;
Dale Johannesen0488fb62010-09-30 23:57:10 +00003485 if (VT.getSizeInBits() == 128) { // SSE
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003486 if (HasSSE2) { // SSE2
3487 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3488 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
3489 } else { // SSE1
3490 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
3491 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
3492 }
3493 } else if (VT.getSizeInBits() == 256) { // AVX
3494 // 256-bit logic and arithmetic instructions in AVX are
3495 // all floating-point, no support for integer ops. Default
3496 // to emitting fp zeroed vectors then.
Owen Anderson825b72b2009-08-11 20:47:22 +00003497 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003498 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
3499 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops, 8);
Evan Chengf0df0312008-05-15 08:39:06 +00003500 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003501 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00003502}
3503
Chris Lattner8a594482007-11-25 00:24:49 +00003504/// getOnesVector - Returns a vector of specified type with all bits set.
3505///
Owen Andersone50ed302009-08-10 22:56:29 +00003506static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003507 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003508
Chris Lattner8a594482007-11-25 00:24:49 +00003509 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
3510 // type. This ensures they get CSE'd.
Owen Anderson825b72b2009-08-11 20:47:22 +00003511 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00003512 SDValue Vec;
Dale Johannesen0488fb62010-09-30 23:57:10 +00003513 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003514 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00003515}
3516
3517
Evan Cheng39623da2006-04-20 08:58:49 +00003518/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
3519/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00003520static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003521 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003522 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003523
Evan Cheng39623da2006-04-20 08:58:49 +00003524 bool Changed = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003525 SmallVector<int, 8> MaskVec;
3526 SVOp->getMask(MaskVec);
Eric Christopherfd179292009-08-27 18:07:15 +00003527
Nate Begeman5a5ca152009-04-29 05:20:52 +00003528 for (unsigned i = 0; i != NumElems; ++i) {
3529 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003530 MaskVec[i] = NumElems;
3531 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00003532 }
Evan Cheng39623da2006-04-20 08:58:49 +00003533 }
Evan Cheng39623da2006-04-20 08:58:49 +00003534 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00003535 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
3536 SVOp->getOperand(1), &MaskVec[0]);
3537 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00003538}
3539
Evan Cheng017dcc62006-04-21 01:05:10 +00003540/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
3541/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00003542static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003543 SDValue V2) {
3544 unsigned NumElems = VT.getVectorNumElements();
3545 SmallVector<int, 8> Mask;
3546 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00003547 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003548 Mask.push_back(i);
3549 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00003550}
3551
Nate Begeman9008ca62009-04-27 18:41:29 +00003552/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003553static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003554 SDValue V2) {
3555 unsigned NumElems = VT.getVectorNumElements();
3556 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00003557 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003558 Mask.push_back(i);
3559 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00003560 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003561 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00003562}
3563
Nate Begeman9008ca62009-04-27 18:41:29 +00003564/// getUnpackhMask - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003565static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003566 SDValue V2) {
3567 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00003568 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00003569 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00003570 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003571 Mask.push_back(i + Half);
3572 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00003573 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003574 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00003575}
3576
Bruno Cardoso Lopesbb0a9482010-08-13 17:50:47 +00003577/// PromoteSplat - Promote a splat of v4i32, v8i16 or v16i8 to v4f32.
3578static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003579 EVT PVT = MVT::v4f32;
Owen Andersone50ed302009-08-10 22:56:29 +00003580 EVT VT = SV->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00003581 DebugLoc dl = SV->getDebugLoc();
3582 SDValue V1 = SV->getOperand(0);
3583 int NumElems = VT.getVectorNumElements();
3584 int EltNo = SV->getSplatIndex();
Rafael Espindola15684b22009-04-24 12:40:33 +00003585
Nate Begeman9008ca62009-04-27 18:41:29 +00003586 // unpack elements to the correct location
3587 while (NumElems > 4) {
3588 if (EltNo < NumElems/2) {
3589 V1 = getUnpackl(DAG, dl, VT, V1, V1);
3590 } else {
3591 V1 = getUnpackh(DAG, dl, VT, V1, V1);
3592 EltNo -= NumElems/2;
3593 }
3594 NumElems >>= 1;
3595 }
Eric Christopherfd179292009-08-27 18:07:15 +00003596
Nate Begeman9008ca62009-04-27 18:41:29 +00003597 // Perform the splat.
3598 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003599 V1 = DAG.getNode(ISD::BITCAST, dl, PVT, V1);
Nate Begeman9008ca62009-04-27 18:41:29 +00003600 V1 = DAG.getVectorShuffle(PVT, dl, V1, DAG.getUNDEF(PVT), &SplatMask[0]);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003601 return DAG.getNode(ISD::BITCAST, dl, VT, V1);
Evan Chengc575ca22006-04-17 20:43:08 +00003602}
3603
Evan Chengba05f722006-04-21 23:03:30 +00003604/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00003605/// vector of zero or undef vector. This produces a shuffle where the low
3606/// element of V2 is swizzled into the zero/undef vector, landing at element
3607/// 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 +00003608static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Chengf0df0312008-05-15 08:39:06 +00003609 bool isZero, bool HasSSE2,
3610 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003611 EVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00003612 SDValue V1 = isZero
Nate Begeman9008ca62009-04-27 18:41:29 +00003613 ? getZeroVector(VT, HasSSE2, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
3614 unsigned NumElems = VT.getVectorNumElements();
3615 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00003616 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003617 // If this is the insertion idx, put the low elt of V2 here.
3618 MaskVec.push_back(i == Idx ? NumElems : i);
3619 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00003620}
3621
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003622/// getShuffleScalarElt - Returns the scalar element that will make up the ith
3623/// element of the result of the vector shuffle.
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003624SDValue getShuffleScalarElt(SDNode *N, int Index, SelectionDAG &DAG,
3625 unsigned Depth) {
3626 if (Depth == 6)
3627 return SDValue(); // Limit search depth.
3628
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003629 SDValue V = SDValue(N, 0);
3630 EVT VT = V.getValueType();
3631 unsigned Opcode = V.getOpcode();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003632
3633 // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
3634 if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
3635 Index = SV->getMaskElt(Index);
3636
3637 if (Index < 0)
3638 return DAG.getUNDEF(VT.getVectorElementType());
3639
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003640 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003641 SDValue NewV = (Index < NumElems) ? SV->getOperand(0) : SV->getOperand(1);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003642 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG, Depth+1);
Evan Chengf26ffe92008-05-29 08:22:04 +00003643 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003644
3645 // Recurse into target specific vector shuffles to find scalars.
3646 if (isTargetShuffle(Opcode)) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003647 int NumElems = VT.getVectorNumElements();
3648 SmallVector<unsigned, 16> ShuffleMask;
3649 SDValue ImmN;
3650
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003651 switch(Opcode) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003652 case X86ISD::SHUFPS:
3653 case X86ISD::SHUFPD:
3654 ImmN = N->getOperand(N->getNumOperands()-1);
3655 DecodeSHUFPSMask(NumElems,
3656 cast<ConstantSDNode>(ImmN)->getZExtValue(),
3657 ShuffleMask);
3658 break;
3659 case X86ISD::PUNPCKHBW:
3660 case X86ISD::PUNPCKHWD:
3661 case X86ISD::PUNPCKHDQ:
3662 case X86ISD::PUNPCKHQDQ:
3663 DecodePUNPCKHMask(NumElems, ShuffleMask);
3664 break;
3665 case X86ISD::UNPCKHPS:
3666 case X86ISD::UNPCKHPD:
3667 DecodeUNPCKHPMask(NumElems, ShuffleMask);
3668 break;
3669 case X86ISD::PUNPCKLBW:
3670 case X86ISD::PUNPCKLWD:
3671 case X86ISD::PUNPCKLDQ:
3672 case X86ISD::PUNPCKLQDQ:
3673 DecodePUNPCKLMask(NumElems, ShuffleMask);
3674 break;
3675 case X86ISD::UNPCKLPS:
3676 case X86ISD::UNPCKLPD:
3677 DecodeUNPCKLPMask(NumElems, ShuffleMask);
3678 break;
3679 case X86ISD::MOVHLPS:
3680 DecodeMOVHLPSMask(NumElems, ShuffleMask);
3681 break;
3682 case X86ISD::MOVLHPS:
3683 DecodeMOVLHPSMask(NumElems, ShuffleMask);
3684 break;
3685 case X86ISD::PSHUFD:
3686 ImmN = N->getOperand(N->getNumOperands()-1);
3687 DecodePSHUFMask(NumElems,
3688 cast<ConstantSDNode>(ImmN)->getZExtValue(),
3689 ShuffleMask);
3690 break;
3691 case X86ISD::PSHUFHW:
3692 ImmN = N->getOperand(N->getNumOperands()-1);
3693 DecodePSHUFHWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
3694 ShuffleMask);
3695 break;
3696 case X86ISD::PSHUFLW:
3697 ImmN = N->getOperand(N->getNumOperands()-1);
3698 DecodePSHUFLWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
3699 ShuffleMask);
3700 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003701 case X86ISD::MOVSS:
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00003702 case X86ISD::MOVSD: {
3703 // The index 0 always comes from the first element of the second source,
3704 // this is why MOVSS and MOVSD are used in the first place. The other
3705 // elements come from the other positions of the first source vector.
3706 unsigned OpNum = (Index == 0) ? 1 : 0;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003707 return getShuffleScalarElt(V.getOperand(OpNum).getNode(), Index, DAG,
3708 Depth+1);
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00003709 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003710 default:
3711 assert("not implemented for target shuffle node");
3712 return SDValue();
3713 }
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003714
3715 Index = ShuffleMask[Index];
3716 if (Index < 0)
3717 return DAG.getUNDEF(VT.getVectorElementType());
3718
3719 SDValue NewV = (Index < NumElems) ? N->getOperand(0) : N->getOperand(1);
3720 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG,
3721 Depth+1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003722 }
3723
3724 // Actual nodes that may contain scalar elements
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003725 if (Opcode == ISD::BITCAST) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003726 V = V.getOperand(0);
3727 EVT SrcVT = V.getValueType();
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003728 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003729
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003730 if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003731 return SDValue();
3732 }
3733
3734 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
3735 return (Index == 0) ? V.getOperand(0)
3736 : DAG.getUNDEF(VT.getVectorElementType());
3737
3738 if (V.getOpcode() == ISD::BUILD_VECTOR)
3739 return V.getOperand(Index);
3740
3741 return SDValue();
3742}
3743
3744/// getNumOfConsecutiveZeros - Return the number of elements of a vector
3745/// shuffle operation which come from a consecutively from a zero. The
3746/// search can start in two diferent directions, from left or right.
3747static
3748unsigned getNumOfConsecutiveZeros(SDNode *N, int NumElems,
3749 bool ZerosFromLeft, SelectionDAG &DAG) {
3750 int i = 0;
3751
3752 while (i < NumElems) {
3753 unsigned Index = ZerosFromLeft ? i : NumElems-i-1;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003754 SDValue Elt = getShuffleScalarElt(N, Index, DAG, 0);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003755 if (!(Elt.getNode() &&
3756 (Elt.getOpcode() == ISD::UNDEF || X86::isZeroNode(Elt))))
3757 break;
3758 ++i;
3759 }
3760
3761 return i;
3762}
3763
3764/// isShuffleMaskConsecutive - Check if the shuffle mask indicies from MaskI to
3765/// MaskE correspond consecutively to elements from one of the vector operands,
3766/// starting from its index OpIdx. Also tell OpNum which source vector operand.
3767static
3768bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp, int MaskI, int MaskE,
3769 int OpIdx, int NumElems, unsigned &OpNum) {
3770 bool SeenV1 = false;
3771 bool SeenV2 = false;
3772
3773 for (int i = MaskI; i <= MaskE; ++i, ++OpIdx) {
3774 int Idx = SVOp->getMaskElt(i);
3775 // Ignore undef indicies
3776 if (Idx < 0)
3777 continue;
3778
3779 if (Idx < NumElems)
3780 SeenV1 = true;
3781 else
3782 SeenV2 = true;
3783
3784 // Only accept consecutive elements from the same vector
3785 if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
3786 return false;
3787 }
3788
3789 OpNum = SeenV1 ? 0 : 1;
3790 return true;
3791}
3792
3793/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
3794/// logical left shift of a vector.
3795static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
3796 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
3797 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
3798 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
3799 false /* check zeros from right */, DAG);
3800 unsigned OpSrc;
3801
3802 if (!NumZeros)
3803 return false;
3804
3805 // Considering the elements in the mask that are not consecutive zeros,
3806 // check if they consecutively come from only one of the source vectors.
3807 //
3808 // V1 = {X, A, B, C} 0
3809 // \ \ \ /
3810 // vector_shuffle V1, V2 <1, 2, 3, X>
3811 //
3812 if (!isShuffleMaskConsecutive(SVOp,
3813 0, // Mask Start Index
3814 NumElems-NumZeros-1, // Mask End Index
3815 NumZeros, // Where to start looking in the src vector
3816 NumElems, // Number of elements in vector
3817 OpSrc)) // Which source operand ?
3818 return false;
3819
3820 isLeft = false;
3821 ShAmt = NumZeros;
3822 ShVal = SVOp->getOperand(OpSrc);
3823 return true;
3824}
3825
3826/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
3827/// logical left shift of a vector.
3828static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
3829 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
3830 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
3831 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
3832 true /* check zeros from left */, DAG);
3833 unsigned OpSrc;
3834
3835 if (!NumZeros)
3836 return false;
3837
3838 // Considering the elements in the mask that are not consecutive zeros,
3839 // check if they consecutively come from only one of the source vectors.
3840 //
3841 // 0 { A, B, X, X } = V2
3842 // / \ / /
3843 // vector_shuffle V1, V2 <X, X, 4, 5>
3844 //
3845 if (!isShuffleMaskConsecutive(SVOp,
3846 NumZeros, // Mask Start Index
3847 NumElems-1, // Mask End Index
3848 0, // Where to start looking in the src vector
3849 NumElems, // Number of elements in vector
3850 OpSrc)) // Which source operand ?
3851 return false;
3852
3853 isLeft = true;
3854 ShAmt = NumZeros;
3855 ShVal = SVOp->getOperand(OpSrc);
3856 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00003857}
3858
3859/// isVectorShift - Returns true if the shuffle can be implemented as a
3860/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003861static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00003862 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003863 if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
3864 isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
3865 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00003866
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003867 return false;
Evan Chengf26ffe92008-05-29 08:22:04 +00003868}
3869
Evan Chengc78d3b42006-04-24 18:01:45 +00003870/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
3871///
Dan Gohman475871a2008-07-27 21:46:04 +00003872static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00003873 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00003874 SelectionDAG &DAG,
3875 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003876 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00003877 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00003878
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003879 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003880 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003881 bool First = true;
3882 for (unsigned i = 0; i < 16; ++i) {
3883 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
3884 if (ThisIsNonZero && First) {
3885 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003886 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00003887 else
Owen Anderson825b72b2009-08-11 20:47:22 +00003888 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00003889 First = false;
3890 }
3891
3892 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00003893 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003894 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
3895 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003896 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003897 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00003898 }
3899 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003900 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
3901 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
3902 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00003903 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003904 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00003905 } else
3906 ThisElt = LastElt;
3907
Gabor Greifba36cb52008-08-28 21:40:38 +00003908 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00003909 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00003910 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00003911 }
3912 }
3913
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003914 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00003915}
3916
Bill Wendlinga348c562007-03-22 18:42:45 +00003917/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00003918///
Dan Gohman475871a2008-07-27 21:46:04 +00003919static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00003920 unsigned NumNonZero, unsigned NumZero,
3921 SelectionDAG &DAG,
3922 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003923 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00003924 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00003925
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003926 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003927 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003928 bool First = true;
3929 for (unsigned i = 0; i < 8; ++i) {
3930 bool isNonZero = (NonZeros & (1 << i)) != 0;
3931 if (isNonZero) {
3932 if (First) {
3933 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003934 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00003935 else
Owen Anderson825b72b2009-08-11 20:47:22 +00003936 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00003937 First = false;
3938 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003939 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003940 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00003941 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00003942 }
3943 }
3944
3945 return V;
3946}
3947
Evan Chengf26ffe92008-05-29 08:22:04 +00003948/// getVShift - Return a vector logical shift node.
3949///
Owen Andersone50ed302009-08-10 22:56:29 +00003950static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00003951 unsigned NumBits, SelectionDAG &DAG,
3952 const TargetLowering &TLI, DebugLoc dl) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00003953 EVT ShVT = MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00003954 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003955 SrcOp = DAG.getNode(ISD::BITCAST, dl, ShVT, SrcOp);
3956 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00003957 DAG.getNode(Opc, dl, ShVT, SrcOp,
Gabor Greif327ef032008-08-28 23:19:51 +00003958 DAG.getConstant(NumBits, TLI.getShiftAmountTy())));
Evan Chengf26ffe92008-05-29 08:22:04 +00003959}
3960
Dan Gohman475871a2008-07-27 21:46:04 +00003961SDValue
Evan Chengc3630942009-12-09 21:00:30 +00003962X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00003963 SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00003964
Evan Chengc3630942009-12-09 21:00:30 +00003965 // Check if the scalar load can be widened into a vector load. And if
3966 // the address is "base + cst" see if the cst can be "absorbed" into
3967 // the shuffle mask.
3968 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
3969 SDValue Ptr = LD->getBasePtr();
3970 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
3971 return SDValue();
3972 EVT PVT = LD->getValueType(0);
3973 if (PVT != MVT::i32 && PVT != MVT::f32)
3974 return SDValue();
3975
3976 int FI = -1;
3977 int64_t Offset = 0;
3978 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
3979 FI = FINode->getIndex();
3980 Offset = 0;
3981 } else if (Ptr.getOpcode() == ISD::ADD &&
3982 isa<ConstantSDNode>(Ptr.getOperand(1)) &&
3983 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
3984 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
3985 Offset = Ptr.getConstantOperandVal(1);
3986 Ptr = Ptr.getOperand(0);
3987 } else {
3988 return SDValue();
3989 }
3990
3991 SDValue Chain = LD->getChain();
3992 // Make sure the stack object alignment is at least 16.
3993 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3994 if (DAG.InferPtrAlignment(Ptr) < 16) {
3995 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00003996 // Can't change the alignment. FIXME: It's possible to compute
3997 // the exact stack offset and reference FI + adjust offset instead.
3998 // If someone *really* cares about this. That's the way to implement it.
3999 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004000 } else {
4001 MFI->setObjectAlignment(FI, 16);
4002 }
4003 }
4004
4005 // (Offset % 16) must be multiple of 4. Then address is then
4006 // Ptr + (Offset & ~15).
4007 if (Offset < 0)
4008 return SDValue();
4009 if ((Offset % 16) & 3)
4010 return SDValue();
4011 int64_t StartOffset = Offset & ~15;
4012 if (StartOffset)
4013 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
4014 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
4015
4016 int EltNo = (Offset - StartOffset) >> 2;
4017 int Mask[4] = { EltNo, EltNo, EltNo, EltNo };
4018 EVT VT = (PVT == MVT::i32) ? MVT::v4i32 : MVT::v4f32;
Chris Lattner51abfe42010-09-21 06:02:19 +00004019 SDValue V1 = DAG.getLoad(VT, dl, Chain, Ptr,
4020 LD->getPointerInfo().getWithOffset(StartOffset),
David Greene67c9d422010-02-15 16:53:33 +00004021 false, false, 0);
Evan Chengc3630942009-12-09 21:00:30 +00004022 // Canonicalize it to a v4i32 shuffle.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004023 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, V1);
4024 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Chengc3630942009-12-09 21:00:30 +00004025 DAG.getVectorShuffle(MVT::v4i32, dl, V1,
Chris Lattner51abfe42010-09-21 06:02:19 +00004026 DAG.getUNDEF(MVT::v4i32),&Mask[0]));
Evan Chengc3630942009-12-09 21:00:30 +00004027 }
4028
4029 return SDValue();
4030}
4031
Michael J. Spencerec38de22010-10-10 22:04:20 +00004032/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
4033/// vector of type 'VT', see if the elements can be replaced by a single large
Nate Begeman1449f292010-03-24 22:19:06 +00004034/// load which has the same value as a build_vector whose operands are 'elts'.
4035///
4036/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
Michael J. Spencerec38de22010-10-10 22:04:20 +00004037///
Nate Begeman1449f292010-03-24 22:19:06 +00004038/// FIXME: we'd also like to handle the case where the last elements are zero
4039/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
4040/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004041static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
Chris Lattner88641552010-09-22 00:34:38 +00004042 DebugLoc &DL, SelectionDAG &DAG) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00004043 EVT EltVT = VT.getVectorElementType();
4044 unsigned NumElems = Elts.size();
Michael J. Spencerec38de22010-10-10 22:04:20 +00004045
Nate Begemanfdea31a2010-03-24 20:49:50 +00004046 LoadSDNode *LDBase = NULL;
4047 unsigned LastLoadedElt = -1U;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004048
Nate Begeman1449f292010-03-24 22:19:06 +00004049 // For each element in the initializer, see if we've found a load or an undef.
Michael J. Spencerec38de22010-10-10 22:04:20 +00004050 // If we don't find an initial load element, or later load elements are
Nate Begeman1449f292010-03-24 22:19:06 +00004051 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004052 for (unsigned i = 0; i < NumElems; ++i) {
4053 SDValue Elt = Elts[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00004054
Nate Begemanfdea31a2010-03-24 20:49:50 +00004055 if (!Elt.getNode() ||
4056 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
4057 return SDValue();
4058 if (!LDBase) {
4059 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
4060 return SDValue();
4061 LDBase = cast<LoadSDNode>(Elt.getNode());
4062 LastLoadedElt = i;
4063 continue;
4064 }
4065 if (Elt.getOpcode() == ISD::UNDEF)
4066 continue;
4067
4068 LoadSDNode *LD = cast<LoadSDNode>(Elt);
4069 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
4070 return SDValue();
4071 LastLoadedElt = i;
4072 }
Nate Begeman1449f292010-03-24 22:19:06 +00004073
4074 // If we have found an entire vector of loads and undefs, then return a large
4075 // load of the entire vector width starting at the base pointer. If we found
4076 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004077 if (LastLoadedElt == NumElems - 1) {
4078 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
Chris Lattner88641552010-09-22 00:34:38 +00004079 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004080 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004081 LDBase->isVolatile(), LDBase->isNonTemporal(), 0);
Chris Lattner88641552010-09-22 00:34:38 +00004082 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004083 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004084 LDBase->isVolatile(), LDBase->isNonTemporal(),
4085 LDBase->getAlignment());
4086 } else if (NumElems == 4 && LastLoadedElt == 1) {
4087 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
4088 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
Chris Lattner88641552010-09-22 00:34:38 +00004089 SDValue ResNode = DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys,
4090 Ops, 2, MVT::i32,
4091 LDBase->getMemOperand());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004092 return DAG.getNode(ISD::BITCAST, DL, VT, ResNode);
Nate Begemanfdea31a2010-03-24 20:49:50 +00004093 }
4094 return SDValue();
4095}
4096
Evan Chengc3630942009-12-09 21:00:30 +00004097SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004098X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004099 DebugLoc dl = Op.getDebugLoc();
Chris Lattner6e80e442010-08-28 17:15:43 +00004100 // All zero's are handled with pxor in SSE2 and above, xorps in SSE1.
4101 // All one's are handled with pcmpeqd. In AVX, zero's are handled with
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004102 // vpxor in 128-bit and xor{pd,ps} in 256-bit, but no 256 version of pcmpeqd
4103 // is present, so AllOnes is ignored.
4104 if (ISD::isBuildVectorAllZeros(Op.getNode()) ||
4105 (Op.getValueType().getSizeInBits() != 256 &&
4106 ISD::isBuildVectorAllOnes(Op.getNode()))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004107 // Canonicalize this to <4 x i32> (SSE) to
Chris Lattner8a594482007-11-25 00:24:49 +00004108 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
4109 // eliminated on x86-32 hosts.
Dale Johannesen0488fb62010-09-30 23:57:10 +00004110 if (Op.getValueType() == MVT::v4i32)
Chris Lattner8a594482007-11-25 00:24:49 +00004111 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004112
Gabor Greifba36cb52008-08-28 21:40:38 +00004113 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00004114 return getOnesVector(Op.getValueType(), DAG, dl);
4115 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00004116 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004117
Owen Andersone50ed302009-08-10 22:56:29 +00004118 EVT VT = Op.getValueType();
4119 EVT ExtVT = VT.getVectorElementType();
4120 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004121
4122 unsigned NumElems = Op.getNumOperands();
4123 unsigned NumZero = 0;
4124 unsigned NumNonZero = 0;
4125 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004126 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00004127 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004128 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00004129 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00004130 if (Elt.getOpcode() == ISD::UNDEF)
4131 continue;
4132 Values.insert(Elt);
4133 if (Elt.getOpcode() != ISD::Constant &&
4134 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004135 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00004136 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00004137 NumZero++;
4138 else {
4139 NonZeros |= (1 << i);
4140 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004141 }
4142 }
4143
Chris Lattner97a2a562010-08-26 05:24:29 +00004144 // All undef vector. Return an UNDEF. All zero vectors were handled above.
4145 if (NumNonZero == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00004146 return DAG.getUNDEF(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004147
Chris Lattner67f453a2008-03-09 05:42:06 +00004148 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00004149 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004150 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00004151 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00004152
Chris Lattner62098042008-03-09 01:05:04 +00004153 // If this is an insertion of an i64 value on x86-32, and if the top bits of
4154 // the value are obviously zero, truncate the value to i32 and do the
4155 // insertion that way. Only do this if the value is non-constant or if the
4156 // value is a constant being inserted into element 0. It is cheaper to do
4157 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00004158 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00004159 (!IsAllConstants || Idx == 0)) {
4160 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004161 // Handle SSE only.
4162 assert(VT == MVT::v2i64 && "Expected an SSE value type!");
4163 EVT VecVT = MVT::v4i32;
4164 unsigned VecElts = 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00004165
Chris Lattner62098042008-03-09 01:05:04 +00004166 // Truncate the value (which may itself be a constant) to i32, and
4167 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00004168 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00004169 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00004170 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
4171 Subtarget->hasSSE2(), DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00004172
Chris Lattner62098042008-03-09 01:05:04 +00004173 // Now we have our 32-bit value zero extended in the low element of
4174 // a vector. If Idx != 0, swizzle it into place.
4175 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004176 SmallVector<int, 4> Mask;
4177 Mask.push_back(Idx);
4178 for (unsigned i = 1; i != VecElts; ++i)
4179 Mask.push_back(i);
4180 Item = DAG.getVectorShuffle(VecVT, dl, Item,
Eric Christopherfd179292009-08-27 18:07:15 +00004181 DAG.getUNDEF(Item.getValueType()),
Nate Begeman9008ca62009-04-27 18:41:29 +00004182 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00004183 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004184 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00004185 }
4186 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004187
Chris Lattner19f79692008-03-08 22:59:52 +00004188 // If we have a constant or non-constant insertion into the low element of
4189 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
4190 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00004191 // depending on what the source datatype is.
4192 if (Idx == 0) {
4193 if (NumZero == 0) {
4194 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Owen Anderson825b72b2009-08-11 20:47:22 +00004195 } else if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
4196 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Eli Friedman10415532009-06-06 06:05:10 +00004197 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
4198 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
4199 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget->hasSSE2(),
4200 DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00004201 } else if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
4202 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
Dale Johannesen0488fb62010-09-30 23:57:10 +00004203 assert(VT.getSizeInBits() == 128 && "Expected an SSE value type!");
4204 EVT MiddleVT = MVT::v4i32;
Eli Friedman10415532009-06-06 06:05:10 +00004205 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MiddleVT, Item);
4206 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
4207 Subtarget->hasSSE2(), DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004208 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Eli Friedman10415532009-06-06 06:05:10 +00004209 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004210 }
Evan Chengf26ffe92008-05-29 08:22:04 +00004211
4212 // Is it a vector logical left shift?
4213 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00004214 X86::isZeroNode(Op.getOperand(0)) &&
4215 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004216 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00004217 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00004218 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004219 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00004220 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004221 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004222
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004223 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00004224 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004225
Chris Lattner19f79692008-03-08 22:59:52 +00004226 // Otherwise, if this is a vector with i32 or f32 elements, and the element
4227 // is a non-constant being inserted into an element other than the low one,
4228 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
4229 // movd/movss) to move this into the low element, then shuffle it into
4230 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004231 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00004232 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00004233
Evan Cheng0db9fe62006-04-25 20:13:52 +00004234 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00004235 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
4236 Subtarget->hasSSE2(), DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00004237 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004238 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00004239 MaskVec.push_back(i == Idx ? 0 : 1);
4240 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004241 }
4242 }
4243
Chris Lattner67f453a2008-03-09 05:42:06 +00004244 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00004245 if (Values.size() == 1) {
4246 if (EVTBits == 32) {
4247 // Instead of a shuffle like this:
4248 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
4249 // Check if it's possible to issue this instead.
4250 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
4251 unsigned Idx = CountTrailingZeros_32(NonZeros);
4252 SDValue Item = Op.getOperand(Idx);
4253 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
4254 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
4255 }
Dan Gohman475871a2008-07-27 21:46:04 +00004256 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004257 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004258
Dan Gohmana3941172007-07-24 22:55:08 +00004259 // A vector full of immediates; various special cases are already
4260 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004261 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00004262 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00004263
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00004264 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00004265 if (EVTBits == 64) {
4266 if (NumNonZero == 1) {
4267 // One half is zero or undef.
4268 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00004269 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00004270 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00004271 return getShuffleVectorZeroOrUndef(V2, Idx, true,
4272 Subtarget->hasSSE2(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00004273 }
Dan Gohman475871a2008-07-27 21:46:04 +00004274 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00004275 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004276
4277 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00004278 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00004279 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00004280 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004281 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004282 }
4283
Bill Wendling826f36f2007-03-28 00:57:11 +00004284 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00004285 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Chris Lattner97a2a562010-08-26 05:24:29 +00004286 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004287 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004288 }
4289
4290 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00004291 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00004292 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004293 if (NumElems == 4 && NumZero > 0) {
4294 for (unsigned i = 0; i < 4; ++i) {
4295 bool isZero = !(NonZeros & (1 << i));
4296 if (isZero)
Dale Johannesenace16102009-02-03 19:33:06 +00004297 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004298 else
Dale Johannesenace16102009-02-03 19:33:06 +00004299 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004300 }
4301
4302 for (unsigned i = 0; i < 2; ++i) {
4303 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
4304 default: break;
4305 case 0:
4306 V[i] = V[i*2]; // Must be a zero vector.
4307 break;
4308 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00004309 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004310 break;
4311 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00004312 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004313 break;
4314 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00004315 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004316 break;
4317 }
4318 }
4319
Nate Begeman9008ca62009-04-27 18:41:29 +00004320 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004321 bool Reverse = (NonZeros & 0x3) == 2;
4322 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004323 MaskVec.push_back(Reverse ? 1-i : i);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004324 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
4325 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004326 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
4327 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004328 }
4329
Nate Begemanfdea31a2010-03-24 20:49:50 +00004330 if (Values.size() > 1 && VT.getSizeInBits() == 128) {
4331 // Check for a build vector of consecutive loads.
4332 for (unsigned i = 0; i < NumElems; ++i)
4333 V[i] = Op.getOperand(i);
Michael J. Spencerec38de22010-10-10 22:04:20 +00004334
Nate Begemanfdea31a2010-03-24 20:49:50 +00004335 // Check for elements which are consecutive loads.
4336 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
4337 if (LD.getNode())
4338 return LD;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004339
4340 // For SSE 4.1, use insertps to put the high elements into the low element.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004341 if (getSubtarget()->hasSSE41()) {
Chris Lattner24faf612010-08-28 17:59:08 +00004342 SDValue Result;
4343 if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
4344 Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
4345 else
4346 Result = DAG.getUNDEF(VT);
Michael J. Spencerec38de22010-10-10 22:04:20 +00004347
Chris Lattner24faf612010-08-28 17:59:08 +00004348 for (unsigned i = 1; i < NumElems; ++i) {
4349 if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
4350 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
Nate Begeman9008ca62009-04-27 18:41:29 +00004351 Op.getOperand(i), DAG.getIntPtrConstant(i));
Chris Lattner24faf612010-08-28 17:59:08 +00004352 }
4353 return Result;
Nate Begeman9008ca62009-04-27 18:41:29 +00004354 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00004355
Chris Lattner6e80e442010-08-28 17:15:43 +00004356 // Otherwise, expand into a number of unpckl*, start by extending each of
4357 // our (non-undef) elements to the full vector width with the element in the
4358 // bottom slot of the vector (which generates no code for SSE).
4359 for (unsigned i = 0; i < NumElems; ++i) {
4360 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
4361 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
4362 else
4363 V[i] = DAG.getUNDEF(VT);
4364 }
4365
4366 // Next, we iteratively mix elements, e.g. for v4f32:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004367 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
4368 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
4369 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Chris Lattner6e80e442010-08-28 17:15:43 +00004370 unsigned EltStride = NumElems >> 1;
4371 while (EltStride != 0) {
Chris Lattner3ddcc432010-08-28 17:28:30 +00004372 for (unsigned i = 0; i < EltStride; ++i) {
4373 // If V[i+EltStride] is undef and this is the first round of mixing,
4374 // then it is safe to just drop this shuffle: V[i] is already in the
4375 // right place, the one element (since it's the first round) being
4376 // inserted as undef can be dropped. This isn't safe for successive
4377 // rounds because they will permute elements within both vectors.
4378 if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
4379 EltStride == NumElems/2)
4380 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004381
Chris Lattner6e80e442010-08-28 17:15:43 +00004382 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
Chris Lattner3ddcc432010-08-28 17:28:30 +00004383 }
Chris Lattner6e80e442010-08-28 17:15:43 +00004384 EltStride >>= 1;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004385 }
4386 return V[0];
4387 }
Dan Gohman475871a2008-07-27 21:46:04 +00004388 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004389}
4390
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004391SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004392X86TargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const {
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004393 // We support concatenate two MMX registers and place them in a MMX
4394 // register. This is better than doing a stack convert.
4395 DebugLoc dl = Op.getDebugLoc();
4396 EVT ResVT = Op.getValueType();
4397 assert(Op.getNumOperands() == 2);
4398 assert(ResVT == MVT::v2i64 || ResVT == MVT::v4i32 ||
4399 ResVT == MVT::v8i16 || ResVT == MVT::v16i8);
4400 int Mask[2];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004401 SDValue InVec = DAG.getNode(ISD::BITCAST,dl, MVT::v1i64, Op.getOperand(0));
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004402 SDValue VecOp = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
4403 InVec = Op.getOperand(1);
4404 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4405 unsigned NumElts = ResVT.getVectorNumElements();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004406 VecOp = DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004407 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, ResVT, VecOp,
4408 InVec.getOperand(0), DAG.getIntPtrConstant(NumElts/2+1));
4409 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004410 InVec = DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, InVec);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004411 SDValue VecOp2 = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
4412 Mask[0] = 0; Mask[1] = 2;
4413 VecOp = DAG.getVectorShuffle(MVT::v2i64, dl, VecOp, VecOp2, Mask);
4414 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004415 return DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004416}
4417
Nate Begemanb9a47b82009-02-23 08:49:38 +00004418// v8i16 shuffles - Prefer shuffles in the following order:
4419// 1. [all] pshuflw, pshufhw, optional move
4420// 2. [ssse3] 1 x pshufb
4421// 3. [ssse3] 2 x pshufb + 1 x por
4422// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004423SDValue
4424X86TargetLowering::LowerVECTOR_SHUFFLEv8i16(SDValue Op,
4425 SelectionDAG &DAG) const {
4426 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00004427 SDValue V1 = SVOp->getOperand(0);
4428 SDValue V2 = SVOp->getOperand(1);
4429 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00004430 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00004431
Nate Begemanb9a47b82009-02-23 08:49:38 +00004432 // Determine if more than 1 of the words in each of the low and high quadwords
4433 // of the result come from the same quadword of one of the two inputs. Undef
4434 // mask values count as coming from any quadword, for better codegen.
4435 SmallVector<unsigned, 4> LoQuad(4);
4436 SmallVector<unsigned, 4> HiQuad(4);
4437 BitVector InputQuads(4);
4438 for (unsigned i = 0; i < 8; ++i) {
4439 SmallVectorImpl<unsigned> &Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00004440 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004441 MaskVals.push_back(EltIdx);
4442 if (EltIdx < 0) {
4443 ++Quad[0];
4444 ++Quad[1];
4445 ++Quad[2];
4446 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00004447 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004448 }
4449 ++Quad[EltIdx / 4];
4450 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00004451 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004452
Nate Begemanb9a47b82009-02-23 08:49:38 +00004453 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00004454 unsigned MaxQuad = 1;
4455 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004456 if (LoQuad[i] > MaxQuad) {
4457 BestLoQuad = i;
4458 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00004459 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004460 }
4461
Nate Begemanb9a47b82009-02-23 08:49:38 +00004462 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00004463 MaxQuad = 1;
4464 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004465 if (HiQuad[i] > MaxQuad) {
4466 BestHiQuad = i;
4467 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00004468 }
4469 }
4470
Nate Begemanb9a47b82009-02-23 08:49:38 +00004471 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00004472 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00004473 // single pshufb instruction is necessary. If There are more than 2 input
4474 // quads, disable the next transformation since it does not help SSSE3.
4475 bool V1Used = InputQuads[0] || InputQuads[1];
4476 bool V2Used = InputQuads[2] || InputQuads[3];
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004477 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004478 if (InputQuads.count() == 2 && V1Used && V2Used) {
4479 BestLoQuad = InputQuads.find_first();
4480 BestHiQuad = InputQuads.find_next(BestLoQuad);
4481 }
4482 if (InputQuads.count() > 2) {
4483 BestLoQuad = -1;
4484 BestHiQuad = -1;
4485 }
4486 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004487
Nate Begemanb9a47b82009-02-23 08:49:38 +00004488 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
4489 // the shuffle mask. If a quad is scored as -1, that means that it contains
4490 // words from all 4 input quadwords.
4491 SDValue NewV;
4492 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004493 SmallVector<int, 8> MaskV;
4494 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
4495 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
Eric Christopherfd179292009-08-27 18:07:15 +00004496 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004497 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V1),
4498 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V2), &MaskV[0]);
4499 NewV = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00004500
Nate Begemanb9a47b82009-02-23 08:49:38 +00004501 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
4502 // source words for the shuffle, to aid later transformations.
4503 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00004504 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00004505 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004506 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00004507 if (idx != (int)i)
4508 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004509 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00004510 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004511 AllWordsInNewV = false;
4512 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00004513 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004514
Nate Begemanb9a47b82009-02-23 08:49:38 +00004515 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
4516 if (AllWordsInNewV) {
4517 for (int i = 0; i != 8; ++i) {
4518 int idx = MaskVals[i];
4519 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00004520 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004521 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004522 if ((idx != i) && idx < 4)
4523 pshufhw = false;
4524 if ((idx != i) && idx > 3)
4525 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00004526 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00004527 V1 = NewV;
4528 V2Used = false;
4529 BestLoQuad = 0;
4530 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004531 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004532
Nate Begemanb9a47b82009-02-23 08:49:38 +00004533 // If we've eliminated the use of V2, and the new mask is a pshuflw or
4534 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00004535 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00004536 unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
4537 unsigned TargetMask = 0;
4538 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00004539 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00004540 TargetMask = pshufhw ? X86::getShufflePSHUFHWImmediate(NewV.getNode()):
4541 X86::getShufflePSHUFLWImmediate(NewV.getNode());
4542 V1 = NewV.getOperand(0);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00004543 return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
Evan Cheng14b32e12007-12-11 01:46:18 +00004544 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004545 }
Eric Christopherfd179292009-08-27 18:07:15 +00004546
Nate Begemanb9a47b82009-02-23 08:49:38 +00004547 // If we have SSSE3, and all words of the result are from 1 input vector,
4548 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
4549 // is present, fall back to case 4.
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004550 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004551 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00004552
Nate Begemanb9a47b82009-02-23 08:49:38 +00004553 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00004554 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00004555 // mask, and elements that come from V1 in the V2 mask, so that the two
4556 // results can be OR'd together.
4557 bool TwoInputs = V1Used && V2Used;
4558 for (unsigned i = 0; i != 8; ++i) {
4559 int EltIdx = MaskVals[i] * 2;
4560 if (TwoInputs && (EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004561 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4562 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004563 continue;
4564 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004565 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
4566 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004567 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004568 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004569 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00004570 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004571 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004572 if (!TwoInputs)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004573 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004574
Nate Begemanb9a47b82009-02-23 08:49:38 +00004575 // Calculate the shuffle mask for the second input, shuffle it, and
4576 // OR it with the first shuffled input.
4577 pshufbMask.clear();
4578 for (unsigned i = 0; i != 8; ++i) {
4579 int EltIdx = MaskVals[i] * 2;
4580 if (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 - 16, MVT::i8));
4586 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004587 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004588 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00004589 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
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));
4592 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004593 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004594 }
4595
4596 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
4597 // and update MaskVals with new element order.
4598 BitVector InOrder(8);
4599 if (BestLoQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004600 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004601 for (int i = 0; i != 4; ++i) {
4602 int idx = MaskVals[i];
4603 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004604 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004605 InOrder.set(i);
4606 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004607 MaskV.push_back(idx & 3);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004608 InOrder.set(i);
4609 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004610 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004611 }
4612 }
4613 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004614 MaskV.push_back(i);
Owen Anderson825b72b2009-08-11 20:47:22 +00004615 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00004616 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00004617
4618 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3())
4619 NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
4620 NewV.getOperand(0),
4621 X86::getShufflePSHUFLWImmediate(NewV.getNode()),
4622 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004623 }
Eric Christopherfd179292009-08-27 18:07:15 +00004624
Nate Begemanb9a47b82009-02-23 08:49:38 +00004625 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
4626 // and update MaskVals with the new element order.
4627 if (BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004628 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004629 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004630 MaskV.push_back(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004631 for (unsigned i = 4; i != 8; ++i) {
4632 int idx = MaskVals[i];
4633 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004634 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004635 InOrder.set(i);
4636 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004637 MaskV.push_back((idx & 3) + 4);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004638 InOrder.set(i);
4639 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004640 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004641 }
4642 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004643 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00004644 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00004645
4646 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3())
4647 NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
4648 NewV.getOperand(0),
4649 X86::getShufflePSHUFHWImmediate(NewV.getNode()),
4650 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004651 }
Eric Christopherfd179292009-08-27 18:07:15 +00004652
Nate Begemanb9a47b82009-02-23 08:49:38 +00004653 // In case BestHi & BestLo were both -1, which means each quadword has a word
4654 // from each of the four input quadwords, calculate the InOrder bitvector now
4655 // before falling through to the insert/extract cleanup.
4656 if (BestLoQuad == -1 && BestHiQuad == -1) {
4657 NewV = V1;
4658 for (int i = 0; i != 8; ++i)
4659 if (MaskVals[i] < 0 || MaskVals[i] == i)
4660 InOrder.set(i);
4661 }
Eric Christopherfd179292009-08-27 18:07:15 +00004662
Nate Begemanb9a47b82009-02-23 08:49:38 +00004663 // The other elements are put in the right place using pextrw and pinsrw.
4664 for (unsigned i = 0; i != 8; ++i) {
4665 if (InOrder[i])
4666 continue;
4667 int EltIdx = MaskVals[i];
4668 if (EltIdx < 0)
4669 continue;
4670 SDValue ExtOp = (EltIdx < 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00004671 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004672 DAG.getIntPtrConstant(EltIdx))
Owen Anderson825b72b2009-08-11 20:47:22 +00004673 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004674 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00004675 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004676 DAG.getIntPtrConstant(i));
4677 }
4678 return NewV;
4679}
4680
4681// v16i8 shuffles - Prefer shuffles in the following order:
4682// 1. [ssse3] 1 x pshufb
4683// 2. [ssse3] 2 x pshufb + 1 x por
4684// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
4685static
Nate Begeman9008ca62009-04-27 18:41:29 +00004686SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00004687 SelectionDAG &DAG,
4688 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004689 SDValue V1 = SVOp->getOperand(0);
4690 SDValue V2 = SVOp->getOperand(1);
4691 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00004692 SmallVector<int, 16> MaskVals;
Nate Begeman9008ca62009-04-27 18:41:29 +00004693 SVOp->getMask(MaskVals);
Eric Christopherfd179292009-08-27 18:07:15 +00004694
Nate Begemanb9a47b82009-02-23 08:49:38 +00004695 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00004696 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00004697 // present, fall back to case 3.
4698 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
4699 bool V1Only = true;
4700 bool V2Only = true;
4701 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004702 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00004703 if (EltIdx < 0)
4704 continue;
4705 if (EltIdx < 16)
4706 V2Only = false;
4707 else
4708 V1Only = false;
4709 }
Eric Christopherfd179292009-08-27 18:07:15 +00004710
Nate Begemanb9a47b82009-02-23 08:49:38 +00004711 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
4712 if (TLI.getSubtarget()->hasSSSE3()) {
4713 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00004714
Nate Begemanb9a47b82009-02-23 08:49:38 +00004715 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00004716 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00004717 //
4718 // Otherwise, we have elements from both input vectors, and must zero out
4719 // elements that come from V2 in the first mask, and V1 in the second mask
4720 // so that we can OR them together.
4721 bool TwoInputs = !(V1Only || V2Only);
4722 for (unsigned i = 0; i != 16; ++i) {
4723 int EltIdx = MaskVals[i];
4724 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004725 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004726 continue;
4727 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004728 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004729 }
4730 // If all the elements are from V2, assign it to V1 and return after
4731 // building the first pshufb.
4732 if (V2Only)
4733 V1 = V2;
Owen Anderson825b72b2009-08-11 20:47:22 +00004734 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00004735 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004736 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004737 if (!TwoInputs)
4738 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00004739
Nate Begemanb9a47b82009-02-23 08:49:38 +00004740 // Calculate the shuffle mask for the second input, shuffle it, and
4741 // OR it with the first shuffled input.
4742 pshufbMask.clear();
4743 for (unsigned i = 0; i != 16; ++i) {
4744 int EltIdx = MaskVals[i];
4745 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004746 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004747 continue;
4748 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004749 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004750 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004751 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00004752 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004753 MVT::v16i8, &pshufbMask[0], 16));
4754 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004755 }
Eric Christopherfd179292009-08-27 18:07:15 +00004756
Nate Begemanb9a47b82009-02-23 08:49:38 +00004757 // No SSSE3 - Calculate in place words and then fix all out of place words
4758 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
4759 // the 16 different words that comprise the two doublequadword input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004760 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
4761 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004762 SDValue NewV = V2Only ? V2 : V1;
4763 for (int i = 0; i != 8; ++i) {
4764 int Elt0 = MaskVals[i*2];
4765 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00004766
Nate Begemanb9a47b82009-02-23 08:49:38 +00004767 // This word of the result is all undef, skip it.
4768 if (Elt0 < 0 && Elt1 < 0)
4769 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004770
Nate Begemanb9a47b82009-02-23 08:49:38 +00004771 // This word of the result is already in the correct place, skip it.
4772 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
4773 continue;
4774 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
4775 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004776
Nate Begemanb9a47b82009-02-23 08:49:38 +00004777 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
4778 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
4779 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00004780
4781 // If Elt0 and Elt1 are defined, are consecutive, and can be load
4782 // using a single extract together, load it and store it.
4783 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004784 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00004785 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00004786 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00004787 DAG.getIntPtrConstant(i));
4788 continue;
4789 }
4790
Nate Begemanb9a47b82009-02-23 08:49:38 +00004791 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00004792 // source byte is not also odd, shift the extracted word left 8 bits
4793 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00004794 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004795 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004796 DAG.getIntPtrConstant(Elt1 / 2));
4797 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004798 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004799 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00004800 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004801 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
4802 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004803 }
4804 // If Elt0 is defined, extract it from the appropriate source. If the
4805 // source byte is not also even, shift the extracted word right 8 bits. If
4806 // Elt1 was also defined, OR the extracted values together before
4807 // inserting them in the result.
4808 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004809 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004810 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
4811 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004812 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004813 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00004814 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004815 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
4816 DAG.getConstant(0x00FF, MVT::i16));
4817 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00004818 : InsElt0;
4819 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004820 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004821 DAG.getIntPtrConstant(i));
4822 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004823 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00004824}
4825
Evan Cheng7a831ce2007-12-15 03:00:47 +00004826/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00004827/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00004828/// done when every pair / quad of shuffle mask elements point to elements in
4829/// the right sequence. e.g.
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00004830/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
Evan Cheng14b32e12007-12-11 01:46:18 +00004831static
Nate Begeman9008ca62009-04-27 18:41:29 +00004832SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00004833 SelectionDAG &DAG, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00004834 EVT VT = SVOp->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00004835 SDValue V1 = SVOp->getOperand(0);
4836 SDValue V2 = SVOp->getOperand(1);
4837 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00004838 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00004839 EVT NewVT;
Owen Anderson825b72b2009-08-11 20:47:22 +00004840 switch (VT.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004841 default: assert(false && "Unexpected!");
Owen Anderson825b72b2009-08-11 20:47:22 +00004842 case MVT::v4f32: NewVT = MVT::v2f64; break;
4843 case MVT::v4i32: NewVT = MVT::v2i64; break;
4844 case MVT::v8i16: NewVT = MVT::v4i32; break;
4845 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00004846 }
4847
Nate Begeman9008ca62009-04-27 18:41:29 +00004848 int Scale = NumElems / NewWidth;
4849 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00004850 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004851 int StartIdx = -1;
4852 for (int j = 0; j < Scale; ++j) {
4853 int EltIdx = SVOp->getMaskElt(i+j);
4854 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00004855 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00004856 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00004857 StartIdx = EltIdx - (EltIdx % Scale);
4858 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00004859 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00004860 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004861 if (StartIdx == -1)
4862 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00004863 else
Nate Begeman9008ca62009-04-27 18:41:29 +00004864 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004865 }
4866
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004867 V1 = DAG.getNode(ISD::BITCAST, dl, NewVT, V1);
4868 V2 = DAG.getNode(ISD::BITCAST, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00004869 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004870}
4871
Evan Chengd880b972008-05-09 21:53:03 +00004872/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00004873///
Owen Andersone50ed302009-08-10 22:56:29 +00004874static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00004875 SDValue SrcOp, SelectionDAG &DAG,
4876 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004877 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004878 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00004879 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00004880 LD = dyn_cast<LoadSDNode>(SrcOp);
4881 if (!LD) {
4882 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
4883 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00004884 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Duncan Sandscdfad362010-11-03 12:17:33 +00004885 if ((ExtVT != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00004886 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004887 SrcOp.getOperand(0).getOpcode() == ISD::BITCAST &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00004888 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004889 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00004890 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004891 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004892 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
4893 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
4894 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00004895 SrcOp.getOperand(0)
4896 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00004897 }
4898 }
4899 }
4900
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004901 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004902 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004903 DAG.getNode(ISD::BITCAST, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00004904 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00004905}
4906
Evan Chengace3c172008-07-22 21:13:36 +00004907/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
4908/// shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00004909static SDValue
Nate Begeman9008ca62009-04-27 18:41:29 +00004910LowerVECTOR_SHUFFLE_4wide(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
4911 SDValue V1 = SVOp->getOperand(0);
4912 SDValue V2 = SVOp->getOperand(1);
4913 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00004914 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00004915
Evan Chengace3c172008-07-22 21:13:36 +00004916 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00004917 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00004918 SmallVector<int, 8> Mask1(4U, -1);
4919 SmallVector<int, 8> PermMask;
4920 SVOp->getMask(PermMask);
4921
Evan Chengace3c172008-07-22 21:13:36 +00004922 unsigned NumHi = 0;
4923 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00004924 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004925 int Idx = PermMask[i];
4926 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00004927 Locs[i] = std::make_pair(-1, -1);
4928 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004929 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
4930 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00004931 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00004932 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004933 NumLo++;
4934 } else {
4935 Locs[i] = std::make_pair(1, NumHi);
4936 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00004937 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004938 NumHi++;
4939 }
4940 }
4941 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004942
Evan Chengace3c172008-07-22 21:13:36 +00004943 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004944 // If no more than two elements come from either vector. This can be
4945 // implemented with two shuffles. First shuffle gather the elements.
4946 // The second shuffle, which takes the first shuffle as both of its
4947 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00004948 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004949
Nate Begeman9008ca62009-04-27 18:41:29 +00004950 SmallVector<int, 8> Mask2(4U, -1);
Eric Christopherfd179292009-08-27 18:07:15 +00004951
Evan Chengace3c172008-07-22 21:13:36 +00004952 for (unsigned i = 0; i != 4; ++i) {
4953 if (Locs[i].first == -1)
4954 continue;
4955 else {
4956 unsigned Idx = (i < 2) ? 0 : 4;
4957 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00004958 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004959 }
4960 }
4961
Nate Begeman9008ca62009-04-27 18:41:29 +00004962 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004963 } else if (NumLo == 3 || NumHi == 3) {
4964 // Otherwise, we must have three elements from one vector, call it X, and
4965 // one element from the other, call it Y. First, use a shufps to build an
4966 // intermediate vector with the one element from Y and the element from X
4967 // that will be in the same half in the final destination (the indexes don't
4968 // matter). Then, use a shufps to build the final vector, taking the half
4969 // containing the element from Y from the intermediate, and the other half
4970 // from X.
4971 if (NumHi == 3) {
4972 // Normalize it so the 3 elements come from V1.
Nate Begeman9008ca62009-04-27 18:41:29 +00004973 CommuteVectorShuffleMask(PermMask, VT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004974 std::swap(V1, V2);
4975 }
4976
4977 // Find the element from V2.
4978 unsigned HiIndex;
4979 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004980 int Val = PermMask[HiIndex];
4981 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004982 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004983 if (Val >= 4)
4984 break;
4985 }
4986
Nate Begeman9008ca62009-04-27 18:41:29 +00004987 Mask1[0] = PermMask[HiIndex];
4988 Mask1[1] = -1;
4989 Mask1[2] = PermMask[HiIndex^1];
4990 Mask1[3] = -1;
4991 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004992
4993 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004994 Mask1[0] = PermMask[0];
4995 Mask1[1] = PermMask[1];
4996 Mask1[2] = HiIndex & 1 ? 6 : 4;
4997 Mask1[3] = HiIndex & 1 ? 4 : 6;
4998 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004999 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005000 Mask1[0] = HiIndex & 1 ? 2 : 0;
5001 Mask1[1] = HiIndex & 1 ? 0 : 2;
5002 Mask1[2] = PermMask[2];
5003 Mask1[3] = PermMask[3];
5004 if (Mask1[2] >= 0)
5005 Mask1[2] += 4;
5006 if (Mask1[3] >= 0)
5007 Mask1[3] += 4;
5008 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005009 }
Evan Chengace3c172008-07-22 21:13:36 +00005010 }
5011
5012 // Break it into (shuffle shuffle_hi, shuffle_lo).
5013 Locs.clear();
Nate Begeman9008ca62009-04-27 18:41:29 +00005014 SmallVector<int,8> LoMask(4U, -1);
5015 SmallVector<int,8> HiMask(4U, -1);
5016
5017 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00005018 unsigned MaskIdx = 0;
5019 unsigned LoIdx = 0;
5020 unsigned HiIdx = 2;
5021 for (unsigned i = 0; i != 4; ++i) {
5022 if (i == 2) {
5023 MaskPtr = &HiMask;
5024 MaskIdx = 1;
5025 LoIdx = 0;
5026 HiIdx = 2;
5027 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005028 int Idx = PermMask[i];
5029 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00005030 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00005031 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00005032 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00005033 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005034 LoIdx++;
5035 } else {
5036 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00005037 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005038 HiIdx++;
5039 }
5040 }
5041
Nate Begeman9008ca62009-04-27 18:41:29 +00005042 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
5043 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
5044 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00005045 for (unsigned i = 0; i != 4; ++i) {
5046 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005047 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00005048 } else {
5049 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00005050 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00005051 }
5052 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005053 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00005054}
5055
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005056static bool MayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005057 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005058 V = V.getOperand(0);
5059 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
5060 V = V.getOperand(0);
5061 if (MayFoldLoad(V))
5062 return true;
5063 return false;
5064}
5065
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005066// FIXME: the version above should always be used. Since there's
5067// a bug where several vector shuffles can't be folded because the
5068// DAG is not updated during lowering and a node claims to have two
5069// uses while it only has one, use this version, and let isel match
5070// another instruction if the load really happens to have more than
5071// one use. Remove this version after this bug get fixed.
Evan Cheng835580f2010-10-07 20:50:20 +00005072// rdar://8434668, PR8156
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005073static bool RelaxedMayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005074 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005075 V = V.getOperand(0);
5076 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
5077 V = V.getOperand(0);
5078 if (ISD::isNormalLoad(V.getNode()))
5079 return true;
5080 return false;
5081}
5082
5083/// CanFoldShuffleIntoVExtract - Check if the current shuffle is used by
5084/// a vector extract, and if both can be later optimized into a single load.
5085/// This is done in visitEXTRACT_VECTOR_ELT and the conditions are checked
5086/// here because otherwise a target specific shuffle node is going to be
5087/// emitted for this shuffle, and the optimization not done.
5088/// FIXME: This is probably not the best approach, but fix the problem
5089/// until the right path is decided.
5090static
5091bool CanXFormVExtractWithShuffleIntoLoad(SDValue V, SelectionDAG &DAG,
5092 const TargetLowering &TLI) {
5093 EVT VT = V.getValueType();
5094 ShuffleVectorSDNode *SVOp = dyn_cast<ShuffleVectorSDNode>(V);
5095
5096 // Be sure that the vector shuffle is present in a pattern like this:
5097 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), c) -> (f32 load $addr)
5098 if (!V.hasOneUse())
5099 return false;
5100
5101 SDNode *N = *V.getNode()->use_begin();
5102 if (N->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
5103 return false;
5104
5105 SDValue EltNo = N->getOperand(1);
5106 if (!isa<ConstantSDNode>(EltNo))
5107 return false;
5108
5109 // If the bit convert changed the number of elements, it is unsafe
5110 // to examine the mask.
5111 bool HasShuffleIntoBitcast = false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005112 if (V.getOpcode() == ISD::BITCAST) {
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005113 EVT SrcVT = V.getOperand(0).getValueType();
5114 if (SrcVT.getVectorNumElements() != VT.getVectorNumElements())
5115 return false;
5116 V = V.getOperand(0);
5117 HasShuffleIntoBitcast = true;
5118 }
5119
5120 // Select the input vector, guarding against out of range extract vector.
5121 unsigned NumElems = VT.getVectorNumElements();
5122 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
5123 int Idx = (Elt > NumElems) ? -1 : SVOp->getMaskElt(Elt);
5124 V = (Idx < (int)NumElems) ? V.getOperand(0) : V.getOperand(1);
5125
5126 // Skip one more bit_convert if necessary
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005127 if (V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005128 V = V.getOperand(0);
5129
5130 if (ISD::isNormalLoad(V.getNode())) {
5131 // Is the original load suitable?
5132 LoadSDNode *LN0 = cast<LoadSDNode>(V);
5133
5134 // FIXME: avoid the multi-use bug that is preventing lots of
5135 // of foldings to be detected, this is still wrong of course, but
5136 // give the temporary desired behavior, and if it happens that
5137 // the load has real more uses, during isel it will not fold, and
5138 // will generate poor code.
5139 if (!LN0 || LN0->isVolatile()) // || !LN0->hasOneUse()
5140 return false;
5141
5142 if (!HasShuffleIntoBitcast)
5143 return true;
5144
5145 // If there's a bitcast before the shuffle, check if the load type and
5146 // alignment is valid.
5147 unsigned Align = LN0->getAlignment();
5148 unsigned NewAlign =
5149 TLI.getTargetData()->getABITypeAlignment(
5150 VT.getTypeForEVT(*DAG.getContext()));
5151
5152 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
5153 return false;
5154 }
5155
5156 return true;
5157}
5158
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005159static
Evan Cheng835580f2010-10-07 20:50:20 +00005160SDValue getMOVDDup(SDValue &Op, DebugLoc &dl, SDValue V1, SelectionDAG &DAG) {
5161 EVT VT = Op.getValueType();
5162
5163 // Canonizalize to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005164 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1);
5165 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng835580f2010-10-07 20:50:20 +00005166 getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64,
5167 V1, DAG));
5168}
5169
5170static
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005171SDValue getMOVLowToHigh(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG,
5172 bool HasSSE2) {
5173 SDValue V1 = Op.getOperand(0);
5174 SDValue V2 = Op.getOperand(1);
5175 EVT VT = Op.getValueType();
5176
5177 assert(VT != MVT::v2i64 && "unsupported shuffle type");
5178
5179 if (HasSSE2 && VT == MVT::v2f64)
5180 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
5181
5182 // v4f32 or v4i32
5183 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V2, DAG);
5184}
5185
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00005186static
5187SDValue getMOVHighToLow(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG) {
5188 SDValue V1 = Op.getOperand(0);
5189 SDValue V2 = Op.getOperand(1);
5190 EVT VT = Op.getValueType();
5191
5192 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
5193 "unsupported shuffle type");
5194
5195 if (V2.getOpcode() == ISD::UNDEF)
5196 V2 = V1;
5197
5198 // v4i32 or v4f32
5199 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
5200}
5201
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005202static
5203SDValue getMOVLP(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG, bool HasSSE2) {
5204 SDValue V1 = Op.getOperand(0);
5205 SDValue V2 = Op.getOperand(1);
5206 EVT VT = Op.getValueType();
5207 unsigned NumElems = VT.getVectorNumElements();
5208
5209 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
5210 // operand of these instructions is only memory, so check if there's a
5211 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
5212 // same masks.
5213 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005214
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00005215 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005216 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005217 CanFoldLoad = true;
5218
5219 // When V1 is a load, it can be folded later into a store in isel, example:
5220 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
5221 // turns into:
5222 // (MOVLPSmr addr:$src1, VR128:$src2)
5223 // So, recognize this potential and also use MOVLPS or MOVLPD
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005224 if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005225 CanFoldLoad = true;
5226
5227 if (CanFoldLoad) {
5228 if (HasSSE2 && NumElems == 2)
5229 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
5230
5231 if (NumElems == 4)
5232 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
5233 }
5234
5235 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5236 // movl and movlp will both match v2i64, but v2i64 is never matched by
5237 // movl earlier because we make it strict to avoid messing with the movlp load
5238 // folding logic (see the code above getMOVLP call). Match it here then,
5239 // this is horrible, but will stay like this until we move all shuffle
5240 // matching to x86 specific nodes. Note that for the 1st condition all
5241 // types are matched with movsd.
5242 if ((HasSSE2 && NumElems == 2) || !X86::isMOVLMask(SVOp))
5243 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
5244 else if (HasSSE2)
5245 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
5246
5247
5248 assert(VT != MVT::v4i32 && "unsupported shuffle type");
5249
5250 // Invert the operand order and use SHUFPS to match it.
5251 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V2, V1,
5252 X86::getShuffleSHUFImmediate(SVOp), DAG);
5253}
5254
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005255static inline unsigned getUNPCKLOpcode(EVT VT) {
5256 switch(VT.getSimpleVT().SimpleTy) {
5257 case MVT::v4i32: return X86ISD::PUNPCKLDQ;
5258 case MVT::v2i64: return X86ISD::PUNPCKLQDQ;
5259 case MVT::v4f32: return X86ISD::UNPCKLPS;
5260 case MVT::v2f64: return X86ISD::UNPCKLPD;
5261 case MVT::v16i8: return X86ISD::PUNPCKLBW;
5262 case MVT::v8i16: return X86ISD::PUNPCKLWD;
5263 default:
5264 llvm_unreachable("Unknow type for unpckl");
5265 }
5266 return 0;
5267}
5268
5269static inline unsigned getUNPCKHOpcode(EVT VT) {
5270 switch(VT.getSimpleVT().SimpleTy) {
5271 case MVT::v4i32: return X86ISD::PUNPCKHDQ;
5272 case MVT::v2i64: return X86ISD::PUNPCKHQDQ;
5273 case MVT::v4f32: return X86ISD::UNPCKHPS;
5274 case MVT::v2f64: return X86ISD::UNPCKHPD;
5275 case MVT::v16i8: return X86ISD::PUNPCKHBW;
5276 case MVT::v8i16: return X86ISD::PUNPCKHWD;
5277 default:
5278 llvm_unreachable("Unknow type for unpckh");
5279 }
5280 return 0;
5281}
5282
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005283static
5284SDValue NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG,
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005285 const TargetLowering &TLI,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005286 const X86Subtarget *Subtarget) {
5287 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5288 EVT VT = Op.getValueType();
5289 DebugLoc dl = Op.getDebugLoc();
5290 SDValue V1 = Op.getOperand(0);
5291 SDValue V2 = Op.getOperand(1);
5292
5293 if (isZeroShuffle(SVOp))
5294 return getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
5295
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005296 // Handle splat operations
5297 if (SVOp->isSplat()) {
5298 // Special case, this is the only place now where it's
5299 // allowed to return a vector_shuffle operation without
5300 // using a target specific node, because *hopefully* it
5301 // will be optimized away by the dag combiner.
5302 if (VT.getVectorNumElements() <= 4 &&
5303 CanXFormVExtractWithShuffleIntoLoad(Op, DAG, TLI))
5304 return Op;
5305
5306 // Handle splats by matching through known masks
5307 if (VT.getVectorNumElements() <= 4)
5308 return SDValue();
5309
Evan Cheng835580f2010-10-07 20:50:20 +00005310 // Canonicalize all of the remaining to v4f32.
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005311 return PromoteSplat(SVOp, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005312 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005313
5314 // If the shuffle can be profitably rewritten as a narrower shuffle, then
5315 // do it!
5316 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
5317 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5318 if (NewOp.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005319 return DAG.getNode(ISD::BITCAST, dl, VT, NewOp);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005320 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
5321 // FIXME: Figure out a cleaner way to do this.
5322 // Try to make use of movq to zero out the top part.
5323 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
5324 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5325 if (NewOp.getNode()) {
5326 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
5327 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
5328 DAG, Subtarget, dl);
5329 }
5330 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
5331 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5332 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
5333 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
5334 DAG, Subtarget, dl);
5335 }
5336 }
5337 return SDValue();
5338}
5339
Dan Gohman475871a2008-07-27 21:46:04 +00005340SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005341X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00005342 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00005343 SDValue V1 = Op.getOperand(0);
5344 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00005345 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005346 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00005347 unsigned NumElems = VT.getVectorNumElements();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005348 bool isMMX = VT.getSizeInBits() == 64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005349 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
5350 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00005351 bool V1IsSplat = false;
5352 bool V2IsSplat = false;
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005353 bool HasSSE2 = Subtarget->hasSSE2() || Subtarget->hasAVX();
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00005354 bool HasSSE3 = Subtarget->hasSSE3() || Subtarget->hasAVX();
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00005355 bool HasSSSE3 = Subtarget->hasSSSE3() || Subtarget->hasAVX();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005356 MachineFunction &MF = DAG.getMachineFunction();
5357 bool OptForSize = MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005358
Dale Johannesen0488fb62010-09-30 23:57:10 +00005359 // Shuffle operations on MMX not supported.
5360 if (isMMX)
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00005361 return Op;
5362
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005363 // Vector shuffle lowering takes 3 steps:
5364 //
5365 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
5366 // narrowing and commutation of operands should be handled.
5367 // 2) Matching of shuffles with known shuffle masks to x86 target specific
5368 // shuffle nodes.
5369 // 3) Rewriting of unmatched masks into new generic shuffle operations,
5370 // so the shuffle can be broken into other shuffles and the legalizer can
5371 // try the lowering again.
5372 //
5373 // The general ideia is that no vector_shuffle operation should be left to
5374 // be matched during isel, all of them must be converted to a target specific
5375 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00005376
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005377 // Normalize the input vectors. Here splats, zeroed vectors, profitable
5378 // narrowing and commutation of operands should be handled. The actual code
5379 // doesn't include all of those, work in progress...
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005380 SDValue NewOp = NormalizeVectorShuffle(Op, DAG, *this, Subtarget);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005381 if (NewOp.getNode())
5382 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00005383
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005384 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
5385 // unpckh_undef). Only use pshufd if speed is more important than size.
5386 if (OptForSize && X86::isUNPCKL_v_undef_Mask(SVOp))
5387 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5388 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V1, DAG);
5389 if (OptForSize && X86::isUNPCKH_v_undef_Mask(SVOp))
5390 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5391 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00005392
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005393 if (X86::isMOVDDUPMask(SVOp) && HasSSE3 && V2IsUndef &&
Dale Johannesen0488fb62010-09-30 23:57:10 +00005394 RelaxedMayFoldVectorLoad(V1))
Evan Cheng835580f2010-10-07 20:50:20 +00005395 return getMOVDDup(Op, dl, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005396
Dale Johannesen0488fb62010-09-30 23:57:10 +00005397 if (X86::isMOVHLPS_v_undef_Mask(SVOp))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005398 return getMOVHighToLow(Op, dl, DAG);
5399
5400 // Use to match splats
5401 if (HasSSE2 && X86::isUNPCKHMask(SVOp) && V2IsUndef &&
5402 (VT == MVT::v2f64 || VT == MVT::v2i64))
5403 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
5404
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005405 if (X86::isPSHUFDMask(SVOp)) {
5406 // The actual implementation will match the mask in the if above and then
5407 // during isel it can match several different instructions, not only pshufd
5408 // as its name says, sad but true, emulate the behavior for now...
5409 if (X86::isMOVDDUPMask(SVOp) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
5410 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
5411
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005412 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
5413
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005414 if (HasSSE2 && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005415 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
5416
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005417 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005418 return getTargetShuffleNode(X86ISD::SHUFPD, dl, VT, V1, V1,
5419 TargetMask, DAG);
5420
5421 if (VT == MVT::v4f32)
5422 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V1, V1,
5423 TargetMask, DAG);
5424 }
Eric Christopherfd179292009-08-27 18:07:15 +00005425
Evan Chengf26ffe92008-05-29 08:22:04 +00005426 // Check if this can be converted into a logical shift.
5427 bool isLeft = false;
5428 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00005429 SDValue ShVal;
Nate Begeman9008ca62009-04-27 18:41:29 +00005430 bool isShift = getSubtarget()->hasSSE2() &&
Evan Chengc3630942009-12-09 21:00:30 +00005431 isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00005432 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00005433 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00005434 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00005435 EVT EltVT = VT.getVectorElementType();
5436 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00005437 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005438 }
Eric Christopherfd179292009-08-27 18:07:15 +00005439
Nate Begeman9008ca62009-04-27 18:41:29 +00005440 if (X86::isMOVLMask(SVOp)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005441 if (V1IsUndef)
5442 return V2;
Gabor Greifba36cb52008-08-28 21:40:38 +00005443 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00005444 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Dale Johannesen0488fb62010-09-30 23:57:10 +00005445 if (!X86::isMOVLPMask(SVOp)) {
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005446 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00005447 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
5448
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005449 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00005450 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
5451 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00005452 }
Eric Christopherfd179292009-08-27 18:07:15 +00005453
Nate Begeman9008ca62009-04-27 18:41:29 +00005454 // FIXME: fold these into legal mask.
Dale Johannesen0488fb62010-09-30 23:57:10 +00005455 if (X86::isMOVLHPSMask(SVOp) && !X86::isUNPCKLMask(SVOp))
5456 return getMOVLowToHigh(Op, dl, DAG, HasSSE2);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005457
Dale Johannesen0488fb62010-09-30 23:57:10 +00005458 if (X86::isMOVHLPSMask(SVOp))
5459 return getMOVHighToLow(Op, dl, DAG);
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00005460
Dale Johannesen0488fb62010-09-30 23:57:10 +00005461 if (X86::isMOVSHDUPMask(SVOp) && HasSSE3 && V2IsUndef && NumElems == 4)
5462 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00005463
Dale Johannesen0488fb62010-09-30 23:57:10 +00005464 if (X86::isMOVSLDUPMask(SVOp) && HasSSE3 && V2IsUndef && NumElems == 4)
5465 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00005466
Dale Johannesen0488fb62010-09-30 23:57:10 +00005467 if (X86::isMOVLPMask(SVOp))
5468 return getMOVLP(Op, dl, DAG, HasSSE2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005469
Nate Begeman9008ca62009-04-27 18:41:29 +00005470 if (ShouldXformToMOVHLPS(SVOp) ||
5471 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
5472 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005473
Evan Chengf26ffe92008-05-29 08:22:04 +00005474 if (isShift) {
5475 // No better options. Use a vshl / vsrl.
Dan Gohman8a55ce42009-09-23 21:02:20 +00005476 EVT EltVT = VT.getVectorElementType();
5477 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00005478 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005479 }
Eric Christopherfd179292009-08-27 18:07:15 +00005480
Evan Cheng9eca5e82006-10-25 21:49:50 +00005481 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00005482 // FIXME: This should also accept a bitcast of a splat? Be careful, not
5483 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00005484 V1IsSplat = isSplatVector(V1.getNode());
5485 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00005486
Chris Lattner8a594482007-11-25 00:24:49 +00005487 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00005488 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005489 Op = CommuteVectorShuffle(SVOp, DAG);
5490 SVOp = cast<ShuffleVectorSDNode>(Op);
5491 V1 = SVOp->getOperand(0);
5492 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00005493 std::swap(V1IsSplat, V2IsSplat);
5494 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00005495 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00005496 }
5497
Nate Begeman9008ca62009-04-27 18:41:29 +00005498 if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) {
5499 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00005500 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00005501 return V1;
5502 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
5503 // the instruction selector will not match, so get a canonical MOVL with
5504 // swapped operands to undo the commute.
5505 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00005506 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005507
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005508 if (X86::isUNPCKLMask(SVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005509 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005510
5511 if (X86::isUNPCKHMask(SVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005512 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V2, DAG);
Evan Chenge1113032006-10-04 18:33:38 +00005513
Evan Cheng9bbbb982006-10-25 20:48:19 +00005514 if (V2IsSplat) {
5515 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005516 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00005517 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00005518 SDValue NewMask = NormalizeMask(SVOp, DAG);
5519 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
5520 if (NSVOp != SVOp) {
5521 if (X86::isUNPCKLMask(NSVOp, true)) {
5522 return NewMask;
5523 } else if (X86::isUNPCKHMask(NSVOp, true)) {
5524 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005525 }
5526 }
5527 }
5528
Evan Cheng9eca5e82006-10-25 21:49:50 +00005529 if (Commuted) {
5530 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00005531 // FIXME: this seems wrong.
5532 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
5533 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005534
5535 if (X86::isUNPCKLMask(NewSVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005536 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V2, V1, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005537
5538 if (X86::isUNPCKHMask(NewSVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005539 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V2, V1, DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00005540 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005541
Nate Begeman9008ca62009-04-27 18:41:29 +00005542 // Normalize the node to match x86 shuffle ops if needed
Dale Johannesen0488fb62010-09-30 23:57:10 +00005543 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp))
Nate Begeman9008ca62009-04-27 18:41:29 +00005544 return CommuteVectorShuffle(SVOp, DAG);
5545
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00005546 // The checks below are all present in isShuffleMaskLegal, but they are
5547 // inlined here right now to enable us to directly emit target specific
5548 // nodes, and remove one by one until they don't return Op anymore.
5549 SmallVector<int, 16> M;
5550 SVOp->getMask(M);
5551
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00005552 if (isPALIGNRMask(M, VT, HasSSSE3))
5553 return getTargetShuffleNode(X86ISD::PALIGN, dl, VT, V1, V2,
5554 X86::getShufflePALIGNRImmediate(SVOp),
5555 DAG);
5556
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00005557 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
5558 SVOp->getSplatIndex() == 0 && V2IsUndef) {
5559 if (VT == MVT::v2f64)
5560 return getTargetShuffleNode(X86ISD::UNPCKLPD, dl, VT, V1, V1, DAG);
5561 if (VT == MVT::v2i64)
5562 return getTargetShuffleNode(X86ISD::PUNPCKLQDQ, dl, VT, V1, V1, DAG);
5563 }
5564
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00005565 if (isPSHUFHWMask(M, VT))
5566 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
5567 X86::getShufflePSHUFHWImmediate(SVOp),
5568 DAG);
5569
5570 if (isPSHUFLWMask(M, VT))
5571 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
5572 X86::getShufflePSHUFLWImmediate(SVOp),
5573 DAG);
5574
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00005575 if (isSHUFPMask(M, VT)) {
5576 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
5577 if (VT == MVT::v4f32 || VT == MVT::v4i32)
5578 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V1, V2,
5579 TargetMask, DAG);
5580 if (VT == MVT::v2f64 || VT == MVT::v2i64)
5581 return getTargetShuffleNode(X86ISD::SHUFPD, dl, VT, V1, V2,
5582 TargetMask, DAG);
5583 }
5584
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005585 if (X86::isUNPCKL_v_undef_Mask(SVOp))
5586 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5587 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V1, DAG);
5588 if (X86::isUNPCKH_v_undef_Mask(SVOp))
5589 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5590 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
5591
Evan Cheng14b32e12007-12-11 01:46:18 +00005592 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
Owen Anderson825b72b2009-08-11 20:47:22 +00005593 if (VT == MVT::v8i16) {
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005594 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00005595 if (NewOp.getNode())
Evan Cheng14b32e12007-12-11 01:46:18 +00005596 return NewOp;
5597 }
5598
Owen Anderson825b72b2009-08-11 20:47:22 +00005599 if (VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005600 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005601 if (NewOp.getNode())
5602 return NewOp;
5603 }
Eric Christopherfd179292009-08-27 18:07:15 +00005604
Dale Johannesen0488fb62010-09-30 23:57:10 +00005605 // Handle all 4 wide cases with a number of shuffles.
5606 if (NumElems == 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00005607 return LowerVECTOR_SHUFFLE_4wide(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005608
Dan Gohman475871a2008-07-27 21:46:04 +00005609 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005610}
5611
Dan Gohman475871a2008-07-27 21:46:04 +00005612SDValue
5613X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00005614 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005615 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005616 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005617 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005618 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005619 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00005620 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005621 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00005622 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005623 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00005624 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
5625 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
5626 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005627 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
5628 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005629 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005630 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00005631 Op.getOperand(0)),
5632 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00005633 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005634 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00005635 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005636 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00005637 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Owen Anderson825b72b2009-08-11 20:47:22 +00005638 } else if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00005639 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
5640 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00005641 // result has a single use which is a store or a bitcast to i32. And in
5642 // the case of a store, it's not worth it if the index is a constant 0,
5643 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00005644 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00005645 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00005646 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00005647 if ((User->getOpcode() != ISD::STORE ||
5648 (isa<ConstantSDNode>(Op.getOperand(1)) &&
5649 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005650 (User->getOpcode() != ISD::BITCAST ||
Owen Anderson825b72b2009-08-11 20:47:22 +00005651 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00005652 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00005653 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005654 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00005655 Op.getOperand(0)),
5656 Op.getOperand(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005657 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Extract);
Owen Anderson825b72b2009-08-11 20:47:22 +00005658 } else if (VT == MVT::i32) {
Mon P Wangf0fcdd82009-01-15 21:10:20 +00005659 // ExtractPS works with constant index.
5660 if (isa<ConstantSDNode>(Op.getOperand(1)))
5661 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00005662 }
Dan Gohman475871a2008-07-27 21:46:04 +00005663 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00005664}
5665
5666
Dan Gohman475871a2008-07-27 21:46:04 +00005667SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005668X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
5669 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005670 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00005671 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005672
Evan Cheng62a3f152008-03-24 21:52:23 +00005673 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005674 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00005675 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00005676 return Res;
5677 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00005678
Owen Andersone50ed302009-08-10 22:56:29 +00005679 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005680 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005681 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005682 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00005683 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005684 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00005685 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005686 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
5687 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005688 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005689 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00005690 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005691 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00005692 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00005693 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00005694 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00005695 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00005696 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00005697 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005698 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005699 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005700 if (Idx == 0)
5701 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00005702
Evan Cheng0db9fe62006-04-25 20:13:52 +00005703 // SHUFPS the element to the lowest double word, then movss.
Nate Begeman9008ca62009-04-27 18:41:29 +00005704 int Mask[4] = { Idx, -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00005705 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00005706 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00005707 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00005708 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00005709 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00005710 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00005711 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
5712 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
5713 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005714 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005715 if (Idx == 0)
5716 return Op;
5717
5718 // UNPCKHPD the element to the lowest double word, then movsd.
5719 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
5720 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00005721 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00005722 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00005723 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00005724 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00005725 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00005726 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005727 }
5728
Dan Gohman475871a2008-07-27 21:46:04 +00005729 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005730}
5731
Dan Gohman475871a2008-07-27 21:46:04 +00005732SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005733X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op,
5734 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005735 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00005736 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005737 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00005738
Dan Gohman475871a2008-07-27 21:46:04 +00005739 SDValue N0 = Op.getOperand(0);
5740 SDValue N1 = Op.getOperand(1);
5741 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00005742
Dan Gohman8a55ce42009-09-23 21:02:20 +00005743 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00005744 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00005745 unsigned Opc;
5746 if (VT == MVT::v8i16)
5747 Opc = X86ISD::PINSRW;
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00005748 else if (VT == MVT::v16i8)
5749 Opc = X86ISD::PINSRB;
5750 else
5751 Opc = X86ISD::PINSRB;
5752
Nate Begeman14d12ca2008-02-11 04:19:36 +00005753 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
5754 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00005755 if (N1.getValueType() != MVT::i32)
5756 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
5757 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005758 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00005759 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00005760 } else if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00005761 // Bits [7:6] of the constant are the source select. This will always be
5762 // zero here. The DAG Combiner may combine an extract_elt index into these
5763 // bits. For example (insert (extract, 3), 2) could be matched by putting
5764 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00005765 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00005766 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00005767 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00005768 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005769 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00005770 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00005771 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00005772 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00005773 } else if (EltVT == MVT::i32 && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00005774 // PINSR* works with constant index.
5775 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00005776 }
Dan Gohman475871a2008-07-27 21:46:04 +00005777 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00005778}
5779
Dan Gohman475871a2008-07-27 21:46:04 +00005780SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005781X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005782 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00005783 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00005784
5785 if (Subtarget->hasSSE41())
5786 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
5787
Dan Gohman8a55ce42009-09-23 21:02:20 +00005788 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00005789 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00005790
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005791 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00005792 SDValue N0 = Op.getOperand(0);
5793 SDValue N1 = Op.getOperand(1);
5794 SDValue N2 = Op.getOperand(2);
Evan Cheng794405e2007-12-12 07:55:34 +00005795
Dan Gohman8a55ce42009-09-23 21:02:20 +00005796 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00005797 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
5798 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00005799 if (N1.getValueType() != MVT::i32)
5800 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
5801 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005802 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesen0488fb62010-09-30 23:57:10 +00005803 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005804 }
Dan Gohman475871a2008-07-27 21:46:04 +00005805 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005806}
5807
Dan Gohman475871a2008-07-27 21:46:04 +00005808SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005809X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005810 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00005811
Chris Lattnerf172ecd2010-07-04 23:07:25 +00005812 if (Op.getValueType() == MVT::v1i64 &&
5813 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00005814 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00005815
Owen Anderson825b72b2009-08-11 20:47:22 +00005816 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Dale Johannesen0488fb62010-09-30 23:57:10 +00005817 assert(Op.getValueType().getSimpleVT().getSizeInBits() == 128 &&
5818 "Expected an SSE type!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005819 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(),
Dale Johannesen0488fb62010-09-30 23:57:10 +00005820 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005821}
5822
David Greene91585092011-01-26 15:38:49 +00005823// Lower a node with an EXTRACT_SUBVECTOR opcode. This may result in
5824// a simple subregister reference or explicit instructions to grab
5825// upper bits of a vector.
5826SDValue
5827X86TargetLowering::LowerEXTRACT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
5828 if (Subtarget->hasAVX()) {
5829 // TODO
5830 }
5831 return SDValue();
5832}
5833
David Greenecfe33c42011-01-26 19:13:22 +00005834// Lower a node with an INSERT_SUBVECTOR opcode. This may result in a
5835// simple superregister reference or explicit instructions to insert
5836// the upper bits of a vector.
5837SDValue
5838X86TargetLowering::LowerINSERT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
5839 if (Subtarget->hasAVX()) {
5840 DebugLoc dl = Op.getNode()->getDebugLoc();
5841 SDValue Vec = Op.getNode()->getOperand(0);
5842 SDValue SubVec = Op.getNode()->getOperand(1);
5843 SDValue Idx = Op.getNode()->getOperand(2);
5844
5845 if (Op.getNode()->getValueType(0).getSizeInBits() == 256
5846 && SubVec.getNode()->getValueType(0).getSizeInBits() == 128) {
5847 // TODO
5848 }
5849 }
5850 return SDValue();
5851}
5852
Bill Wendling056292f2008-09-16 21:48:12 +00005853// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
5854// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
5855// one of the above mentioned nodes. It has to be wrapped because otherwise
5856// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
5857// be used to form addressing mode. These wrapped nodes will be selected
5858// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00005859SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005860X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005861 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00005862
Chris Lattner41621a22009-06-26 19:22:52 +00005863 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
5864 // global base reg.
5865 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00005866 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005867 CodeModel::Model M = getTargetMachine().getCodeModel();
5868
Chris Lattner4f066492009-07-11 20:29:19 +00005869 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005870 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00005871 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00005872 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005873 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00005874 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005875 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00005876
Evan Cheng1606e8e2009-03-13 07:51:59 +00005877 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00005878 CP->getAlignment(),
5879 CP->getOffset(), OpFlag);
5880 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00005881 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00005882 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00005883 if (OpFlag) {
5884 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00005885 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00005886 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00005887 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005888 }
5889
5890 return Result;
5891}
5892
Dan Gohmand858e902010-04-17 15:26:15 +00005893SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00005894 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00005895
Chris Lattner18c59872009-06-27 04:16:01 +00005896 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
5897 // global base reg.
5898 unsigned char OpFlag = 0;
5899 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005900 CodeModel::Model M = getTargetMachine().getCodeModel();
5901
Chris Lattner4f066492009-07-11 20:29:19 +00005902 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005903 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00005904 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00005905 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005906 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00005907 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005908 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00005909
Chris Lattner18c59872009-06-27 04:16:01 +00005910 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
5911 OpFlag);
5912 DebugLoc DL = JT->getDebugLoc();
5913 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00005914
Chris Lattner18c59872009-06-27 04:16:01 +00005915 // With PIC, the address is actually $g + Offset.
Chris Lattner1e61e692010-11-15 02:46:57 +00005916 if (OpFlag)
Chris Lattner18c59872009-06-27 04:16:01 +00005917 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
5918 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00005919 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00005920 Result);
Eric Christopherfd179292009-08-27 18:07:15 +00005921
Chris Lattner18c59872009-06-27 04:16:01 +00005922 return Result;
5923}
5924
5925SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005926X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00005927 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00005928
Chris Lattner18c59872009-06-27 04:16:01 +00005929 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
5930 // global base reg.
5931 unsigned char OpFlag = 0;
5932 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005933 CodeModel::Model M = getTargetMachine().getCodeModel();
5934
Chris Lattner4f066492009-07-11 20:29:19 +00005935 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005936 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00005937 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00005938 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005939 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00005940 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005941 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00005942
Chris Lattner18c59872009-06-27 04:16:01 +00005943 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00005944
Chris Lattner18c59872009-06-27 04:16:01 +00005945 DebugLoc DL = Op.getDebugLoc();
5946 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00005947
5948
Chris Lattner18c59872009-06-27 04:16:01 +00005949 // With PIC, the address is actually $g + Offset.
5950 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00005951 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00005952 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
5953 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00005954 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00005955 Result);
5956 }
Eric Christopherfd179292009-08-27 18:07:15 +00005957
Chris Lattner18c59872009-06-27 04:16:01 +00005958 return Result;
5959}
5960
Dan Gohman475871a2008-07-27 21:46:04 +00005961SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005962X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00005963 // Create the TargetBlockAddressAddress node.
5964 unsigned char OpFlags =
5965 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00005966 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00005967 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Dan Gohman29cbade2009-11-20 23:18:13 +00005968 DebugLoc dl = Op.getDebugLoc();
5969 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(),
5970 /*isTarget=*/true, OpFlags);
5971
Dan Gohmanf705adb2009-10-30 01:28:02 +00005972 if (Subtarget->isPICStyleRIPRel() &&
5973 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00005974 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
5975 else
5976 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00005977
Dan Gohman29cbade2009-11-20 23:18:13 +00005978 // With PIC, the address is actually $g + Offset.
5979 if (isGlobalRelativeToPICBase(OpFlags)) {
5980 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
5981 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
5982 Result);
5983 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00005984
5985 return Result;
5986}
5987
5988SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00005989X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00005990 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00005991 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00005992 // Create the TargetGlobalAddress node, folding in the constant
5993 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00005994 unsigned char OpFlags =
5995 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005996 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00005997 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005998 if (OpFlags == X86II::MO_NO_FLAG &&
5999 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00006000 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00006001 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00006002 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00006003 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00006004 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00006005 }
Eric Christopherfd179292009-08-27 18:07:15 +00006006
Chris Lattner4f066492009-07-11 20:29:19 +00006007 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006008 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00006009 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
6010 else
6011 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00006012
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006013 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00006014 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00006015 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
6016 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006017 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006018 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006019
Chris Lattner36c25012009-07-10 07:34:39 +00006020 // For globals that require a load from a stub to get the address, emit the
6021 // load.
6022 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00006023 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00006024 MachinePointerInfo::getGOT(), false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006025
Dan Gohman6520e202008-10-18 02:06:02 +00006026 // If there was a non-zero offset that we didn't fold, create an explicit
6027 // addition for it.
6028 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00006029 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00006030 DAG.getConstant(Offset, getPointerTy()));
6031
Evan Cheng0db9fe62006-04-25 20:13:52 +00006032 return Result;
6033}
6034
Evan Chengda43bcf2008-09-24 00:05:32 +00006035SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006036X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00006037 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00006038 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006039 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00006040}
6041
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006042static SDValue
6043GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00006044 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Chris Lattnerb903bed2009-06-26 21:20:29 +00006045 unsigned char OperandFlags) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006046 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006047 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006048 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00006049 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006050 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00006051 GA->getOffset(),
6052 OperandFlags);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006053 if (InFlag) {
6054 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00006055 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006056 } else {
6057 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00006058 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006059 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006060
6061 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00006062 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006063
Rafael Espindola15f1b662009-04-24 12:59:40 +00006064 SDValue Flag = Chain.getValue(1);
6065 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006066}
6067
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006068// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00006069static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006070LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006071 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00006072 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00006073 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
6074 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006075 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006076 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006077 InFlag = Chain.getValue(1);
6078
Chris Lattnerb903bed2009-06-26 21:20:29 +00006079 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006080}
6081
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006082// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00006083static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006084LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006085 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00006086 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
6087 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006088}
6089
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006090// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
6091// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00006092static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006093 const EVT PtrVT, TLSModel::Model model,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00006094 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00006095 DebugLoc dl = GA->getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00006096
Chris Lattnerf93b90c2010-09-22 04:39:11 +00006097 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
6098 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
6099 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00006100
Michael J. Spencerec38de22010-10-10 22:04:20 +00006101 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Chris Lattnerf93b90c2010-09-22 04:39:11 +00006102 DAG.getIntPtrConstant(0),
6103 MachinePointerInfo(Ptr), false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00006104
Chris Lattnerb903bed2009-06-26 21:20:29 +00006105 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00006106 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
6107 // initialexec.
6108 unsigned WrapperKind = X86ISD::Wrapper;
6109 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00006110 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Chris Lattner18c59872009-06-27 04:16:01 +00006111 } else if (is64Bit) {
6112 assert(model == TLSModel::InitialExec);
6113 OperandFlags = X86II::MO_GOTTPOFF;
6114 WrapperKind = X86ISD::WrapperRIP;
6115 } else {
6116 assert(model == TLSModel::InitialExec);
6117 OperandFlags = X86II::MO_INDNTPOFF;
Chris Lattnerb903bed2009-06-26 21:20:29 +00006118 }
Eric Christopherfd179292009-08-27 18:07:15 +00006119
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006120 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
6121 // exec)
Michael J. Spencerec38de22010-10-10 22:04:20 +00006122 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Devang Patel0d881da2010-07-06 22:08:15 +00006123 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00006124 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00006125 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00006126
Rafael Espindola9a580232009-02-27 13:37:18 +00006127 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00006128 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00006129 MachinePointerInfo::getGOT(), false, false, 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00006130
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006131 // The address of the thread local variable is the add of the thread
6132 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00006133 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006134}
6135
Dan Gohman475871a2008-07-27 21:46:04 +00006136SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006137X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00006138
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006139 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00006140 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00006141
Eric Christopher30ef0e52010-06-03 04:07:48 +00006142 if (Subtarget->isTargetELF()) {
6143 // TODO: implement the "local dynamic" model
6144 // TODO: implement the "initial exec"model for pic executables
Michael J. Spencerec38de22010-10-10 22:04:20 +00006145
Eric Christopher30ef0e52010-06-03 04:07:48 +00006146 // If GV is an alias then use the aliasee for determining
6147 // thread-localness.
6148 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
6149 GV = GA->resolveAliasedGlobal(false);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006150
6151 TLSModel::Model model
Eric Christopher30ef0e52010-06-03 04:07:48 +00006152 = getTLSModel(GV, getTargetMachine().getRelocationModel());
Michael J. Spencerec38de22010-10-10 22:04:20 +00006153
Eric Christopher30ef0e52010-06-03 04:07:48 +00006154 switch (model) {
6155 case TLSModel::GeneralDynamic:
6156 case TLSModel::LocalDynamic: // not implemented
6157 if (Subtarget->is64Bit())
6158 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
6159 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Michael J. Spencerec38de22010-10-10 22:04:20 +00006160
Eric Christopher30ef0e52010-06-03 04:07:48 +00006161 case TLSModel::InitialExec:
6162 case TLSModel::LocalExec:
6163 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
6164 Subtarget->is64Bit());
6165 }
6166 } else if (Subtarget->isTargetDarwin()) {
6167 // Darwin only has one model of TLS. Lower to that.
6168 unsigned char OpFlag = 0;
6169 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
6170 X86ISD::WrapperRIP : X86ISD::Wrapper;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006171
Eric Christopher30ef0e52010-06-03 04:07:48 +00006172 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6173 // global base reg.
6174 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
6175 !Subtarget->is64Bit();
6176 if (PIC32)
6177 OpFlag = X86II::MO_TLVP_PIC_BASE;
6178 else
6179 OpFlag = X86II::MO_TLVP;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006180 DebugLoc DL = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00006181 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopherd8c05362010-12-09 06:25:53 +00006182 GA->getValueType(0),
Eric Christopher30ef0e52010-06-03 04:07:48 +00006183 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00006184 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006185
Eric Christopher30ef0e52010-06-03 04:07:48 +00006186 // With PIC32, the address is actually $g + Offset.
6187 if (PIC32)
6188 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
6189 DAG.getNode(X86ISD::GlobalBaseReg,
6190 DebugLoc(), getPointerTy()),
6191 Offset);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006192
Eric Christopher30ef0e52010-06-03 04:07:48 +00006193 // Lowering the machine isd will make sure everything is in the right
6194 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00006195 SDValue Chain = DAG.getEntryNode();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006196 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Eric Christopherd8c05362010-12-09 06:25:53 +00006197 SDValue Args[] = { Chain, Offset };
6198 Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006199
Eric Christopher30ef0e52010-06-03 04:07:48 +00006200 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
6201 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
6202 MFI->setAdjustsStack(true);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00006203
Eric Christopher30ef0e52010-06-03 04:07:48 +00006204 // And our return value (tls address) is in the standard call return value
6205 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00006206 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
6207 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy());
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006208 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00006209
Eric Christopher30ef0e52010-06-03 04:07:48 +00006210 assert(false &&
6211 "TLS not implemented for this target.");
Eric Christopherfd179292009-08-27 18:07:15 +00006212
Torok Edwinc23197a2009-07-14 16:55:14 +00006213 llvm_unreachable("Unreachable");
Chris Lattner5867de12009-04-01 22:14:45 +00006214 return SDValue();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006215}
6216
Evan Cheng0db9fe62006-04-25 20:13:52 +00006217
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006218/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michelfdc40a02009-02-17 22:15:04 +00006219/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00006220SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman4c1fa612008-03-03 22:22:09 +00006221 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00006222 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006223 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006224 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006225 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00006226 SDValue ShOpLo = Op.getOperand(0);
6227 SDValue ShOpHi = Op.getOperand(1);
6228 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00006229 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00006230 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00006231 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00006232
Dan Gohman475871a2008-07-27 21:46:04 +00006233 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006234 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00006235 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
6236 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006237 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00006238 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
6239 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006240 }
Evan Chenge3413162006-01-09 18:33:28 +00006241
Owen Anderson825b72b2009-08-11 20:47:22 +00006242 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
6243 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00006244 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00006245 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00006246
Dan Gohman475871a2008-07-27 21:46:04 +00006247 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00006248 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00006249 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
6250 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00006251
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006252 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00006253 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
6254 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006255 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00006256 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
6257 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006258 }
6259
Dan Gohman475871a2008-07-27 21:46:04 +00006260 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00006261 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006262}
Evan Chenga3195e82006-01-12 22:54:21 +00006263
Dan Gohmand858e902010-04-17 15:26:15 +00006264SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
6265 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006266 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00006267
Dale Johannesen0488fb62010-09-30 23:57:10 +00006268 if (SrcVT.isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00006269 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00006270
Owen Anderson825b72b2009-08-11 20:47:22 +00006271 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00006272 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00006273
Eli Friedman36df4992009-05-27 00:47:34 +00006274 // These are really Legal; return the operand so the caller accepts it as
6275 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00006276 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00006277 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00006278 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00006279 Subtarget->is64Bit()) {
6280 return Op;
6281 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006282
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006283 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006284 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006285 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00006286 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006287 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00006288 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00006289 StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006290 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006291 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00006292 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
6293}
Evan Cheng0db9fe62006-04-25 20:13:52 +00006294
Owen Andersone50ed302009-08-10 22:56:29 +00006295SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Michael J. Spencerec38de22010-10-10 22:04:20 +00006296 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00006297 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006298 // Build the FILD
Chris Lattner492a43e2010-09-22 01:28:21 +00006299 DebugLoc DL = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00006300 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00006301 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006302 if (useSSE)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006303 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Glue);
Chris Lattner5a88b832007-02-25 07:10:00 +00006304 else
Owen Anderson825b72b2009-08-11 20:47:22 +00006305 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006306
Chris Lattner492a43e2010-09-22 01:28:21 +00006307 unsigned ByteSize = SrcVT.getSizeInBits()/8;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006308
Chris Lattner492a43e2010-09-22 01:28:21 +00006309 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
6310 MachineMemOperand *MMO =
6311 DAG.getMachineFunction()
6312 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6313 MachineMemOperand::MOLoad, ByteSize, ByteSize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006314
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006315 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Chris Lattner492a43e2010-09-22 01:28:21 +00006316 SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
6317 X86ISD::FILD, DL,
6318 Tys, Ops, array_lengthof(Ops),
6319 SrcVT, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006320
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006321 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006322 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00006323 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006324
6325 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
6326 // shouldn't be necessary except that RFP cannot be live across
6327 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006328 MachineFunction &MF = DAG.getMachineFunction();
Bob Wilsoneafca4e2010-09-22 17:35:14 +00006329 unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
6330 int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006331 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00006332 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006333 SDValue Ops[] = {
6334 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
6335 };
Chris Lattner492a43e2010-09-22 01:28:21 +00006336 MachineMemOperand *MMO =
6337 DAG.getMachineFunction()
6338 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
Bob Wilsoneafca4e2010-09-22 17:35:14 +00006339 MachineMemOperand::MOStore, SSFISize, SSFISize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006340
Chris Lattner492a43e2010-09-22 01:28:21 +00006341 Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
6342 Ops, array_lengthof(Ops),
6343 Op.getValueType(), MMO);
6344 Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006345 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006346 false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006347 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006348
Evan Cheng0db9fe62006-04-25 20:13:52 +00006349 return Result;
6350}
6351
Bill Wendling8b8a6362009-01-17 03:56:04 +00006352// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00006353SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
6354 SelectionDAG &DAG) const {
Bill Wendling8b8a6362009-01-17 03:56:04 +00006355 // This algorithm is not obvious. Here it is in C code, more or less:
6356 /*
6357 double uint64_to_double( uint32_t hi, uint32_t lo ) {
6358 static const __m128i exp = { 0x4330000045300000ULL, 0 };
6359 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00006360
Bill Wendling8b8a6362009-01-17 03:56:04 +00006361 // Copy ints to xmm registers.
6362 __m128i xh = _mm_cvtsi32_si128( hi );
6363 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00006364
Bill Wendling8b8a6362009-01-17 03:56:04 +00006365 // Combine into low half of a single xmm register.
6366 __m128i x = _mm_unpacklo_epi32( xh, xl );
6367 __m128d d;
6368 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00006369
Bill Wendling8b8a6362009-01-17 03:56:04 +00006370 // Merge in appropriate exponents to give the integer bits the right
6371 // magnitude.
6372 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00006373
Bill Wendling8b8a6362009-01-17 03:56:04 +00006374 // Subtract away the biases to deal with the IEEE-754 double precision
6375 // implicit 1.
6376 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00006377
Bill Wendling8b8a6362009-01-17 03:56:04 +00006378 // All conversions up to here are exact. The correctly rounded result is
6379 // calculated using the current rounding mode using the following
6380 // horizontal add.
6381 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
6382 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
6383 // store doesn't really need to be here (except
6384 // maybe to zero the other double)
6385 return sd;
6386 }
6387 */
Dale Johannesen040225f2008-10-21 23:07:49 +00006388
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006389 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00006390 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00006391
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006392 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00006393 std::vector<Constant*> CV0;
Owen Andersoneed707b2009-07-24 23:12:02 +00006394 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x45300000)));
6395 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x43300000)));
6396 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
6397 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
Owen Andersonaf7ec972009-07-28 21:19:26 +00006398 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006399 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006400
Bill Wendling8b8a6362009-01-17 03:56:04 +00006401 std::vector<Constant*> CV1;
Owen Andersona90b3dc2009-07-15 21:51:10 +00006402 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006403 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
Owen Andersona90b3dc2009-07-15 21:51:10 +00006404 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006405 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Owen Andersonaf7ec972009-07-28 21:19:26 +00006406 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006407 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006408
Owen Anderson825b72b2009-08-11 20:47:22 +00006409 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6410 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00006411 Op.getOperand(0),
6412 DAG.getIntPtrConstant(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006413 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6414 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00006415 Op.getOperand(0),
6416 DAG.getIntPtrConstant(0)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006417 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
6418 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Chris Lattnere8639032010-09-21 06:22:23 +00006419 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006420 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00006421 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006422 SDValue XR2F = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Unpck2);
Owen Anderson825b72b2009-08-11 20:47:22 +00006423 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00006424 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006425 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00006426 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006427
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006428 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman9008ca62009-04-27 18:41:29 +00006429 int ShufMask[2] = { 1, -1 };
Owen Anderson825b72b2009-08-11 20:47:22 +00006430 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
6431 DAG.getUNDEF(MVT::v2f64), ShufMask);
6432 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
6433 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006434 DAG.getIntPtrConstant(0));
6435}
6436
Bill Wendling8b8a6362009-01-17 03:56:04 +00006437// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00006438SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
6439 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006440 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00006441 // FP constant to bias correct the final result.
6442 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00006443 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006444
6445 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00006446 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6447 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Bill Wendling8b8a6362009-01-17 03:56:04 +00006448 Op.getOperand(0),
6449 DAG.getIntPtrConstant(0)));
6450
Owen Anderson825b72b2009-08-11 20:47:22 +00006451 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006452 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00006453 DAG.getIntPtrConstant(0));
6454
6455 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00006456 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006457 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00006458 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006459 MVT::v2f64, Load)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006460 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00006461 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006462 MVT::v2f64, Bias)));
6463 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006464 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00006465 DAG.getIntPtrConstant(0));
6466
6467 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00006468 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006469
6470 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00006471 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00006472
Owen Anderson825b72b2009-08-11 20:47:22 +00006473 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006474 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00006475 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00006476 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006477 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00006478 }
6479
6480 // Handle final rounding.
6481 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00006482}
6483
Dan Gohmand858e902010-04-17 15:26:15 +00006484SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
6485 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00006486 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006487 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00006488
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006489 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00006490 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
6491 // the optimization here.
6492 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00006493 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00006494
Owen Andersone50ed302009-08-10 22:56:29 +00006495 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006496 EVT DstVT = Op.getValueType();
6497 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00006498 return LowerUINT_TO_FP_i64(Op, DAG);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006499 else if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00006500 return LowerUINT_TO_FP_i32(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00006501
6502 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00006503 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006504 if (SrcVT == MVT::i32) {
6505 SDValue WordOff = DAG.getConstant(4, getPointerTy());
6506 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
6507 getPointerTy(), StackSlot, WordOff);
6508 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006509 StackSlot, MachinePointerInfo(),
6510 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006511 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006512 OffsetSlot, MachinePointerInfo(),
6513 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006514 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
6515 return Fild;
6516 }
6517
6518 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
6519 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006520 StackSlot, MachinePointerInfo(),
6521 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006522 // For i64 source, we need to add the appropriate power of 2 if the input
6523 // was negative. This is the same as the optimization in
6524 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
6525 // we must be careful to do the computation in x87 extended precision, not
6526 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00006527 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
6528 MachineMemOperand *MMO =
6529 DAG.getMachineFunction()
6530 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6531 MachineMemOperand::MOLoad, 8, 8);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006532
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006533 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
6534 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Chris Lattner492a43e2010-09-22 01:28:21 +00006535 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, 3,
6536 MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006537
6538 APInt FF(32, 0x5F800000ULL);
6539
6540 // Check whether the sign bit is set.
6541 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
6542 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
6543 ISD::SETLT);
6544
6545 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
6546 SDValue FudgePtr = DAG.getConstantPool(
6547 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
6548 getPointerTy());
6549
6550 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
6551 SDValue Zero = DAG.getIntPtrConstant(0);
6552 SDValue Four = DAG.getIntPtrConstant(4);
6553 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
6554 Zero, Four);
6555 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
6556
6557 // Load the value out, extending it from f32 to f80.
6558 // FIXME: Avoid the extend by constructing the right constant pool?
Evan Chengbcc80172010-07-07 22:15:37 +00006559 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, MVT::f80, dl, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00006560 FudgePtr, MachinePointerInfo::getConstantPool(),
6561 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006562 // Extend everything to 80 bits to force it to be done on x87.
6563 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
6564 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00006565}
6566
Dan Gohman475871a2008-07-27 21:46:04 +00006567std::pair<SDValue,SDValue> X86TargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00006568FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) const {
Chris Lattner07290932010-09-22 01:05:16 +00006569 DebugLoc DL = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00006570
Owen Andersone50ed302009-08-10 22:56:29 +00006571 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00006572
6573 if (!IsSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006574 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
6575 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00006576 }
6577
Owen Anderson825b72b2009-08-11 20:47:22 +00006578 assert(DstTy.getSimpleVT() <= MVT::i64 &&
6579 DstTy.getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00006580 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00006581
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006582 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00006583 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00006584 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00006585 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00006586 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00006587 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00006588 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00006589 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006590
Evan Cheng87c89352007-10-15 20:11:21 +00006591 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
6592 // stack slot.
6593 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00006594 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00006595 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006596 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00006597
Michael J. Spencerec38de22010-10-10 22:04:20 +00006598
6599
Evan Cheng0db9fe62006-04-25 20:13:52 +00006600 unsigned Opc;
Owen Anderson825b72b2009-08-11 20:47:22 +00006601 switch (DstTy.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00006602 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
Owen Anderson825b72b2009-08-11 20:47:22 +00006603 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
6604 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
6605 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006606 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006607
Dan Gohman475871a2008-07-27 21:46:04 +00006608 SDValue Chain = DAG.getEntryNode();
6609 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00006610 EVT TheVT = Op.getOperand(0).getValueType();
6611 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006612 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00006613 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006614 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006615 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00006616 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00006617 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00006618 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00006619 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00006620
Chris Lattner492a43e2010-09-22 01:28:21 +00006621 MachineMemOperand *MMO =
6622 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6623 MachineMemOperand::MOLoad, MemSize, MemSize);
6624 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, 3,
6625 DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006626 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00006627 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006628 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
6629 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00006630
Chris Lattner07290932010-09-22 01:05:16 +00006631 MachineMemOperand *MMO =
6632 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6633 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006634
Evan Cheng0db9fe62006-04-25 20:13:52 +00006635 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00006636 SDValue Ops[] = { Chain, Value, StackSlot };
Chris Lattner07290932010-09-22 01:05:16 +00006637 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
6638 Ops, 3, DstTy, MMO);
Evan Chengd9558e02006-01-06 00:43:03 +00006639
Chris Lattner27a6c732007-11-24 07:07:01 +00006640 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006641}
6642
Dan Gohmand858e902010-04-17 15:26:15 +00006643SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
6644 SelectionDAG &DAG) const {
Dale Johannesen0488fb62010-09-30 23:57:10 +00006645 if (Op.getValueType().isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00006646 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00006647
Eli Friedman948e95a2009-05-23 09:59:16 +00006648 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00006649 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00006650 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
6651 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00006652
Chris Lattner27a6c732007-11-24 07:07:01 +00006653 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006654 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00006655 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00006656}
6657
Dan Gohmand858e902010-04-17 15:26:15 +00006658SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
6659 SelectionDAG &DAG) const {
Eli Friedman948e95a2009-05-23 09:59:16 +00006660 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
6661 SDValue FIST = Vals.first, StackSlot = Vals.second;
6662 assert(FIST.getNode() && "Unexpected failure");
6663
6664 // Load the result.
6665 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00006666 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00006667}
6668
Dan Gohmand858e902010-04-17 15:26:15 +00006669SDValue X86TargetLowering::LowerFABS(SDValue Op,
6670 SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00006671 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006672 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00006673 EVT VT = Op.getValueType();
6674 EVT EltVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00006675 if (VT.isVector())
6676 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006677 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00006678 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006679 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00006680 CV.push_back(C);
6681 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006682 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006683 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00006684 CV.push_back(C);
6685 CV.push_back(C);
6686 CV.push_back(C);
6687 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006688 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00006689 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006690 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006691 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00006692 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006693 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006694 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006695}
6696
Dan Gohmand858e902010-04-17 15:26:15 +00006697SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00006698 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006699 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00006700 EVT VT = Op.getValueType();
6701 EVT EltVT = VT;
Duncan Sandsda9ad382009-09-06 19:29:07 +00006702 if (VT.isVector())
Duncan Sands83ec4b62008-06-06 12:08:01 +00006703 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006704 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00006705 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006706 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00006707 CV.push_back(C);
6708 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006709 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006710 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00006711 CV.push_back(C);
6712 CV.push_back(C);
6713 CV.push_back(C);
6714 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006715 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00006716 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006717 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006718 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00006719 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006720 false, false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006721 if (VT.isVector()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006722 return DAG.getNode(ISD::BITCAST, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00006723 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006724 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00006725 Op.getOperand(0)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006726 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00006727 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00006728 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00006729 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006730}
6731
Dan Gohmand858e902010-04-17 15:26:15 +00006732SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00006733 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00006734 SDValue Op0 = Op.getOperand(0);
6735 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006736 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00006737 EVT VT = Op.getValueType();
6738 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00006739
6740 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00006741 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006742 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00006743 SrcVT = VT;
6744 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00006745 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00006746 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006747 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00006748 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00006749 }
6750
6751 // At this point the operands and the result should have the same
6752 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00006753
Evan Cheng68c47cb2007-01-05 07:55:56 +00006754 // First get the sign bit of second operand.
6755 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00006756 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006757 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
6758 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00006759 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006760 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
6761 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
6762 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
6763 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00006764 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00006765 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006766 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006767 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00006768 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006769 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006770 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00006771
6772 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00006773 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006774 // Op0 is MVT::f32, Op1 is MVT::f64.
6775 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
6776 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
6777 DAG.getConstant(32, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006778 SignBit = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, SignBit);
Owen Anderson825b72b2009-08-11 20:47:22 +00006779 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00006780 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00006781 }
6782
Evan Cheng73d6cf12007-01-05 21:37:56 +00006783 // Clear first operand sign bit.
6784 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00006785 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006786 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
6787 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00006788 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006789 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
6790 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
6791 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
6792 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00006793 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00006794 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006795 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006796 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00006797 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006798 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006799 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00006800
6801 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00006802 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00006803}
6804
Dan Gohman076aee32009-03-04 19:44:21 +00006805/// Emit nodes that will be selected as "test Op0,Op0", or something
6806/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00006807SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00006808 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00006809 DebugLoc dl = Op.getDebugLoc();
6810
Dan Gohman31125812009-03-07 01:58:32 +00006811 // CF and OF aren't always set the way we want. Determine which
6812 // of these we need.
6813 bool NeedCF = false;
6814 bool NeedOF = false;
6815 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006816 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00006817 case X86::COND_A: case X86::COND_AE:
6818 case X86::COND_B: case X86::COND_BE:
6819 NeedCF = true;
6820 break;
6821 case X86::COND_G: case X86::COND_GE:
6822 case X86::COND_L: case X86::COND_LE:
6823 case X86::COND_O: case X86::COND_NO:
6824 NeedOF = true;
6825 break;
Dan Gohman31125812009-03-07 01:58:32 +00006826 }
6827
Dan Gohman076aee32009-03-04 19:44:21 +00006828 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00006829 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
6830 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006831 if (Op.getResNo() != 0 || NeedOF || NeedCF)
6832 // Emit a CMP with 0, which is the TEST pattern.
6833 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
6834 DAG.getConstant(0, Op.getValueType()));
6835
6836 unsigned Opcode = 0;
6837 unsigned NumOperands = 0;
6838 switch (Op.getNode()->getOpcode()) {
6839 case ISD::ADD:
6840 // Due to an isel shortcoming, be conservative if this add is likely to be
6841 // selected as part of a load-modify-store instruction. When the root node
6842 // in a match is a store, isel doesn't know how to remap non-chain non-flag
6843 // uses of other nodes in the match, such as the ADD in this case. This
6844 // leads to the ADD being left around and reselected, with the result being
6845 // two adds in the output. Alas, even if none our users are stores, that
6846 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
6847 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
6848 // climbing the DAG back to the root, and it doesn't seem to be worth the
6849 // effort.
6850 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Dan Gohman076aee32009-03-04 19:44:21 +00006851 UE = Op.getNode()->use_end(); UI != UE; ++UI)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006852 if (UI->getOpcode() != ISD::CopyToReg && UI->getOpcode() != ISD::SETCC)
6853 goto default_case;
6854
6855 if (ConstantSDNode *C =
6856 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
6857 // An add of one will be selected as an INC.
6858 if (C->getAPIntValue() == 1) {
6859 Opcode = X86ISD::INC;
6860 NumOperands = 1;
6861 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00006862 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006863
6864 // An add of negative one (subtract of one) will be selected as a DEC.
6865 if (C->getAPIntValue().isAllOnesValue()) {
6866 Opcode = X86ISD::DEC;
6867 NumOperands = 1;
6868 break;
6869 }
Dan Gohman076aee32009-03-04 19:44:21 +00006870 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006871
6872 // Otherwise use a regular EFLAGS-setting add.
6873 Opcode = X86ISD::ADD;
6874 NumOperands = 2;
6875 break;
6876 case ISD::AND: {
6877 // If the primary and result isn't used, don't bother using X86ISD::AND,
6878 // because a TEST instruction will be better.
6879 bool NonFlagUse = false;
6880 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
6881 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
6882 SDNode *User = *UI;
6883 unsigned UOpNo = UI.getOperandNo();
6884 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
6885 // Look pass truncate.
6886 UOpNo = User->use_begin().getOperandNo();
6887 User = *User->use_begin();
6888 }
6889
6890 if (User->getOpcode() != ISD::BRCOND &&
6891 User->getOpcode() != ISD::SETCC &&
6892 (User->getOpcode() != ISD::SELECT || UOpNo != 0)) {
6893 NonFlagUse = true;
6894 break;
6895 }
Dan Gohman076aee32009-03-04 19:44:21 +00006896 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006897
6898 if (!NonFlagUse)
6899 break;
6900 }
6901 // FALL THROUGH
6902 case ISD::SUB:
6903 case ISD::OR:
6904 case ISD::XOR:
6905 // Due to the ISEL shortcoming noted above, be conservative if this op is
6906 // likely to be selected as part of a load-modify-store instruction.
6907 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
6908 UE = Op.getNode()->use_end(); UI != UE; ++UI)
6909 if (UI->getOpcode() == ISD::STORE)
6910 goto default_case;
6911
6912 // Otherwise use a regular EFLAGS-setting instruction.
6913 switch (Op.getNode()->getOpcode()) {
6914 default: llvm_unreachable("unexpected operator!");
6915 case ISD::SUB: Opcode = X86ISD::SUB; break;
6916 case ISD::OR: Opcode = X86ISD::OR; break;
6917 case ISD::XOR: Opcode = X86ISD::XOR; break;
6918 case ISD::AND: Opcode = X86ISD::AND; break;
6919 }
6920
6921 NumOperands = 2;
6922 break;
6923 case X86ISD::ADD:
6924 case X86ISD::SUB:
6925 case X86ISD::INC:
6926 case X86ISD::DEC:
6927 case X86ISD::OR:
6928 case X86ISD::XOR:
6929 case X86ISD::AND:
6930 return SDValue(Op.getNode(), 1);
6931 default:
6932 default_case:
6933 break;
Dan Gohman076aee32009-03-04 19:44:21 +00006934 }
6935
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006936 if (Opcode == 0)
6937 // Emit a CMP with 0, which is the TEST pattern.
6938 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
6939 DAG.getConstant(0, Op.getValueType()));
6940
6941 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
6942 SmallVector<SDValue, 4> Ops;
6943 for (unsigned i = 0; i != NumOperands; ++i)
6944 Ops.push_back(Op.getOperand(i));
6945
6946 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
6947 DAG.ReplaceAllUsesWith(Op, New);
6948 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00006949}
6950
6951/// Emit nodes that will be selected as "cmp Op0,Op1", or something
6952/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00006953SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00006954 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00006955 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
6956 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00006957 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00006958
6959 DebugLoc dl = Op0.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00006960 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00006961}
6962
Evan Chengd40d03e2010-01-06 19:38:29 +00006963/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
6964/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00006965SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
6966 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00006967 SDValue Op0 = And.getOperand(0);
6968 SDValue Op1 = And.getOperand(1);
6969 if (Op0.getOpcode() == ISD::TRUNCATE)
6970 Op0 = Op0.getOperand(0);
6971 if (Op1.getOpcode() == ISD::TRUNCATE)
6972 Op1 = Op1.getOperand(0);
6973
Evan Chengd40d03e2010-01-06 19:38:29 +00006974 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00006975 if (Op1.getOpcode() == ISD::SHL)
6976 std::swap(Op0, Op1);
6977 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00006978 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
6979 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00006980 // If we looked past a truncate, check that it's only truncating away
6981 // known zeros.
6982 unsigned BitWidth = Op0.getValueSizeInBits();
6983 unsigned AndBitWidth = And.getValueSizeInBits();
6984 if (BitWidth > AndBitWidth) {
6985 APInt Mask = APInt::getAllOnesValue(BitWidth), Zeros, Ones;
6986 DAG.ComputeMaskedBits(Op0, Mask, Zeros, Ones);
6987 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
6988 return SDValue();
6989 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00006990 LHS = Op1;
6991 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00006992 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00006993 } else if (Op1.getOpcode() == ISD::Constant) {
6994 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
6995 SDValue AndLHS = Op0;
Evan Chengd40d03e2010-01-06 19:38:29 +00006996 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
6997 LHS = AndLHS.getOperand(0);
6998 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00006999 }
Evan Chengd40d03e2010-01-06 19:38:29 +00007000 }
Evan Cheng0488db92007-09-25 01:57:46 +00007001
Evan Chengd40d03e2010-01-06 19:38:29 +00007002 if (LHS.getNode()) {
Evan Chenge5b51ac2010-04-17 06:13:15 +00007003 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00007004 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00007005 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00007006 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00007007 // Also promote i16 to i32 for performance / code size reason.
7008 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00007009 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00007010 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00007011
Evan Chengd40d03e2010-01-06 19:38:29 +00007012 // If the operand types disagree, extend the shift amount to match. Since
7013 // BT ignores high bits (like shifts) we can use anyextend.
7014 if (LHS.getValueType() != RHS.getValueType())
7015 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00007016
Evan Chengd40d03e2010-01-06 19:38:29 +00007017 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
7018 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
7019 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
7020 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00007021 }
7022
Evan Cheng54de3ea2010-01-05 06:52:31 +00007023 return SDValue();
7024}
7025
Dan Gohmand858e902010-04-17 15:26:15 +00007026SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng54de3ea2010-01-05 06:52:31 +00007027 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
7028 SDValue Op0 = Op.getOperand(0);
7029 SDValue Op1 = Op.getOperand(1);
7030 DebugLoc dl = Op.getDebugLoc();
7031 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
7032
7033 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00007034 // Lower (X & (1 << N)) == 0 to BT(X, N).
7035 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
7036 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Chris Lattner481eebc2010-12-19 21:23:48 +00007037 if (Op0.getOpcode() == ISD::AND && Op0.hasOneUse() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00007038 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane368b462010-06-18 14:22:04 +00007039 cast<ConstantSDNode>(Op1)->isNullValue() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00007040 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
7041 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
7042 if (NewSetCC.getNode())
7043 return NewSetCC;
7044 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00007045
Chris Lattner481eebc2010-12-19 21:23:48 +00007046 // Look for X == 0, X == 1, X != 0, or X != 1. We can simplify some forms of
7047 // these.
7048 if (Op1.getOpcode() == ISD::Constant &&
Evan Cheng2c755ba2010-02-27 07:36:59 +00007049 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
7050 cast<ConstantSDNode>(Op1)->isNullValue()) &&
7051 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007052
Chris Lattner481eebc2010-12-19 21:23:48 +00007053 // If the input is a setcc, then reuse the input setcc or use a new one with
7054 // the inverted condition.
7055 if (Op0.getOpcode() == X86ISD::SETCC) {
7056 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
7057 bool Invert = (CC == ISD::SETNE) ^
7058 cast<ConstantSDNode>(Op1)->isNullValue();
7059 if (!Invert) return Op0;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007060
Evan Cheng2c755ba2010-02-27 07:36:59 +00007061 CCode = X86::GetOppositeBranchCondition(CCode);
Chris Lattner481eebc2010-12-19 21:23:48 +00007062 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
7063 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
7064 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00007065 }
7066
Evan Chenge5b51ac2010-04-17 06:13:15 +00007067 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00007068 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00007069 if (X86CC == X86::COND_INVALID)
7070 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00007071
Chris Lattnerc19d1c32010-12-19 22:08:31 +00007072 SDValue EFLAGS = EmitCmp(Op0, Op1, X86CC, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00007073 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattnerc19d1c32010-12-19 22:08:31 +00007074 DAG.getConstant(X86CC, MVT::i8), EFLAGS);
Evan Cheng0488db92007-09-25 01:57:46 +00007075}
7076
Dan Gohmand858e902010-04-17 15:26:15 +00007077SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00007078 SDValue Cond;
7079 SDValue Op0 = Op.getOperand(0);
7080 SDValue Op1 = Op.getOperand(1);
7081 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00007082 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00007083 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
7084 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007085 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00007086
7087 if (isFP) {
7088 unsigned SSECC = 8;
Owen Andersone50ed302009-08-10 22:56:29 +00007089 EVT VT0 = Op0.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00007090 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
7091 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00007092 bool Swap = false;
7093
7094 switch (SetCCOpcode) {
7095 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00007096 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00007097 case ISD::SETEQ: SSECC = 0; break;
Scott Michelfdc40a02009-02-17 22:15:04 +00007098 case ISD::SETOGT:
Nate Begeman30a0de92008-07-17 16:51:19 +00007099 case ISD::SETGT: Swap = true; // Fallthrough
7100 case ISD::SETLT:
7101 case ISD::SETOLT: SSECC = 1; break;
7102 case ISD::SETOGE:
7103 case ISD::SETGE: Swap = true; // Fallthrough
7104 case ISD::SETLE:
7105 case ISD::SETOLE: SSECC = 2; break;
7106 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00007107 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00007108 case ISD::SETNE: SSECC = 4; break;
7109 case ISD::SETULE: Swap = true;
7110 case ISD::SETUGE: SSECC = 5; break;
7111 case ISD::SETULT: Swap = true;
7112 case ISD::SETUGT: SSECC = 6; break;
7113 case ISD::SETO: SSECC = 7; break;
7114 }
7115 if (Swap)
7116 std::swap(Op0, Op1);
7117
Nate Begemanfb8ead02008-07-25 19:05:58 +00007118 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00007119 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00007120 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00007121 SDValue UNORD, EQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00007122 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
7123 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00007124 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00007125 }
7126 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00007127 SDValue ORD, NEQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00007128 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
7129 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00007130 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00007131 }
Torok Edwinc23197a2009-07-14 16:55:14 +00007132 llvm_unreachable("Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00007133 }
7134 // Handle all other FP comparisons here.
Owen Anderson825b72b2009-08-11 20:47:22 +00007135 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00007136 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007137
Nate Begeman30a0de92008-07-17 16:51:19 +00007138 // We are handling one of the integer comparisons here. Since SSE only has
7139 // GT and EQ comparisons for integer, swapping operands and multiple
7140 // operations may be required for some comparisons.
7141 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
7142 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00007143
Owen Anderson825b72b2009-08-11 20:47:22 +00007144 switch (VT.getSimpleVT().SimpleTy) {
Nate Begeman30a0de92008-07-17 16:51:19 +00007145 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007146 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007147 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007148 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
7149 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00007150 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007151
Nate Begeman30a0de92008-07-17 16:51:19 +00007152 switch (SetCCOpcode) {
7153 default: break;
7154 case ISD::SETNE: Invert = true;
7155 case ISD::SETEQ: Opc = EQOpc; break;
7156 case ISD::SETLT: Swap = true;
7157 case ISD::SETGT: Opc = GTOpc; break;
7158 case ISD::SETGE: Swap = true;
7159 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
7160 case ISD::SETULT: Swap = true;
7161 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
7162 case ISD::SETUGE: Swap = true;
7163 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
7164 }
7165 if (Swap)
7166 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007167
Nate Begeman30a0de92008-07-17 16:51:19 +00007168 // Since SSE has no unsigned integer comparisons, we need to flip the sign
7169 // bits of the inputs before performing those operations.
7170 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00007171 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00007172 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
7173 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00007174 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00007175 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
7176 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00007177 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
7178 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00007179 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007180
Dale Johannesenace16102009-02-03 19:33:06 +00007181 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00007182
7183 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00007184 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00007185 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00007186
Nate Begeman30a0de92008-07-17 16:51:19 +00007187 return Result;
7188}
Evan Cheng0488db92007-09-25 01:57:46 +00007189
Evan Cheng370e5342008-12-03 08:38:43 +00007190// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00007191static bool isX86LogicalCmp(SDValue Op) {
7192 unsigned Opc = Op.getNode()->getOpcode();
7193 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
7194 return true;
7195 if (Op.getResNo() == 1 &&
7196 (Opc == X86ISD::ADD ||
7197 Opc == X86ISD::SUB ||
Chris Lattner5b856542010-12-20 00:59:46 +00007198 Opc == X86ISD::ADC ||
7199 Opc == X86ISD::SBB ||
Dan Gohman076aee32009-03-04 19:44:21 +00007200 Opc == X86ISD::SMUL ||
7201 Opc == X86ISD::UMUL ||
7202 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00007203 Opc == X86ISD::DEC ||
7204 Opc == X86ISD::OR ||
7205 Opc == X86ISD::XOR ||
7206 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00007207 return true;
7208
Chris Lattner9637d5b2010-12-05 07:49:54 +00007209 if (Op.getResNo() == 2 && Opc == X86ISD::UMUL)
7210 return true;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007211
Dan Gohman076aee32009-03-04 19:44:21 +00007212 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00007213}
7214
Chris Lattnera2b56002010-12-05 01:23:24 +00007215static bool isZero(SDValue V) {
7216 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
7217 return C && C->isNullValue();
7218}
7219
Chris Lattner96908b12010-12-05 02:00:51 +00007220static bool isAllOnes(SDValue V) {
7221 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
7222 return C && C->isAllOnesValue();
7223}
7224
Dan Gohmand858e902010-04-17 15:26:15 +00007225SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00007226 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00007227 SDValue Cond = Op.getOperand(0);
Chris Lattnera2b56002010-12-05 01:23:24 +00007228 SDValue Op1 = Op.getOperand(1);
7229 SDValue Op2 = Op.getOperand(2);
7230 DebugLoc DL = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00007231 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00007232
Dan Gohman1a492952009-10-20 16:22:37 +00007233 if (Cond.getOpcode() == ISD::SETCC) {
7234 SDValue NewCond = LowerSETCC(Cond, DAG);
7235 if (NewCond.getNode())
7236 Cond = NewCond;
7237 }
Evan Cheng734503b2006-09-11 02:19:56 +00007238
Chris Lattnera2b56002010-12-05 01:23:24 +00007239 // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00007240 // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
Chris Lattnera2b56002010-12-05 01:23:24 +00007241 // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00007242 // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007243 if (Cond.getOpcode() == X86ISD::SETCC &&
Chris Lattner96908b12010-12-05 02:00:51 +00007244 Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
7245 isZero(Cond.getOperand(1).getOperand(1))) {
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007246 SDValue Cmp = Cond.getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007247
Chris Lattnera2b56002010-12-05 01:23:24 +00007248 unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007249
7250 if ((isAllOnes(Op1) || isAllOnes(Op2)) &&
Chris Lattner96908b12010-12-05 02:00:51 +00007251 (CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
7252 SDValue Y = isAllOnes(Op2) ? Op1 : Op2;
Chris Lattnera2b56002010-12-05 01:23:24 +00007253
7254 SDValue CmpOp0 = Cmp.getOperand(0);
7255 Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32,
7256 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007257
Chris Lattner96908b12010-12-05 02:00:51 +00007258 SDValue Res = // Res = 0 or -1.
Chris Lattnera2b56002010-12-05 01:23:24 +00007259 DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
7260 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007261
Chris Lattner96908b12010-12-05 02:00:51 +00007262 if (isAllOnes(Op1) != (CondCode == X86::COND_E))
7263 Res = DAG.getNOT(DL, Res, Res.getValueType());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007264
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007265 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
Chris Lattnera2b56002010-12-05 01:23:24 +00007266 if (N2C == 0 || !N2C->isNullValue())
7267 Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
7268 return Res;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007269 }
7270 }
7271
Chris Lattnera2b56002010-12-05 01:23:24 +00007272 // Look past (and (setcc_carry (cmp ...)), 1).
Evan Chengad9c0a32009-12-15 00:53:42 +00007273 if (Cond.getOpcode() == ISD::AND &&
7274 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
7275 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00007276 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00007277 Cond = Cond.getOperand(0);
7278 }
7279
Evan Cheng3f41d662007-10-08 22:16:29 +00007280 // If condition flag is set by a X86ISD::CMP, then use it as the condition
7281 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00007282 if (Cond.getOpcode() == X86ISD::SETCC ||
7283 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00007284 CC = Cond.getOperand(0);
7285
Dan Gohman475871a2008-07-27 21:46:04 +00007286 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00007287 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00007288 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00007289
Evan Cheng3f41d662007-10-08 22:16:29 +00007290 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007291 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00007292 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00007293 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00007294
Chris Lattnerd1980a52009-03-12 06:52:53 +00007295 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
7296 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00007297 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00007298 addTest = false;
7299 }
7300 }
7301
7302 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007303 // Look pass the truncate.
7304 if (Cond.getOpcode() == ISD::TRUNCATE)
7305 Cond = Cond.getOperand(0);
7306
7307 // We know the result of AND is compared against zero. Try to match
7308 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00007309 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Chris Lattnera2b56002010-12-05 01:23:24 +00007310 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG);
Evan Chengd40d03e2010-01-06 19:38:29 +00007311 if (NewSetCC.getNode()) {
7312 CC = NewSetCC.getOperand(0);
7313 Cond = NewSetCC.getOperand(1);
7314 addTest = false;
7315 }
7316 }
7317 }
7318
7319 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007320 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00007321 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00007322 }
7323
Benjamin Kramere915ff32010-12-22 23:09:28 +00007324 // a < b ? -1 : 0 -> RES = ~setcc_carry
7325 // a < b ? 0 : -1 -> RES = setcc_carry
7326 // a >= b ? -1 : 0 -> RES = setcc_carry
7327 // a >= b ? 0 : -1 -> RES = ~setcc_carry
7328 if (Cond.getOpcode() == X86ISD::CMP) {
7329 unsigned CondCode = cast<ConstantSDNode>(CC)->getZExtValue();
7330
7331 if ((CondCode == X86::COND_AE || CondCode == X86::COND_B) &&
7332 (isAllOnes(Op1) || isAllOnes(Op2)) && (isZero(Op1) || isZero(Op2))) {
7333 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
7334 DAG.getConstant(X86::COND_B, MVT::i8), Cond);
7335 if (isAllOnes(Op1) != (CondCode == X86::COND_B))
7336 return DAG.getNOT(DL, Res, Res.getValueType());
7337 return Res;
7338 }
7339 }
7340
Evan Cheng0488db92007-09-25 01:57:46 +00007341 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
7342 // condition is true.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007343 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007344 SDValue Ops[] = { Op2, Op1, CC, Cond };
Chris Lattnera2b56002010-12-05 01:23:24 +00007345 return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00007346}
7347
Evan Cheng370e5342008-12-03 08:38:43 +00007348// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
7349// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
7350// from the AND / OR.
7351static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
7352 Opc = Op.getOpcode();
7353 if (Opc != ISD::OR && Opc != ISD::AND)
7354 return false;
7355 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
7356 Op.getOperand(0).hasOneUse() &&
7357 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
7358 Op.getOperand(1).hasOneUse());
7359}
7360
Evan Cheng961d6d42009-02-02 08:19:07 +00007361// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
7362// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00007363static bool isXor1OfSetCC(SDValue Op) {
7364 if (Op.getOpcode() != ISD::XOR)
7365 return false;
7366 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
7367 if (N1C && N1C->getAPIntValue() == 1) {
7368 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
7369 Op.getOperand(0).hasOneUse();
7370 }
7371 return false;
7372}
7373
Dan Gohmand858e902010-04-17 15:26:15 +00007374SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00007375 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00007376 SDValue Chain = Op.getOperand(0);
7377 SDValue Cond = Op.getOperand(1);
7378 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007379 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00007380 SDValue CC;
Evan Cheng734503b2006-09-11 02:19:56 +00007381
Dan Gohman1a492952009-10-20 16:22:37 +00007382 if (Cond.getOpcode() == ISD::SETCC) {
7383 SDValue NewCond = LowerSETCC(Cond, DAG);
7384 if (NewCond.getNode())
7385 Cond = NewCond;
7386 }
Chris Lattnere55484e2008-12-25 05:34:37 +00007387#if 0
7388 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00007389 else if (Cond.getOpcode() == X86ISD::ADD ||
7390 Cond.getOpcode() == X86ISD::SUB ||
7391 Cond.getOpcode() == X86ISD::SMUL ||
7392 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00007393 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00007394#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00007395
Evan Chengad9c0a32009-12-15 00:53:42 +00007396 // Look pass (and (setcc_carry (cmp ...)), 1).
7397 if (Cond.getOpcode() == ISD::AND &&
7398 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
7399 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00007400 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00007401 Cond = Cond.getOperand(0);
7402 }
7403
Evan Cheng3f41d662007-10-08 22:16:29 +00007404 // If condition flag is set by a X86ISD::CMP, then use it as the condition
7405 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00007406 if (Cond.getOpcode() == X86ISD::SETCC ||
7407 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00007408 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007409
Dan Gohman475871a2008-07-27 21:46:04 +00007410 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00007411 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00007412 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00007413 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00007414 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00007415 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00007416 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00007417 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00007418 default: break;
7419 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00007420 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00007421 // These can only come from an arithmetic instruction with overflow,
7422 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00007423 Cond = Cond.getNode()->getOperand(1);
7424 addTest = false;
7425 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00007426 }
Evan Cheng0488db92007-09-25 01:57:46 +00007427 }
Evan Cheng370e5342008-12-03 08:38:43 +00007428 } else {
7429 unsigned CondOpc;
7430 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
7431 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00007432 if (CondOpc == ISD::OR) {
7433 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
7434 // two branches instead of an explicit OR instruction with a
7435 // separate test.
7436 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00007437 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00007438 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007439 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00007440 Chain, Dest, CC, Cmp);
7441 CC = Cond.getOperand(1).getOperand(0);
7442 Cond = Cmp;
7443 addTest = false;
7444 }
7445 } else { // ISD::AND
7446 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
7447 // two branches instead of an explicit AND instruction with a
7448 // separate test. However, we only do this if this block doesn't
7449 // have a fall-through edge, because this requires an explicit
7450 // jmp when the condition is false.
7451 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00007452 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00007453 Op.getNode()->hasOneUse()) {
7454 X86::CondCode CCode =
7455 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
7456 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007457 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00007458 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00007459 // Look for an unconditional branch following this conditional branch.
7460 // We need this because we need to reverse the successors in order
7461 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00007462 if (User->getOpcode() == ISD::BR) {
7463 SDValue FalseBB = User->getOperand(1);
7464 SDNode *NewBR =
7465 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00007466 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00007467 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00007468 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00007469
Dale Johannesene4d209d2009-02-03 20:21:25 +00007470 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00007471 Chain, Dest, CC, Cmp);
7472 X86::CondCode CCode =
7473 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
7474 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007475 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00007476 Cond = Cmp;
7477 addTest = false;
7478 }
7479 }
Dan Gohman279c22e2008-10-21 03:29:32 +00007480 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00007481 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
7482 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
7483 // It should be transformed during dag combiner except when the condition
7484 // is set by a arithmetics with overflow node.
7485 X86::CondCode CCode =
7486 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
7487 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007488 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00007489 Cond = Cond.getOperand(0).getOperand(1);
7490 addTest = false;
Dan Gohman279c22e2008-10-21 03:29:32 +00007491 }
Evan Cheng0488db92007-09-25 01:57:46 +00007492 }
7493
7494 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007495 // Look pass the truncate.
7496 if (Cond.getOpcode() == ISD::TRUNCATE)
7497 Cond = Cond.getOperand(0);
7498
7499 // We know the result of AND is compared against zero. Try to match
7500 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00007501 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007502 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
7503 if (NewSetCC.getNode()) {
7504 CC = NewSetCC.getOperand(0);
7505 Cond = NewSetCC.getOperand(1);
7506 addTest = false;
7507 }
7508 }
7509 }
7510
7511 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007512 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00007513 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00007514 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00007515 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00007516 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00007517}
7518
Anton Korobeynikove060b532007-04-17 19:34:00 +00007519
7520// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
7521// Calls to _alloca is needed to probe the stack when allocating more than 4k
7522// bytes in one go. Touching the stack at 4K increments is necessary to ensure
7523// that the guard pages used by the OS virtual memory manager are allocated in
7524// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00007525SDValue
7526X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00007527 SelectionDAG &DAG) const {
Duncan Sands1e1ca0b2010-10-21 16:02:12 +00007528 assert((Subtarget->isTargetCygMing() || Subtarget->isTargetWindows()) &&
Michael J. Spencere9c253e2010-10-21 01:41:01 +00007529 "This should be used only on Windows targets");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007530 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007531
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007532 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00007533 SDValue Chain = Op.getOperand(0);
7534 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007535 // FIXME: Ensure alignment here
7536
Dan Gohman475871a2008-07-27 21:46:04 +00007537 SDValue Flag;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007538
Owen Anderson825b72b2009-08-11 20:47:22 +00007539 EVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007540
Dale Johannesendd64c412009-02-04 00:33:20 +00007541 Chain = DAG.getCopyToReg(Chain, dl, X86::EAX, Size, Flag);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00007542 Flag = Chain.getValue(1);
7543
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007544 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00007545
Michael J. Spencere9c253e2010-10-21 01:41:01 +00007546 Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag);
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00007547 Flag = Chain.getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007548
Dale Johannesendd64c412009-02-04 00:33:20 +00007549 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007550
Dan Gohman475871a2008-07-27 21:46:04 +00007551 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Dale Johannesene4d209d2009-02-03 20:21:25 +00007552 return DAG.getMergeValues(Ops1, 2, dl);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007553}
7554
Dan Gohmand858e902010-04-17 15:26:15 +00007555SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00007556 MachineFunction &MF = DAG.getMachineFunction();
7557 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
7558
Dan Gohman69de1932008-02-06 22:27:42 +00007559 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00007560 DebugLoc DL = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00007561
Anton Korobeynikove7beda12010-10-03 22:52:07 +00007562 if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00007563 // vastart just stores the address of the VarArgsFrameIndex slot into the
7564 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +00007565 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
7566 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00007567 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
7568 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007569 }
7570
7571 // __va_list_tag:
7572 // gp_offset (0 - 6 * 8)
7573 // fp_offset (48 - 48 + 8 * 16)
7574 // overflow_arg_area (point to parameters coming in memory).
7575 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00007576 SmallVector<SDValue, 8> MemOps;
7577 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00007578 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00007579 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00007580 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
7581 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007582 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007583 MemOps.push_back(Store);
7584
7585 // Store fp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00007586 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007587 FIN, DAG.getIntPtrConstant(4));
Chris Lattner8026a9d2010-09-21 17:50:43 +00007588 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00007589 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
7590 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007591 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007592 MemOps.push_back(Store);
7593
7594 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +00007595 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007596 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +00007597 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
7598 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00007599 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
7600 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +00007601 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007602 MemOps.push_back(Store);
7603
7604 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +00007605 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007606 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +00007607 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
7608 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00007609 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
7610 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007611 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +00007612 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00007613 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00007614}
7615
Dan Gohmand858e902010-04-17 15:26:15 +00007616SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman320afb82010-10-12 18:00:49 +00007617 assert(Subtarget->is64Bit() &&
7618 "LowerVAARG only handles 64-bit va_arg!");
7619 assert((Subtarget->isTargetLinux() ||
7620 Subtarget->isTargetDarwin()) &&
7621 "Unhandled target in LowerVAARG");
7622 assert(Op.getNode()->getNumOperands() == 4);
7623 SDValue Chain = Op.getOperand(0);
7624 SDValue SrcPtr = Op.getOperand(1);
7625 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
7626 unsigned Align = Op.getConstantOperandVal(3);
7627 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9018e832008-05-10 01:26:14 +00007628
Dan Gohman320afb82010-10-12 18:00:49 +00007629 EVT ArgVT = Op.getNode()->getValueType(0);
7630 const Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
7631 uint32_t ArgSize = getTargetData()->getTypeAllocSize(ArgTy);
7632 uint8_t ArgMode;
7633
7634 // Decide which area this value should be read from.
7635 // TODO: Implement the AMD64 ABI in its entirety. This simple
7636 // selection mechanism works only for the basic types.
7637 if (ArgVT == MVT::f80) {
7638 llvm_unreachable("va_arg for f80 not yet implemented");
7639 } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) {
7640 ArgMode = 2; // Argument passed in XMM register. Use fp_offset.
7641 } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) {
7642 ArgMode = 1; // Argument passed in GPR64 register(s). Use gp_offset.
7643 } else {
7644 llvm_unreachable("Unhandled argument type in LowerVAARG");
7645 }
7646
7647 if (ArgMode == 2) {
7648 // Sanity Check: Make sure using fp_offset makes sense.
Michael J. Spencer87b86652010-10-19 07:32:42 +00007649 assert(!UseSoftFloat &&
Eric Christopher52b45052010-10-12 19:44:17 +00007650 !(DAG.getMachineFunction()
7651 .getFunction()->hasFnAttr(Attribute::NoImplicitFloat)) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00007652 Subtarget->hasXMM());
Dan Gohman320afb82010-10-12 18:00:49 +00007653 }
7654
7655 // Insert VAARG_64 node into the DAG
7656 // VAARG_64 returns two values: Variable Argument Address, Chain
7657 SmallVector<SDValue, 11> InstOps;
7658 InstOps.push_back(Chain);
7659 InstOps.push_back(SrcPtr);
7660 InstOps.push_back(DAG.getConstant(ArgSize, MVT::i32));
7661 InstOps.push_back(DAG.getConstant(ArgMode, MVT::i8));
7662 InstOps.push_back(DAG.getConstant(Align, MVT::i32));
7663 SDVTList VTs = DAG.getVTList(getPointerTy(), MVT::Other);
7664 SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl,
7665 VTs, &InstOps[0], InstOps.size(),
7666 MVT::i64,
7667 MachinePointerInfo(SV),
7668 /*Align=*/0,
7669 /*Volatile=*/false,
7670 /*ReadMem=*/true,
7671 /*WriteMem=*/true);
7672 Chain = VAARG.getValue(1);
7673
7674 // Load the next argument and return it
7675 return DAG.getLoad(ArgVT, dl,
7676 Chain,
7677 VAARG,
7678 MachinePointerInfo(),
7679 false, false, 0);
Dan Gohman9018e832008-05-10 01:26:14 +00007680}
7681
Dan Gohmand858e902010-04-17 15:26:15 +00007682SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) const {
Evan Chengae642192007-03-02 23:16:35 +00007683 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00007684 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00007685 SDValue Chain = Op.getOperand(0);
7686 SDValue DstPtr = Op.getOperand(1);
7687 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00007688 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
7689 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Chris Lattnere72f2022010-09-21 05:40:29 +00007690 DebugLoc DL = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00007691
Chris Lattnere72f2022010-09-21 05:40:29 +00007692 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +00007693 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Michael J. Spencerec38de22010-10-10 22:04:20 +00007694 false,
Chris Lattnere72f2022010-09-21 05:40:29 +00007695 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +00007696}
7697
Dan Gohman475871a2008-07-27 21:46:04 +00007698SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007699X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007700 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007701 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007702 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00007703 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00007704 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00007705 case Intrinsic::x86_sse_comieq_ss:
7706 case Intrinsic::x86_sse_comilt_ss:
7707 case Intrinsic::x86_sse_comile_ss:
7708 case Intrinsic::x86_sse_comigt_ss:
7709 case Intrinsic::x86_sse_comige_ss:
7710 case Intrinsic::x86_sse_comineq_ss:
7711 case Intrinsic::x86_sse_ucomieq_ss:
7712 case Intrinsic::x86_sse_ucomilt_ss:
7713 case Intrinsic::x86_sse_ucomile_ss:
7714 case Intrinsic::x86_sse_ucomigt_ss:
7715 case Intrinsic::x86_sse_ucomige_ss:
7716 case Intrinsic::x86_sse_ucomineq_ss:
7717 case Intrinsic::x86_sse2_comieq_sd:
7718 case Intrinsic::x86_sse2_comilt_sd:
7719 case Intrinsic::x86_sse2_comile_sd:
7720 case Intrinsic::x86_sse2_comigt_sd:
7721 case Intrinsic::x86_sse2_comige_sd:
7722 case Intrinsic::x86_sse2_comineq_sd:
7723 case Intrinsic::x86_sse2_ucomieq_sd:
7724 case Intrinsic::x86_sse2_ucomilt_sd:
7725 case Intrinsic::x86_sse2_ucomile_sd:
7726 case Intrinsic::x86_sse2_ucomigt_sd:
7727 case Intrinsic::x86_sse2_ucomige_sd:
7728 case Intrinsic::x86_sse2_ucomineq_sd: {
7729 unsigned Opc = 0;
7730 ISD::CondCode CC = ISD::SETCC_INVALID;
7731 switch (IntNo) {
7732 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007733 case Intrinsic::x86_sse_comieq_ss:
7734 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007735 Opc = X86ISD::COMI;
7736 CC = ISD::SETEQ;
7737 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00007738 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007739 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007740 Opc = X86ISD::COMI;
7741 CC = ISD::SETLT;
7742 break;
7743 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007744 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007745 Opc = X86ISD::COMI;
7746 CC = ISD::SETLE;
7747 break;
7748 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007749 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007750 Opc = X86ISD::COMI;
7751 CC = ISD::SETGT;
7752 break;
7753 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007754 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007755 Opc = X86ISD::COMI;
7756 CC = ISD::SETGE;
7757 break;
7758 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007759 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007760 Opc = X86ISD::COMI;
7761 CC = ISD::SETNE;
7762 break;
7763 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007764 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007765 Opc = X86ISD::UCOMI;
7766 CC = ISD::SETEQ;
7767 break;
7768 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007769 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007770 Opc = X86ISD::UCOMI;
7771 CC = ISD::SETLT;
7772 break;
7773 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007774 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007775 Opc = X86ISD::UCOMI;
7776 CC = ISD::SETLE;
7777 break;
7778 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007779 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007780 Opc = X86ISD::UCOMI;
7781 CC = ISD::SETGT;
7782 break;
7783 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007784 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007785 Opc = X86ISD::UCOMI;
7786 CC = ISD::SETGE;
7787 break;
7788 case Intrinsic::x86_sse_ucomineq_ss:
7789 case Intrinsic::x86_sse2_ucomineq_sd:
7790 Opc = X86ISD::UCOMI;
7791 CC = ISD::SETNE;
7792 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00007793 }
Evan Cheng734503b2006-09-11 02:19:56 +00007794
Dan Gohman475871a2008-07-27 21:46:04 +00007795 SDValue LHS = Op.getOperand(1);
7796 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00007797 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00007798 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00007799 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
7800 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
7801 DAG.getConstant(X86CC, MVT::i8), Cond);
7802 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00007803 }
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007804 // ptest and testp intrinsics. The intrinsic these come from are designed to
7805 // return an integer value, not just an instruction so lower it to the ptest
7806 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00007807 case Intrinsic::x86_sse41_ptestz:
7808 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007809 case Intrinsic::x86_sse41_ptestnzc:
7810 case Intrinsic::x86_avx_ptestz_256:
7811 case Intrinsic::x86_avx_ptestc_256:
7812 case Intrinsic::x86_avx_ptestnzc_256:
7813 case Intrinsic::x86_avx_vtestz_ps:
7814 case Intrinsic::x86_avx_vtestc_ps:
7815 case Intrinsic::x86_avx_vtestnzc_ps:
7816 case Intrinsic::x86_avx_vtestz_pd:
7817 case Intrinsic::x86_avx_vtestc_pd:
7818 case Intrinsic::x86_avx_vtestnzc_pd:
7819 case Intrinsic::x86_avx_vtestz_ps_256:
7820 case Intrinsic::x86_avx_vtestc_ps_256:
7821 case Intrinsic::x86_avx_vtestnzc_ps_256:
7822 case Intrinsic::x86_avx_vtestz_pd_256:
7823 case Intrinsic::x86_avx_vtestc_pd_256:
7824 case Intrinsic::x86_avx_vtestnzc_pd_256: {
7825 bool IsTestPacked = false;
Eric Christopher71c67532009-07-29 00:28:05 +00007826 unsigned X86CC = 0;
7827 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00007828 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007829 case Intrinsic::x86_avx_vtestz_ps:
7830 case Intrinsic::x86_avx_vtestz_pd:
7831 case Intrinsic::x86_avx_vtestz_ps_256:
7832 case Intrinsic::x86_avx_vtestz_pd_256:
7833 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00007834 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007835 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +00007836 // ZF = 1
7837 X86CC = X86::COND_E;
7838 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007839 case Intrinsic::x86_avx_vtestc_ps:
7840 case Intrinsic::x86_avx_vtestc_pd:
7841 case Intrinsic::x86_avx_vtestc_ps_256:
7842 case Intrinsic::x86_avx_vtestc_pd_256:
7843 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00007844 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007845 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00007846 // CF = 1
7847 X86CC = X86::COND_B;
7848 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007849 case Intrinsic::x86_avx_vtestnzc_ps:
7850 case Intrinsic::x86_avx_vtestnzc_pd:
7851 case Intrinsic::x86_avx_vtestnzc_ps_256:
7852 case Intrinsic::x86_avx_vtestnzc_pd_256:
7853 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +00007854 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007855 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00007856 // ZF and CF = 0
7857 X86CC = X86::COND_A;
7858 break;
7859 }
Eric Christopherfd179292009-08-27 18:07:15 +00007860
Eric Christopher71c67532009-07-29 00:28:05 +00007861 SDValue LHS = Op.getOperand(1);
7862 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007863 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
7864 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00007865 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
7866 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
7867 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +00007868 }
Evan Cheng5759f972008-05-04 09:15:50 +00007869
7870 // Fix vector shift instructions where the last operand is a non-immediate
7871 // i32 value.
7872 case Intrinsic::x86_sse2_pslli_w:
7873 case Intrinsic::x86_sse2_pslli_d:
7874 case Intrinsic::x86_sse2_pslli_q:
7875 case Intrinsic::x86_sse2_psrli_w:
7876 case Intrinsic::x86_sse2_psrli_d:
7877 case Intrinsic::x86_sse2_psrli_q:
7878 case Intrinsic::x86_sse2_psrai_w:
7879 case Intrinsic::x86_sse2_psrai_d:
7880 case Intrinsic::x86_mmx_pslli_w:
7881 case Intrinsic::x86_mmx_pslli_d:
7882 case Intrinsic::x86_mmx_pslli_q:
7883 case Intrinsic::x86_mmx_psrli_w:
7884 case Intrinsic::x86_mmx_psrli_d:
7885 case Intrinsic::x86_mmx_psrli_q:
7886 case Intrinsic::x86_mmx_psrai_w:
7887 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00007888 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00007889 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00007890 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00007891
7892 unsigned NewIntNo = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00007893 EVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00007894 switch (IntNo) {
7895 case Intrinsic::x86_sse2_pslli_w:
7896 NewIntNo = Intrinsic::x86_sse2_psll_w;
7897 break;
7898 case Intrinsic::x86_sse2_pslli_d:
7899 NewIntNo = Intrinsic::x86_sse2_psll_d;
7900 break;
7901 case Intrinsic::x86_sse2_pslli_q:
7902 NewIntNo = Intrinsic::x86_sse2_psll_q;
7903 break;
7904 case Intrinsic::x86_sse2_psrli_w:
7905 NewIntNo = Intrinsic::x86_sse2_psrl_w;
7906 break;
7907 case Intrinsic::x86_sse2_psrli_d:
7908 NewIntNo = Intrinsic::x86_sse2_psrl_d;
7909 break;
7910 case Intrinsic::x86_sse2_psrli_q:
7911 NewIntNo = Intrinsic::x86_sse2_psrl_q;
7912 break;
7913 case Intrinsic::x86_sse2_psrai_w:
7914 NewIntNo = Intrinsic::x86_sse2_psra_w;
7915 break;
7916 case Intrinsic::x86_sse2_psrai_d:
7917 NewIntNo = Intrinsic::x86_sse2_psra_d;
7918 break;
7919 default: {
Owen Anderson825b72b2009-08-11 20:47:22 +00007920 ShAmtVT = MVT::v2i32;
Evan Cheng5759f972008-05-04 09:15:50 +00007921 switch (IntNo) {
7922 case Intrinsic::x86_mmx_pslli_w:
7923 NewIntNo = Intrinsic::x86_mmx_psll_w;
7924 break;
7925 case Intrinsic::x86_mmx_pslli_d:
7926 NewIntNo = Intrinsic::x86_mmx_psll_d;
7927 break;
7928 case Intrinsic::x86_mmx_pslli_q:
7929 NewIntNo = Intrinsic::x86_mmx_psll_q;
7930 break;
7931 case Intrinsic::x86_mmx_psrli_w:
7932 NewIntNo = Intrinsic::x86_mmx_psrl_w;
7933 break;
7934 case Intrinsic::x86_mmx_psrli_d:
7935 NewIntNo = Intrinsic::x86_mmx_psrl_d;
7936 break;
7937 case Intrinsic::x86_mmx_psrli_q:
7938 NewIntNo = Intrinsic::x86_mmx_psrl_q;
7939 break;
7940 case Intrinsic::x86_mmx_psrai_w:
7941 NewIntNo = Intrinsic::x86_mmx_psra_w;
7942 break;
7943 case Intrinsic::x86_mmx_psrai_d:
7944 NewIntNo = Intrinsic::x86_mmx_psra_d;
7945 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00007946 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng5759f972008-05-04 09:15:50 +00007947 }
7948 break;
7949 }
7950 }
Mon P Wangefa42202009-09-03 19:56:25 +00007951
7952 // The vector shift intrinsics with scalars uses 32b shift amounts but
7953 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
7954 // to be zero.
7955 SDValue ShOps[4];
7956 ShOps[0] = ShAmt;
7957 ShOps[1] = DAG.getConstant(0, MVT::i32);
7958 if (ShAmtVT == MVT::v4i32) {
7959 ShOps[2] = DAG.getUNDEF(MVT::i32);
7960 ShOps[3] = DAG.getUNDEF(MVT::i32);
7961 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 4);
7962 } else {
7963 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 2);
Dale Johannesen0488fb62010-09-30 23:57:10 +00007964// FIXME this must be lowered to get rid of the invalid type.
Mon P Wangefa42202009-09-03 19:56:25 +00007965 }
7966
Owen Andersone50ed302009-08-10 22:56:29 +00007967 EVT VT = Op.getValueType();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007968 ShAmt = DAG.getNode(ISD::BITCAST, dl, VT, ShAmt);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007969 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007970 DAG.getConstant(NewIntNo, MVT::i32),
Evan Cheng5759f972008-05-04 09:15:50 +00007971 Op.getOperand(1), ShAmt);
7972 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00007973 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007974}
Evan Cheng72261582005-12-20 06:22:03 +00007975
Dan Gohmand858e902010-04-17 15:26:15 +00007976SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
7977 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +00007978 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7979 MFI->setReturnAddressIsTaken(true);
7980
Bill Wendling64e87322009-01-16 19:25:27 +00007981 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007982 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00007983
7984 if (Depth > 0) {
7985 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
7986 SDValue Offset =
7987 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +00007988 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007989 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00007990 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007991 FrameAddr, Offset),
Chris Lattner51abfe42010-09-21 06:02:19 +00007992 MachinePointerInfo(), false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +00007993 }
7994
7995 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00007996 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00007997 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Chris Lattner51abfe42010-09-21 06:02:19 +00007998 RetAddrFI, MachinePointerInfo(), false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00007999}
8000
Dan Gohmand858e902010-04-17 15:26:15 +00008001SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +00008002 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
8003 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00008004
Owen Andersone50ed302009-08-10 22:56:29 +00008005 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008006 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00008007 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
8008 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00008009 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00008010 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +00008011 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
8012 MachinePointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +00008013 false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00008014 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00008015}
8016
Dan Gohman475871a2008-07-27 21:46:04 +00008017SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008018 SelectionDAG &DAG) const {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00008019 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008020}
8021
Dan Gohmand858e902010-04-17 15:26:15 +00008022SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008023 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00008024 SDValue Chain = Op.getOperand(0);
8025 SDValue Offset = Op.getOperand(1);
8026 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008027 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008028
Dan Gohmand8816272010-08-11 18:14:00 +00008029 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
8030 Subtarget->is64Bit() ? X86::RBP : X86::EBP,
8031 getPointerTy());
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008032 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008033
Dan Gohmand8816272010-08-11 18:14:00 +00008034 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Frame,
8035 DAG.getIntPtrConstant(TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008036 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +00008037 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
8038 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00008039 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008040 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008041
Dale Johannesene4d209d2009-02-03 20:21:25 +00008042 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008043 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008044 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008045}
8046
Dan Gohman475871a2008-07-27 21:46:04 +00008047SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008048 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00008049 SDValue Root = Op.getOperand(0);
8050 SDValue Trmp = Op.getOperand(1); // trampoline
8051 SDValue FPtr = Op.getOperand(2); // nested function
8052 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008053 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008054
Dan Gohman69de1932008-02-06 22:27:42 +00008055 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008056
8057 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00008058 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00008059
8060 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +00008061 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
8062 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +00008063
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00008064 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
8065 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00008066
8067 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
8068
8069 // Load the pointer to the nested function into R11.
8070 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00008071 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00008072 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008073 Addr, MachinePointerInfo(TrmpAddr),
8074 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008075
Owen Anderson825b72b2009-08-11 20:47:22 +00008076 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8077 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008078 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
8079 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +00008080 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00008081
8082 // Load the 'nest' parameter value into R10.
8083 // R10 is specified in X86CallingConv.td
8084 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +00008085 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8086 DAG.getConstant(10, MVT::i64));
8087 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008088 Addr, MachinePointerInfo(TrmpAddr, 10),
8089 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008090
Owen Anderson825b72b2009-08-11 20:47:22 +00008091 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8092 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008093 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
8094 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +00008095 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00008096
8097 // Jump to the nested function.
8098 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +00008099 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8100 DAG.getConstant(20, MVT::i64));
8101 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008102 Addr, MachinePointerInfo(TrmpAddr, 20),
8103 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008104
8105 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +00008106 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8107 DAG.getConstant(22, MVT::i64));
8108 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00008109 MachinePointerInfo(TrmpAddr, 22),
8110 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008111
Dan Gohman475871a2008-07-27 21:46:04 +00008112 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00008113 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008114 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008115 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00008116 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00008117 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00008118 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00008119 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008120
8121 switch (CC) {
8122 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00008123 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +00008124 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00008125 case CallingConv::X86_StdCall: {
8126 // Pass 'nest' parameter in ECX.
8127 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00008128 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008129
8130 // Check that ECX wasn't needed by an 'inreg' parameter.
8131 const FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00008132 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008133
Chris Lattner58d74912008-03-12 17:45:29 +00008134 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00008135 unsigned InRegCount = 0;
8136 unsigned Idx = 1;
8137
8138 for (FunctionType::param_iterator I = FTy->param_begin(),
8139 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00008140 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00008141 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00008142 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008143
8144 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +00008145 report_fatal_error("Nest register in use - reduce number of inreg"
8146 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +00008147 }
8148 }
8149 break;
8150 }
8151 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +00008152 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00008153 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00008154 // Pass 'nest' parameter in EAX.
8155 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00008156 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008157 break;
8158 }
8159
Dan Gohman475871a2008-07-27 21:46:04 +00008160 SDValue OutChains[4];
8161 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008162
Owen Anderson825b72b2009-08-11 20:47:22 +00008163 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8164 DAG.getConstant(10, MVT::i32));
8165 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008166
Chris Lattnera62fe662010-02-05 19:20:30 +00008167 // This is storing the opcode for MOV32ri.
8168 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00008169 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00008170 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008171 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008172 Trmp, MachinePointerInfo(TrmpAddr),
8173 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008174
Owen Anderson825b72b2009-08-11 20:47:22 +00008175 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8176 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008177 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
8178 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +00008179 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008180
Chris Lattnera62fe662010-02-05 19:20:30 +00008181 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +00008182 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8183 DAG.getConstant(5, MVT::i32));
8184 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00008185 MachinePointerInfo(TrmpAddr, 5),
8186 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008187
Owen Anderson825b72b2009-08-11 20:47:22 +00008188 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8189 DAG.getConstant(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008190 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
8191 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +00008192 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008193
Dan Gohman475871a2008-07-27 21:46:04 +00008194 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00008195 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008196 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008197 }
8198}
8199
Dan Gohmand858e902010-04-17 15:26:15 +00008200SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
8201 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008202 /*
8203 The rounding mode is in bits 11:10 of FPSR, and has the following
8204 settings:
8205 00 Round to nearest
8206 01 Round to -inf
8207 10 Round to +inf
8208 11 Round to 0
8209
8210 FLT_ROUNDS, on the other hand, expects the following:
8211 -1 Undefined
8212 0 Round to 0
8213 1 Round to nearest
8214 2 Round to +inf
8215 3 Round to -inf
8216
8217 To perform the conversion, we do:
8218 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
8219 */
8220
8221 MachineFunction &MF = DAG.getMachineFunction();
8222 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00008223 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008224 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00008225 EVT VT = Op.getValueType();
Chris Lattner2156b792010-09-22 01:11:26 +00008226 DebugLoc DL = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008227
8228 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +00008229 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +00008230 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008231
Michael J. Spencerec38de22010-10-10 22:04:20 +00008232
Chris Lattner2156b792010-09-22 01:11:26 +00008233 MachineMemOperand *MMO =
8234 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8235 MachineMemOperand::MOStore, 2, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008236
Chris Lattner2156b792010-09-22 01:11:26 +00008237 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
8238 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
8239 DAG.getVTList(MVT::Other),
8240 Ops, 2, MVT::i16, MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008241
8242 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +00008243 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Chris Lattner51abfe42010-09-21 06:02:19 +00008244 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008245
8246 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00008247 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +00008248 DAG.getNode(ISD::SRL, DL, MVT::i16,
8249 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00008250 CWD, DAG.getConstant(0x800, MVT::i16)),
8251 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00008252 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +00008253 DAG.getNode(ISD::SRL, DL, MVT::i16,
8254 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00008255 CWD, DAG.getConstant(0x400, MVT::i16)),
8256 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008257
Dan Gohman475871a2008-07-27 21:46:04 +00008258 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +00008259 DAG.getNode(ISD::AND, DL, MVT::i16,
8260 DAG.getNode(ISD::ADD, DL, MVT::i16,
8261 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +00008262 DAG.getConstant(1, MVT::i16)),
8263 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008264
8265
Duncan Sands83ec4b62008-06-06 12:08:01 +00008266 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +00008267 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008268}
8269
Dan Gohmand858e902010-04-17 15:26:15 +00008270SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008271 EVT VT = Op.getValueType();
8272 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008273 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008274 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00008275
8276 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008277 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00008278 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +00008279 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00008280 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008281 }
Evan Cheng18efe262007-12-14 02:13:44 +00008282
Evan Cheng152804e2007-12-14 08:30:15 +00008283 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008284 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008285 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00008286
8287 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00008288 SDValue Ops[] = {
8289 Op,
8290 DAG.getConstant(NumBits+NumBits-1, OpVT),
8291 DAG.getConstant(X86::COND_E, MVT::i8),
8292 Op.getValue(1)
8293 };
8294 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00008295
8296 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +00008297 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +00008298
Owen Anderson825b72b2009-08-11 20:47:22 +00008299 if (VT == MVT::i8)
8300 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008301 return Op;
8302}
8303
Dan Gohmand858e902010-04-17 15:26:15 +00008304SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008305 EVT VT = Op.getValueType();
8306 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008307 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008308 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00008309
8310 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008311 if (VT == MVT::i8) {
8312 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00008313 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008314 }
Evan Cheng152804e2007-12-14 08:30:15 +00008315
8316 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008317 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008318 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00008319
8320 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00008321 SDValue Ops[] = {
8322 Op,
8323 DAG.getConstant(NumBits, OpVT),
8324 DAG.getConstant(X86::COND_E, MVT::i8),
8325 Op.getValue(1)
8326 };
8327 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00008328
Owen Anderson825b72b2009-08-11 20:47:22 +00008329 if (VT == MVT::i8)
8330 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008331 return Op;
8332}
8333
Dan Gohmand858e902010-04-17 15:26:15 +00008334SDValue X86TargetLowering::LowerMUL_V2I64(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008335 EVT VT = Op.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00008336 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008337 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00008338
Mon P Wangaf9b9522008-12-18 21:42:19 +00008339 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
8340 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
8341 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
8342 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
8343 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
8344 //
8345 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
8346 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
8347 // return AloBlo + AloBhi + AhiBlo;
8348
8349 SDValue A = Op.getOperand(0);
8350 SDValue B = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008351
Dale Johannesene4d209d2009-02-03 20:21:25 +00008352 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008353 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
8354 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008355 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008356 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
8357 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008358 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008359 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008360 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008361 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008362 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008363 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008364 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008365 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008366 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008367 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008368 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
8369 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008370 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008371 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
8372 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008373 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
8374 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +00008375 return Res;
8376}
8377
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008378SDValue X86TargetLowering::LowerSHL(SDValue Op, SelectionDAG &DAG) const {
8379 EVT VT = Op.getValueType();
8380 DebugLoc dl = Op.getDebugLoc();
8381 SDValue R = Op.getOperand(0);
8382
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008383 LLVMContext *Context = DAG.getContext();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008384
Nate Begeman51409212010-07-28 00:21:48 +00008385 assert(Subtarget->hasSSE41() && "Cannot lower SHL without SSE4.1 or later");
8386
8387 if (VT == MVT::v4i32) {
8388 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8389 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
8390 Op.getOperand(1), DAG.getConstant(23, MVT::i32));
8391
8392 ConstantInt *CI = ConstantInt::get(*Context, APInt(32, 0x3f800000U));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008393
Nate Begeman51409212010-07-28 00:21:48 +00008394 std::vector<Constant*> CV(4, CI);
8395 Constant *C = ConstantVector::get(CV);
8396 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8397 SDValue Addend = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008398 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +00008399 false, false, 16);
8400
8401 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Addend);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008402 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op);
Nate Begeman51409212010-07-28 00:21:48 +00008403 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
8404 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
8405 }
8406 if (VT == MVT::v16i8) {
8407 // a = a << 5;
8408 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8409 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
8410 Op.getOperand(1), DAG.getConstant(5, MVT::i32));
8411
8412 ConstantInt *CM1 = ConstantInt::get(*Context, APInt(8, 15));
8413 ConstantInt *CM2 = ConstantInt::get(*Context, APInt(8, 63));
8414
8415 std::vector<Constant*> CVM1(16, CM1);
8416 std::vector<Constant*> CVM2(16, CM2);
8417 Constant *C = ConstantVector::get(CVM1);
8418 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8419 SDValue M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008420 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +00008421 false, false, 16);
8422
8423 // r = pblendv(r, psllw(r & (char16)15, 4), a);
8424 M = DAG.getNode(ISD::AND, dl, VT, R, M);
8425 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8426 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
8427 DAG.getConstant(4, MVT::i32));
Nate Begeman672fb622010-12-20 22:04:24 +00008428 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT, R, M, Op);
Nate Begeman51409212010-07-28 00:21:48 +00008429 // a += a
8430 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008431
Nate Begeman51409212010-07-28 00:21:48 +00008432 C = ConstantVector::get(CVM2);
8433 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8434 M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008435 MachinePointerInfo::getConstantPool(),
Chris Lattner51abfe42010-09-21 06:02:19 +00008436 false, false, 16);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008437
Nate Begeman51409212010-07-28 00:21:48 +00008438 // r = pblendv(r, psllw(r & (char16)63, 2), a);
8439 M = DAG.getNode(ISD::AND, dl, VT, R, M);
8440 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8441 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
8442 DAG.getConstant(2, MVT::i32));
Nate Begeman672fb622010-12-20 22:04:24 +00008443 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT, R, M, Op);
Nate Begeman51409212010-07-28 00:21:48 +00008444 // a += a
8445 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008446
Nate Begeman51409212010-07-28 00:21:48 +00008447 // return pblendv(r, r+r, a);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008448 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT,
Nate Begeman51409212010-07-28 00:21:48 +00008449 R, DAG.getNode(ISD::ADD, dl, VT, R, R), Op);
8450 return R;
8451 }
8452 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008453}
Mon P Wangaf9b9522008-12-18 21:42:19 +00008454
Dan Gohmand858e902010-04-17 15:26:15 +00008455SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
Bill Wendling74c37652008-12-09 22:08:41 +00008456 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
8457 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +00008458 // looks for this combo and may remove the "setcc" instruction if the "setcc"
8459 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +00008460 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +00008461 SDValue LHS = N->getOperand(0);
8462 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +00008463 unsigned BaseOp = 0;
8464 unsigned Cond = 0;
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008465 DebugLoc DL = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +00008466 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008467 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +00008468 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +00008469 // A subtract of one will be selected as a INC. Note that INC doesn't
8470 // set CF, so we can't do this for UADDO.
8471 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
8472 if (C->getAPIntValue() == 1) {
8473 BaseOp = X86ISD::INC;
8474 Cond = X86::COND_O;
8475 break;
8476 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008477 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +00008478 Cond = X86::COND_O;
8479 break;
8480 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008481 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +00008482 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00008483 break;
8484 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +00008485 // A subtract of one will be selected as a DEC. Note that DEC doesn't
8486 // set CF, so we can't do this for USUBO.
8487 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
8488 if (C->getAPIntValue() == 1) {
8489 BaseOp = X86ISD::DEC;
8490 Cond = X86::COND_O;
8491 break;
8492 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008493 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +00008494 Cond = X86::COND_O;
8495 break;
8496 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008497 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +00008498 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00008499 break;
8500 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00008501 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +00008502 Cond = X86::COND_O;
8503 break;
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008504 case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs
8505 SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0),
8506 MVT::i32);
8507 SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008508
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008509 SDValue SetCC =
8510 DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
8511 DAG.getConstant(X86::COND_O, MVT::i32),
8512 SDValue(Sum.getNode(), 2));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008513
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008514 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
8515 return Sum;
8516 }
Bill Wendling74c37652008-12-09 22:08:41 +00008517 }
Bill Wendling3fafd932008-11-26 22:37:40 +00008518
Bill Wendling61edeb52008-12-02 01:06:39 +00008519 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008520 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008521 SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +00008522
Bill Wendling61edeb52008-12-02 01:06:39 +00008523 SDValue SetCC =
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008524 DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1),
8525 DAG.getConstant(Cond, MVT::i32),
8526 SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +00008527
Bill Wendling61edeb52008-12-02 01:06:39 +00008528 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
8529 return Sum;
Bill Wendling41ea7e72008-11-24 19:21:46 +00008530}
8531
Eric Christopher9a9d2752010-07-22 02:48:34 +00008532SDValue X86TargetLowering::LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG) const{
8533 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00008534
Eric Christopherb6729dc2010-08-04 23:03:04 +00008535 if (!Subtarget->hasSSE2()) {
Eric Christopherc0b2a202010-08-14 21:51:50 +00008536 SDValue Chain = Op.getOperand(0);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008537 SDValue Zero = DAG.getConstant(0,
Eric Christopherb6729dc2010-08-04 23:03:04 +00008538 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Eric Christopherc0b2a202010-08-14 21:51:50 +00008539 SDValue Ops[] = {
8540 DAG.getRegister(X86::ESP, MVT::i32), // Base
8541 DAG.getTargetConstant(1, MVT::i8), // Scale
8542 DAG.getRegister(0, MVT::i32), // Index
8543 DAG.getTargetConstant(0, MVT::i32), // Disp
8544 DAG.getRegister(0, MVT::i32), // Segment.
8545 Zero,
8546 Chain
8547 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00008548 SDNode *Res =
Eric Christopherc0b2a202010-08-14 21:51:50 +00008549 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
8550 array_lengthof(Ops));
8551 return SDValue(Res, 0);
Eric Christopherb6729dc2010-08-04 23:03:04 +00008552 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00008553
Eric Christopher9a9d2752010-07-22 02:48:34 +00008554 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
Chris Lattner132929a2010-08-14 17:26:09 +00008555 if (!isDev)
Eric Christopher9a9d2752010-07-22 02:48:34 +00008556 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008557
Chris Lattner132929a2010-08-14 17:26:09 +00008558 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
8559 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
8560 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
8561 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
Michael J. Spencerec38de22010-10-10 22:04:20 +00008562
Chris Lattner132929a2010-08-14 17:26:09 +00008563 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
8564 if (!Op1 && !Op2 && !Op3 && Op4)
8565 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008566
Chris Lattner132929a2010-08-14 17:26:09 +00008567 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
8568 if (Op1 && !Op2 && !Op3 && !Op4)
8569 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008570
8571 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
Chris Lattner132929a2010-08-14 17:26:09 +00008572 // (MFENCE)>;
8573 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
Eric Christopher9a9d2752010-07-22 02:48:34 +00008574}
8575
Dan Gohmand858e902010-04-17 15:26:15 +00008576SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008577 EVT T = Op.getValueType();
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008578 DebugLoc DL = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +00008579 unsigned Reg = 0;
8580 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00008581 switch(T.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00008582 default:
8583 assert(false && "Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +00008584 case MVT::i8: Reg = X86::AL; size = 1; break;
8585 case MVT::i16: Reg = X86::AX; size = 2; break;
8586 case MVT::i32: Reg = X86::EAX; size = 4; break;
8587 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +00008588 assert(Subtarget->is64Bit() && "Node not type legal!");
8589 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +00008590 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00008591 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008592 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +00008593 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +00008594 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +00008595 Op.getOperand(1),
8596 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +00008597 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +00008598 cpIn.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00008599 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008600 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
8601 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
8602 Ops, 5, T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +00008603 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008604 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +00008605 return cpOut;
8606}
8607
Duncan Sands1607f052008-12-01 11:39:25 +00008608SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008609 SelectionDAG &DAG) const {
Duncan Sands1607f052008-12-01 11:39:25 +00008610 assert(Subtarget->is64Bit() && "Result not type legalized?");
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00008611 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +00008612 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008613 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +00008614 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00008615 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
8616 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +00008617 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +00008618 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
8619 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +00008620 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +00008621 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +00008622 rdx.getValue(1)
8623 };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008624 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008625}
8626
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008627SDValue X86TargetLowering::LowerBITCAST(SDValue Op,
Dale Johannesen7d07b482010-05-21 00:52:33 +00008628 SelectionDAG &DAG) const {
8629 EVT SrcVT = Op.getOperand(0).getValueType();
8630 EVT DstVT = Op.getValueType();
Chris Lattner2a786eb2010-12-19 20:19:20 +00008631 assert(Subtarget->is64Bit() && !Subtarget->hasSSE2() &&
8632 Subtarget->hasMMX() && "Unexpected custom BITCAST");
Michael J. Spencerec38de22010-10-10 22:04:20 +00008633 assert((DstVT == MVT::i64 ||
Dale Johannesen7d07b482010-05-21 00:52:33 +00008634 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008635 "Unexpected custom BITCAST");
Dale Johannesen7d07b482010-05-21 00:52:33 +00008636 // i64 <=> MMX conversions are Legal.
8637 if (SrcVT==MVT::i64 && DstVT.isVector())
8638 return Op;
8639 if (DstVT==MVT::i64 && SrcVT.isVector())
8640 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +00008641 // MMX <=> MMX conversions are Legal.
8642 if (SrcVT.isVector() && DstVT.isVector())
8643 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +00008644 // All other conversions need to be expanded.
8645 return SDValue();
8646}
Chris Lattner5b856542010-12-20 00:59:46 +00008647
Dan Gohmand858e902010-04-17 15:26:15 +00008648SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen71d1bf52008-09-29 22:25:26 +00008649 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +00008650 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008651 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008652 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +00008653 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008654 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008655 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +00008656 Node->getOperand(0),
8657 Node->getOperand(1), negOp,
8658 cast<AtomicSDNode>(Node)->getSrcValue(),
8659 cast<AtomicSDNode>(Node)->getAlignment());
Mon P Wang63307c32008-05-05 19:05:59 +00008660}
8661
Chris Lattner5b856542010-12-20 00:59:46 +00008662static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
8663 EVT VT = Op.getNode()->getValueType(0);
8664
8665 // Let legalize expand this if it isn't a legal type yet.
8666 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
8667 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008668
Chris Lattner5b856542010-12-20 00:59:46 +00008669 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008670
Chris Lattner5b856542010-12-20 00:59:46 +00008671 unsigned Opc;
8672 bool ExtraOp = false;
8673 switch (Op.getOpcode()) {
8674 default: assert(0 && "Invalid code");
8675 case ISD::ADDC: Opc = X86ISD::ADD; break;
8676 case ISD::ADDE: Opc = X86ISD::ADC; ExtraOp = true; break;
8677 case ISD::SUBC: Opc = X86ISD::SUB; break;
8678 case ISD::SUBE: Opc = X86ISD::SBB; ExtraOp = true; break;
8679 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008680
Chris Lattner5b856542010-12-20 00:59:46 +00008681 if (!ExtraOp)
8682 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
8683 Op.getOperand(1));
8684 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
8685 Op.getOperand(1), Op.getOperand(2));
8686}
8687
Evan Cheng0db9fe62006-04-25 20:13:52 +00008688/// LowerOperation - Provide custom lowering hooks for some operations.
8689///
Dan Gohmand858e902010-04-17 15:26:15 +00008690SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00008691 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008692 default: llvm_unreachable("Should not custom lower this!");
Eric Christopher9a9d2752010-07-22 02:48:34 +00008693 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op,DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008694 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
8695 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008696 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00008697 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008698 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
8699 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
8700 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
David Greene91585092011-01-26 15:38:49 +00008701 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op, DAG);
David Greenecfe33c42011-01-26 19:13:22 +00008702 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008703 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
8704 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
8705 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00008706 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +00008707 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +00008708 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008709 case ISD::SHL_PARTS:
8710 case ISD::SRA_PARTS:
8711 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
8712 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00008713 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008714 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00008715 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008716 case ISD::FABS: return LowerFABS(Op, DAG);
8717 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008718 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00008719 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman30a0de92008-07-17 16:51:19 +00008720 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00008721 case ISD::SELECT: return LowerSELECT(Op, DAG);
8722 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008723 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008724 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +00008725 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +00008726 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008727 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00008728 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
8729 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008730 case ISD::FRAME_TO_ARGS_OFFSET:
8731 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008732 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008733 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008734 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +00008735 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +00008736 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
8737 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Mon P Wangaf9b9522008-12-18 21:42:19 +00008738 case ISD::MUL: return LowerMUL_V2I64(Op, DAG);
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008739 case ISD::SHL: return LowerSHL(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +00008740 case ISD::SADDO:
8741 case ISD::UADDO:
8742 case ISD::SSUBO:
8743 case ISD::USUBO:
8744 case ISD::SMULO:
8745 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +00008746 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008747 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Chris Lattner5b856542010-12-20 00:59:46 +00008748 case ISD::ADDC:
8749 case ISD::ADDE:
8750 case ISD::SUBC:
8751 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008752 }
Chris Lattner27a6c732007-11-24 07:07:01 +00008753}
8754
Duncan Sands1607f052008-12-01 11:39:25 +00008755void X86TargetLowering::
8756ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00008757 SelectionDAG &DAG, unsigned NewOp) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008758 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008759 DebugLoc dl = Node->getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00008760 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +00008761
8762 SDValue Chain = Node->getOperand(0);
8763 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00008764 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00008765 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00008766 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00008767 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +00008768 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +00008769 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +00008770 SDValue Result =
8771 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
8772 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +00008773 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +00008774 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00008775 Results.push_back(Result.getValue(2));
8776}
8777
Duncan Sands126d9072008-07-04 11:47:58 +00008778/// ReplaceNodeResults - Replace a node with an illegal result type
8779/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00008780void X86TargetLowering::ReplaceNodeResults(SDNode *N,
8781 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00008782 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +00008783 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +00008784 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +00008785 default:
Duncan Sands1607f052008-12-01 11:39:25 +00008786 assert(false && "Do not know how to custom type legalize this operation!");
8787 return;
Chris Lattner5b856542010-12-20 00:59:46 +00008788 case ISD::ADDC:
8789 case ISD::ADDE:
8790 case ISD::SUBC:
8791 case ISD::SUBE:
8792 // We don't want to expand or promote these.
8793 return;
Duncan Sands1607f052008-12-01 11:39:25 +00008794 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +00008795 std::pair<SDValue,SDValue> Vals =
8796 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +00008797 SDValue FIST = Vals.first, StackSlot = Vals.second;
8798 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +00008799 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +00008800 // Return a load from the stack slot.
Chris Lattner51abfe42010-09-21 06:02:19 +00008801 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
8802 MachinePointerInfo(), false, false, 0));
Duncan Sands1607f052008-12-01 11:39:25 +00008803 }
8804 return;
8805 }
8806 case ISD::READCYCLECOUNTER: {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00008807 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +00008808 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008809 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00008810 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +00008811 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00008812 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00008813 eax.getValue(2));
8814 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
8815 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +00008816 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00008817 Results.push_back(edx.getValue(1));
8818 return;
8819 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008820 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +00008821 EVT T = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008822 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
Duncan Sands1607f052008-12-01 11:39:25 +00008823 SDValue cpInL, cpInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00008824 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
8825 DAG.getConstant(0, MVT::i32));
8826 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
8827 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00008828 cpInL = DAG.getCopyToReg(N->getOperand(0), dl, X86::EAX, cpInL, SDValue());
8829 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl, X86::EDX, cpInH,
Duncan Sands1607f052008-12-01 11:39:25 +00008830 cpInL.getValue(1));
8831 SDValue swapInL, swapInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00008832 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
8833 DAG.getConstant(0, MVT::i32));
8834 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
8835 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00008836 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl, X86::EBX, swapInL,
Duncan Sands1607f052008-12-01 11:39:25 +00008837 cpInH.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00008838 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl, X86::ECX, swapInH,
Duncan Sands1607f052008-12-01 11:39:25 +00008839 swapInL.getValue(1));
8840 SDValue Ops[] = { swapInH.getValue(0),
8841 N->getOperand(1),
8842 swapInH.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00008843 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Andrew Trick1a2cf3b2010-10-11 19:02:04 +00008844 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
8845 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG8_DAG, dl, Tys,
8846 Ops, 3, T, MMO);
Dale Johannesendd64c412009-02-04 00:33:20 +00008847 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl, X86::EAX,
Owen Anderson825b72b2009-08-11 20:47:22 +00008848 MVT::i32, Result.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00008849 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl, X86::EDX,
Owen Anderson825b72b2009-08-11 20:47:22 +00008850 MVT::i32, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +00008851 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Owen Anderson825b72b2009-08-11 20:47:22 +00008852 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00008853 Results.push_back(cpOutH.getValue(1));
8854 return;
8855 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008856 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +00008857 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
8858 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008859 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +00008860 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
8861 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008862 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +00008863 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
8864 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008865 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +00008866 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
8867 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008868 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +00008869 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
8870 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008871 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +00008872 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
8873 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008874 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +00008875 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
8876 return;
Chris Lattner27a6c732007-11-24 07:07:01 +00008877 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00008878}
8879
Evan Cheng72261582005-12-20 06:22:03 +00008880const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
8881 switch (Opcode) {
8882 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +00008883 case X86ISD::BSF: return "X86ISD::BSF";
8884 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +00008885 case X86ISD::SHLD: return "X86ISD::SHLD";
8886 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00008887 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +00008888 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +00008889 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +00008890 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +00008891 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00008892 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00008893 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
8894 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
8895 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00008896 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00008897 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +00008898 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +00008899 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +00008900 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +00008901 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +00008902 case X86ISD::COMI: return "X86ISD::COMI";
8903 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +00008904 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +00008905 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Evan Cheng72261582005-12-20 06:22:03 +00008906 case X86ISD::CMOV: return "X86ISD::CMOV";
8907 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00008908 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00008909 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
8910 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +00008911 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00008912 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +00008913 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +00008914 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +00008915 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +00008916 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
8917 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +00008918 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +00008919 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Nate Begemanb65c1752010-12-17 22:55:37 +00008920 case X86ISD::PANDN: return "X86ISD::PANDN";
8921 case X86ISD::PSIGNB: return "X86ISD::PSIGNB";
8922 case X86ISD::PSIGNW: return "X86ISD::PSIGNW";
8923 case X86ISD::PSIGND: return "X86ISD::PSIGND";
Nate Begeman672fb622010-12-20 22:04:24 +00008924 case X86ISD::PBLENDVB: return "X86ISD::PBLENDVB";
Evan Cheng8ca29322006-11-10 21:43:37 +00008925 case X86ISD::FMAX: return "X86ISD::FMAX";
8926 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +00008927 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
8928 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00008929 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +00008930 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008931 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00008932 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008933 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +00008934 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
8935 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008936 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
8937 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
8938 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
8939 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
8940 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
8941 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +00008942 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
8943 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +00008944 case X86ISD::VSHL: return "X86ISD::VSHL";
8945 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +00008946 case X86ISD::CMPPD: return "X86ISD::CMPPD";
8947 case X86ISD::CMPPS: return "X86ISD::CMPPS";
8948 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
8949 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
8950 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
8951 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
8952 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
8953 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
8954 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
8955 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008956 case X86ISD::ADD: return "X86ISD::ADD";
8957 case X86ISD::SUB: return "X86ISD::SUB";
Chris Lattner5b856542010-12-20 00:59:46 +00008958 case X86ISD::ADC: return "X86ISD::ADC";
8959 case X86ISD::SBB: return "X86ISD::SBB";
Bill Wendlingd350e022008-12-12 21:15:41 +00008960 case X86ISD::SMUL: return "X86ISD::SMUL";
8961 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +00008962 case X86ISD::INC: return "X86ISD::INC";
8963 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +00008964 case X86ISD::OR: return "X86ISD::OR";
8965 case X86ISD::XOR: return "X86ISD::XOR";
8966 case X86ISD::AND: return "X86ISD::AND";
Evan Cheng73f24c92009-03-30 21:36:47 +00008967 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +00008968 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008969 case X86ISD::TESTP: return "X86ISD::TESTP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00008970 case X86ISD::PALIGN: return "X86ISD::PALIGN";
8971 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
8972 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
8973 case X86ISD::PSHUFHW_LD: return "X86ISD::PSHUFHW_LD";
8974 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
8975 case X86ISD::PSHUFLW_LD: return "X86ISD::PSHUFLW_LD";
8976 case X86ISD::SHUFPS: return "X86ISD::SHUFPS";
8977 case X86ISD::SHUFPD: return "X86ISD::SHUFPD";
8978 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00008979 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00008980 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00008981 case X86ISD::MOVHLPD: return "X86ISD::MOVHLPD";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00008982 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
8983 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00008984 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
8985 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
8986 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
8987 case X86ISD::MOVSHDUP_LD: return "X86ISD::MOVSHDUP_LD";
8988 case X86ISD::MOVSLDUP_LD: return "X86ISD::MOVSLDUP_LD";
8989 case X86ISD::MOVSD: return "X86ISD::MOVSD";
8990 case X86ISD::MOVSS: return "X86ISD::MOVSS";
8991 case X86ISD::UNPCKLPS: return "X86ISD::UNPCKLPS";
8992 case X86ISD::UNPCKLPD: return "X86ISD::UNPCKLPD";
8993 case X86ISD::UNPCKHPS: return "X86ISD::UNPCKHPS";
8994 case X86ISD::UNPCKHPD: return "X86ISD::UNPCKHPD";
8995 case X86ISD::PUNPCKLBW: return "X86ISD::PUNPCKLBW";
8996 case X86ISD::PUNPCKLWD: return "X86ISD::PUNPCKLWD";
8997 case X86ISD::PUNPCKLDQ: return "X86ISD::PUNPCKLDQ";
8998 case X86ISD::PUNPCKLQDQ: return "X86ISD::PUNPCKLQDQ";
8999 case X86ISD::PUNPCKHBW: return "X86ISD::PUNPCKHBW";
9000 case X86ISD::PUNPCKHWD: return "X86ISD::PUNPCKHWD";
9001 case X86ISD::PUNPCKHDQ: return "X86ISD::PUNPCKHDQ";
9002 case X86ISD::PUNPCKHQDQ: return "X86ISD::PUNPCKHQDQ";
Dan Gohmand6708ea2009-08-15 01:38:56 +00009003 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Dan Gohman320afb82010-10-12 18:00:49 +00009004 case X86ISD::VAARG_64: return "X86ISD::VAARG_64";
Michael J. Spencere9c253e2010-10-21 01:41:01 +00009005 case X86ISD::WIN_ALLOCA: return "X86ISD::WIN_ALLOCA";
Evan Cheng72261582005-12-20 06:22:03 +00009006 }
9007}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00009008
Chris Lattnerc9addb72007-03-30 23:15:24 +00009009// isLegalAddressingMode - Return true if the addressing mode represented
9010// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +00009011bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerc9addb72007-03-30 23:15:24 +00009012 const Type *Ty) const {
9013 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009014 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +00009015 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +00009016
Chris Lattnerc9addb72007-03-30 23:15:24 +00009017 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009018 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +00009019 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00009020
Chris Lattnerc9addb72007-03-30 23:15:24 +00009021 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +00009022 unsigned GVFlags =
9023 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009024
Chris Lattnerdfed4132009-07-10 07:38:24 +00009025 // If a reference to this global requires an extra load, we can't fold it.
9026 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +00009027 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009028
Chris Lattnerdfed4132009-07-10 07:38:24 +00009029 // If BaseGV requires a register for the PIC base, we cannot also have a
9030 // BaseReg specified.
9031 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +00009032 return false;
Evan Cheng52787842007-08-01 23:46:47 +00009033
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009034 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +00009035 if ((M != CodeModel::Small || R != Reloc::Static) &&
9036 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009037 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +00009038 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009039
Chris Lattnerc9addb72007-03-30 23:15:24 +00009040 switch (AM.Scale) {
9041 case 0:
9042 case 1:
9043 case 2:
9044 case 4:
9045 case 8:
9046 // These scales always work.
9047 break;
9048 case 3:
9049 case 5:
9050 case 9:
9051 // These scales are formed with basereg+scalereg. Only accept if there is
9052 // no basereg yet.
9053 if (AM.HasBaseReg)
9054 return false;
9055 break;
9056 default: // Other stuff never works.
9057 return false;
9058 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009059
Chris Lattnerc9addb72007-03-30 23:15:24 +00009060 return true;
9061}
9062
9063
Evan Cheng2bd122c2007-10-26 01:56:11 +00009064bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00009065 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +00009066 return false;
Evan Chenge127a732007-10-29 07:57:50 +00009067 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
9068 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00009069 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +00009070 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00009071 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +00009072}
9073
Owen Andersone50ed302009-08-10 22:56:29 +00009074bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +00009075 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009076 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009077 unsigned NumBits1 = VT1.getSizeInBits();
9078 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00009079 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009080 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00009081 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009082}
Evan Cheng2bd122c2007-10-26 01:56:11 +00009083
Dan Gohman97121ba2009-04-08 00:15:30 +00009084bool X86TargetLowering::isZExtFree(const Type *Ty1, const Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00009085 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00009086 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00009087}
9088
Owen Andersone50ed302009-08-10 22:56:29 +00009089bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00009090 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00009091 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00009092}
9093
Owen Andersone50ed302009-08-10 22:56:29 +00009094bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +00009095 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +00009096 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +00009097}
9098
Evan Cheng60c07e12006-07-05 22:17:51 +00009099/// isShuffleMaskLegal - Targets can use this to indicate that they only
9100/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
9101/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
9102/// are assumed to be legal.
9103bool
Eric Christopherfd179292009-08-27 18:07:15 +00009104X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +00009105 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +00009106 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +00009107 if (VT.getSizeInBits() == 64)
Eric Christophercff6f852010-04-15 01:40:20 +00009108 return isPALIGNRMask(M, VT, Subtarget->hasSSSE3());
Nate Begeman9008ca62009-04-27 18:41:29 +00009109
Nate Begemana09008b2009-10-19 02:17:23 +00009110 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +00009111 return (VT.getVectorNumElements() == 2 ||
9112 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
9113 isMOVLMask(M, VT) ||
9114 isSHUFPMask(M, VT) ||
9115 isPSHUFDMask(M, VT) ||
9116 isPSHUFHWMask(M, VT) ||
9117 isPSHUFLWMask(M, VT) ||
Nate Begemana09008b2009-10-19 02:17:23 +00009118 isPALIGNRMask(M, VT, Subtarget->hasSSSE3()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +00009119 isUNPCKLMask(M, VT) ||
9120 isUNPCKHMask(M, VT) ||
9121 isUNPCKL_v_undef_Mask(M, VT) ||
9122 isUNPCKH_v_undef_Mask(M, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00009123}
9124
Dan Gohman7d8143f2008-04-09 20:09:42 +00009125bool
Nate Begeman5a5ca152009-04-29 05:20:52 +00009126X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +00009127 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00009128 unsigned NumElts = VT.getVectorNumElements();
9129 // FIXME: This collection of masks seems suspect.
9130 if (NumElts == 2)
9131 return true;
9132 if (NumElts == 4 && VT.getSizeInBits() == 128) {
9133 return (isMOVLMask(Mask, VT) ||
9134 isCommutedMOVLMask(Mask, VT, true) ||
9135 isSHUFPMask(Mask, VT) ||
9136 isCommutedSHUFPMask(Mask, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00009137 }
9138 return false;
9139}
9140
9141//===----------------------------------------------------------------------===//
9142// X86 Scheduler Hooks
9143//===----------------------------------------------------------------------===//
9144
Mon P Wang63307c32008-05-05 19:05:59 +00009145// private utility function
9146MachineBasicBlock *
9147X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
9148 MachineBasicBlock *MBB,
9149 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009150 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009151 unsigned LoadOpc,
9152 unsigned CXchgOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009153 unsigned notOpc,
9154 unsigned EAXreg,
9155 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009156 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00009157 // For the atomic bitwise operator, we generate
9158 // thisMBB:
9159 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00009160 // ld t1 = [bitinstr.addr]
9161 // op t2 = t1, [bitinstr.val]
9162 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00009163 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
9164 // bz newMBB
9165 // fallthrough -->nextMBB
9166 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9167 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009168 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00009169 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009170
Mon P Wang63307c32008-05-05 19:05:59 +00009171 /// First build the CFG
9172 MachineFunction *F = MBB->getParent();
9173 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009174 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9175 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9176 F->insert(MBBIter, newMBB);
9177 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009178
Dan Gohman14152b42010-07-06 20:24:04 +00009179 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9180 nextMBB->splice(nextMBB->begin(), thisMBB,
9181 llvm::next(MachineBasicBlock::iterator(bInstr)),
9182 thisMBB->end());
9183 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009184
Mon P Wang63307c32008-05-05 19:05:59 +00009185 // Update thisMBB to fall through to newMBB
9186 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009187
Mon P Wang63307c32008-05-05 19:05:59 +00009188 // newMBB jumps to itself and fall through to nextMBB
9189 newMBB->addSuccessor(nextMBB);
9190 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009191
Mon P Wang63307c32008-05-05 19:05:59 +00009192 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009193 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009194 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +00009195 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00009196 MachineOperand& destOper = bInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009197 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00009198 int numArgs = bInstr->getNumOperands() - 1;
9199 for (int i=0; i < numArgs; ++i)
9200 argOpers[i] = &bInstr->getOperand(i+1);
9201
9202 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009203 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009204 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00009205
Dale Johannesen140be2d2008-08-19 18:47:28 +00009206 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009207 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00009208 for (int i=0; i <= lastAddrIndx; ++i)
9209 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009210
Dale Johannesen140be2d2008-08-19 18:47:28 +00009211 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009212 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00009213 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009214 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009215 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009216 tt = t1;
9217
Dale Johannesen140be2d2008-08-19 18:47:28 +00009218 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +00009219 assert((argOpers[valArgIndx]->isReg() ||
9220 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00009221 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +00009222 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009223 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00009224 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009225 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009226 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +00009227 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009228
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009229 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +00009230 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009231
Dale Johannesene4d209d2009-02-03 20:21:25 +00009232 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +00009233 for (int i=0; i <= lastAddrIndx; ++i)
9234 (*MIB).addOperand(*argOpers[i]);
9235 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +00009236 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00009237 (*MIB).setMemRefs(bInstr->memoperands_begin(),
9238 bInstr->memoperands_end());
Mon P Wangf5952662008-07-17 04:54:06 +00009239
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009240 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +00009241 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +00009242
Mon P Wang63307c32008-05-05 19:05:59 +00009243 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009244 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00009245
Dan Gohman14152b42010-07-06 20:24:04 +00009246 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00009247 return nextMBB;
9248}
9249
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00009250// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +00009251MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009252X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
9253 MachineBasicBlock *MBB,
9254 unsigned regOpcL,
9255 unsigned regOpcH,
9256 unsigned immOpcL,
9257 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009258 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009259 // For the atomic bitwise operator, we generate
9260 // thisMBB (instructions are in pairs, except cmpxchg8b)
9261 // ld t1,t2 = [bitinstr.addr]
9262 // newMBB:
9263 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
9264 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +00009265 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009266 // mov ECX, EBX <- t5, t6
9267 // mov EAX, EDX <- t1, t2
9268 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
9269 // mov t3, t4 <- EAX, EDX
9270 // bz newMBB
9271 // result in out1, out2
9272 // fallthrough -->nextMBB
9273
9274 const TargetRegisterClass *RC = X86::GR32RegisterClass;
9275 const unsigned LoadOpc = X86::MOV32rm;
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009276 const unsigned NotOpc = X86::NOT32r;
9277 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9278 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
9279 MachineFunction::iterator MBBIter = MBB;
9280 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009281
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009282 /// First build the CFG
9283 MachineFunction *F = MBB->getParent();
9284 MachineBasicBlock *thisMBB = MBB;
9285 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9286 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9287 F->insert(MBBIter, newMBB);
9288 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009289
Dan Gohman14152b42010-07-06 20:24:04 +00009290 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9291 nextMBB->splice(nextMBB->begin(), thisMBB,
9292 llvm::next(MachineBasicBlock::iterator(bInstr)),
9293 thisMBB->end());
9294 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009295
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009296 // Update thisMBB to fall through to newMBB
9297 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009298
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009299 // newMBB jumps to itself and fall through to nextMBB
9300 newMBB->addSuccessor(nextMBB);
9301 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009302
Dale Johannesene4d209d2009-02-03 20:21:25 +00009303 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009304 // Insert instructions into newMBB based on incoming instruction
9305 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009306 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009307 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009308 MachineOperand& dest1Oper = bInstr->getOperand(0);
9309 MachineOperand& dest2Oper = bInstr->getOperand(1);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009310 MachineOperand* argOpers[2 + X86::AddrNumOperands];
9311 for (int i=0; i < 2 + X86::AddrNumOperands; ++i) {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009312 argOpers[i] = &bInstr->getOperand(i+2);
9313
Dan Gohman71ea4e52010-05-14 21:01:44 +00009314 // We use some of the operands multiple times, so conservatively just
9315 // clear any kill flags that might be present.
9316 if (argOpers[i]->isReg() && argOpers[i]->isUse())
9317 argOpers[i]->setIsKill(false);
9318 }
9319
Evan Chengad5b52f2010-01-08 19:14:57 +00009320 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009321 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +00009322
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009323 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009324 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009325 for (int i=0; i <= lastAddrIndx; ++i)
9326 (*MIB).addOperand(*argOpers[i]);
9327 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009328 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +00009329 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +00009330 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009331 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +00009332 MachineOperand newOp3 = *(argOpers[3]);
9333 if (newOp3.isImm())
9334 newOp3.setImm(newOp3.getImm()+4);
9335 else
9336 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009337 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +00009338 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009339
9340 // t3/4 are defined later, at the bottom of the loop
9341 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
9342 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009343 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009344 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009345 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009346 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
9347
Evan Cheng306b4ca2010-01-08 23:41:50 +00009348 // The subsequent operations should be using the destination registers of
9349 //the PHI instructions.
Scott Michelfdc40a02009-02-17 22:15:04 +00009350 if (invSrc) {
Evan Cheng306b4ca2010-01-08 23:41:50 +00009351 t1 = F->getRegInfo().createVirtualRegister(RC);
9352 t2 = F->getRegInfo().createVirtualRegister(RC);
9353 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t1).addReg(dest1Oper.getReg());
9354 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t2).addReg(dest2Oper.getReg());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009355 } else {
Evan Cheng306b4ca2010-01-08 23:41:50 +00009356 t1 = dest1Oper.getReg();
9357 t2 = dest2Oper.getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009358 }
9359
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009360 int valArgIndx = lastAddrIndx + 1;
9361 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +00009362 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009363 "invalid operand");
9364 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
9365 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009366 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009367 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009368 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009369 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +00009370 if (regOpcL != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00009371 MIB.addReg(t1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009372 (*MIB).addOperand(*argOpers[valArgIndx]);
9373 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00009374 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009375 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00009376 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009377 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009378 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009379 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009380 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +00009381 if (regOpcH != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00009382 MIB.addReg(t2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009383 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009384
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009385 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009386 MIB.addReg(t1);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009387 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009388 MIB.addReg(t2);
9389
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009390 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009391 MIB.addReg(t5);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009392 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009393 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +00009394
Dale Johannesene4d209d2009-02-03 20:21:25 +00009395 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009396 for (int i=0; i <= lastAddrIndx; ++i)
9397 (*MIB).addOperand(*argOpers[i]);
9398
9399 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00009400 (*MIB).setMemRefs(bInstr->memoperands_begin(),
9401 bInstr->memoperands_end());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009402
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009403 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009404 MIB.addReg(X86::EAX);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009405 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009406 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +00009407
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009408 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009409 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009410
Dan Gohman14152b42010-07-06 20:24:04 +00009411 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009412 return nextMBB;
9413}
9414
9415// private utility function
9416MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +00009417X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
9418 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009419 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00009420 // For the atomic min/max operator, we generate
9421 // thisMBB:
9422 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00009423 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +00009424 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +00009425 // cmp t1, t2
9426 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +00009427 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00009428 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
9429 // bz newMBB
9430 // fallthrough -->nextMBB
9431 //
9432 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9433 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009434 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00009435 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009436
Mon P Wang63307c32008-05-05 19:05:59 +00009437 /// First build the CFG
9438 MachineFunction *F = MBB->getParent();
9439 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009440 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9441 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9442 F->insert(MBBIter, newMBB);
9443 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009444
Dan Gohman14152b42010-07-06 20:24:04 +00009445 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9446 nextMBB->splice(nextMBB->begin(), thisMBB,
9447 llvm::next(MachineBasicBlock::iterator(mInstr)),
9448 thisMBB->end());
9449 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009450
Mon P Wang63307c32008-05-05 19:05:59 +00009451 // Update thisMBB to fall through to newMBB
9452 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009453
Mon P Wang63307c32008-05-05 19:05:59 +00009454 // newMBB jumps to newMBB and fall through to nextMBB
9455 newMBB->addSuccessor(nextMBB);
9456 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009457
Dale Johannesene4d209d2009-02-03 20:21:25 +00009458 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00009459 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009460 assert(mInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009461 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +00009462 MachineOperand& destOper = mInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009463 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00009464 int numArgs = mInstr->getNumOperands() - 1;
9465 for (int i=0; i < numArgs; ++i)
9466 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009467
Mon P Wang63307c32008-05-05 19:05:59 +00009468 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009469 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009470 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00009471
Mon P Wangab3e7472008-05-05 22:56:23 +00009472 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009473 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00009474 for (int i=0; i <= lastAddrIndx; ++i)
9475 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +00009476
Mon P Wang63307c32008-05-05 19:05:59 +00009477 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +00009478 assert((argOpers[valArgIndx]->isReg() ||
9479 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00009480 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +00009481
9482 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +00009483 if (argOpers[valArgIndx]->isReg())
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009484 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +00009485 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009486 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00009487 (*MIB).addOperand(*argOpers[valArgIndx]);
9488
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009489 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +00009490 MIB.addReg(t1);
9491
Dale Johannesene4d209d2009-02-03 20:21:25 +00009492 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +00009493 MIB.addReg(t1);
9494 MIB.addReg(t2);
9495
9496 // Generate movc
9497 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009498 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +00009499 MIB.addReg(t2);
9500 MIB.addReg(t1);
9501
9502 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +00009503 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +00009504 for (int i=0; i <= lastAddrIndx; ++i)
9505 (*MIB).addOperand(*argOpers[i]);
9506 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +00009507 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00009508 (*MIB).setMemRefs(mInstr->memoperands_begin(),
9509 mInstr->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +00009510
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009511 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +00009512 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +00009513
Mon P Wang63307c32008-05-05 19:05:59 +00009514 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009515 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00009516
Dan Gohman14152b42010-07-06 20:24:04 +00009517 mInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00009518 return nextMBB;
9519}
9520
Eric Christopherf83a5de2009-08-27 18:08:16 +00009521// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00009522// or XMM0_V32I8 in AVX all of this code can be replaced with that
9523// in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +00009524MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +00009525X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +00009526 unsigned numArgs, bool memArg) const {
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00009527 assert((Subtarget->hasSSE42() || Subtarget->hasAVX()) &&
9528 "Target must have SSE4.2 or AVX features enabled");
9529
Eric Christopherb120ab42009-08-18 22:50:32 +00009530 DebugLoc dl = MI->getDebugLoc();
9531 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Eric Christopherb120ab42009-08-18 22:50:32 +00009532 unsigned Opc;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00009533 if (!Subtarget->hasAVX()) {
9534 if (memArg)
9535 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
9536 else
9537 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
9538 } else {
9539 if (memArg)
9540 Opc = numArgs == 3 ? X86::VPCMPISTRM128rm : X86::VPCMPESTRM128rm;
9541 else
9542 Opc = numArgs == 3 ? X86::VPCMPISTRM128rr : X86::VPCMPESTRM128rr;
9543 }
Eric Christopherb120ab42009-08-18 22:50:32 +00009544
Eric Christopher41c902f2010-11-30 08:20:21 +00009545 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Eric Christopherb120ab42009-08-18 22:50:32 +00009546 for (unsigned i = 0; i < numArgs; ++i) {
9547 MachineOperand &Op = MI->getOperand(i+1);
Eric Christopherb120ab42009-08-18 22:50:32 +00009548 if (!(Op.isReg() && Op.isImplicit()))
9549 MIB.addOperand(Op);
9550 }
Eric Christopher41c902f2010-11-30 08:20:21 +00009551 BuildMI(*BB, MI, dl, TII->get(X86::MOVAPSrr), MI->getOperand(0).getReg())
Eric Christopherb120ab42009-08-18 22:50:32 +00009552 .addReg(X86::XMM0);
9553
Dan Gohman14152b42010-07-06 20:24:04 +00009554 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +00009555 return BB;
9556}
9557
9558MachineBasicBlock *
Eric Christopher228232b2010-11-30 07:20:12 +00009559X86TargetLowering::EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +00009560 DebugLoc dl = MI->getDebugLoc();
9561 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009562
Eric Christopher228232b2010-11-30 07:20:12 +00009563 // Address into RAX/EAX, other two args into ECX, EDX.
9564 unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
9565 unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
9566 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg);
9567 for (int i = 0; i < X86::AddrNumOperands; ++i)
Eric Christopher82be2202010-11-30 08:10:28 +00009568 MIB.addOperand(MI->getOperand(i));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009569
Eric Christopher228232b2010-11-30 07:20:12 +00009570 unsigned ValOps = X86::AddrNumOperands;
9571 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
9572 .addReg(MI->getOperand(ValOps).getReg());
9573 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX)
9574 .addReg(MI->getOperand(ValOps+1).getReg());
9575
9576 // The instruction doesn't actually take any operands though.
9577 BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009578
Eric Christopher228232b2010-11-30 07:20:12 +00009579 MI->eraseFromParent(); // The pseudo is gone now.
9580 return BB;
9581}
9582
9583MachineBasicBlock *
9584X86TargetLowering::EmitMwait(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +00009585 DebugLoc dl = MI->getDebugLoc();
9586 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009587
Eric Christopher228232b2010-11-30 07:20:12 +00009588 // First arg in ECX, the second in EAX.
9589 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
9590 .addReg(MI->getOperand(0).getReg());
9591 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EAX)
9592 .addReg(MI->getOperand(1).getReg());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009593
Eric Christopher228232b2010-11-30 07:20:12 +00009594 // The instruction doesn't actually take any operands though.
9595 BuildMI(*BB, MI, dl, TII->get(X86::MWAITrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009596
Eric Christopher228232b2010-11-30 07:20:12 +00009597 MI->eraseFromParent(); // The pseudo is gone now.
9598 return BB;
9599}
9600
9601MachineBasicBlock *
Dan Gohman320afb82010-10-12 18:00:49 +00009602X86TargetLowering::EmitVAARG64WithCustomInserter(
9603 MachineInstr *MI,
9604 MachineBasicBlock *MBB) const {
9605 // Emit va_arg instruction on X86-64.
9606
9607 // Operands to this pseudo-instruction:
9608 // 0 ) Output : destination address (reg)
9609 // 1-5) Input : va_list address (addr, i64mem)
9610 // 6 ) ArgSize : Size (in bytes) of vararg type
9611 // 7 ) ArgMode : 0=overflow only, 1=use gp_offset, 2=use fp_offset
9612 // 8 ) Align : Alignment of type
9613 // 9 ) EFLAGS (implicit-def)
9614
9615 assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
9616 assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands");
9617
9618 unsigned DestReg = MI->getOperand(0).getReg();
9619 MachineOperand &Base = MI->getOperand(1);
9620 MachineOperand &Scale = MI->getOperand(2);
9621 MachineOperand &Index = MI->getOperand(3);
9622 MachineOperand &Disp = MI->getOperand(4);
9623 MachineOperand &Segment = MI->getOperand(5);
9624 unsigned ArgSize = MI->getOperand(6).getImm();
9625 unsigned ArgMode = MI->getOperand(7).getImm();
9626 unsigned Align = MI->getOperand(8).getImm();
9627
9628 // Memory Reference
9629 assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand");
9630 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
9631 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
9632
9633 // Machine Information
9634 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9635 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
9636 const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64);
9637 const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32);
9638 DebugLoc DL = MI->getDebugLoc();
9639
9640 // struct va_list {
9641 // i32 gp_offset
9642 // i32 fp_offset
9643 // i64 overflow_area (address)
9644 // i64 reg_save_area (address)
9645 // }
9646 // sizeof(va_list) = 24
9647 // alignment(va_list) = 8
9648
9649 unsigned TotalNumIntRegs = 6;
9650 unsigned TotalNumXMMRegs = 8;
9651 bool UseGPOffset = (ArgMode == 1);
9652 bool UseFPOffset = (ArgMode == 2);
9653 unsigned MaxOffset = TotalNumIntRegs * 8 +
9654 (UseFPOffset ? TotalNumXMMRegs * 16 : 0);
9655
9656 /* Align ArgSize to a multiple of 8 */
9657 unsigned ArgSizeA8 = (ArgSize + 7) & ~7;
9658 bool NeedsAlign = (Align > 8);
9659
9660 MachineBasicBlock *thisMBB = MBB;
9661 MachineBasicBlock *overflowMBB;
9662 MachineBasicBlock *offsetMBB;
9663 MachineBasicBlock *endMBB;
9664
9665 unsigned OffsetDestReg = 0; // Argument address computed by offsetMBB
9666 unsigned OverflowDestReg = 0; // Argument address computed by overflowMBB
9667 unsigned OffsetReg = 0;
9668
9669 if (!UseGPOffset && !UseFPOffset) {
9670 // If we only pull from the overflow region, we don't create a branch.
9671 // We don't need to alter control flow.
9672 OffsetDestReg = 0; // unused
9673 OverflowDestReg = DestReg;
9674
9675 offsetMBB = NULL;
9676 overflowMBB = thisMBB;
9677 endMBB = thisMBB;
9678 } else {
9679 // First emit code to check if gp_offset (or fp_offset) is below the bound.
9680 // If so, pull the argument from reg_save_area. (branch to offsetMBB)
9681 // If not, pull from overflow_area. (branch to overflowMBB)
9682 //
9683 // thisMBB
9684 // | .
9685 // | .
9686 // offsetMBB overflowMBB
9687 // | .
9688 // | .
9689 // endMBB
9690
9691 // Registers for the PHI in endMBB
9692 OffsetDestReg = MRI.createVirtualRegister(AddrRegClass);
9693 OverflowDestReg = MRI.createVirtualRegister(AddrRegClass);
9694
9695 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
9696 MachineFunction *MF = MBB->getParent();
9697 overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB);
9698 offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB);
9699 endMBB = MF->CreateMachineBasicBlock(LLVM_BB);
9700
9701 MachineFunction::iterator MBBIter = MBB;
9702 ++MBBIter;
9703
9704 // Insert the new basic blocks
9705 MF->insert(MBBIter, offsetMBB);
9706 MF->insert(MBBIter, overflowMBB);
9707 MF->insert(MBBIter, endMBB);
9708
9709 // Transfer the remainder of MBB and its successor edges to endMBB.
9710 endMBB->splice(endMBB->begin(), thisMBB,
9711 llvm::next(MachineBasicBlock::iterator(MI)),
9712 thisMBB->end());
9713 endMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
9714
9715 // Make offsetMBB and overflowMBB successors of thisMBB
9716 thisMBB->addSuccessor(offsetMBB);
9717 thisMBB->addSuccessor(overflowMBB);
9718
9719 // endMBB is a successor of both offsetMBB and overflowMBB
9720 offsetMBB->addSuccessor(endMBB);
9721 overflowMBB->addSuccessor(endMBB);
9722
9723 // Load the offset value into a register
9724 OffsetReg = MRI.createVirtualRegister(OffsetRegClass);
9725 BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg)
9726 .addOperand(Base)
9727 .addOperand(Scale)
9728 .addOperand(Index)
9729 .addDisp(Disp, UseFPOffset ? 4 : 0)
9730 .addOperand(Segment)
9731 .setMemRefs(MMOBegin, MMOEnd);
9732
9733 // Check if there is enough room left to pull this argument.
9734 BuildMI(thisMBB, DL, TII->get(X86::CMP32ri))
9735 .addReg(OffsetReg)
9736 .addImm(MaxOffset + 8 - ArgSizeA8);
9737
9738 // Branch to "overflowMBB" if offset >= max
9739 // Fall through to "offsetMBB" otherwise
9740 BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE)))
9741 .addMBB(overflowMBB);
9742 }
9743
9744 // In offsetMBB, emit code to use the reg_save_area.
9745 if (offsetMBB) {
9746 assert(OffsetReg != 0);
9747
9748 // Read the reg_save_area address.
9749 unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass);
9750 BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg)
9751 .addOperand(Base)
9752 .addOperand(Scale)
9753 .addOperand(Index)
9754 .addDisp(Disp, 16)
9755 .addOperand(Segment)
9756 .setMemRefs(MMOBegin, MMOEnd);
9757
9758 // Zero-extend the offset
9759 unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass);
9760 BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64)
9761 .addImm(0)
9762 .addReg(OffsetReg)
9763 .addImm(X86::sub_32bit);
9764
9765 // Add the offset to the reg_save_area to get the final address.
9766 BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg)
9767 .addReg(OffsetReg64)
9768 .addReg(RegSaveReg);
9769
9770 // Compute the offset for the next argument
9771 unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass);
9772 BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg)
9773 .addReg(OffsetReg)
9774 .addImm(UseFPOffset ? 16 : 8);
9775
9776 // Store it back into the va_list.
9777 BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr))
9778 .addOperand(Base)
9779 .addOperand(Scale)
9780 .addOperand(Index)
9781 .addDisp(Disp, UseFPOffset ? 4 : 0)
9782 .addOperand(Segment)
9783 .addReg(NextOffsetReg)
9784 .setMemRefs(MMOBegin, MMOEnd);
9785
9786 // Jump to endMBB
9787 BuildMI(offsetMBB, DL, TII->get(X86::JMP_4))
9788 .addMBB(endMBB);
9789 }
9790
9791 //
9792 // Emit code to use overflow area
9793 //
9794
9795 // Load the overflow_area address into a register.
9796 unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass);
9797 BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg)
9798 .addOperand(Base)
9799 .addOperand(Scale)
9800 .addOperand(Index)
9801 .addDisp(Disp, 8)
9802 .addOperand(Segment)
9803 .setMemRefs(MMOBegin, MMOEnd);
9804
9805 // If we need to align it, do so. Otherwise, just copy the address
9806 // to OverflowDestReg.
9807 if (NeedsAlign) {
9808 // Align the overflow address
9809 assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2");
9810 unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass);
9811
9812 // aligned_addr = (addr + (align-1)) & ~(align-1)
9813 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg)
9814 .addReg(OverflowAddrReg)
9815 .addImm(Align-1);
9816
9817 BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg)
9818 .addReg(TmpReg)
9819 .addImm(~(uint64_t)(Align-1));
9820 } else {
9821 BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg)
9822 .addReg(OverflowAddrReg);
9823 }
9824
9825 // Compute the next overflow address after this argument.
9826 // (the overflow address should be kept 8-byte aligned)
9827 unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass);
9828 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg)
9829 .addReg(OverflowDestReg)
9830 .addImm(ArgSizeA8);
9831
9832 // Store the new overflow address.
9833 BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr))
9834 .addOperand(Base)
9835 .addOperand(Scale)
9836 .addOperand(Index)
9837 .addDisp(Disp, 8)
9838 .addOperand(Segment)
9839 .addReg(NextAddrReg)
9840 .setMemRefs(MMOBegin, MMOEnd);
9841
9842 // If we branched, emit the PHI to the front of endMBB.
9843 if (offsetMBB) {
9844 BuildMI(*endMBB, endMBB->begin(), DL,
9845 TII->get(X86::PHI), DestReg)
9846 .addReg(OffsetDestReg).addMBB(offsetMBB)
9847 .addReg(OverflowDestReg).addMBB(overflowMBB);
9848 }
9849
9850 // Erase the pseudo instruction
9851 MI->eraseFromParent();
9852
9853 return endMBB;
9854}
9855
9856MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +00009857X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
9858 MachineInstr *MI,
9859 MachineBasicBlock *MBB) const {
9860 // Emit code to save XMM registers to the stack. The ABI says that the
9861 // number of registers to save is given in %al, so it's theoretically
9862 // possible to do an indirect jump trick to avoid saving all of them,
9863 // however this code takes a simpler approach and just executes all
9864 // of the stores if %al is non-zero. It's less code, and it's probably
9865 // easier on the hardware branch predictor, and stores aren't all that
9866 // expensive anyway.
9867
9868 // Create the new basic blocks. One block contains all the XMM stores,
9869 // and one block is the final destination regardless of whether any
9870 // stores were performed.
9871 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
9872 MachineFunction *F = MBB->getParent();
9873 MachineFunction::iterator MBBIter = MBB;
9874 ++MBBIter;
9875 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
9876 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
9877 F->insert(MBBIter, XMMSaveMBB);
9878 F->insert(MBBIter, EndMBB);
9879
Dan Gohman14152b42010-07-06 20:24:04 +00009880 // Transfer the remainder of MBB and its successor edges to EndMBB.
9881 EndMBB->splice(EndMBB->begin(), MBB,
9882 llvm::next(MachineBasicBlock::iterator(MI)),
9883 MBB->end());
9884 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
9885
Dan Gohmand6708ea2009-08-15 01:38:56 +00009886 // The original block will now fall through to the XMM save block.
9887 MBB->addSuccessor(XMMSaveMBB);
9888 // The XMMSaveMBB will fall through to the end block.
9889 XMMSaveMBB->addSuccessor(EndMBB);
9890
9891 // Now add the instructions.
9892 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9893 DebugLoc DL = MI->getDebugLoc();
9894
9895 unsigned CountReg = MI->getOperand(0).getReg();
9896 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
9897 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
9898
9899 if (!Subtarget->isTargetWin64()) {
9900 // If %al is 0, branch around the XMM save block.
9901 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009902 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +00009903 MBB->addSuccessor(EndMBB);
9904 }
9905
9906 // In the XMM save block, save all the XMM argument registers.
9907 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
9908 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +00009909 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +00009910 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +00009911 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +00009912 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +00009913 /*Size=*/16, /*Align=*/16);
Dan Gohmand6708ea2009-08-15 01:38:56 +00009914 BuildMI(XMMSaveMBB, DL, TII->get(X86::MOVAPSmr))
9915 .addFrameIndex(RegSaveFrameIndex)
9916 .addImm(/*Scale=*/1)
9917 .addReg(/*IndexReg=*/0)
9918 .addImm(/*Disp=*/Offset)
9919 .addReg(/*Segment=*/0)
9920 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +00009921 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +00009922 }
9923
Dan Gohman14152b42010-07-06 20:24:04 +00009924 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +00009925
9926 return EndMBB;
9927}
Mon P Wang63307c32008-05-05 19:05:59 +00009928
Evan Cheng60c07e12006-07-05 22:17:51 +00009929MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +00009930X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00009931 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +00009932 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9933 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +00009934
Chris Lattner52600972009-09-02 05:57:00 +00009935 // To "insert" a SELECT_CC instruction, we actually have to insert the
9936 // diamond control-flow pattern. The incoming instruction knows the
9937 // destination vreg to set, the condition code register to branch on, the
9938 // true/false values to select between, and a branch opcode to use.
9939 const BasicBlock *LLVM_BB = BB->getBasicBlock();
9940 MachineFunction::iterator It = BB;
9941 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +00009942
Chris Lattner52600972009-09-02 05:57:00 +00009943 // thisMBB:
9944 // ...
9945 // TrueVal = ...
9946 // cmpTY ccX, r1, r2
9947 // bCC copy1MBB
9948 // fallthrough --> copy0MBB
9949 MachineBasicBlock *thisMBB = BB;
9950 MachineFunction *F = BB->getParent();
9951 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
9952 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +00009953 F->insert(It, copy0MBB);
9954 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +00009955
Bill Wendling730c07e2010-06-25 20:48:10 +00009956 // If the EFLAGS register isn't dead in the terminator, then claim that it's
9957 // live into the sink and copy blocks.
9958 const MachineFunction *MF = BB->getParent();
9959 const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo();
9960 BitVector ReservedRegs = TRI->getReservedRegs(*MF);
Bill Wendling730c07e2010-06-25 20:48:10 +00009961
Dan Gohman14152b42010-07-06 20:24:04 +00009962 for (unsigned I = 0, E = MI->getNumOperands(); I != E; ++I) {
9963 const MachineOperand &MO = MI->getOperand(I);
9964 if (!MO.isReg() || !MO.isUse() || MO.isKill()) continue;
Bill Wendling730c07e2010-06-25 20:48:10 +00009965 unsigned Reg = MO.getReg();
9966 if (Reg != X86::EFLAGS) continue;
9967 copy0MBB->addLiveIn(Reg);
9968 sinkMBB->addLiveIn(Reg);
9969 }
9970
Dan Gohman14152b42010-07-06 20:24:04 +00009971 // Transfer the remainder of BB and its successor edges to sinkMBB.
9972 sinkMBB->splice(sinkMBB->begin(), BB,
9973 llvm::next(MachineBasicBlock::iterator(MI)),
9974 BB->end());
9975 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
9976
9977 // Add the true and fallthrough blocks as its successors.
9978 BB->addSuccessor(copy0MBB);
9979 BB->addSuccessor(sinkMBB);
9980
9981 // Create the conditional branch instruction.
9982 unsigned Opc =
9983 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
9984 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
9985
Chris Lattner52600972009-09-02 05:57:00 +00009986 // copy0MBB:
9987 // %FalseValue = ...
9988 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +00009989 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +00009990
Chris Lattner52600972009-09-02 05:57:00 +00009991 // sinkMBB:
9992 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
9993 // ...
Dan Gohman14152b42010-07-06 20:24:04 +00009994 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
9995 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +00009996 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
9997 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
9998
Dan Gohman14152b42010-07-06 20:24:04 +00009999 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +000010000 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +000010001}
10002
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010003MachineBasicBlock *
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010004X86TargetLowering::EmitLoweredWinAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010005 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010006 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10007 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010008
10009 // The lowering is pretty easy: we're just emitting the call to _alloca. The
10010 // non-trivial part is impdef of ESP.
10011 // FIXME: The code should be tweaked as soon as we'll try to do codegen for
10012 // mingw-w64.
10013
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010014 const char *StackProbeSymbol =
10015 Subtarget->isTargetWindows() ? "_chkstk" : "_alloca";
10016
Dan Gohman14152b42010-07-06 20:24:04 +000010017 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010018 .addExternalSymbol(StackProbeSymbol)
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010019 .addReg(X86::EAX, RegState::Implicit)
10020 .addReg(X86::ESP, RegState::Implicit)
10021 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
Anton Korobeynikov9f7f83b2010-08-25 07:50:11 +000010022 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
10023 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010024
Dan Gohman14152b42010-07-06 20:24:04 +000010025 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010026 return BB;
10027}
Chris Lattner52600972009-09-02 05:57:00 +000010028
10029MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +000010030X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
10031 MachineBasicBlock *BB) const {
10032 // This is pretty easy. We're taking the value that we received from
10033 // our load from the relocation, sticking it in either RDI (x86-64)
10034 // or EAX and doing an indirect call. The return value will then
10035 // be in the normal return register.
Michael J. Spencerec38de22010-10-10 22:04:20 +000010036 const X86InstrInfo *TII
Eric Christopher54415362010-06-08 22:04:25 +000010037 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +000010038 DebugLoc DL = MI->getDebugLoc();
10039 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +000010040
10041 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +000010042 assert(MI->getOperand(3).isGlobal() && "This should be a global");
Michael J. Spencerec38de22010-10-10 22:04:20 +000010043
Eric Christopher30ef0e52010-06-03 04:07:48 +000010044 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +000010045 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
10046 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +000010047 .addReg(X86::RIP)
10048 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000010049 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000010050 MI->getOperand(3).getTargetFlags())
10051 .addReg(0);
Eric Christopher722d3152010-09-27 06:01:51 +000010052 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +000010053 addDirectMem(MIB, X86::RDI);
Eric Christopher61025492010-06-15 23:08:42 +000010054 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +000010055 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
10056 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +000010057 .addReg(0)
10058 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000010059 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher61025492010-06-15 23:08:42 +000010060 MI->getOperand(3).getTargetFlags())
10061 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000010062 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000010063 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010064 } else {
Dan Gohman14152b42010-07-06 20:24:04 +000010065 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
10066 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +000010067 .addReg(TII->getGlobalBaseReg(F))
10068 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000010069 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000010070 MI->getOperand(3).getTargetFlags())
10071 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000010072 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000010073 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010074 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000010075
Dan Gohman14152b42010-07-06 20:24:04 +000010076 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +000010077 return BB;
10078}
10079
10080MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +000010081X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010082 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +000010083 switch (MI->getOpcode()) {
10084 default: assert(false && "Unexpected instr type to insert");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000010085 case X86::TAILJMPd64:
10086 case X86::TAILJMPr64:
10087 case X86::TAILJMPm64:
10088 assert(!"TAILJMP64 would not be touched here.");
10089 case X86::TCRETURNdi64:
10090 case X86::TCRETURNri64:
10091 case X86::TCRETURNmi64:
10092 // Defs of TCRETURNxx64 has Win64's callee-saved registers, as subset.
10093 // On AMD64, additional defs should be added before register allocation.
10094 if (!Subtarget->isTargetWin64()) {
10095 MI->addRegisterDefined(X86::RSI);
10096 MI->addRegisterDefined(X86::RDI);
10097 MI->addRegisterDefined(X86::XMM6);
10098 MI->addRegisterDefined(X86::XMM7);
10099 MI->addRegisterDefined(X86::XMM8);
10100 MI->addRegisterDefined(X86::XMM9);
10101 MI->addRegisterDefined(X86::XMM10);
10102 MI->addRegisterDefined(X86::XMM11);
10103 MI->addRegisterDefined(X86::XMM12);
10104 MI->addRegisterDefined(X86::XMM13);
10105 MI->addRegisterDefined(X86::XMM14);
10106 MI->addRegisterDefined(X86::XMM15);
10107 }
10108 return BB;
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010109 case X86::WIN_ALLOCA:
10110 return EmitLoweredWinAlloca(MI, BB);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010111 case X86::TLSCall_32:
10112 case X86::TLSCall_64:
10113 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +000010114 case X86::CMOV_GR8:
Evan Cheng60c07e12006-07-05 22:17:51 +000010115 case X86::CMOV_FR32:
10116 case X86::CMOV_FR64:
10117 case X86::CMOV_V4F32:
10118 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +000010119 case X86::CMOV_V2I64:
Chris Lattner314a1132010-03-14 18:31:44 +000010120 case X86::CMOV_GR16:
10121 case X86::CMOV_GR32:
10122 case X86::CMOV_RFP32:
10123 case X86::CMOV_RFP64:
10124 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010125 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000010126
Dale Johannesen849f2142007-07-03 00:53:03 +000010127 case X86::FP32_TO_INT16_IN_MEM:
10128 case X86::FP32_TO_INT32_IN_MEM:
10129 case X86::FP32_TO_INT64_IN_MEM:
10130 case X86::FP64_TO_INT16_IN_MEM:
10131 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +000010132 case X86::FP64_TO_INT64_IN_MEM:
10133 case X86::FP80_TO_INT16_IN_MEM:
10134 case X86::FP80_TO_INT32_IN_MEM:
10135 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +000010136 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10137 DebugLoc DL = MI->getDebugLoc();
10138
Evan Cheng60c07e12006-07-05 22:17:51 +000010139 // Change the floating point control register to use "round towards zero"
10140 // mode when truncating to an integer value.
10141 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +000010142 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +000010143 addFrameReference(BuildMI(*BB, MI, DL,
10144 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010145
10146 // Load the old value of the high byte of the control word...
10147 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +000010148 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohman14152b42010-07-06 20:24:04 +000010149 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +000010150 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010151
10152 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +000010153 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000010154 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +000010155
10156 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +000010157 addFrameReference(BuildMI(*BB, MI, DL,
10158 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010159
10160 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +000010161 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000010162 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +000010163
10164 // Get the X86 opcode to use.
10165 unsigned Opc;
10166 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000010167 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +000010168 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
10169 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
10170 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
10171 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
10172 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
10173 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +000010174 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
10175 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
10176 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +000010177 }
10178
10179 X86AddressMode AM;
10180 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +000010181 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000010182 AM.BaseType = X86AddressMode::RegBase;
10183 AM.Base.Reg = Op.getReg();
10184 } else {
10185 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +000010186 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +000010187 }
10188 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +000010189 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000010190 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010191 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +000010192 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000010193 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010194 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +000010195 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000010196 AM.GV = Op.getGlobal();
10197 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +000010198 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010199 }
Dan Gohman14152b42010-07-06 20:24:04 +000010200 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010201 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +000010202
10203 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +000010204 addFrameReference(BuildMI(*BB, MI, DL,
10205 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010206
Dan Gohman14152b42010-07-06 20:24:04 +000010207 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +000010208 return BB;
10209 }
Eric Christopherb120ab42009-08-18 22:50:32 +000010210 // String/text processing lowering.
10211 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010212 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000010213 return EmitPCMP(MI, BB, 3, false /* in-mem */);
10214 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010215 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000010216 return EmitPCMP(MI, BB, 3, true /* in-mem */);
10217 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010218 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000010219 return EmitPCMP(MI, BB, 5, false /* in mem */);
10220 case X86::PCMPESTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010221 case X86::VPCMPESTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000010222 return EmitPCMP(MI, BB, 5, true /* in mem */);
10223
Eric Christopher228232b2010-11-30 07:20:12 +000010224 // Thread synchronization.
10225 case X86::MONITOR:
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010226 return EmitMonitor(MI, BB);
Eric Christopher228232b2010-11-30 07:20:12 +000010227 case X86::MWAIT:
10228 return EmitMwait(MI, BB);
10229
Eric Christopherb120ab42009-08-18 22:50:32 +000010230 // Atomic Lowering.
Mon P Wang63307c32008-05-05 19:05:59 +000010231 case X86::ATOMAND32:
10232 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010233 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010234 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010235 X86::NOT32r, X86::EAX,
10236 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000010237 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +000010238 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
10239 X86::OR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010240 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010241 X86::NOT32r, X86::EAX,
10242 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000010243 case X86::ATOMXOR32:
10244 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010245 X86::XOR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010246 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010247 X86::NOT32r, X86::EAX,
10248 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010249 case X86::ATOMNAND32:
10250 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010251 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010252 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010253 X86::NOT32r, X86::EAX,
10254 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +000010255 case X86::ATOMMIN32:
10256 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
10257 case X86::ATOMMAX32:
10258 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
10259 case X86::ATOMUMIN32:
10260 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
10261 case X86::ATOMUMAX32:
10262 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +000010263
10264 case X86::ATOMAND16:
10265 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
10266 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010267 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010268 X86::NOT16r, X86::AX,
10269 X86::GR16RegisterClass);
10270 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +000010271 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010272 X86::OR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010273 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010274 X86::NOT16r, X86::AX,
10275 X86::GR16RegisterClass);
10276 case X86::ATOMXOR16:
10277 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
10278 X86::XOR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010279 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010280 X86::NOT16r, X86::AX,
10281 X86::GR16RegisterClass);
10282 case X86::ATOMNAND16:
10283 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
10284 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010285 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010286 X86::NOT16r, X86::AX,
10287 X86::GR16RegisterClass, true);
10288 case X86::ATOMMIN16:
10289 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
10290 case X86::ATOMMAX16:
10291 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
10292 case X86::ATOMUMIN16:
10293 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
10294 case X86::ATOMUMAX16:
10295 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
10296
10297 case X86::ATOMAND8:
10298 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
10299 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010300 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010301 X86::NOT8r, X86::AL,
10302 X86::GR8RegisterClass);
10303 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +000010304 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010305 X86::OR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010306 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010307 X86::NOT8r, X86::AL,
10308 X86::GR8RegisterClass);
10309 case X86::ATOMXOR8:
10310 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
10311 X86::XOR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010312 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010313 X86::NOT8r, X86::AL,
10314 X86::GR8RegisterClass);
10315 case X86::ATOMNAND8:
10316 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
10317 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010318 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010319 X86::NOT8r, X86::AL,
10320 X86::GR8RegisterClass, true);
10321 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010322 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +000010323 case X86::ATOMAND64:
10324 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010325 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010326 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010327 X86::NOT64r, X86::RAX,
10328 X86::GR64RegisterClass);
10329 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +000010330 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
10331 X86::OR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010332 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010333 X86::NOT64r, X86::RAX,
10334 X86::GR64RegisterClass);
10335 case X86::ATOMXOR64:
10336 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010337 X86::XOR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010338 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010339 X86::NOT64r, X86::RAX,
10340 X86::GR64RegisterClass);
10341 case X86::ATOMNAND64:
10342 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
10343 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010344 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010345 X86::NOT64r, X86::RAX,
10346 X86::GR64RegisterClass, true);
10347 case X86::ATOMMIN64:
10348 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
10349 case X86::ATOMMAX64:
10350 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
10351 case X86::ATOMUMIN64:
10352 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
10353 case X86::ATOMUMAX64:
10354 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010355
10356 // This group does 64-bit operations on a 32-bit host.
10357 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010358 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010359 X86::AND32rr, X86::AND32rr,
10360 X86::AND32ri, X86::AND32ri,
10361 false);
10362 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010363 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010364 X86::OR32rr, X86::OR32rr,
10365 X86::OR32ri, X86::OR32ri,
10366 false);
10367 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010368 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010369 X86::XOR32rr, X86::XOR32rr,
10370 X86::XOR32ri, X86::XOR32ri,
10371 false);
10372 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010373 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010374 X86::AND32rr, X86::AND32rr,
10375 X86::AND32ri, X86::AND32ri,
10376 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010377 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010378 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010379 X86::ADD32rr, X86::ADC32rr,
10380 X86::ADD32ri, X86::ADC32ri,
10381 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010382 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010383 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010384 X86::SUB32rr, X86::SBB32rr,
10385 X86::SUB32ri, X86::SBB32ri,
10386 false);
Dale Johannesen880ae362008-10-03 22:25:52 +000010387 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010388 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +000010389 X86::MOV32rr, X86::MOV32rr,
10390 X86::MOV32ri, X86::MOV32ri,
10391 false);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010392 case X86::VASTART_SAVE_XMM_REGS:
10393 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Dan Gohman320afb82010-10-12 18:00:49 +000010394
10395 case X86::VAARG_64:
10396 return EmitVAARG64WithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000010397 }
10398}
10399
10400//===----------------------------------------------------------------------===//
10401// X86 Optimization Hooks
10402//===----------------------------------------------------------------------===//
10403
Dan Gohman475871a2008-07-27 21:46:04 +000010404void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +000010405 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000010406 APInt &KnownZero,
10407 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +000010408 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +000010409 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010410 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +000010411 assert((Opc >= ISD::BUILTIN_OP_END ||
10412 Opc == ISD::INTRINSIC_WO_CHAIN ||
10413 Opc == ISD::INTRINSIC_W_CHAIN ||
10414 Opc == ISD::INTRINSIC_VOID) &&
10415 "Should use MaskedValueIsZero if you don't know whether Op"
10416 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010417
Dan Gohmanf4f92f52008-02-13 23:07:24 +000010418 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010419 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +000010420 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +000010421 case X86ISD::ADD:
10422 case X86ISD::SUB:
Chris Lattner5b856542010-12-20 00:59:46 +000010423 case X86ISD::ADC:
10424 case X86ISD::SBB:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000010425 case X86ISD::SMUL:
10426 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +000010427 case X86ISD::INC:
10428 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +000010429 case X86ISD::OR:
10430 case X86ISD::XOR:
10431 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000010432 // These nodes' second result is a boolean.
10433 if (Op.getResNo() == 0)
10434 break;
10435 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010436 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000010437 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
10438 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +000010439 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010440 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010441}
Chris Lattner259e97c2006-01-31 19:43:35 +000010442
Owen Andersonbc146b02010-09-21 20:42:50 +000010443unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
10444 unsigned Depth) const {
10445 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
10446 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
10447 return Op.getValueType().getScalarType().getSizeInBits();
Michael J. Spencerec38de22010-10-10 22:04:20 +000010448
Owen Andersonbc146b02010-09-21 20:42:50 +000010449 // Fallback case.
10450 return 1;
10451}
10452
Evan Cheng206ee9d2006-07-07 08:33:52 +000010453/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +000010454/// node is a GlobalAddress + offset.
10455bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +000010456 const GlobalValue* &GA,
10457 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +000010458 if (N->getOpcode() == X86ISD::Wrapper) {
10459 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +000010460 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +000010461 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +000010462 return true;
10463 }
Evan Cheng206ee9d2006-07-07 08:33:52 +000010464 }
Evan Chengad4196b2008-05-12 19:56:52 +000010465 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +000010466}
10467
Evan Cheng206ee9d2006-07-07 08:33:52 +000010468/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
10469/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
10470/// if the load addresses are consecutive, non-overlapping, and in the right
Nate Begemanfdea31a2010-03-24 20:49:50 +000010471/// order.
Dan Gohman475871a2008-07-27 21:46:04 +000010472static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Mon P Wanga0fd0d52010-12-19 23:55:53 +000010473 TargetLowering::DAGCombinerInfo &DCI) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000010474 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000010475 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +000010476
Eli Friedman7a5e5552009-06-07 06:52:44 +000010477 if (VT.getSizeInBits() != 128)
10478 return SDValue();
10479
Mon P Wanga0fd0d52010-12-19 23:55:53 +000010480 // Don't create instructions with illegal types after legalize types has run.
10481 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10482 if (!DCI.isBeforeLegalize() && !TLI.isTypeLegal(VT.getVectorElementType()))
10483 return SDValue();
10484
Nate Begemanfdea31a2010-03-24 20:49:50 +000010485 SmallVector<SDValue, 16> Elts;
10486 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000010487 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +000010488
Nate Begemanfdea31a2010-03-24 20:49:50 +000010489 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000010490}
Evan Chengd880b972008-05-09 21:53:03 +000010491
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +000010492/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
10493/// generation and convert it from being a bunch of shuffles and extracts
10494/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010495static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
10496 const TargetLowering &TLI) {
10497 SDValue InputVector = N->getOperand(0);
10498
10499 // Only operate on vectors of 4 elements, where the alternative shuffling
10500 // gets to be more expensive.
10501 if (InputVector.getValueType() != MVT::v4i32)
10502 return SDValue();
10503
10504 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
10505 // single use which is a sign-extend or zero-extend, and all elements are
10506 // used.
10507 SmallVector<SDNode *, 4> Uses;
10508 unsigned ExtractedElements = 0;
10509 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
10510 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
10511 if (UI.getUse().getResNo() != InputVector.getResNo())
10512 return SDValue();
10513
10514 SDNode *Extract = *UI;
10515 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
10516 return SDValue();
10517
10518 if (Extract->getValueType(0) != MVT::i32)
10519 return SDValue();
10520 if (!Extract->hasOneUse())
10521 return SDValue();
10522 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
10523 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
10524 return SDValue();
10525 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
10526 return SDValue();
10527
10528 // Record which element was extracted.
10529 ExtractedElements |=
10530 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
10531
10532 Uses.push_back(Extract);
10533 }
10534
10535 // If not all the elements were used, this may not be worthwhile.
10536 if (ExtractedElements != 15)
10537 return SDValue();
10538
10539 // Ok, we've now decided to do the transformation.
10540 DebugLoc dl = InputVector.getDebugLoc();
10541
10542 // Store the value to a temporary stack slot.
10543 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000010544 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
10545 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010546
10547 // Replace each use (extract) with a load of the appropriate element.
10548 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
10549 UE = Uses.end(); UI != UE; ++UI) {
10550 SDNode *Extract = *UI;
10551
10552 // Compute the element's address.
10553 SDValue Idx = Extract->getOperand(1);
10554 unsigned EltSize =
10555 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
10556 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
10557 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
10558
Eric Christopher90eb4022010-07-22 00:26:08 +000010559 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, Idx.getValueType(),
Chris Lattner51abfe42010-09-21 06:02:19 +000010560 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010561
10562 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000010563 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000010564 ScalarAddr, MachinePointerInfo(),
10565 false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010566
10567 // Replace the exact with the load.
10568 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
10569 }
10570
10571 // The replacement was made in place; don't return anything.
10572 return SDValue();
10573}
10574
Chris Lattner83e6c992006-10-04 06:57:07 +000010575/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000010576static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner47b4ce82009-03-11 05:48:52 +000010577 const X86Subtarget *Subtarget) {
10578 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +000010579 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000010580 // Get the LHS/RHS of the select.
10581 SDValue LHS = N->getOperand(1);
10582 SDValue RHS = N->getOperand(2);
Eric Christopherfd179292009-08-27 18:07:15 +000010583
Dan Gohman670e5392009-09-21 18:03:22 +000010584 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000010585 // instructions match the semantics of the common C idiom x<y?x:y but not
10586 // x<=y?x:y, because of how they handle negative zero (which can be
10587 // ignored in unsafe-math mode).
Chris Lattner83e6c992006-10-04 06:57:07 +000010588 if (Subtarget->hasSSE2() &&
Owen Anderson825b72b2009-08-11 20:47:22 +000010589 (LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64) &&
Chris Lattner47b4ce82009-03-11 05:48:52 +000010590 Cond.getOpcode() == ISD::SETCC) {
10591 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010592
Chris Lattner47b4ce82009-03-11 05:48:52 +000010593 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000010594 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000010595 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
10596 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000010597 switch (CC) {
10598 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000010599 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000010600 // Converting this to a min would handle NaNs incorrectly, and swapping
10601 // the operands would cause it to handle comparisons between positive
10602 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000010603 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000010604 if (!UnsafeFPMath &&
10605 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
10606 break;
10607 std::swap(LHS, RHS);
10608 }
Dan Gohman670e5392009-09-21 18:03:22 +000010609 Opcode = X86ISD::FMIN;
10610 break;
10611 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000010612 // Converting this to a min would handle comparisons between positive
10613 // and negative zero incorrectly.
10614 if (!UnsafeFPMath &&
10615 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
10616 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010617 Opcode = X86ISD::FMIN;
10618 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000010619 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000010620 // Converting this to a min would handle both negative zeros and NaNs
10621 // incorrectly, but we can swap the operands to fix both.
10622 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000010623 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010624 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000010625 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010626 Opcode = X86ISD::FMIN;
10627 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010628
Dan Gohman670e5392009-09-21 18:03:22 +000010629 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000010630 // Converting this to a max would handle comparisons between positive
10631 // and negative zero incorrectly.
10632 if (!UnsafeFPMath &&
10633 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(LHS))
10634 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010635 Opcode = X86ISD::FMAX;
10636 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000010637 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000010638 // Converting this to a max would handle NaNs incorrectly, and swapping
10639 // the operands would cause it to handle comparisons between positive
10640 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000010641 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000010642 if (!UnsafeFPMath &&
10643 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
10644 break;
10645 std::swap(LHS, RHS);
10646 }
Dan Gohman670e5392009-09-21 18:03:22 +000010647 Opcode = X86ISD::FMAX;
10648 break;
10649 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000010650 // Converting this to a max would handle both negative zeros and NaNs
10651 // incorrectly, but we can swap the operands to fix both.
10652 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000010653 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010654 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010655 case ISD::SETGE:
10656 Opcode = X86ISD::FMAX;
10657 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000010658 }
Dan Gohman670e5392009-09-21 18:03:22 +000010659 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000010660 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
10661 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000010662 switch (CC) {
10663 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000010664 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000010665 // Converting this to a min would handle comparisons between positive
10666 // and negative zero incorrectly, and swapping the operands would
10667 // cause it to handle NaNs incorrectly.
10668 if (!UnsafeFPMath &&
10669 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000010670 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000010671 break;
10672 std::swap(LHS, RHS);
10673 }
Dan Gohman670e5392009-09-21 18:03:22 +000010674 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000010675 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010676 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000010677 // Converting this to a min would handle NaNs incorrectly.
10678 if (!UnsafeFPMath &&
10679 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
10680 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010681 Opcode = X86ISD::FMIN;
10682 break;
10683 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000010684 // Converting this to a min would handle both negative zeros and NaNs
10685 // incorrectly, but we can swap the operands to fix both.
10686 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000010687 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010688 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010689 case ISD::SETGE:
10690 Opcode = X86ISD::FMIN;
10691 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010692
Dan Gohman670e5392009-09-21 18:03:22 +000010693 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000010694 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000010695 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000010696 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010697 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000010698 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010699 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000010700 // Converting this to a max would handle comparisons between positive
10701 // and negative zero incorrectly, and swapping the operands would
10702 // cause it to handle NaNs incorrectly.
10703 if (!UnsafeFPMath &&
10704 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000010705 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000010706 break;
10707 std::swap(LHS, RHS);
10708 }
Dan Gohman670e5392009-09-21 18:03:22 +000010709 Opcode = X86ISD::FMAX;
10710 break;
10711 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000010712 // Converting this to a max would handle both negative zeros and NaNs
10713 // incorrectly, but we can swap the operands to fix both.
10714 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000010715 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010716 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000010717 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010718 Opcode = X86ISD::FMAX;
10719 break;
10720 }
Chris Lattner83e6c992006-10-04 06:57:07 +000010721 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010722
Chris Lattner47b4ce82009-03-11 05:48:52 +000010723 if (Opcode)
10724 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000010725 }
Eric Christopherfd179292009-08-27 18:07:15 +000010726
Chris Lattnerd1980a52009-03-12 06:52:53 +000010727 // If this is a select between two integer constants, try to do some
10728 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000010729 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
10730 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000010731 // Don't do this for crazy integer types.
10732 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
10733 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000010734 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000010735 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000010736
Chris Lattnercee56e72009-03-13 05:53:31 +000010737 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000010738 // Efficiently invertible.
10739 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
10740 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
10741 isa<ConstantSDNode>(Cond.getOperand(1))))) {
10742 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000010743 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000010744 }
Eric Christopherfd179292009-08-27 18:07:15 +000010745
Chris Lattnerd1980a52009-03-12 06:52:53 +000010746 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000010747 if (FalseC->getAPIntValue() == 0 &&
10748 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000010749 if (NeedsCondInvert) // Invert the condition if needed.
10750 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
10751 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000010752
Chris Lattnerd1980a52009-03-12 06:52:53 +000010753 // Zero extend the condition if needed.
10754 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000010755
Chris Lattnercee56e72009-03-13 05:53:31 +000010756 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000010757 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000010758 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000010759 }
Eric Christopherfd179292009-08-27 18:07:15 +000010760
Chris Lattner97a29a52009-03-13 05:22:11 +000010761 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000010762 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +000010763 if (NeedsCondInvert) // Invert the condition if needed.
10764 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
10765 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000010766
Chris Lattner97a29a52009-03-13 05:22:11 +000010767 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000010768 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
10769 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000010770 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000010771 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000010772 }
Eric Christopherfd179292009-08-27 18:07:15 +000010773
Chris Lattnercee56e72009-03-13 05:53:31 +000010774 // Optimize cases that will turn into an LEA instruction. This requires
10775 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000010776 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000010777 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000010778 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000010779
Chris Lattnercee56e72009-03-13 05:53:31 +000010780 bool isFastMultiplier = false;
10781 if (Diff < 10) {
10782 switch ((unsigned char)Diff) {
10783 default: break;
10784 case 1: // result = add base, cond
10785 case 2: // result = lea base( , cond*2)
10786 case 3: // result = lea base(cond, cond*2)
10787 case 4: // result = lea base( , cond*4)
10788 case 5: // result = lea base(cond, cond*4)
10789 case 8: // result = lea base( , cond*8)
10790 case 9: // result = lea base(cond, cond*8)
10791 isFastMultiplier = true;
10792 break;
10793 }
10794 }
Eric Christopherfd179292009-08-27 18:07:15 +000010795
Chris Lattnercee56e72009-03-13 05:53:31 +000010796 if (isFastMultiplier) {
10797 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
10798 if (NeedsCondInvert) // Invert the condition if needed.
10799 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
10800 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000010801
Chris Lattnercee56e72009-03-13 05:53:31 +000010802 // Zero extend the condition if needed.
10803 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
10804 Cond);
10805 // Scale the condition by the difference.
10806 if (Diff != 1)
10807 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
10808 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000010809
Chris Lattnercee56e72009-03-13 05:53:31 +000010810 // Add the base if non-zero.
10811 if (FalseC->getAPIntValue() != 0)
10812 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
10813 SDValue(FalseC, 0));
10814 return Cond;
10815 }
Eric Christopherfd179292009-08-27 18:07:15 +000010816 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000010817 }
10818 }
Eric Christopherfd179292009-08-27 18:07:15 +000010819
Dan Gohman475871a2008-07-27 21:46:04 +000010820 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000010821}
10822
Chris Lattnerd1980a52009-03-12 06:52:53 +000010823/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
10824static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
10825 TargetLowering::DAGCombinerInfo &DCI) {
10826 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +000010827
Chris Lattnerd1980a52009-03-12 06:52:53 +000010828 // If the flag operand isn't dead, don't touch this CMOV.
10829 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
10830 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +000010831
Chris Lattnerd1980a52009-03-12 06:52:53 +000010832 // If this is a select between two integer constants, try to do some
10833 // optimizations. Note that the operands are ordered the opposite of SELECT
10834 // operands.
10835 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
10836 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
10837 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
10838 // larger than FalseC (the false value).
10839 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
Eric Christopherfd179292009-08-27 18:07:15 +000010840
Chris Lattnerd1980a52009-03-12 06:52:53 +000010841 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
10842 CC = X86::GetOppositeBranchCondition(CC);
10843 std::swap(TrueC, FalseC);
10844 }
Eric Christopherfd179292009-08-27 18:07:15 +000010845
Chris Lattnerd1980a52009-03-12 06:52:53 +000010846 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000010847 // This is efficient for any integer data type (including i8/i16) and
10848 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000010849 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
10850 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +000010851 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
10852 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000010853
Chris Lattnerd1980a52009-03-12 06:52:53 +000010854 // Zero extend the condition if needed.
10855 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000010856
Chris Lattnerd1980a52009-03-12 06:52:53 +000010857 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
10858 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000010859 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000010860 if (N->getNumValues() == 2) // Dead flag value?
10861 return DCI.CombineTo(N, Cond, SDValue());
10862 return Cond;
10863 }
Eric Christopherfd179292009-08-27 18:07:15 +000010864
Chris Lattnercee56e72009-03-13 05:53:31 +000010865 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
10866 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000010867 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
10868 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +000010869 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
10870 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000010871
Chris Lattner97a29a52009-03-13 05:22:11 +000010872 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000010873 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
10874 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000010875 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
10876 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000010877
Chris Lattner97a29a52009-03-13 05:22:11 +000010878 if (N->getNumValues() == 2) // Dead flag value?
10879 return DCI.CombineTo(N, Cond, SDValue());
10880 return Cond;
10881 }
Eric Christopherfd179292009-08-27 18:07:15 +000010882
Chris Lattnercee56e72009-03-13 05:53:31 +000010883 // Optimize cases that will turn into an LEA instruction. This requires
10884 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000010885 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000010886 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000010887 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000010888
Chris Lattnercee56e72009-03-13 05:53:31 +000010889 bool isFastMultiplier = false;
10890 if (Diff < 10) {
10891 switch ((unsigned char)Diff) {
10892 default: break;
10893 case 1: // result = add base, cond
10894 case 2: // result = lea base( , cond*2)
10895 case 3: // result = lea base(cond, cond*2)
10896 case 4: // result = lea base( , cond*4)
10897 case 5: // result = lea base(cond, cond*4)
10898 case 8: // result = lea base( , cond*8)
10899 case 9: // result = lea base(cond, cond*8)
10900 isFastMultiplier = true;
10901 break;
10902 }
10903 }
Eric Christopherfd179292009-08-27 18:07:15 +000010904
Chris Lattnercee56e72009-03-13 05:53:31 +000010905 if (isFastMultiplier) {
10906 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
10907 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +000010908 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
10909 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000010910 // Zero extend the condition if needed.
10911 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
10912 Cond);
10913 // Scale the condition by the difference.
10914 if (Diff != 1)
10915 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
10916 DAG.getConstant(Diff, Cond.getValueType()));
10917
10918 // Add the base if non-zero.
10919 if (FalseC->getAPIntValue() != 0)
10920 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
10921 SDValue(FalseC, 0));
10922 if (N->getNumValues() == 2) // Dead flag value?
10923 return DCI.CombineTo(N, Cond, SDValue());
10924 return Cond;
10925 }
Eric Christopherfd179292009-08-27 18:07:15 +000010926 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000010927 }
10928 }
10929 return SDValue();
10930}
10931
10932
Evan Cheng0b0cd912009-03-28 05:57:29 +000010933/// PerformMulCombine - Optimize a single multiply with constant into two
10934/// in order to implement it with two cheaper instructions, e.g.
10935/// LEA + SHL, LEA + LEA.
10936static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
10937 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000010938 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
10939 return SDValue();
10940
Owen Andersone50ed302009-08-10 22:56:29 +000010941 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010942 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000010943 return SDValue();
10944
10945 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
10946 if (!C)
10947 return SDValue();
10948 uint64_t MulAmt = C->getZExtValue();
10949 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
10950 return SDValue();
10951
10952 uint64_t MulAmt1 = 0;
10953 uint64_t MulAmt2 = 0;
10954 if ((MulAmt % 9) == 0) {
10955 MulAmt1 = 9;
10956 MulAmt2 = MulAmt / 9;
10957 } else if ((MulAmt % 5) == 0) {
10958 MulAmt1 = 5;
10959 MulAmt2 = MulAmt / 5;
10960 } else if ((MulAmt % 3) == 0) {
10961 MulAmt1 = 3;
10962 MulAmt2 = MulAmt / 3;
10963 }
10964 if (MulAmt2 &&
10965 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
10966 DebugLoc DL = N->getDebugLoc();
10967
10968 if (isPowerOf2_64(MulAmt2) &&
10969 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
10970 // If second multiplifer is pow2, issue it first. We want the multiply by
10971 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
10972 // is an add.
10973 std::swap(MulAmt1, MulAmt2);
10974
10975 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000010976 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000010977 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000010978 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000010979 else
Evan Cheng73f24c92009-03-30 21:36:47 +000010980 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000010981 DAG.getConstant(MulAmt1, VT));
10982
Eric Christopherfd179292009-08-27 18:07:15 +000010983 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000010984 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000010985 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000010986 else
Evan Cheng73f24c92009-03-30 21:36:47 +000010987 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000010988 DAG.getConstant(MulAmt2, VT));
10989
10990 // Do not add new nodes to DAG combiner worklist.
10991 DCI.CombineTo(N, NewMul, false);
10992 }
10993 return SDValue();
10994}
10995
Evan Chengad9c0a32009-12-15 00:53:42 +000010996static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
10997 SDValue N0 = N->getOperand(0);
10998 SDValue N1 = N->getOperand(1);
10999 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
11000 EVT VT = N0.getValueType();
11001
11002 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
11003 // since the result of setcc_c is all zero's or all ones.
11004 if (N1C && N0.getOpcode() == ISD::AND &&
11005 N0.getOperand(1).getOpcode() == ISD::Constant) {
11006 SDValue N00 = N0.getOperand(0);
11007 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
11008 ((N00.getOpcode() == ISD::ANY_EXTEND ||
11009 N00.getOpcode() == ISD::ZERO_EXTEND) &&
11010 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
11011 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
11012 APInt ShAmt = N1C->getAPIntValue();
11013 Mask = Mask.shl(ShAmt);
11014 if (Mask != 0)
11015 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
11016 N00, DAG.getConstant(Mask, VT));
11017 }
11018 }
11019
11020 return SDValue();
11021}
Evan Cheng0b0cd912009-03-28 05:57:29 +000011022
Nate Begeman740ab032009-01-26 00:52:55 +000011023/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
11024/// when possible.
11025static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
11026 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +000011027 EVT VT = N->getValueType(0);
11028 if (!VT.isVector() && VT.isInteger() &&
11029 N->getOpcode() == ISD::SHL)
11030 return PerformSHLCombine(N, DAG);
11031
Nate Begeman740ab032009-01-26 00:52:55 +000011032 // On X86 with SSE2 support, we can transform this to a vector shift if
11033 // all elements are shifted by the same amount. We can't do this in legalize
11034 // because the a constant vector is typically transformed to a constant pool
11035 // so we have no knowledge of the shift amount.
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011036 if (!Subtarget->hasSSE2())
11037 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000011038
Owen Anderson825b72b2009-08-11 20:47:22 +000011039 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011040 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000011041
Mon P Wang3becd092009-01-28 08:12:05 +000011042 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +000011043 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +000011044 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +000011045 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +000011046 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
11047 unsigned NumElts = VT.getVectorNumElements();
11048 unsigned i = 0;
11049 for (; i != NumElts; ++i) {
11050 SDValue Arg = ShAmtOp.getOperand(i);
11051 if (Arg.getOpcode() == ISD::UNDEF) continue;
11052 BaseShAmt = Arg;
11053 break;
11054 }
11055 for (; i != NumElts; ++i) {
11056 SDValue Arg = ShAmtOp.getOperand(i);
11057 if (Arg.getOpcode() == ISD::UNDEF) continue;
11058 if (Arg != BaseShAmt) {
11059 return SDValue();
11060 }
11061 }
11062 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +000011063 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +000011064 SDValue InVec = ShAmtOp.getOperand(0);
11065 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
11066 unsigned NumElts = InVec.getValueType().getVectorNumElements();
11067 unsigned i = 0;
11068 for (; i != NumElts; ++i) {
11069 SDValue Arg = InVec.getOperand(i);
11070 if (Arg.getOpcode() == ISD::UNDEF) continue;
11071 BaseShAmt = Arg;
11072 break;
11073 }
11074 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
11075 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +000011076 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +000011077 if (C->getZExtValue() == SplatIdx)
11078 BaseShAmt = InVec.getOperand(1);
11079 }
11080 }
11081 if (BaseShAmt.getNode() == 0)
11082 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
11083 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +000011084 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011085 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000011086
Mon P Wangefa42202009-09-03 19:56:25 +000011087 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +000011088 if (EltVT.bitsGT(MVT::i32))
11089 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
11090 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +000011091 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +000011092
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011093 // The shift amount is identical so we can do a vector shift.
11094 SDValue ValOp = N->getOperand(0);
11095 switch (N->getOpcode()) {
11096 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000011097 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011098 break;
11099 case ISD::SHL:
Owen Anderson825b72b2009-08-11 20:47:22 +000011100 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011101 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011102 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011103 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011104 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011105 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011106 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011107 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011108 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011109 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011110 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011111 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011112 break;
11113 case ISD::SRA:
Owen Anderson825b72b2009-08-11 20:47:22 +000011114 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011115 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011116 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011117 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011118 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011119 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011120 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011121 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011122 break;
11123 case ISD::SRL:
Owen Anderson825b72b2009-08-11 20:47:22 +000011124 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011125 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011126 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011127 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011128 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011129 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011130 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011131 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011132 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011133 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011134 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011135 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011136 break;
Nate Begeman740ab032009-01-26 00:52:55 +000011137 }
11138 return SDValue();
11139}
11140
Nate Begemanb65c1752010-12-17 22:55:37 +000011141
11142static SDValue PerformAndCombine(SDNode *N, SelectionDAG &DAG,
11143 TargetLowering::DAGCombinerInfo &DCI,
11144 const X86Subtarget *Subtarget) {
11145 if (DCI.isBeforeLegalizeOps())
11146 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011147
Nate Begemanb65c1752010-12-17 22:55:37 +000011148 // Want to form PANDN nodes, in the hopes of then easily combining them with
11149 // OR and AND nodes to form PBLEND/PSIGN.
11150 EVT VT = N->getValueType(0);
11151 if (VT != MVT::v2i64)
11152 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011153
Nate Begemanb65c1752010-12-17 22:55:37 +000011154 SDValue N0 = N->getOperand(0);
11155 SDValue N1 = N->getOperand(1);
11156 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011157
Nate Begemanb65c1752010-12-17 22:55:37 +000011158 // Check LHS for vnot
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011159 if (N0.getOpcode() == ISD::XOR &&
Nate Begemanb65c1752010-12-17 22:55:37 +000011160 ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode()))
11161 return DAG.getNode(X86ISD::PANDN, DL, VT, N0.getOperand(0), N1);
11162
11163 // Check RHS for vnot
11164 if (N1.getOpcode() == ISD::XOR &&
11165 ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode()))
11166 return DAG.getNode(X86ISD::PANDN, DL, VT, N1.getOperand(0), N0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011167
Nate Begemanb65c1752010-12-17 22:55:37 +000011168 return SDValue();
11169}
11170
Evan Cheng760d1942010-01-04 21:22:48 +000011171static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000011172 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000011173 const X86Subtarget *Subtarget) {
Evan Cheng39cfeec2010-04-28 02:25:18 +000011174 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000011175 return SDValue();
11176
Evan Cheng760d1942010-01-04 21:22:48 +000011177 EVT VT = N->getValueType(0);
Nate Begemanb65c1752010-12-17 22:55:37 +000011178 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64 && VT != MVT::v2i64)
Evan Cheng760d1942010-01-04 21:22:48 +000011179 return SDValue();
11180
Evan Cheng760d1942010-01-04 21:22:48 +000011181 SDValue N0 = N->getOperand(0);
11182 SDValue N1 = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011183
Nate Begemanb65c1752010-12-17 22:55:37 +000011184 // look for psign/blend
11185 if (Subtarget->hasSSSE3()) {
11186 if (VT == MVT::v2i64) {
11187 // Canonicalize pandn to RHS
11188 if (N0.getOpcode() == X86ISD::PANDN)
11189 std::swap(N0, N1);
11190 // or (and (m, x), (pandn m, y))
11191 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::PANDN) {
11192 SDValue Mask = N1.getOperand(0);
11193 SDValue X = N1.getOperand(1);
11194 SDValue Y;
11195 if (N0.getOperand(0) == Mask)
11196 Y = N0.getOperand(1);
11197 if (N0.getOperand(1) == Mask)
11198 Y = N0.getOperand(0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011199
Nate Begemanb65c1752010-12-17 22:55:37 +000011200 // Check to see if the mask appeared in both the AND and PANDN and
11201 if (!Y.getNode())
11202 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011203
Nate Begemanb65c1752010-12-17 22:55:37 +000011204 // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them.
11205 if (Mask.getOpcode() != ISD::BITCAST ||
11206 X.getOpcode() != ISD::BITCAST ||
11207 Y.getOpcode() != ISD::BITCAST)
11208 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011209
Nate Begemanb65c1752010-12-17 22:55:37 +000011210 // Look through mask bitcast.
11211 Mask = Mask.getOperand(0);
11212 EVT MaskVT = Mask.getValueType();
11213
11214 // Validate that the Mask operand is a vector sra node. The sra node
11215 // will be an intrinsic.
11216 if (Mask.getOpcode() != ISD::INTRINSIC_WO_CHAIN)
11217 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011218
Nate Begemanb65c1752010-12-17 22:55:37 +000011219 // FIXME: what to do for bytes, since there is a psignb/pblendvb, but
11220 // there is no psrai.b
11221 switch (cast<ConstantSDNode>(Mask.getOperand(0))->getZExtValue()) {
11222 case Intrinsic::x86_sse2_psrai_w:
11223 case Intrinsic::x86_sse2_psrai_d:
11224 break;
11225 default: return SDValue();
11226 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011227
Nate Begemanb65c1752010-12-17 22:55:37 +000011228 // Check that the SRA is all signbits.
11229 SDValue SraC = Mask.getOperand(2);
11230 unsigned SraAmt = cast<ConstantSDNode>(SraC)->getZExtValue();
11231 unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits();
11232 if ((SraAmt + 1) != EltBits)
11233 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011234
Nate Begemanb65c1752010-12-17 22:55:37 +000011235 DebugLoc DL = N->getDebugLoc();
11236
11237 // Now we know we at least have a plendvb with the mask val. See if
11238 // we can form a psignb/w/d.
11239 // psign = x.type == y.type == mask.type && y = sub(0, x);
11240 X = X.getOperand(0);
11241 Y = Y.getOperand(0);
11242 if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X &&
11243 ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) &&
11244 X.getValueType() == MaskVT && X.getValueType() == Y.getValueType()){
11245 unsigned Opc = 0;
11246 switch (EltBits) {
11247 case 8: Opc = X86ISD::PSIGNB; break;
11248 case 16: Opc = X86ISD::PSIGNW; break;
11249 case 32: Opc = X86ISD::PSIGND; break;
11250 default: break;
11251 }
11252 if (Opc) {
11253 SDValue Sign = DAG.getNode(Opc, DL, MaskVT, X, Mask.getOperand(1));
11254 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Sign);
11255 }
11256 }
11257 // PBLENDVB only available on SSE 4.1
11258 if (!Subtarget->hasSSE41())
11259 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011260
Nate Begemanb65c1752010-12-17 22:55:37 +000011261 X = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, X);
11262 Y = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Y);
11263 Mask = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Mask);
Nate Begeman672fb622010-12-20 22:04:24 +000011264 Mask = DAG.getNode(X86ISD::PBLENDVB, DL, MVT::v16i8, X, Y, Mask);
Nate Begemanb65c1752010-12-17 22:55:37 +000011265 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Mask);
11266 }
11267 }
11268 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011269
Nate Begemanb65c1752010-12-17 22:55:37 +000011270 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
Evan Cheng760d1942010-01-04 21:22:48 +000011271 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
11272 std::swap(N0, N1);
11273 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
11274 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000011275 if (!N0.hasOneUse() || !N1.hasOneUse())
11276 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000011277
11278 SDValue ShAmt0 = N0.getOperand(1);
11279 if (ShAmt0.getValueType() != MVT::i8)
11280 return SDValue();
11281 SDValue ShAmt1 = N1.getOperand(1);
11282 if (ShAmt1.getValueType() != MVT::i8)
11283 return SDValue();
11284 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
11285 ShAmt0 = ShAmt0.getOperand(0);
11286 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
11287 ShAmt1 = ShAmt1.getOperand(0);
11288
11289 DebugLoc DL = N->getDebugLoc();
11290 unsigned Opc = X86ISD::SHLD;
11291 SDValue Op0 = N0.getOperand(0);
11292 SDValue Op1 = N1.getOperand(0);
11293 if (ShAmt0.getOpcode() == ISD::SUB) {
11294 Opc = X86ISD::SHRD;
11295 std::swap(Op0, Op1);
11296 std::swap(ShAmt0, ShAmt1);
11297 }
11298
Evan Cheng8b1190a2010-04-28 01:18:01 +000011299 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000011300 if (ShAmt1.getOpcode() == ISD::SUB) {
11301 SDValue Sum = ShAmt1.getOperand(0);
11302 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000011303 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
11304 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
11305 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
11306 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000011307 return DAG.getNode(Opc, DL, VT,
11308 Op0, Op1,
11309 DAG.getNode(ISD::TRUNCATE, DL,
11310 MVT::i8, ShAmt0));
11311 }
11312 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
11313 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
11314 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000011315 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000011316 return DAG.getNode(Opc, DL, VT,
11317 N0.getOperand(0), N1.getOperand(0),
11318 DAG.getNode(ISD::TRUNCATE, DL,
11319 MVT::i8, ShAmt0));
11320 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011321
Evan Cheng760d1942010-01-04 21:22:48 +000011322 return SDValue();
11323}
11324
Chris Lattner149a4e52008-02-22 02:09:43 +000011325/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000011326static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000011327 const X86Subtarget *Subtarget) {
Chris Lattner149a4e52008-02-22 02:09:43 +000011328 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
11329 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000011330 // A preferable solution to the general problem is to figure out the right
11331 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000011332
11333 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng7e2ff772008-05-08 00:57:18 +000011334 StoreSDNode *St = cast<StoreSDNode>(N);
Owen Andersone50ed302009-08-10 22:56:29 +000011335 EVT VT = St->getValue().getValueType();
Evan Cheng536e6672009-03-12 05:59:15 +000011336 if (VT.getSizeInBits() != 64)
11337 return SDValue();
11338
Devang Patel578efa92009-06-05 21:57:13 +000011339 const Function *F = DAG.getMachineFunction().getFunction();
11340 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Eric Christopherfd179292009-08-27 18:07:15 +000011341 bool F64IsLegal = !UseSoftFloat && !NoImplicitFloatOps
Devang Patel578efa92009-06-05 21:57:13 +000011342 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +000011343 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000011344 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000011345 isa<LoadSDNode>(St->getValue()) &&
11346 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
11347 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000011348 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000011349 LoadSDNode *Ld = 0;
11350 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000011351 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000011352 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000011353 // Must be a store of a load. We currently handle two cases: the load
11354 // is a direct child, and it's under an intervening TokenFactor. It is
11355 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000011356 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000011357 Ld = cast<LoadSDNode>(St->getChain());
11358 else if (St->getValue().hasOneUse() &&
11359 ChainVal->getOpcode() == ISD::TokenFactor) {
11360 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000011361 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000011362 TokenFactorIndex = i;
11363 Ld = cast<LoadSDNode>(St->getValue());
11364 } else
11365 Ops.push_back(ChainVal->getOperand(i));
11366 }
11367 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000011368
Evan Cheng536e6672009-03-12 05:59:15 +000011369 if (!Ld || !ISD::isNormalLoad(Ld))
11370 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000011371
Evan Cheng536e6672009-03-12 05:59:15 +000011372 // If this is not the MMX case, i.e. we are just turning i64 load/store
11373 // into f64 load/store, avoid the transformation if there are multiple
11374 // uses of the loaded value.
11375 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
11376 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000011377
Evan Cheng536e6672009-03-12 05:59:15 +000011378 DebugLoc LdDL = Ld->getDebugLoc();
11379 DebugLoc StDL = N->getDebugLoc();
11380 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
11381 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
11382 // pair instead.
11383 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000011384 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000011385 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
11386 Ld->getPointerInfo(), Ld->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000011387 Ld->isNonTemporal(), Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000011388 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000011389 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000011390 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000011391 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000011392 Ops.size());
11393 }
Evan Cheng536e6672009-03-12 05:59:15 +000011394 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000011395 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000011396 St->isVolatile(), St->isNonTemporal(),
11397 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000011398 }
Evan Cheng536e6672009-03-12 05:59:15 +000011399
11400 // Otherwise, lower to two pairs of 32-bit loads / stores.
11401 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000011402 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
11403 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000011404
Owen Anderson825b72b2009-08-11 20:47:22 +000011405 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000011406 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000011407 Ld->isVolatile(), Ld->isNonTemporal(),
11408 Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000011409 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000011410 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000011411 Ld->isVolatile(), Ld->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000011412 MinAlign(Ld->getAlignment(), 4));
11413
11414 SDValue NewChain = LoLd.getValue(1);
11415 if (TokenFactorIndex != -1) {
11416 Ops.push_back(LoLd);
11417 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000011418 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000011419 Ops.size());
11420 }
11421
11422 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000011423 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
11424 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000011425
11426 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000011427 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000011428 St->isVolatile(), St->isNonTemporal(),
11429 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000011430 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000011431 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000011432 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000011433 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000011434 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000011435 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000011436 }
Dan Gohman475871a2008-07-27 21:46:04 +000011437 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000011438}
11439
Chris Lattner6cf73262008-01-25 06:14:17 +000011440/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
11441/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000011442static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000011443 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
11444 // F[X]OR(0.0, x) -> x
11445 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000011446 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
11447 if (C->getValueAPF().isPosZero())
11448 return N->getOperand(1);
11449 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
11450 if (C->getValueAPF().isPosZero())
11451 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000011452 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000011453}
11454
11455/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000011456static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000011457 // FAND(0.0, x) -> 0.0
11458 // FAND(x, 0.0) -> 0.0
11459 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
11460 if (C->getValueAPF().isPosZero())
11461 return N->getOperand(0);
11462 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
11463 if (C->getValueAPF().isPosZero())
11464 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000011465 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000011466}
11467
Dan Gohmane5af2d32009-01-29 01:59:02 +000011468static SDValue PerformBTCombine(SDNode *N,
11469 SelectionDAG &DAG,
11470 TargetLowering::DAGCombinerInfo &DCI) {
11471 // BT ignores high bits in the bit index operand.
11472 SDValue Op1 = N->getOperand(1);
11473 if (Op1.hasOneUse()) {
11474 unsigned BitWidth = Op1.getValueSizeInBits();
11475 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
11476 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000011477 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
11478 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000011479 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000011480 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
11481 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
11482 DCI.CommitTargetLoweringOpt(TLO);
11483 }
11484 return SDValue();
11485}
Chris Lattner83e6c992006-10-04 06:57:07 +000011486
Eli Friedman7a5e5552009-06-07 06:52:44 +000011487static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
11488 SDValue Op = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011489 if (Op.getOpcode() == ISD::BITCAST)
Eli Friedman7a5e5552009-06-07 06:52:44 +000011490 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000011491 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000011492 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000011493 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000011494 OpVT.getVectorElementType().getSizeInBits()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011495 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Eli Friedman7a5e5552009-06-07 06:52:44 +000011496 }
11497 return SDValue();
11498}
11499
Evan Cheng2e489c42009-12-16 00:53:11 +000011500static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG) {
11501 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
11502 // (and (i32 x86isd::setcc_carry), 1)
11503 // This eliminates the zext. This transformation is necessary because
11504 // ISD::SETCC is always legalized to i8.
11505 DebugLoc dl = N->getDebugLoc();
11506 SDValue N0 = N->getOperand(0);
11507 EVT VT = N->getValueType(0);
11508 if (N0.getOpcode() == ISD::AND &&
11509 N0.hasOneUse() &&
11510 N0.getOperand(0).hasOneUse()) {
11511 SDValue N00 = N0.getOperand(0);
11512 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
11513 return SDValue();
11514 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
11515 if (!C || C->getZExtValue() != 1)
11516 return SDValue();
11517 return DAG.getNode(ISD::AND, dl, VT,
11518 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
11519 N00.getOperand(0), N00.getOperand(1)),
11520 DAG.getConstant(1, VT));
11521 }
11522
11523 return SDValue();
11524}
11525
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011526// Optimize RES = X86ISD::SETCC CONDCODE, EFLAG_INPUT
11527static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG &DAG) {
11528 unsigned X86CC = N->getConstantOperandVal(0);
11529 SDValue EFLAG = N->getOperand(1);
11530 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011531
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011532 // Materialize "setb reg" as "sbb reg,reg", since it can be extended without
11533 // a zext and produces an all-ones bit which is more useful than 0/1 in some
11534 // cases.
11535 if (X86CC == X86::COND_B)
11536 return DAG.getNode(ISD::AND, DL, MVT::i8,
11537 DAG.getNode(X86ISD::SETCC_CARRY, DL, MVT::i8,
11538 DAG.getConstant(X86CC, MVT::i8), EFLAG),
11539 DAG.getConstant(1, MVT::i8));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011540
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011541 return SDValue();
11542}
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011543
Chris Lattner23a01992010-12-20 01:37:09 +000011544// Optimize RES, EFLAGS = X86ISD::ADC LHS, RHS, EFLAGS
11545static SDValue PerformADCCombine(SDNode *N, SelectionDAG &DAG,
11546 X86TargetLowering::DAGCombinerInfo &DCI) {
11547 // If the LHS and RHS of the ADC node are zero, then it can't overflow and
11548 // the result is either zero or one (depending on the input carry bit).
11549 // Strength reduce this down to a "set on carry" aka SETCC_CARRY&1.
11550 if (X86::isZeroNode(N->getOperand(0)) &&
11551 X86::isZeroNode(N->getOperand(1)) &&
11552 // We don't have a good way to replace an EFLAGS use, so only do this when
11553 // dead right now.
11554 SDValue(N, 1).use_empty()) {
11555 DebugLoc DL = N->getDebugLoc();
11556 EVT VT = N->getValueType(0);
11557 SDValue CarryOut = DAG.getConstant(0, N->getValueType(1));
11558 SDValue Res1 = DAG.getNode(ISD::AND, DL, VT,
11559 DAG.getNode(X86ISD::SETCC_CARRY, DL, VT,
11560 DAG.getConstant(X86::COND_B,MVT::i8),
11561 N->getOperand(2)),
11562 DAG.getConstant(1, VT));
11563 return DCI.CombineTo(N, Res1, CarryOut);
11564 }
11565
11566 return SDValue();
11567}
11568
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000011569// fold (add Y, (sete X, 0)) -> adc 0, Y
11570// (add Y, (setne X, 0)) -> sbb -1, Y
11571// (sub (sete X, 0), Y) -> sbb 0, Y
11572// (sub (setne X, 0), Y) -> adc -1, Y
11573static SDValue OptimizeConditonalInDecrement(SDNode *N, SelectionDAG &DAG) {
11574 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011575
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000011576 // Look through ZExts.
11577 SDValue Ext = N->getOperand(N->getOpcode() == ISD::SUB ? 1 : 0);
11578 if (Ext.getOpcode() != ISD::ZERO_EXTEND || !Ext.hasOneUse())
11579 return SDValue();
11580
11581 SDValue SetCC = Ext.getOperand(0);
11582 if (SetCC.getOpcode() != X86ISD::SETCC || !SetCC.hasOneUse())
11583 return SDValue();
11584
11585 X86::CondCode CC = (X86::CondCode)SetCC.getConstantOperandVal(0);
11586 if (CC != X86::COND_E && CC != X86::COND_NE)
11587 return SDValue();
11588
11589 SDValue Cmp = SetCC.getOperand(1);
11590 if (Cmp.getOpcode() != X86ISD::CMP || !Cmp.hasOneUse() ||
Chris Lattner9cd3da42011-01-16 02:56:53 +000011591 !X86::isZeroNode(Cmp.getOperand(1)) ||
11592 !Cmp.getOperand(0).getValueType().isInteger())
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000011593 return SDValue();
11594
11595 SDValue CmpOp0 = Cmp.getOperand(0);
11596 SDValue NewCmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32, CmpOp0,
11597 DAG.getConstant(1, CmpOp0.getValueType()));
11598
11599 SDValue OtherVal = N->getOperand(N->getOpcode() == ISD::SUB ? 0 : 1);
11600 if (CC == X86::COND_NE)
11601 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::ADC : X86ISD::SBB,
11602 DL, OtherVal.getValueType(), OtherVal,
11603 DAG.getConstant(-1ULL, OtherVal.getValueType()), NewCmp);
11604 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::SBB : X86ISD::ADC,
11605 DL, OtherVal.getValueType(), OtherVal,
11606 DAG.getConstant(0, OtherVal.getValueType()), NewCmp);
11607}
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011608
Dan Gohman475871a2008-07-27 21:46:04 +000011609SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000011610 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000011611 SelectionDAG &DAG = DCI.DAG;
11612 switch (N->getOpcode()) {
11613 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000011614 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011615 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, *this);
Chris Lattneraf723b92008-01-25 05:46:26 +000011616 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +000011617 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000011618 case ISD::ADD:
11619 case ISD::SUB: return OptimizeConditonalInDecrement(N, DAG);
Chris Lattner23a01992010-12-20 01:37:09 +000011620 case X86ISD::ADC: return PerformADCCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000011621 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000011622 case ISD::SHL:
11623 case ISD::SRA:
11624 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Nate Begemanb65c1752010-12-17 22:55:37 +000011625 case ISD::AND: return PerformAndCombine(N, DAG, DCI, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000011626 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000011627 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +000011628 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000011629 case X86ISD::FOR: return PerformFORCombine(N, DAG);
11630 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000011631 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000011632 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Evan Cheng2e489c42009-12-16 00:53:11 +000011633 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG);
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011634 case X86ISD::SETCC: return PerformSETCCCombine(N, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000011635 case X86ISD::SHUFPS: // Handle all target specific shuffles
11636 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +000011637 case X86ISD::PALIGN:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000011638 case X86ISD::PUNPCKHBW:
11639 case X86ISD::PUNPCKHWD:
11640 case X86ISD::PUNPCKHDQ:
11641 case X86ISD::PUNPCKHQDQ:
11642 case X86ISD::UNPCKHPS:
11643 case X86ISD::UNPCKHPD:
11644 case X86ISD::PUNPCKLBW:
11645 case X86ISD::PUNPCKLWD:
11646 case X86ISD::PUNPCKLDQ:
11647 case X86ISD::PUNPCKLQDQ:
11648 case X86ISD::UNPCKLPS:
11649 case X86ISD::UNPCKLPD:
11650 case X86ISD::MOVHLPS:
11651 case X86ISD::MOVLHPS:
11652 case X86ISD::PSHUFD:
11653 case X86ISD::PSHUFHW:
11654 case X86ISD::PSHUFLW:
11655 case X86ISD::MOVSS:
11656 case X86ISD::MOVSD:
Mon P Wanga0fd0d52010-12-19 23:55:53 +000011657 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, DCI);
Evan Cheng206ee9d2006-07-07 08:33:52 +000011658 }
11659
Dan Gohman475871a2008-07-27 21:46:04 +000011660 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000011661}
11662
Evan Chenge5b51ac2010-04-17 06:13:15 +000011663/// isTypeDesirableForOp - Return true if the target has native support for
11664/// the specified value type and it is 'desirable' to use the type for the
11665/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
11666/// instruction encodings are longer and some i16 instructions are slow.
11667bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
11668 if (!isTypeLegal(VT))
11669 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000011670 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000011671 return true;
11672
11673 switch (Opc) {
11674 default:
11675 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000011676 case ISD::LOAD:
11677 case ISD::SIGN_EXTEND:
11678 case ISD::ZERO_EXTEND:
11679 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000011680 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000011681 case ISD::SRL:
11682 case ISD::SUB:
11683 case ISD::ADD:
11684 case ISD::MUL:
11685 case ISD::AND:
11686 case ISD::OR:
11687 case ISD::XOR:
11688 return false;
11689 }
11690}
11691
11692/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000011693/// beneficial for dag combiner to promote the specified node. If true, it
11694/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000011695bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000011696 EVT VT = Op.getValueType();
11697 if (VT != MVT::i16)
11698 return false;
11699
Evan Cheng4c26e932010-04-19 19:29:22 +000011700 bool Promote = false;
11701 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000011702 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000011703 default: break;
11704 case ISD::LOAD: {
11705 LoadSDNode *LD = cast<LoadSDNode>(Op);
11706 // If the non-extending load has a single use and it's not live out, then it
11707 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000011708 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
11709 Op.hasOneUse()*/) {
11710 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
11711 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
11712 // The only case where we'd want to promote LOAD (rather then it being
11713 // promoted as an operand is when it's only use is liveout.
11714 if (UI->getOpcode() != ISD::CopyToReg)
11715 return false;
11716 }
11717 }
Evan Cheng4c26e932010-04-19 19:29:22 +000011718 Promote = true;
11719 break;
11720 }
11721 case ISD::SIGN_EXTEND:
11722 case ISD::ZERO_EXTEND:
11723 case ISD::ANY_EXTEND:
11724 Promote = true;
11725 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000011726 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000011727 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000011728 SDValue N0 = Op.getOperand(0);
11729 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000011730 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000011731 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000011732 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000011733 break;
11734 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000011735 case ISD::ADD:
11736 case ISD::MUL:
11737 case ISD::AND:
11738 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000011739 case ISD::XOR:
11740 Commute = true;
11741 // fallthrough
11742 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000011743 SDValue N0 = Op.getOperand(0);
11744 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000011745 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000011746 return false;
11747 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000011748 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000011749 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000011750 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000011751 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000011752 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000011753 }
11754 }
11755
11756 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000011757 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000011758}
11759
Evan Cheng60c07e12006-07-05 22:17:51 +000011760//===----------------------------------------------------------------------===//
11761// X86 Inline Assembly Support
11762//===----------------------------------------------------------------------===//
11763
Chris Lattnerb8105652009-07-20 17:51:36 +000011764bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
11765 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattnerb8105652009-07-20 17:51:36 +000011766
11767 std::string AsmStr = IA->getAsmString();
11768
11769 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000011770 SmallVector<StringRef, 4> AsmPieces;
Peter Collingbourne98361182010-11-13 19:54:23 +000011771 SplitString(AsmStr, AsmPieces, ";\n");
Chris Lattnerb8105652009-07-20 17:51:36 +000011772
11773 switch (AsmPieces.size()) {
11774 default: return false;
11775 case 1:
11776 AsmStr = AsmPieces[0];
11777 AsmPieces.clear();
11778 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
11779
Evan Cheng55d42002011-01-08 01:24:27 +000011780 // FIXME: this should verify that we are targetting a 486 or better. If not,
11781 // we will turn this bswap into something that will be lowered to logical ops
11782 // instead of emitting the bswap asm. For now, we don't support 486 or lower
11783 // so don't worry about this.
Chris Lattnerb8105652009-07-20 17:51:36 +000011784 // bswap $0
11785 if (AsmPieces.size() == 2 &&
11786 (AsmPieces[0] == "bswap" ||
11787 AsmPieces[0] == "bswapq" ||
11788 AsmPieces[0] == "bswapl") &&
11789 (AsmPieces[1] == "$0" ||
11790 AsmPieces[1] == "${0:q}")) {
11791 // No need to check constraints, nothing other than the equivalent of
11792 // "=r,0" would be valid here.
Evan Cheng55d42002011-01-08 01:24:27 +000011793 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
11794 if (!Ty || Ty->getBitWidth() % 16 != 0)
11795 return false;
11796 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000011797 }
11798 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000011799 if (CI->getType()->isIntegerTy(16) &&
Chris Lattnerb8105652009-07-20 17:51:36 +000011800 AsmPieces.size() == 3 &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000011801 (AsmPieces[0] == "rorw" || AsmPieces[0] == "rolw") &&
Chris Lattnerb8105652009-07-20 17:51:36 +000011802 AsmPieces[1] == "$$8," &&
11803 AsmPieces[2] == "${0:w}" &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000011804 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
11805 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000011806 const std::string &ConstraintsStr = IA->getConstraintString();
11807 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000011808 std::sort(AsmPieces.begin(), AsmPieces.end());
11809 if (AsmPieces.size() == 4 &&
11810 AsmPieces[0] == "~{cc}" &&
11811 AsmPieces[1] == "~{dirflag}" &&
11812 AsmPieces[2] == "~{flags}" &&
11813 AsmPieces[3] == "~{fpsr}") {
Evan Cheng55d42002011-01-08 01:24:27 +000011814 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
11815 if (!Ty || Ty->getBitWidth() % 16 != 0)
11816 return false;
11817 return IntrinsicLowering::LowerToByteSwap(CI);
Dan Gohman0ef701e2010-03-04 19:58:08 +000011818 }
Chris Lattnerb8105652009-07-20 17:51:36 +000011819 }
11820 break;
11821 case 3:
Peter Collingbourne948cf022010-11-13 19:54:30 +000011822 if (CI->getType()->isIntegerTy(32) &&
11823 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
11824 SmallVector<StringRef, 4> Words;
11825 SplitString(AsmPieces[0], Words, " \t,");
11826 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
11827 Words[2] == "${0:w}") {
11828 Words.clear();
11829 SplitString(AsmPieces[1], Words, " \t,");
11830 if (Words.size() == 3 && Words[0] == "rorl" && Words[1] == "$$16" &&
11831 Words[2] == "$0") {
11832 Words.clear();
11833 SplitString(AsmPieces[2], Words, " \t,");
11834 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
11835 Words[2] == "${0:w}") {
11836 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000011837 const std::string &ConstraintsStr = IA->getConstraintString();
11838 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Peter Collingbourne948cf022010-11-13 19:54:30 +000011839 std::sort(AsmPieces.begin(), AsmPieces.end());
11840 if (AsmPieces.size() == 4 &&
11841 AsmPieces[0] == "~{cc}" &&
11842 AsmPieces[1] == "~{dirflag}" &&
11843 AsmPieces[2] == "~{flags}" &&
11844 AsmPieces[3] == "~{fpsr}") {
Evan Cheng55d42002011-01-08 01:24:27 +000011845 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
11846 if (!Ty || Ty->getBitWidth() % 16 != 0)
11847 return false;
11848 return IntrinsicLowering::LowerToByteSwap(CI);
Peter Collingbourne948cf022010-11-13 19:54:30 +000011849 }
11850 }
11851 }
11852 }
11853 }
Evan Cheng55d42002011-01-08 01:24:27 +000011854
11855 if (CI->getType()->isIntegerTy(64)) {
11856 InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
11857 if (Constraints.size() >= 2 &&
11858 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
11859 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
11860 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
11861 SmallVector<StringRef, 4> Words;
11862 SplitString(AsmPieces[0], Words, " \t");
11863 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
Chris Lattnerb8105652009-07-20 17:51:36 +000011864 Words.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000011865 SplitString(AsmPieces[1], Words, " \t");
11866 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
11867 Words.clear();
11868 SplitString(AsmPieces[2], Words, " \t,");
11869 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
11870 Words[2] == "%edx") {
11871 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
11872 if (!Ty || Ty->getBitWidth() % 16 != 0)
11873 return false;
11874 return IntrinsicLowering::LowerToByteSwap(CI);
11875 }
Chris Lattnerb8105652009-07-20 17:51:36 +000011876 }
11877 }
11878 }
11879 }
11880 break;
11881 }
11882 return false;
11883}
11884
11885
11886
Chris Lattnerf4dff842006-07-11 02:54:03 +000011887/// getConstraintType - Given a constraint letter, return the type of
11888/// constraint it is for this target.
11889X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000011890X86TargetLowering::getConstraintType(const std::string &Constraint) const {
11891 if (Constraint.size() == 1) {
11892 switch (Constraint[0]) {
Chris Lattner4234f572007-03-25 02:14:49 +000011893 case 'R':
Chris Lattner4234f572007-03-25 02:14:49 +000011894 case 'q':
11895 case 'Q':
John Thompson44ab89e2010-10-29 17:29:13 +000011896 case 'f':
11897 case 't':
11898 case 'u':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000011899 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +000011900 case 'x':
Chris Lattner4234f572007-03-25 02:14:49 +000011901 case 'Y':
11902 return C_RegisterClass;
John Thompson44ab89e2010-10-29 17:29:13 +000011903 case 'a':
11904 case 'b':
11905 case 'c':
11906 case 'd':
11907 case 'S':
11908 case 'D':
11909 case 'A':
11910 return C_Register;
11911 case 'I':
11912 case 'J':
11913 case 'K':
11914 case 'L':
11915 case 'M':
11916 case 'N':
11917 case 'G':
11918 case 'C':
Dale Johannesen78e3e522009-02-12 20:58:09 +000011919 case 'e':
11920 case 'Z':
11921 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000011922 default:
11923 break;
11924 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000011925 }
Chris Lattner4234f572007-03-25 02:14:49 +000011926 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000011927}
11928
John Thompson44ab89e2010-10-29 17:29:13 +000011929/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +000011930/// This object must already have been set up with the operand type
11931/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +000011932TargetLowering::ConstraintWeight
11933 X86TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +000011934 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +000011935 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +000011936 Value *CallOperandVal = info.CallOperandVal;
11937 // If we don't have a value, we can't do a match,
11938 // but allow it at the lowest weight.
11939 if (CallOperandVal == NULL)
John Thompson44ab89e2010-10-29 17:29:13 +000011940 return CW_Default;
11941 const Type *type = CallOperandVal->getType();
John Thompsoneac6e1d2010-09-13 18:15:37 +000011942 // Look at the constraint type.
11943 switch (*constraint) {
11944 default:
John Thompson44ab89e2010-10-29 17:29:13 +000011945 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
11946 case 'R':
11947 case 'q':
11948 case 'Q':
11949 case 'a':
11950 case 'b':
11951 case 'c':
11952 case 'd':
11953 case 'S':
11954 case 'D':
11955 case 'A':
11956 if (CallOperandVal->getType()->isIntegerTy())
11957 weight = CW_SpecificReg;
11958 break;
11959 case 'f':
11960 case 't':
11961 case 'u':
11962 if (type->isFloatingPointTy())
11963 weight = CW_SpecificReg;
11964 break;
11965 case 'y':
Chris Lattner2a786eb2010-12-19 20:19:20 +000011966 if (type->isX86_MMXTy() && Subtarget->hasMMX())
John Thompson44ab89e2010-10-29 17:29:13 +000011967 weight = CW_SpecificReg;
11968 break;
11969 case 'x':
11970 case 'Y':
Nate Begeman2ea8ee72010-12-10 00:26:57 +000011971 if ((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasXMM())
John Thompson44ab89e2010-10-29 17:29:13 +000011972 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +000011973 break;
11974 case 'I':
11975 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
11976 if (C->getZExtValue() <= 31)
John Thompson44ab89e2010-10-29 17:29:13 +000011977 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +000011978 }
11979 break;
John Thompson44ab89e2010-10-29 17:29:13 +000011980 case 'J':
11981 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
11982 if (C->getZExtValue() <= 63)
11983 weight = CW_Constant;
11984 }
11985 break;
11986 case 'K':
11987 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
11988 if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f))
11989 weight = CW_Constant;
11990 }
11991 break;
11992 case 'L':
11993 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
11994 if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff))
11995 weight = CW_Constant;
11996 }
11997 break;
11998 case 'M':
11999 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12000 if (C->getZExtValue() <= 3)
12001 weight = CW_Constant;
12002 }
12003 break;
12004 case 'N':
12005 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12006 if (C->getZExtValue() <= 0xff)
12007 weight = CW_Constant;
12008 }
12009 break;
12010 case 'G':
12011 case 'C':
12012 if (dyn_cast<ConstantFP>(CallOperandVal)) {
12013 weight = CW_Constant;
12014 }
12015 break;
12016 case 'e':
12017 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12018 if ((C->getSExtValue() >= -0x80000000LL) &&
12019 (C->getSExtValue() <= 0x7fffffffLL))
12020 weight = CW_Constant;
12021 }
12022 break;
12023 case 'Z':
12024 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12025 if (C->getZExtValue() <= 0xffffffff)
12026 weight = CW_Constant;
12027 }
12028 break;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012029 }
12030 return weight;
12031}
12032
Dale Johannesenba2a0b92008-01-29 02:21:21 +000012033/// LowerXConstraint - try to replace an X constraint, which matches anything,
12034/// with another that has more specific requirements based on the type of the
12035/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000012036const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000012037LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000012038 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
12039 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000012040 if (ConstraintVT.isFloatingPoint()) {
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012041 if (Subtarget->hasXMMInt())
Chris Lattner5e764232008-04-26 23:02:14 +000012042 return "Y";
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012043 if (Subtarget->hasXMM())
Chris Lattner5e764232008-04-26 23:02:14 +000012044 return "x";
12045 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012046
Chris Lattner5e764232008-04-26 23:02:14 +000012047 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000012048}
12049
Chris Lattner48884cd2007-08-25 00:47:38 +000012050/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
12051/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000012052void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattner48884cd2007-08-25 00:47:38 +000012053 char Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000012054 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000012055 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000012056 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000012057
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012058 switch (Constraint) {
12059 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000012060 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000012061 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000012062 if (C->getZExtValue() <= 31) {
12063 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000012064 break;
12065 }
Devang Patel84f7fd22007-03-17 00:13:28 +000012066 }
Chris Lattner48884cd2007-08-25 00:47:38 +000012067 return;
Evan Cheng364091e2008-09-22 23:57:37 +000012068 case 'J':
12069 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000012070 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000012071 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
12072 break;
12073 }
12074 }
12075 return;
12076 case 'K':
12077 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000012078 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +000012079 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
12080 break;
12081 }
12082 }
12083 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000012084 case 'N':
12085 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000012086 if (C->getZExtValue() <= 255) {
12087 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000012088 break;
12089 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000012090 }
Chris Lattner48884cd2007-08-25 00:47:38 +000012091 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000012092 case 'e': {
12093 // 32-bit signed value
12094 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000012095 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
12096 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000012097 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000012098 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000012099 break;
12100 }
12101 // FIXME gcc accepts some relocatable values here too, but only in certain
12102 // memory models; it's complicated.
12103 }
12104 return;
12105 }
12106 case 'Z': {
12107 // 32-bit unsigned value
12108 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000012109 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
12110 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000012111 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
12112 break;
12113 }
12114 }
12115 // FIXME gcc accepts some relocatable values here too, but only in certain
12116 // memory models; it's complicated.
12117 return;
12118 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000012119 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012120 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000012121 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000012122 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000012123 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000012124 break;
12125 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012126
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000012127 // In any sort of PIC mode addresses need to be computed at runtime by
12128 // adding in a register or some sort of table lookup. These can't
12129 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000012130 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000012131 return;
12132
Chris Lattnerdc43a882007-05-03 16:52:29 +000012133 // If we are in non-pic codegen mode, we allow the address of a global (with
12134 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000012135 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000012136 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000012137
Chris Lattner49921962009-05-08 18:23:14 +000012138 // Match either (GA), (GA+C), (GA+C1+C2), etc.
12139 while (1) {
12140 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
12141 Offset += GA->getOffset();
12142 break;
12143 } else if (Op.getOpcode() == ISD::ADD) {
12144 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
12145 Offset += C->getZExtValue();
12146 Op = Op.getOperand(0);
12147 continue;
12148 }
12149 } else if (Op.getOpcode() == ISD::SUB) {
12150 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
12151 Offset += -C->getZExtValue();
12152 Op = Op.getOperand(0);
12153 continue;
12154 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000012155 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000012156
Chris Lattner49921962009-05-08 18:23:14 +000012157 // Otherwise, this isn't something we can handle, reject it.
12158 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000012159 }
Eric Christopherfd179292009-08-27 18:07:15 +000012160
Dan Gohman46510a72010-04-15 01:51:59 +000012161 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000012162 // If we require an extra load to get this address, as in PIC mode, we
12163 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000012164 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
12165 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000012166 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000012167
Devang Patel0d881da2010-07-06 22:08:15 +000012168 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
12169 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000012170 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012171 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000012172 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012173
Gabor Greifba36cb52008-08-28 21:40:38 +000012174 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000012175 Ops.push_back(Result);
12176 return;
12177 }
Dale Johannesen1784d162010-06-25 21:55:36 +000012178 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012179}
12180
Chris Lattner259e97c2006-01-31 19:43:35 +000012181std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +000012182getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000012183 EVT VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +000012184 if (Constraint.size() == 1) {
12185 // FIXME: not handling fp-stack yet!
Chris Lattner259e97c2006-01-31 19:43:35 +000012186 switch (Constraint[0]) { // GCC X86 Constraint Letters
Chris Lattnerf4dff842006-07-11 02:54:03 +000012187 default: break; // Unknown constraint letter
Evan Cheng47e9fab2009-07-17 22:13:25 +000012188 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
12189 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000012190 if (VT == MVT::i32)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012191 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX,
12192 X86::ESI, X86::EDI, X86::R8D, X86::R9D,
12193 X86::R10D,X86::R11D,X86::R12D,
12194 X86::R13D,X86::R14D,X86::R15D,
12195 X86::EBP, X86::ESP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012196 else if (VT == MVT::i16)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012197 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX,
12198 X86::SI, X86::DI, X86::R8W,X86::R9W,
12199 X86::R10W,X86::R11W,X86::R12W,
12200 X86::R13W,X86::R14W,X86::R15W,
12201 X86::BP, X86::SP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012202 else if (VT == MVT::i8)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012203 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL,
12204 X86::SIL, X86::DIL, X86::R8B,X86::R9B,
12205 X86::R10B,X86::R11B,X86::R12B,
12206 X86::R13B,X86::R14B,X86::R15B,
12207 X86::BPL, X86::SPL, 0);
12208
Owen Anderson825b72b2009-08-11 20:47:22 +000012209 else if (VT == MVT::i64)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012210 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX,
12211 X86::RSI, X86::RDI, X86::R8, X86::R9,
12212 X86::R10, X86::R11, X86::R12,
12213 X86::R13, X86::R14, X86::R15,
12214 X86::RBP, X86::RSP, 0);
12215
12216 break;
12217 }
Eric Christopherfd179292009-08-27 18:07:15 +000012218 // 32-bit fallthrough
Chris Lattner259e97c2006-01-31 19:43:35 +000012219 case 'Q': // Q_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +000012220 if (VT == MVT::i32)
Chris Lattner80a7ecc2006-05-06 00:29:37 +000012221 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012222 else if (VT == MVT::i16)
Chris Lattner80a7ecc2006-05-06 00:29:37 +000012223 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012224 else if (VT == MVT::i8)
Evan Cheng12914382007-08-13 23:27:11 +000012225 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012226 else if (VT == MVT::i64)
Chris Lattner03e6c702007-11-04 06:51:12 +000012227 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
12228 break;
Chris Lattner259e97c2006-01-31 19:43:35 +000012229 }
12230 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012231
Chris Lattner1efa40f2006-02-22 00:56:39 +000012232 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +000012233}
Chris Lattnerf76d1802006-07-31 23:26:50 +000012234
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012235std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000012236X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000012237 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000012238 // First, see if this is a constraint that directly corresponds to an LLVM
12239 // register class.
12240 if (Constraint.size() == 1) {
12241 // GCC Constraint Letters
12242 switch (Constraint[0]) {
12243 default: break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000012244 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000012245 case 'l': // INDEX_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +000012246 if (VT == MVT::i8)
Chris Lattner0f65cad2007-04-09 05:49:22 +000012247 return std::make_pair(0U, X86::GR8RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012248 if (VT == MVT::i16)
Chris Lattner1fa71982008-10-17 18:15:05 +000012249 return std::make_pair(0U, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012250 if (VT == MVT::i32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +000012251 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +000012252 return std::make_pair(0U, X86::GR64RegisterClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000012253 case 'R': // LEGACY_REGS
12254 if (VT == MVT::i8)
12255 return std::make_pair(0U, X86::GR8_NOREXRegisterClass);
12256 if (VT == MVT::i16)
12257 return std::make_pair(0U, X86::GR16_NOREXRegisterClass);
12258 if (VT == MVT::i32 || !Subtarget->is64Bit())
12259 return std::make_pair(0U, X86::GR32_NOREXRegisterClass);
12260 return std::make_pair(0U, X86::GR64_NOREXRegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000012261 case 'f': // FP Stack registers.
12262 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
12263 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000012264 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000012265 return std::make_pair(0U, X86::RFP32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012266 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000012267 return std::make_pair(0U, X86::RFP64RegisterClass);
12268 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000012269 case 'y': // MMX_REGS if MMX allowed.
12270 if (!Subtarget->hasMMX()) break;
12271 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000012272 case 'Y': // SSE_REGS if SSE2 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012273 if (!Subtarget->hasXMMInt()) break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000012274 // FALL THROUGH.
12275 case 'x': // SSE_REGS if SSE1 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012276 if (!Subtarget->hasXMM()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000012277
Owen Anderson825b72b2009-08-11 20:47:22 +000012278 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000012279 default: break;
12280 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000012281 case MVT::f32:
12282 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +000012283 return std::make_pair(0U, X86::FR32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012284 case MVT::f64:
12285 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +000012286 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000012287 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000012288 case MVT::v16i8:
12289 case MVT::v8i16:
12290 case MVT::v4i32:
12291 case MVT::v2i64:
12292 case MVT::v4f32:
12293 case MVT::v2f64:
Chris Lattner0f65cad2007-04-09 05:49:22 +000012294 return std::make_pair(0U, X86::VR128RegisterClass);
12295 }
Chris Lattnerad043e82007-04-09 05:11:28 +000012296 break;
12297 }
12298 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012299
Chris Lattnerf76d1802006-07-31 23:26:50 +000012300 // Use the default implementation in TargetLowering to convert the register
12301 // constraint into a member of a register class.
12302 std::pair<unsigned, const TargetRegisterClass*> Res;
12303 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000012304
12305 // Not found as a standard register?
12306 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000012307 // Map st(0) -> st(7) -> ST0
12308 if (Constraint.size() == 7 && Constraint[0] == '{' &&
12309 tolower(Constraint[1]) == 's' &&
12310 tolower(Constraint[2]) == 't' &&
12311 Constraint[3] == '(' &&
12312 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
12313 Constraint[5] == ')' &&
12314 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000012315
Chris Lattner56d77c72009-09-13 22:41:48 +000012316 Res.first = X86::ST0+Constraint[4]-'0';
12317 Res.second = X86::RFP80RegisterClass;
12318 return Res;
12319 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000012320
Chris Lattner56d77c72009-09-13 22:41:48 +000012321 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000012322 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000012323 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +000012324 Res.second = X86::RFP80RegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000012325 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000012326 }
Chris Lattner56d77c72009-09-13 22:41:48 +000012327
12328 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000012329 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000012330 Res.first = X86::EFLAGS;
12331 Res.second = X86::CCRRegisterClass;
12332 return Res;
12333 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000012334
Dale Johannesen330169f2008-11-13 21:52:36 +000012335 // 'A' means EAX + EDX.
12336 if (Constraint == "A") {
12337 Res.first = X86::EAX;
Dan Gohman68a31c22009-07-30 17:02:08 +000012338 Res.second = X86::GR32_ADRegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000012339 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000012340 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000012341 return Res;
12342 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012343
Chris Lattnerf76d1802006-07-31 23:26:50 +000012344 // Otherwise, check to see if this is a register class of the wrong value
12345 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
12346 // turn into {ax},{dx}.
12347 if (Res.second->hasType(VT))
12348 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012349
Chris Lattnerf76d1802006-07-31 23:26:50 +000012350 // All of the single-register GCC register classes map their values onto
12351 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
12352 // really want an 8-bit or 32-bit register, map to the appropriate register
12353 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +000012354 if (Res.second == X86::GR16RegisterClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000012355 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000012356 unsigned DestReg = 0;
12357 switch (Res.first) {
12358 default: break;
12359 case X86::AX: DestReg = X86::AL; break;
12360 case X86::DX: DestReg = X86::DL; break;
12361 case X86::CX: DestReg = X86::CL; break;
12362 case X86::BX: DestReg = X86::BL; break;
12363 }
12364 if (DestReg) {
12365 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000012366 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000012367 }
Owen Anderson825b72b2009-08-11 20:47:22 +000012368 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000012369 unsigned DestReg = 0;
12370 switch (Res.first) {
12371 default: break;
12372 case X86::AX: DestReg = X86::EAX; break;
12373 case X86::DX: DestReg = X86::EDX; break;
12374 case X86::CX: DestReg = X86::ECX; break;
12375 case X86::BX: DestReg = X86::EBX; break;
12376 case X86::SI: DestReg = X86::ESI; break;
12377 case X86::DI: DestReg = X86::EDI; break;
12378 case X86::BP: DestReg = X86::EBP; break;
12379 case X86::SP: DestReg = X86::ESP; break;
12380 }
12381 if (DestReg) {
12382 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000012383 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000012384 }
Owen Anderson825b72b2009-08-11 20:47:22 +000012385 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000012386 unsigned DestReg = 0;
12387 switch (Res.first) {
12388 default: break;
12389 case X86::AX: DestReg = X86::RAX; break;
12390 case X86::DX: DestReg = X86::RDX; break;
12391 case X86::CX: DestReg = X86::RCX; break;
12392 case X86::BX: DestReg = X86::RBX; break;
12393 case X86::SI: DestReg = X86::RSI; break;
12394 case X86::DI: DestReg = X86::RDI; break;
12395 case X86::BP: DestReg = X86::RBP; break;
12396 case X86::SP: DestReg = X86::RSP; break;
12397 }
12398 if (DestReg) {
12399 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000012400 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000012401 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000012402 }
Chris Lattner6ba50a92008-08-26 06:19:02 +000012403 } else if (Res.second == X86::FR32RegisterClass ||
12404 Res.second == X86::FR64RegisterClass ||
12405 Res.second == X86::VR128RegisterClass) {
12406 // Handle references to XMM physical registers that got mapped into the
12407 // wrong class. This can happen with constraints like {xmm0} where the
12408 // target independent register mapper will just pick the first match it can
12409 // find, ignoring the required type.
Owen Anderson825b72b2009-08-11 20:47:22 +000012410 if (VT == MVT::f32)
Chris Lattner6ba50a92008-08-26 06:19:02 +000012411 Res.second = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +000012412 else if (VT == MVT::f64)
Chris Lattner6ba50a92008-08-26 06:19:02 +000012413 Res.second = X86::FR64RegisterClass;
12414 else if (X86::VR128RegisterClass->hasType(VT))
12415 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000012416 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012417
Chris Lattnerf76d1802006-07-31 23:26:50 +000012418 return Res;
12419}