blob: d519531816212d215ff702e883b96b8d62983028 [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);
834 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
835 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
836 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
837 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
838 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
839 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +0000840 setOperationAction(ISD::BUILD_VECTOR, MVT::v8f32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000841 //setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8f32, Custom);
842 //setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8f32, Custom);
843 //setOperationAction(ISD::SELECT, MVT::v8f32, Custom);
844 //setOperationAction(ISD::VSETCC, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000845
846 // Operations to consider commented out -v16i16 v32i8
Owen Anderson825b72b2009-08-11 20:47:22 +0000847 //setOperationAction(ISD::ADD, MVT::v16i16, Legal);
848 setOperationAction(ISD::ADD, MVT::v8i32, Custom);
849 setOperationAction(ISD::ADD, MVT::v4i64, Custom);
850 //setOperationAction(ISD::SUB, MVT::v32i8, Legal);
851 //setOperationAction(ISD::SUB, MVT::v16i16, Legal);
852 setOperationAction(ISD::SUB, MVT::v8i32, Custom);
853 setOperationAction(ISD::SUB, MVT::v4i64, Custom);
854 //setOperationAction(ISD::MUL, MVT::v16i16, Legal);
855 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
856 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
857 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
858 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
859 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
860 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000861
Owen Anderson825b72b2009-08-11 20:47:22 +0000862 setOperationAction(ISD::VSETCC, MVT::v4f64, Custom);
863 // setOperationAction(ISD::VSETCC, MVT::v32i8, Custom);
864 // setOperationAction(ISD::VSETCC, MVT::v16i16, Custom);
865 setOperationAction(ISD::VSETCC, MVT::v8i32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000866
Owen Anderson825b72b2009-08-11 20:47:22 +0000867 // setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v32i8, Custom);
868 // setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i16, Custom);
869 // setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i16, Custom);
870 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i32, Custom);
871 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000872
Owen Anderson825b72b2009-08-11 20:47:22 +0000873 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f64, Custom);
874 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i64, Custom);
875 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f64, Custom);
876 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i64, Custom);
877 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f64, Custom);
878 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000879
880#if 0
881 // Not sure we want to do this since there are no 256-bit integer
882 // operations in AVX
883
884 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
885 // This includes 256-bit vectors
Owen Anderson825b72b2009-08-11 20:47:22 +0000886 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v4i64; ++i) {
887 EVT VT = (MVT::SimpleValueType)i;
David Greene9b9838d2009-06-29 16:47:10 +0000888
889 // Do not attempt to custom lower non-power-of-2 vectors
890 if (!isPowerOf2_32(VT.getVectorNumElements()))
891 continue;
892
893 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
894 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
895 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
896 }
897
898 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000899 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i64, Custom);
900 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i64, Custom);
Eric Christopherfd179292009-08-27 18:07:15 +0000901 }
David Greene9b9838d2009-06-29 16:47:10 +0000902#endif
903
904#if 0
905 // Not sure we want to do this since there are no 256-bit integer
906 // operations in AVX
907
908 // Promote v32i8, v16i16, v8i32 load, select, and, or, xor to v4i64.
909 // Including 256-bit vectors
Owen Anderson825b72b2009-08-11 20:47:22 +0000910 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v4i64; i++) {
911 EVT VT = (MVT::SimpleValueType)i;
David Greene9b9838d2009-06-29 16:47:10 +0000912
913 if (!VT.is256BitVector()) {
914 continue;
915 }
916 setOperationAction(ISD::AND, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000917 AddPromotedToType (ISD::AND, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000918 setOperationAction(ISD::OR, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000919 AddPromotedToType (ISD::OR, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000920 setOperationAction(ISD::XOR, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000921 AddPromotedToType (ISD::XOR, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000922 setOperationAction(ISD::LOAD, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000923 AddPromotedToType (ISD::LOAD, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000924 setOperationAction(ISD::SELECT, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000925 AddPromotedToType (ISD::SELECT, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000926 }
927
Owen Anderson825b72b2009-08-11 20:47:22 +0000928 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
David Greene9b9838d2009-06-29 16:47:10 +0000929#endif
930 }
931
Evan Cheng6be2c582006-04-05 23:38:46 +0000932 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000933 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +0000934
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000935
Eli Friedman962f5492010-06-02 19:35:46 +0000936 // Only custom-lower 64-bit SADDO and friends on 64-bit because we don't
937 // handle type legalization for these operations here.
Dan Gohman71c62a22010-06-02 19:13:40 +0000938 //
Eli Friedman962f5492010-06-02 19:35:46 +0000939 // FIXME: We really should do custom legalization for addition and
940 // subtraction on x86-32 once PR3203 is fixed. We really can't do much better
941 // than generic legalization for 64-bit multiplication-with-overflow, though.
Chris Lattnera34b3cf2010-12-19 20:03:11 +0000942 for (unsigned i = 0, e = 3+Subtarget->is64Bit(); i != e; ++i) {
943 // Add/Sub/Mul with overflow operations are custom lowered.
944 MVT VT = IntVTs[i];
945 setOperationAction(ISD::SADDO, VT, Custom);
946 setOperationAction(ISD::UADDO, VT, Custom);
947 setOperationAction(ISD::SSUBO, VT, Custom);
948 setOperationAction(ISD::USUBO, VT, Custom);
949 setOperationAction(ISD::SMULO, VT, Custom);
950 setOperationAction(ISD::UMULO, VT, Custom);
Eli Friedmana993f0a2010-06-02 00:27:18 +0000951 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000952
Chris Lattnera34b3cf2010-12-19 20:03:11 +0000953 // There are no 8-bit 3-address imul/mul instructions
954 setOperationAction(ISD::SMULO, MVT::i8, Expand);
955 setOperationAction(ISD::UMULO, MVT::i8, Expand);
Bill Wendling41ea7e72008-11-24 19:21:46 +0000956
Evan Chengd54f2d52009-03-31 19:38:51 +0000957 if (!Subtarget->is64Bit()) {
958 // These libcalls are not available in 32-bit.
959 setLibcallName(RTLIB::SHL_I128, 0);
960 setLibcallName(RTLIB::SRL_I128, 0);
961 setLibcallName(RTLIB::SRA_I128, 0);
962 }
963
Evan Cheng206ee9d2006-07-07 08:33:52 +0000964 // We have target-specific dag combine patterns for the following nodes:
965 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Dan Gohman1bbf72b2010-03-15 23:23:03 +0000966 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Evan Chengd880b972008-05-09 21:53:03 +0000967 setTargetDAGCombine(ISD::BUILD_VECTOR);
Chris Lattner83e6c992006-10-04 06:57:07 +0000968 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +0000969 setTargetDAGCombine(ISD::SHL);
970 setTargetDAGCombine(ISD::SRA);
971 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +0000972 setTargetDAGCombine(ISD::OR);
Nate Begemanb65c1752010-12-17 22:55:37 +0000973 setTargetDAGCombine(ISD::AND);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +0000974 setTargetDAGCombine(ISD::ADD);
975 setTargetDAGCombine(ISD::SUB);
Chris Lattner149a4e52008-02-22 02:09:43 +0000976 setTargetDAGCombine(ISD::STORE);
Evan Cheng2e489c42009-12-16 00:53:11 +0000977 setTargetDAGCombine(ISD::ZERO_EXTEND);
Evan Cheng0b0cd912009-03-28 05:57:29 +0000978 if (Subtarget->is64Bit())
979 setTargetDAGCombine(ISD::MUL);
Evan Cheng206ee9d2006-07-07 08:33:52 +0000980
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000981 computeRegisterProperties();
982
Evan Cheng05219282011-01-06 06:52:41 +0000983 // On Darwin, -Os means optimize for size without hurting performance,
984 // do not reduce the limit.
Dan Gohman87060f52008-06-30 21:00:56 +0000985 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +0000986 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 16 : 8;
Evan Cheng255f20f2010-04-01 06:04:33 +0000987 maxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +0000988 maxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
989 maxStoresPerMemmove = 8; // For @llvm.memmove -> sequence of stores
990 maxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Evan Chengfb8075d2008-02-28 00:43:03 +0000991 setPrefLoopAlignment(16);
Evan Cheng6ebf7bc2009-05-13 21:42:09 +0000992 benefitFromCodePlacementOpt = true;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000993}
994
Scott Michel5b8f82e2008-03-10 15:42:14 +0000995
Owen Anderson825b72b2009-08-11 20:47:22 +0000996MVT::SimpleValueType X86TargetLowering::getSetCCResultType(EVT VT) const {
997 return MVT::i8;
Scott Michel5b8f82e2008-03-10 15:42:14 +0000998}
999
1000
Evan Cheng29286502008-01-23 23:17:41 +00001001/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1002/// the desired ByVal argument alignment.
1003static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
1004 if (MaxAlign == 16)
1005 return;
1006 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
1007 if (VTy->getBitWidth() == 128)
1008 MaxAlign = 16;
Evan Cheng29286502008-01-23 23:17:41 +00001009 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
1010 unsigned EltAlign = 0;
1011 getMaxByValAlign(ATy->getElementType(), EltAlign);
1012 if (EltAlign > MaxAlign)
1013 MaxAlign = EltAlign;
1014 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
1015 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1016 unsigned EltAlign = 0;
1017 getMaxByValAlign(STy->getElementType(i), EltAlign);
1018 if (EltAlign > MaxAlign)
1019 MaxAlign = EltAlign;
1020 if (MaxAlign == 16)
1021 break;
1022 }
1023 }
1024 return;
1025}
1026
1027/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1028/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001029/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1030/// are at 4-byte boundaries.
Evan Cheng29286502008-01-23 23:17:41 +00001031unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001032 if (Subtarget->is64Bit()) {
1033 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001034 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001035 if (TyAlign > 8)
1036 return TyAlign;
1037 return 8;
1038 }
1039
Evan Cheng29286502008-01-23 23:17:41 +00001040 unsigned Align = 4;
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001041 if (Subtarget->hasXMM())
Dale Johannesen0c191872008-02-08 19:48:20 +00001042 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001043 return Align;
1044}
Chris Lattner2b02a442007-02-25 08:29:00 +00001045
Evan Chengf0df0312008-05-15 08:39:06 +00001046/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengc3b0c342010-04-08 07:37:57 +00001047/// and store operations as a result of memset, memcpy, and memmove
1048/// lowering. If DstAlign is zero that means it's safe to destination
1049/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1050/// means there isn't a need to check it against alignment requirement,
1051/// probably because the source does not need to be loaded. If
1052/// 'NonScalarIntSafe' is true, that means it's safe to return a
1053/// non-scalar-integer type, e.g. empty string source, constant, or loaded
1054/// from memory. 'MemcpyStrSrc' indicates whether the memcpy source is
1055/// constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001056/// It returns EVT::Other if the type should be determined using generic
1057/// target-independent logic.
Owen Andersone50ed302009-08-10 22:56:29 +00001058EVT
Evan Cheng255f20f2010-04-01 06:04:33 +00001059X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1060 unsigned DstAlign, unsigned SrcAlign,
Evan Chengf28f8bc2010-04-02 19:36:14 +00001061 bool NonScalarIntSafe,
Evan Chengc3b0c342010-04-08 07:37:57 +00001062 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00001063 MachineFunction &MF) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001064 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
1065 // linux. This is because the stack realignment code can't handle certain
1066 // cases like PR2962. This should be removed when PR2962 is fixed.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001067 const Function *F = MF.getFunction();
Evan Chenga5e13622011-01-07 19:35:30 +00001068 if (NonScalarIntSafe &&
1069 !F->hasFnAttr(Attribute::NoImplicitFloat)) {
Evan Cheng255f20f2010-04-01 06:04:33 +00001070 if (Size >= 16 &&
Evan Chenga5e13622011-01-07 19:35:30 +00001071 (Subtarget->isUnalignedMemAccessFast() ||
1072 ((DstAlign == 0 || DstAlign >= 16) &&
1073 (SrcAlign == 0 || SrcAlign >= 16))) &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001074 Subtarget->getStackAlignment() >= 16) {
1075 if (Subtarget->hasSSE2())
1076 return MVT::v4i32;
Evan Chengf28f8bc2010-04-02 19:36:14 +00001077 if (Subtarget->hasSSE1())
Evan Cheng255f20f2010-04-01 06:04:33 +00001078 return MVT::v4f32;
Evan Chengc3b0c342010-04-08 07:37:57 +00001079 } else if (!MemcpyStrSrc && Size >= 8 &&
Evan Cheng3ea97552010-04-01 20:27:45 +00001080 !Subtarget->is64Bit() &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001081 Subtarget->getStackAlignment() >= 8 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001082 Subtarget->hasXMMInt()) {
Evan Chengc3b0c342010-04-08 07:37:57 +00001083 // Do not use f64 to lower memcpy if source is string constant. It's
1084 // better to use i32 to avoid the loads.
Evan Cheng255f20f2010-04-01 06:04:33 +00001085 return MVT::f64;
Evan Chengc3b0c342010-04-08 07:37:57 +00001086 }
Chris Lattner4002a1b2008-10-28 05:49:35 +00001087 }
Evan Chengf0df0312008-05-15 08:39:06 +00001088 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001089 return MVT::i64;
1090 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001091}
1092
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001093/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1094/// current function. The returned value is a member of the
1095/// MachineJumpTableInfo::JTEntryKind enum.
1096unsigned X86TargetLowering::getJumpTableEncoding() const {
1097 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1098 // symbol.
1099 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1100 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001101 return MachineJumpTableInfo::EK_Custom32;
Michael J. Spencerec38de22010-10-10 22:04:20 +00001102
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001103 // Otherwise, use the normal jump table encoding heuristics.
1104 return TargetLowering::getJumpTableEncoding();
1105}
1106
Chris Lattnerc64daab2010-01-26 05:02:42 +00001107const MCExpr *
1108X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1109 const MachineBasicBlock *MBB,
1110 unsigned uid,MCContext &Ctx) const{
1111 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1112 Subtarget->isPICStyleGOT());
1113 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1114 // entries.
Daniel Dunbar4e815f82010-03-15 23:51:06 +00001115 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1116 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001117}
1118
Evan Chengcc415862007-11-09 01:32:10 +00001119/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1120/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001121SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001122 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001123 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001124 // This doesn't have DebugLoc associated with it, but is not really the
1125 // same as a Register.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001126 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001127 return Table;
1128}
1129
Chris Lattner589c6f62010-01-26 06:28:43 +00001130/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1131/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1132/// MCExpr.
1133const MCExpr *X86TargetLowering::
1134getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1135 MCContext &Ctx) const {
1136 // X86-64 uses RIP relative addressing based on the jump table label.
1137 if (Subtarget->isPICStyleRIPRel())
1138 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1139
1140 // Otherwise, the reference is relative to the PIC base.
Chris Lattner142b5312010-11-14 22:48:15 +00001141 return MCSymbolRefExpr::Create(MF->getPICBaseSymbol(), Ctx);
Chris Lattner589c6f62010-01-26 06:28:43 +00001142}
1143
Bill Wendlingb4202b82009-07-01 18:50:55 +00001144/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +00001145unsigned X86TargetLowering::getFunctionAlignment(const Function *F) const {
Dan Gohman25103a22009-08-18 00:20:06 +00001146 return F->hasFnAttr(Attribute::OptimizeForSize) ? 0 : 4;
Bill Wendling20c568f2009-06-30 22:38:32 +00001147}
1148
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001149// FIXME: Why this routine is here? Move to RegInfo!
Evan Chengdee81012010-07-26 21:50:05 +00001150std::pair<const TargetRegisterClass*, uint8_t>
1151X86TargetLowering::findRepresentativeClass(EVT VT) const{
1152 const TargetRegisterClass *RRC = 0;
1153 uint8_t Cost = 1;
1154 switch (VT.getSimpleVT().SimpleTy) {
1155 default:
1156 return TargetLowering::findRepresentativeClass(VT);
1157 case MVT::i8: case MVT::i16: case MVT::i32: case MVT::i64:
1158 RRC = (Subtarget->is64Bit()
1159 ? X86::GR64RegisterClass : X86::GR32RegisterClass);
1160 break;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001161 case MVT::x86mmx:
Evan Chengdee81012010-07-26 21:50:05 +00001162 RRC = X86::VR64RegisterClass;
1163 break;
1164 case MVT::f32: case MVT::f64:
1165 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1166 case MVT::v4f32: case MVT::v2f64:
1167 case MVT::v32i8: case MVT::v8i32: case MVT::v4i64: case MVT::v8f32:
1168 case MVT::v4f64:
1169 RRC = X86::VR128RegisterClass;
1170 break;
1171 }
1172 return std::make_pair(RRC, Cost);
1173}
1174
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001175// FIXME: Why this routine is here? Move to RegInfo!
Evan Cheng70017e42010-07-24 00:39:05 +00001176unsigned
1177X86TargetLowering::getRegPressureLimit(const TargetRegisterClass *RC,
1178 MachineFunction &MF) const {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001179 const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
Anton Korobeynikovd0c38172010-11-18 21:19:35 +00001180
1181 unsigned FPDiff = TFI->hasFP(MF) ? 1 : 0;
Evan Cheng70017e42010-07-24 00:39:05 +00001182 switch (RC->getID()) {
1183 default:
1184 return 0;
1185 case X86::GR32RegClassID:
1186 return 4 - FPDiff;
1187 case X86::GR64RegClassID:
1188 return 8 - FPDiff;
1189 case X86::VR128RegClassID:
1190 return Subtarget->is64Bit() ? 10 : 4;
1191 case X86::VR64RegClassID:
1192 return 4;
1193 }
1194}
1195
Eric Christopherf7a0c7b2010-07-06 05:18:56 +00001196bool X86TargetLowering::getStackCookieLocation(unsigned &AddressSpace,
1197 unsigned &Offset) const {
1198 if (!Subtarget->isTargetLinux())
1199 return false;
1200
1201 if (Subtarget->is64Bit()) {
1202 // %fs:0x28, unless we're using a Kernel code model, in which case it's %gs:
1203 Offset = 0x28;
1204 if (getTargetMachine().getCodeModel() == CodeModel::Kernel)
1205 AddressSpace = 256;
1206 else
1207 AddressSpace = 257;
1208 } else {
1209 // %gs:0x14 on i386
1210 Offset = 0x14;
1211 AddressSpace = 256;
1212 }
1213 return true;
1214}
1215
1216
Chris Lattner2b02a442007-02-25 08:29:00 +00001217//===----------------------------------------------------------------------===//
1218// Return Value Calling Convention Implementation
1219//===----------------------------------------------------------------------===//
1220
Chris Lattner59ed56b2007-02-28 04:55:35 +00001221#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001222
Michael J. Spencerec38de22010-10-10 22:04:20 +00001223bool
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001224X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv, bool isVarArg,
Dan Gohman84023e02010-07-10 09:00:22 +00001225 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001226 LLVMContext &Context) const {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001227 SmallVector<CCValAssign, 16> RVLocs;
1228 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001229 RVLocs, Context);
Dan Gohman84023e02010-07-10 09:00:22 +00001230 return CCInfo.CheckReturn(Outs, RetCC_X86);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001231}
1232
Dan Gohman98ca4f22009-08-05 01:29:28 +00001233SDValue
1234X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001235 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001236 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001237 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001238 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001239 MachineFunction &MF = DAG.getMachineFunction();
1240 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001241
Chris Lattner9774c912007-02-27 05:28:59 +00001242 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001243 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1244 RVLocs, *DAG.getContext());
1245 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001246
Evan Chengdcea1632010-02-04 02:40:39 +00001247 // Add the regs to the liveout set for the function.
1248 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1249 for (unsigned i = 0; i != RVLocs.size(); ++i)
1250 if (RVLocs[i].isRegLoc() && !MRI.isLiveOut(RVLocs[i].getLocReg()))
1251 MRI.addLiveOut(RVLocs[i].getLocReg());
Scott Michelfdc40a02009-02-17 22:15:04 +00001252
Dan Gohman475871a2008-07-27 21:46:04 +00001253 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001254
Dan Gohman475871a2008-07-27 21:46:04 +00001255 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001256 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1257 // Operand #1 = Bytes To Pop
Dan Gohman1e93df62010-04-17 14:41:14 +00001258 RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1259 MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001260
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001261 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001262 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1263 CCValAssign &VA = RVLocs[i];
1264 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohmanc9403652010-07-07 15:54:55 +00001265 SDValue ValToCopy = OutVals[i];
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001266 EVT ValVT = ValToCopy.getValueType();
1267
Dale Johannesenc4510512010-09-24 19:05:48 +00001268 // If this is x86-64, and we disabled SSE, we can't return FP values,
1269 // or SSE or MMX vectors.
1270 if ((ValVT == MVT::f32 || ValVT == MVT::f64 ||
1271 VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001272 (Subtarget->is64Bit() && !Subtarget->hasXMM())) {
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001273 report_fatal_error("SSE register return with SSE disabled");
1274 }
1275 // Likewise we can't return F64 values with SSE1 only. gcc does so, but
1276 // llvm-gcc has never done it right and no one has noticed, so this
1277 // should be OK for now.
1278 if (ValVT == MVT::f64 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001279 (Subtarget->is64Bit() && !Subtarget->hasXMMInt()))
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001280 report_fatal_error("SSE2 register return with SSE2 disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001281
Chris Lattner447ff682008-03-11 03:23:40 +00001282 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1283 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001284 if (VA.getLocReg() == X86::ST0 ||
1285 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001286 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1287 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001288 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001289 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001290 RetOps.push_back(ValToCopy);
1291 // Don't emit a copytoreg.
1292 continue;
1293 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001294
Evan Cheng242b38b2009-02-23 09:03:22 +00001295 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1296 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001297 if (Subtarget->is64Bit()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001298 if (ValVT == MVT::x86mmx) {
Chris Lattner97a2a562010-08-26 05:24:29 +00001299 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001300 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ValToCopy);
Eric Christopher90eb4022010-07-22 00:26:08 +00001301 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
1302 ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001303 // If we don't have SSE2 available, convert to v4f32 so the generated
1304 // register is legal.
1305 if (!Subtarget->hasSSE2())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001306 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32,ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001307 }
Evan Cheng242b38b2009-02-23 09:03:22 +00001308 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001309 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00001310
Dale Johannesendd64c412009-02-04 00:33:20 +00001311 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001312 Flag = Chain.getValue(1);
1313 }
Dan Gohman61a92132008-04-21 23:59:07 +00001314
1315 // The x86-64 ABI for returning structs by value requires that we copy
1316 // the sret argument into %rax for the return. We saved the argument into
1317 // a virtual register in the entry block, so now we copy the value out
1318 // and into %rax.
1319 if (Subtarget->is64Bit() &&
1320 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1321 MachineFunction &MF = DAG.getMachineFunction();
1322 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1323 unsigned Reg = FuncInfo->getSRetReturnReg();
Michael J. Spencerec38de22010-10-10 22:04:20 +00001324 assert(Reg &&
Zhongxing Xuc2798a12010-05-26 08:10:02 +00001325 "SRetReturnReg should have been set in LowerFormalArguments().");
Dale Johannesendd64c412009-02-04 00:33:20 +00001326 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001327
Dale Johannesendd64c412009-02-04 00:33:20 +00001328 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001329 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001330
1331 // RAX now acts like a return value.
Evan Chengdcea1632010-02-04 02:40:39 +00001332 MRI.addLiveOut(X86::RAX);
Dan Gohman61a92132008-04-21 23:59:07 +00001333 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001334
Chris Lattner447ff682008-03-11 03:23:40 +00001335 RetOps[0] = Chain; // Update chain.
1336
1337 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001338 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001339 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001340
1341 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001342 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001343}
1344
Evan Cheng3d2125c2010-11-30 23:55:39 +00001345bool X86TargetLowering::isUsedByReturnOnly(SDNode *N) const {
1346 if (N->getNumValues() != 1)
1347 return false;
1348 if (!N->hasNUsesOfValue(1, 0))
1349 return false;
1350
1351 SDNode *Copy = *N->use_begin();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001352 if (Copy->getOpcode() != ISD::CopyToReg &&
1353 Copy->getOpcode() != ISD::FP_EXTEND)
Evan Cheng3d2125c2010-11-30 23:55:39 +00001354 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001355
1356 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001357 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001358 UI != UE; ++UI) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001359 if (UI->getOpcode() != X86ISD::RET_FLAG)
1360 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001361 HasRet = true;
1362 }
Evan Cheng3d2125c2010-11-30 23:55:39 +00001363
Evan Cheng1bf891a2010-12-01 22:59:46 +00001364 return HasRet;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001365}
1366
Dan Gohman98ca4f22009-08-05 01:29:28 +00001367/// LowerCallResult - Lower the result values of a call into the
1368/// appropriate copies out of appropriate physical registers.
1369///
1370SDValue
1371X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001372 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001373 const SmallVectorImpl<ISD::InputArg> &Ins,
1374 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001375 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001376
Chris Lattnere32bbf62007-02-28 07:09:55 +00001377 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001378 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001379 bool Is64Bit = Subtarget->is64Bit();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001380 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +00001381 RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001382 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001383
Chris Lattner3085e152007-02-25 08:59:22 +00001384 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001385 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001386 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001387 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001388
Torok Edwin3f142c32009-02-01 18:15:56 +00001389 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001390 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001391 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasXMM())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001392 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001393 }
1394
Evan Cheng79fb3b42009-02-20 20:43:02 +00001395 SDValue Val;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001396
1397 // If this is a call to a function that returns an fp value on the floating
1398 // point stack, we must guarantee the the value is popped from the stack, so
1399 // a CopyFromReg is not good enough - the copy instruction may be eliminated
1400 // if the return value is not used. We use the FpGET_ST0 instructions
1401 // instead.
1402 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1) {
1403 // If we prefer to use the value in xmm registers, copy it out as f80 and
1404 // use a truncate to move it from fp stack reg to xmm reg.
1405 if (isScalarFPTypeInSSEReg(VA.getValVT())) CopyVT = MVT::f80;
1406 bool isST0 = VA.getLocReg() == X86::ST0;
1407 unsigned Opc = 0;
1408 if (CopyVT == MVT::f32) Opc = isST0 ? X86::FpGET_ST0_32:X86::FpGET_ST1_32;
1409 if (CopyVT == MVT::f64) Opc = isST0 ? X86::FpGET_ST0_64:X86::FpGET_ST1_64;
1410 if (CopyVT == MVT::f80) Opc = isST0 ? X86::FpGET_ST0_80:X86::FpGET_ST1_80;
1411 SDValue Ops[] = { Chain, InFlag };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001412 Chain = SDValue(DAG.getMachineNode(Opc, dl, CopyVT, MVT::Other, MVT::Glue,
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001413 Ops, 2), 1);
1414 Val = Chain.getValue(0);
1415
1416 // Round the f80 to the right size, which also moves it to the appropriate
1417 // xmm register.
1418 if (CopyVT != VA.getValVT())
1419 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
1420 // This truncation won't change the value.
1421 DAG.getIntPtrConstant(1));
1422 } else if (Is64Bit && CopyVT.isVector() && CopyVT.getSizeInBits() == 64) {
Evan Cheng242b38b2009-02-23 09:03:22 +00001423 // For x86-64, MMX values are returned in XMM0 / XMM1 except for v1i64.
1424 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
1425 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001426 MVT::v2i64, InFlag).getValue(1);
Evan Cheng242b38b2009-02-23 09:03:22 +00001427 Val = Chain.getValue(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00001428 Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64,
1429 Val, DAG.getConstant(0, MVT::i64));
Evan Cheng242b38b2009-02-23 09:03:22 +00001430 } else {
1431 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001432 MVT::i64, InFlag).getValue(1);
Evan Cheng242b38b2009-02-23 09:03:22 +00001433 Val = Chain.getValue(0);
1434 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001435 Val = DAG.getNode(ISD::BITCAST, dl, CopyVT, Val);
Evan Cheng79fb3b42009-02-20 20:43:02 +00001436 } else {
1437 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1438 CopyVT, InFlag).getValue(1);
1439 Val = Chain.getValue(0);
1440 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001441 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001442 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001443 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001444
Dan Gohman98ca4f22009-08-05 01:29:28 +00001445 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001446}
1447
1448
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001449//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001450// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001451//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001452// StdCall calling convention seems to be standard for many Windows' API
1453// routines and around. It differs from C calling convention just a little:
1454// callee should clean up the stack, not caller. Symbols should be also
1455// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001456// For info on fast calling convention see Fast Calling Convention (tail call)
1457// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001458
Dan Gohman98ca4f22009-08-05 01:29:28 +00001459/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001460/// semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001461static bool CallIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
1462 if (Outs.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001463 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001464
Dan Gohman98ca4f22009-08-05 01:29:28 +00001465 return Outs[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001466}
1467
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001468/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001469/// return semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001470static bool
1471ArgsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
1472 if (Ins.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001473 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001474
Dan Gohman98ca4f22009-08-05 01:29:28 +00001475 return Ins[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001476}
1477
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001478/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1479/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001480/// the specific parameter attribute. The copy will be passed as a byval
1481/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001482static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001483CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001484 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1485 DebugLoc dl) {
Chris Lattnere72f2022010-09-21 05:40:29 +00001486 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Michael J. Spencerec38de22010-10-10 22:04:20 +00001487
Dale Johannesendd64c412009-02-04 00:33:20 +00001488 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Mon P Wang20adc9d2010-04-04 03:10:48 +00001489 /*isVolatile*/false, /*AlwaysInline=*/true,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001490 MachinePointerInfo(), MachinePointerInfo());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001491}
1492
Chris Lattner29689432010-03-11 00:22:57 +00001493/// IsTailCallConvention - Return true if the calling convention is one that
1494/// supports tail call optimization.
1495static bool IsTailCallConvention(CallingConv::ID CC) {
1496 return (CC == CallingConv::Fast || CC == CallingConv::GHC);
1497}
1498
Evan Cheng0c439eb2010-01-27 00:07:07 +00001499/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1500/// a tailcall target by changing its ABI.
1501static bool FuncIsMadeTailCallSafe(CallingConv::ID CC) {
Chris Lattner29689432010-03-11 00:22:57 +00001502 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001503}
1504
Dan Gohman98ca4f22009-08-05 01:29:28 +00001505SDValue
1506X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001507 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001508 const SmallVectorImpl<ISD::InputArg> &Ins,
1509 DebugLoc dl, SelectionDAG &DAG,
1510 const CCValAssign &VA,
1511 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00001512 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00001513 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001514 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Evan Cheng0c439eb2010-01-27 00:07:07 +00001515 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001516 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001517 EVT ValVT;
1518
1519 // If value is passed by pointer we have address passed instead of the value
1520 // itself.
1521 if (VA.getLocInfo() == CCValAssign::Indirect)
1522 ValVT = VA.getLocVT();
1523 else
1524 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001525
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001526 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001527 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001528 // In case of tail call optimization mark all arguments mutable. Since they
1529 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001530 if (Flags.isByVal()) {
1531 int FI = MFI->CreateFixedObject(Flags.getByValSize(),
Evan Chenged2ae132010-07-03 00:40:23 +00001532 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001533 return DAG.getFrameIndex(FI, getPointerTy());
1534 } else {
1535 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00001536 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001537 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1538 return DAG.getLoad(ValVT, dl, Chain, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001539 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00001540 false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001541 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001542}
1543
Dan Gohman475871a2008-07-27 21:46:04 +00001544SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001545X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001546 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001547 bool isVarArg,
1548 const SmallVectorImpl<ISD::InputArg> &Ins,
1549 DebugLoc dl,
1550 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001551 SmallVectorImpl<SDValue> &InVals)
1552 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00001553 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001554 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001555
Gordon Henriksen86737662008-01-05 16:56:59 +00001556 const Function* Fn = MF.getFunction();
1557 if (Fn->hasExternalLinkage() &&
1558 Subtarget->isTargetCygMing() &&
1559 Fn->getName() == "main")
1560 FuncInfo->setForceFramePointer(true);
1561
Evan Cheng1bc78042006-04-26 01:20:17 +00001562 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001563 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001564 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001565
Chris Lattner29689432010-03-11 00:22:57 +00001566 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1567 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001568
Chris Lattner638402b2007-02-28 07:00:42 +00001569 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001570 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001571 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1572 ArgLocs, *DAG.getContext());
Duncan Sands45907662010-10-31 13:21:44 +00001573 CCInfo.AnalyzeFormalArguments(Ins, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001574
Chris Lattnerf39f7712007-02-28 05:46:49 +00001575 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001576 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001577 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1578 CCValAssign &VA = ArgLocs[i];
1579 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1580 // places.
1581 assert(VA.getValNo() != LastVal &&
1582 "Don't support value assigned to multiple locs yet");
1583 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001584
Chris Lattnerf39f7712007-02-28 05:46:49 +00001585 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001586 EVT RegVT = VA.getLocVT();
Devang Patel8a84e442009-01-05 17:31:22 +00001587 TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001588 if (RegVT == MVT::i32)
Chris Lattnerf39f7712007-02-28 05:46:49 +00001589 RC = X86::GR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001590 else if (Is64Bit && RegVT == MVT::i64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001591 RC = X86::GR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001592 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001593 RC = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001594 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001595 RC = X86::FR64RegisterClass;
Bruno Cardoso Lopesac098352010-08-05 23:35:51 +00001596 else if (RegVT.isVector() && RegVT.getSizeInBits() == 256)
1597 RC = X86::VR256RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001598 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001599 RC = X86::VR128RegisterClass;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001600 else if (RegVT == MVT::x86mmx)
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001601 RC = X86::VR64RegisterClass;
1602 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001603 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001604
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001605 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001606 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001607
Chris Lattnerf39f7712007-02-28 05:46:49 +00001608 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1609 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1610 // right size.
1611 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001612 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001613 DAG.getValueType(VA.getValVT()));
1614 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001615 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001616 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001617 else if (VA.getLocInfo() == CCValAssign::BCvt)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001618 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001619
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001620 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001621 // Handle MMX values passed in XMM regs.
1622 if (RegVT.isVector()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001623 ArgValue = DAG.getNode(X86ISD::MOVDQ2Q, dl, VA.getValVT(),
1624 ArgValue);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001625 } else
1626 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001627 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001628 } else {
1629 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001630 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001631 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001632
1633 // If value is passed via pointer - do a load.
1634 if (VA.getLocInfo() == CCValAssign::Indirect)
Chris Lattner51abfe42010-09-21 06:02:19 +00001635 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue,
1636 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001637
Dan Gohman98ca4f22009-08-05 01:29:28 +00001638 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001639 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001640
Dan Gohman61a92132008-04-21 23:59:07 +00001641 // The x86-64 ABI for returning structs by value requires that we copy
1642 // the sret argument into %rax for the return. Save the argument into
1643 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001644 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001645 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1646 unsigned Reg = FuncInfo->getSRetReturnReg();
1647 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001648 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001649 FuncInfo->setSRetReturnReg(Reg);
1650 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001651 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001652 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001653 }
1654
Chris Lattnerf39f7712007-02-28 05:46:49 +00001655 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00001656 // Align stack specially for tail calls.
1657 if (FuncIsMadeTailCallSafe(CallConv))
Gordon Henriksenae636f82008-01-03 16:47:34 +00001658 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001659
Evan Cheng1bc78042006-04-26 01:20:17 +00001660 // If the function takes variable number of arguments, make a frame index for
1661 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001662 if (isVarArg) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001663 if (!IsWin64 && (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
1664 CallConv != CallingConv::X86_ThisCall))) {
Jakob Stoklund Olesenb2eeed72010-07-29 17:42:27 +00001665 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,true));
Gordon Henriksen86737662008-01-05 16:56:59 +00001666 }
1667 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001668 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1669
1670 // FIXME: We should really autogenerate these arrays
1671 static const unsigned GPR64ArgRegsWin64[] = {
1672 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001673 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001674 static const unsigned GPR64ArgRegs64Bit[] = {
1675 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1676 };
1677 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001678 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1679 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1680 };
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001681 const unsigned *GPR64ArgRegs;
1682 unsigned NumXMMRegs = 0;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001683
1684 if (IsWin64) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001685 // The XMM registers which might contain var arg parameters are shadowed
1686 // in their paired GPR. So we only need to save the GPR to their home
1687 // slots.
1688 TotalNumIntRegs = 4;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001689 GPR64ArgRegs = GPR64ArgRegsWin64;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001690 } else {
1691 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1692 GPR64ArgRegs = GPR64ArgRegs64Bit;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001693
1694 NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs64Bit, TotalNumXMMRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001695 }
1696 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1697 TotalNumIntRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001698
Devang Patel578efa92009-06-05 21:57:13 +00001699 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001700 assert(!(NumXMMRegs && !Subtarget->hasXMM()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001701 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001702 assert(!(NumXMMRegs && UseSoftFloat && NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00001703 "SSE register cannot be used when SSE is disabled!");
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001704 if (UseSoftFloat || NoImplicitFloatOps || !Subtarget->hasXMM())
Torok Edwin3f142c32009-02-01 18:15:56 +00001705 // Kernel mode asks for SSE to be disabled, so don't push them
1706 // on the stack.
1707 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001708
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001709 if (IsWin64) {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001710 const TargetFrameLowering &TFI = *getTargetMachine().getFrameLowering();
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001711 // Get to the caller-allocated home save location. Add 8 to account
1712 // for the return address.
1713 int HomeOffset = TFI.getOffsetOfLocalArea() + 8;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001714 FuncInfo->setRegSaveFrameIndex(
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001715 MFI->CreateFixedObject(1, NumIntRegs * 8 + HomeOffset, false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001716 FuncInfo->setVarArgsFrameIndex(FuncInfo->getRegSaveFrameIndex());
1717 } else {
1718 // For X86-64, if there are vararg parameters that are passed via
1719 // registers, then we must store them to their spots on the stack so they
1720 // may be loaded by deferencing the result of va_next.
1721 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
1722 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
1723 FuncInfo->setRegSaveFrameIndex(
1724 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
Dan Gohman1e93df62010-04-17 14:41:14 +00001725 false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001726 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001727
Gordon Henriksen86737662008-01-05 16:56:59 +00001728 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001729 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00001730 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
1731 getPointerTy());
1732 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001733 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00001734 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
1735 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00001736 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
1737 X86::GR64RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00001738 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00001739 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001740 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001741 MachinePointerInfo::getFixedStack(
1742 FuncInfo->getRegSaveFrameIndex(), Offset),
1743 false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001744 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001745 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001746 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001747
Dan Gohmanface41a2009-08-16 21:24:25 +00001748 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
1749 // Now store the XMM (fp + vector) parameter registers.
1750 SmallVector<SDValue, 11> SaveXMMOps;
1751 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001752
Dan Gohmanface41a2009-08-16 21:24:25 +00001753 unsigned AL = MF.addLiveIn(X86::AL, X86::GR8RegisterClass);
1754 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
1755 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001756
Dan Gohman1e93df62010-04-17 14:41:14 +00001757 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1758 FuncInfo->getRegSaveFrameIndex()));
1759 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1760 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00001761
Dan Gohmanface41a2009-08-16 21:24:25 +00001762 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001763 unsigned VReg = MF.addLiveIn(XMMArgRegs64Bit[NumXMMRegs],
Dan Gohmanface41a2009-08-16 21:24:25 +00001764 X86::VR128RegisterClass);
1765 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
1766 SaveXMMOps.push_back(Val);
1767 }
1768 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
1769 MVT::Other,
1770 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00001771 }
Dan Gohmanface41a2009-08-16 21:24:25 +00001772
1773 if (!MemOps.empty())
1774 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1775 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001776 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001777 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001778
Gordon Henriksen86737662008-01-05 16:56:59 +00001779 // Some CCs need callee pop.
Dan Gohman4d3d6e12010-05-27 18:43:40 +00001780 if (Subtarget->IsCalleePop(isVarArg, CallConv)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001781 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001782 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00001783 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001784 // If this is an sret function, the return should pop the hidden pointer.
Chris Lattner29689432010-03-11 00:22:57 +00001785 if (!Is64Bit && !IsTailCallConvention(CallConv) && ArgsAreStructReturn(Ins))
Dan Gohman1e93df62010-04-17 14:41:14 +00001786 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001787 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001788
Gordon Henriksen86737662008-01-05 16:56:59 +00001789 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001790 // RegSaveFrameIndex is X86-64 only.
1791 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Anton Korobeynikovded05e32010-05-16 09:08:45 +00001792 if (CallConv == CallingConv::X86_FastCall ||
1793 CallConv == CallingConv::X86_ThisCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00001794 // fastcc functions can't have varargs.
1795 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00001796 }
Evan Cheng25caf632006-05-23 21:06:34 +00001797
Dan Gohman98ca4f22009-08-05 01:29:28 +00001798 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001799}
1800
Dan Gohman475871a2008-07-27 21:46:04 +00001801SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001802X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
1803 SDValue StackPtr, SDValue Arg,
1804 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00001805 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001806 ISD::ArgFlagsTy Flags) const {
Anton Korobeynikovc7c62bb2010-09-02 22:31:32 +00001807 const unsigned FirstStackArgOffset = (Subtarget->isTargetWin64() ? 32 : 0);
1808 unsigned LocMemOffset = FirstStackArgOffset + VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00001809 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00001810 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001811 if (Flags.isByVal())
Dale Johannesendd64c412009-02-04 00:33:20 +00001812 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001813
1814 return DAG.getStore(Chain, dl, Arg, PtrOff,
1815 MachinePointerInfo::getStack(LocMemOffset),
David Greene67c9d422010-02-15 16:53:33 +00001816 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00001817}
1818
Bill Wendling64e87322009-01-16 19:25:27 +00001819/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001820/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00001821SDValue
1822X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00001823 SDValue &OutRetAddr, SDValue Chain,
1824 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00001825 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001826 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00001827 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001828 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00001829
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001830 // Load the "old" Return address.
Chris Lattner51abfe42010-09-21 06:02:19 +00001831 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, MachinePointerInfo(),
1832 false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001833 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001834}
1835
1836/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1837/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00001838static SDValue
1839EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00001840 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00001841 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001842 // Store the return address to the appropriate stack slot.
1843 if (!FPDiff) return Chain;
1844 // Calculate the new stack slot for the return address.
1845 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00001846 int NewReturnAddrFI =
Evan Chenged2ae132010-07-03 00:40:23 +00001847 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false);
Owen Anderson825b72b2009-08-11 20:47:22 +00001848 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00001849 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001850 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00001851 MachinePointerInfo::getFixedStack(NewReturnAddrFI),
David Greene67c9d422010-02-15 16:53:33 +00001852 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001853 return Chain;
1854}
1855
Dan Gohman98ca4f22009-08-05 01:29:28 +00001856SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001857X86TargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001858 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001859 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001860 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001861 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001862 const SmallVectorImpl<ISD::InputArg> &Ins,
1863 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001864 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001865 MachineFunction &MF = DAG.getMachineFunction();
1866 bool Is64Bit = Subtarget->is64Bit();
1867 bool IsStructRet = CallIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00001868 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001869
Evan Cheng5f941932010-02-05 02:21:12 +00001870 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00001871 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00001872 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1873 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001874 Outs, OutVals, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00001875
1876 // Sibcalls are automatically detected tailcalls which do not require
1877 // ABI changes.
Dan Gohman1797ed52010-02-08 20:27:50 +00001878 if (!GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00001879 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00001880
1881 if (isTailCall)
1882 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00001883 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00001884
Chris Lattner29689432010-03-11 00:22:57 +00001885 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1886 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001887
Chris Lattner638402b2007-02-28 07:00:42 +00001888 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00001889 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001890 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1891 ArgLocs, *DAG.getContext());
Duncan Sands45907662010-10-31 13:21:44 +00001892 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001893
Chris Lattner423c5f42007-02-28 05:31:48 +00001894 // Get a count of how many bytes are to be pushed on the stack.
1895 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00001896 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00001897 // This is a sibcall. The memory operands are available in caller's
1898 // own caller's stack.
1899 NumBytes = 0;
Chris Lattner29689432010-03-11 00:22:57 +00001900 else if (GuaranteedTailCallOpt && IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00001901 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001902
Gordon Henriksen86737662008-01-05 16:56:59 +00001903 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00001904 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001905 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00001906 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00001907 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1908 FPDiff = NumBytesCallerPushed - NumBytes;
1909
1910 // Set the delta of movement of the returnaddr stackslot.
1911 // But only set if delta is greater than previous delta.
1912 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1913 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1914 }
1915
Evan Chengf22f9b32010-02-06 03:28:46 +00001916 if (!IsSibcall)
1917 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001918
Dan Gohman475871a2008-07-27 21:46:04 +00001919 SDValue RetAddrFrIdx;
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001920 // Load return adress for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00001921 if (isTailCall && FPDiff)
1922 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
1923 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00001924
Dan Gohman475871a2008-07-27 21:46:04 +00001925 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
1926 SmallVector<SDValue, 8> MemOpChains;
1927 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00001928
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001929 // Walk the register/memloc assignments, inserting copies/loads. In the case
1930 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00001931 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1932 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001933 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00001934 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00001935 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00001936 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00001937
Chris Lattner423c5f42007-02-28 05:31:48 +00001938 // Promote the value if needed.
1939 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001940 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00001941 case CCValAssign::Full: break;
1942 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001943 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001944 break;
1945 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001946 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001947 break;
1948 case CCValAssign::AExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001949 if (RegVT.isVector() && RegVT.getSizeInBits() == 128) {
1950 // Special case: passing MMX values in XMM registers.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001951 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00001952 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
1953 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001954 } else
1955 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
1956 break;
1957 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001958 Arg = DAG.getNode(ISD::BITCAST, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001959 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001960 case CCValAssign::Indirect: {
1961 // Store the argument.
1962 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00001963 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001964 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00001965 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00001966 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001967 Arg = SpillSlot;
1968 break;
1969 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00001970 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001971
Chris Lattner423c5f42007-02-28 05:31:48 +00001972 if (VA.isRegLoc()) {
1973 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00001974 if (isVarArg && Subtarget->isTargetWin64()) {
1975 // Win64 ABI requires argument XMM reg to be copied to the corresponding
1976 // shadow reg if callee is a varargs function.
1977 unsigned ShadowReg = 0;
1978 switch (VA.getLocReg()) {
1979 case X86::XMM0: ShadowReg = X86::RCX; break;
1980 case X86::XMM1: ShadowReg = X86::RDX; break;
1981 case X86::XMM2: ShadowReg = X86::R8; break;
1982 case X86::XMM3: ShadowReg = X86::R9; break;
1983 }
1984 if (ShadowReg)
1985 RegsToPass.push_back(std::make_pair(ShadowReg, Arg));
1986 }
Evan Chengf22f9b32010-02-06 03:28:46 +00001987 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00001988 assert(VA.isMemLoc());
1989 if (StackPtr.getNode() == 0)
1990 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
1991 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1992 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001993 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001994 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001995
Evan Cheng32fe1032006-05-25 00:59:30 +00001996 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001997 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001998 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001999
Evan Cheng347d5f72006-04-28 21:29:37 +00002000 // Build a sequence of copy-to-reg nodes chained together with token chain
2001 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00002002 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002003 // Tail call byval lowering might overwrite argument registers so in case of
2004 // tail call optimization the copies to registers are lowered later.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002005 if (!isTailCall)
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002006 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002007 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002008 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002009 InFlag = Chain.getValue(1);
2010 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002011
Chris Lattner88e1fd52009-07-09 04:24:46 +00002012 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002013 // ELF / PIC requires GOT in the EBX register before function calls via PLT
2014 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002015 if (!isTailCall) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002016 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
2017 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00002018 DebugLoc(), getPointerTy()),
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002019 InFlag);
2020 InFlag = Chain.getValue(1);
2021 } else {
2022 // If we are tail calling and generating PIC/GOT style code load the
2023 // address of the callee into ECX. The value in ecx is used as target of
2024 // the tail jump. This is done to circumvent the ebx/callee-saved problem
2025 // for tail calls on PIC/GOT architectures. Normally we would just put the
2026 // address of GOT into ebx and then call target@PLT. But for tail calls
2027 // ebx would be restored (since ebx is callee saved) before jumping to the
2028 // target@PLT.
2029
2030 // Note: The actual moving to ECX is done further down.
2031 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
2032 if (G && !G->getGlobal()->hasHiddenVisibility() &&
2033 !G->getGlobal()->hasProtectedVisibility())
2034 Callee = LowerGlobalAddress(Callee, DAG);
2035 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00002036 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002037 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002038 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002039
Nate Begemanc8ea6732010-07-21 20:49:52 +00002040 if (Is64Bit && isVarArg && !Subtarget->isTargetWin64()) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002041 // From AMD64 ABI document:
2042 // For calls that may call functions that use varargs or stdargs
2043 // (prototype-less calls or calls to functions containing ellipsis (...) in
2044 // the declaration) %al is used as hidden argument to specify the number
2045 // of SSE registers used. The contents of %al do not need to match exactly
2046 // the number of registers, but must be an ubound on the number of SSE
2047 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002048
Gordon Henriksen86737662008-01-05 16:56:59 +00002049 // Count the number of XMM registers allocated.
2050 static const unsigned XMMArgRegs[] = {
2051 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2052 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2053 };
2054 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00002055 assert((Subtarget->hasXMM() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00002056 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00002057
Dale Johannesendd64c412009-02-04 00:33:20 +00002058 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Owen Anderson825b72b2009-08-11 20:47:22 +00002059 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002060 InFlag = Chain.getValue(1);
2061 }
2062
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002063
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002064 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002065 if (isTailCall) {
2066 // Force all the incoming stack arguments to be loaded from the stack
2067 // before any new outgoing arguments are stored to the stack, because the
2068 // outgoing stack slots may alias the incoming argument stack slots, and
2069 // the alias isn't otherwise explicit. This is slightly more conservative
2070 // than necessary, because it means that each store effectively depends
2071 // on every argument instead of just those arguments it would clobber.
2072 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2073
Dan Gohman475871a2008-07-27 21:46:04 +00002074 SmallVector<SDValue, 8> MemOpChains2;
2075 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002076 int FI = 0;
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002077 // Do not flag preceeding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00002078 InFlag = SDValue();
Dan Gohman1797ed52010-02-08 20:27:50 +00002079 if (GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002080 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2081 CCValAssign &VA = ArgLocs[i];
2082 if (VA.isRegLoc())
2083 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002084 assert(VA.isMemLoc());
Dan Gohmanc9403652010-07-07 15:54:55 +00002085 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002086 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002087 // Create frame index.
2088 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002089 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002090 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002091 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002092
Duncan Sands276dcbd2008-03-21 09:14:45 +00002093 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002094 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002095 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002096 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00002097 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00002098 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002099 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002100
Dan Gohman98ca4f22009-08-05 01:29:28 +00002101 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2102 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002103 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002104 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002105 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002106 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002107 DAG.getStore(ArgChain, dl, Arg, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002108 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002109 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002110 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002111 }
2112 }
2113
2114 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002115 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002116 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002117
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002118 // Copy arguments to their registers.
2119 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002120 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002121 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002122 InFlag = Chain.getValue(1);
2123 }
Dan Gohman475871a2008-07-27 21:46:04 +00002124 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002125
Gordon Henriksen86737662008-01-05 16:56:59 +00002126 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002127 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00002128 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002129 }
2130
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002131 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2132 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2133 // In the 64-bit large code model, we have to make all calls
2134 // through a register, since the call instruction's 32-bit
2135 // pc-relative offset may not be large enough to hold the whole
2136 // address.
2137 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002138 // If the callee is a GlobalAddress node (quite common, every direct call
2139 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2140 // it.
2141
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002142 // We should use extra load for direct calls to dllimported functions in
2143 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002144 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002145 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002146 unsigned char OpFlags = 0;
Eric Christopherfd179292009-08-27 18:07:15 +00002147
Chris Lattner48a7d022009-07-09 05:02:21 +00002148 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2149 // external symbols most go through the PLT in PIC mode. If the symbol
2150 // has hidden or protected visibility, or if it is static or local, then
2151 // we don't need to use the PLT - we can directly call it.
2152 if (Subtarget->isTargetELF() &&
2153 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002154 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002155 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002156 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner80945782010-09-27 06:34:01 +00002157 (GV->isDeclaration() || GV->isWeakForLinker()) &&
2158 Subtarget->getDarwinVers() < 9) {
Chris Lattner74e726e2009-07-09 05:27:35 +00002159 // PC-relative references to external symbols should go through $stub,
2160 // unless we're building with the leopard linker or later, which
2161 // automatically synthesizes these stubs.
2162 OpFlags = X86II::MO_DARWIN_STUB;
2163 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002164
Devang Patel0d881da2010-07-06 22:08:15 +00002165 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002166 G->getOffset(), OpFlags);
2167 }
Bill Wendling056292f2008-09-16 21:48:12 +00002168 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002169 unsigned char OpFlags = 0;
2170
Evan Cheng1bf891a2010-12-01 22:59:46 +00002171 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to
2172 // external symbols should go through the PLT.
2173 if (Subtarget->isTargetELF() &&
2174 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
2175 OpFlags = X86II::MO_PLT;
2176 } else if (Subtarget->isPICStyleStubAny() &&
2177 Subtarget->getDarwinVers() < 9) {
2178 // PC-relative references to external symbols should go through $stub,
2179 // unless we're building with the leopard linker or later, which
2180 // automatically synthesizes these stubs.
2181 OpFlags = X86II::MO_DARWIN_STUB;
Chris Lattner74e726e2009-07-09 05:27:35 +00002182 }
Eric Christopherfd179292009-08-27 18:07:15 +00002183
Chris Lattner48a7d022009-07-09 05:02:21 +00002184 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2185 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002186 }
2187
Chris Lattnerd96d0722007-02-25 06:40:16 +00002188 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002189 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002190 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002191
Evan Chengf22f9b32010-02-06 03:28:46 +00002192 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002193 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2194 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002195 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002196 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002197
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002198 Ops.push_back(Chain);
2199 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002200
Dan Gohman98ca4f22009-08-05 01:29:28 +00002201 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002202 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002203
Gordon Henriksen86737662008-01-05 16:56:59 +00002204 // Add argument registers to the end of the list so that they are known live
2205 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002206 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2207 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2208 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002209
Evan Cheng586ccac2008-03-18 23:36:35 +00002210 // Add an implicit use GOT pointer in EBX.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002211 if (!isTailCall && Subtarget->isPICStyleGOT())
Evan Cheng586ccac2008-03-18 23:36:35 +00002212 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
2213
Anton Korobeynikov3a1e54a2010-08-17 21:06:07 +00002214 // Add an implicit use of AL for non-Windows x86 64-bit vararg functions.
2215 if (Is64Bit && isVarArg && !Subtarget->isTargetWin64())
Owen Anderson825b72b2009-08-11 20:47:22 +00002216 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
Evan Cheng586ccac2008-03-18 23:36:35 +00002217
Gabor Greifba36cb52008-08-28 21:40:38 +00002218 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002219 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002220
Dan Gohman98ca4f22009-08-05 01:29:28 +00002221 if (isTailCall) {
Dale Johannesen88004c22010-06-05 00:30:45 +00002222 // We used to do:
2223 //// If this is the first return lowered for this function, add the regs
2224 //// to the liveout set for the function.
2225 // This isn't right, although it's probably harmless on x86; liveouts
2226 // should be computed from returns not tail calls. Consider a void
2227 // function making a tail call to a function returning int.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002228 return DAG.getNode(X86ISD::TC_RETURN, dl,
2229 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002230 }
2231
Dale Johannesenace16102009-02-03 19:33:06 +00002232 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002233 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002234
Chris Lattner2d297092006-05-23 18:50:38 +00002235 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002236 unsigned NumBytesForCalleeToPush;
Dan Gohman4d3d6e12010-05-27 18:43:40 +00002237 if (Subtarget->IsCalleePop(isVarArg, CallConv))
Gordon Henriksen86737662008-01-05 16:56:59 +00002238 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Chris Lattner29689432010-03-11 00:22:57 +00002239 else if (!Is64Bit && !IsTailCallConvention(CallConv) && IsStructRet)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002240 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002241 // pops the hidden struct pointer, so we have to push it back.
2242 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002243 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002244 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002245 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002246
Gordon Henriksenae636f82008-01-03 16:47:34 +00002247 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002248 if (!IsSibcall) {
2249 Chain = DAG.getCALLSEQ_END(Chain,
2250 DAG.getIntPtrConstant(NumBytes, true),
2251 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2252 true),
2253 InFlag);
2254 InFlag = Chain.getValue(1);
2255 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002256
Chris Lattner3085e152007-02-25 08:59:22 +00002257 // Handle result values, copying them out of physregs into vregs that we
2258 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002259 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2260 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002261}
2262
Evan Cheng25ab6902006-09-08 06:48:29 +00002263
2264//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002265// Fast Calling Convention (tail call) implementation
2266//===----------------------------------------------------------------------===//
2267
2268// Like std call, callee cleans arguments, convention except that ECX is
2269// reserved for storing the tail called function address. Only 2 registers are
2270// free for argument passing (inreg). Tail call optimization is performed
2271// provided:
2272// * tailcallopt is enabled
2273// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002274// On X86_64 architecture with GOT-style position independent code only local
2275// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002276// To keep the stack aligned according to platform abi the function
2277// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2278// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002279// If a tail called function callee has more arguments than the caller the
2280// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002281// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002282// original REtADDR, but before the saved framepointer or the spilled registers
2283// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2284// stack layout:
2285// arg1
2286// arg2
2287// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002288// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002289// move area ]
2290// (possible EBP)
2291// ESI
2292// EDI
2293// local1 ..
2294
2295/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2296/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002297unsigned
2298X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2299 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002300 MachineFunction &MF = DAG.getMachineFunction();
2301 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002302 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002303 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002304 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002305 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002306 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002307 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2308 // Number smaller than 12 so just add the difference.
2309 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2310 } else {
2311 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002312 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002313 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002314 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002315 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002316}
2317
Evan Cheng5f941932010-02-05 02:21:12 +00002318/// MatchingStackOffset - Return true if the given stack call argument is
2319/// already available in the same position (relatively) of the caller's
2320/// incoming argument stack.
2321static
2322bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2323 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2324 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002325 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2326 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002327 if (Arg.getOpcode() == ISD::CopyFromReg) {
2328 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00002329 if (!TargetRegisterInfo::isVirtualRegister(VR))
Evan Cheng5f941932010-02-05 02:21:12 +00002330 return false;
2331 MachineInstr *Def = MRI->getVRegDef(VR);
2332 if (!Def)
2333 return false;
2334 if (!Flags.isByVal()) {
2335 if (!TII->isLoadFromStackSlot(Def, FI))
2336 return false;
2337 } else {
2338 unsigned Opcode = Def->getOpcode();
2339 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2340 Def->getOperand(1).isFI()) {
2341 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002342 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002343 } else
2344 return false;
2345 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002346 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2347 if (Flags.isByVal())
2348 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002349 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002350 // define @foo(%struct.X* %A) {
2351 // tail call @bar(%struct.X* byval %A)
2352 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002353 return false;
2354 SDValue Ptr = Ld->getBasePtr();
2355 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2356 if (!FINode)
2357 return false;
2358 FI = FINode->getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002359 } else
2360 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002361
Evan Cheng4cae1332010-03-05 08:38:04 +00002362 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002363 if (!MFI->isFixedObjectIndex(FI))
2364 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002365 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002366}
2367
Dan Gohman98ca4f22009-08-05 01:29:28 +00002368/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2369/// for tail call optimization. Targets which want to do tail call
2370/// optimization should implement this function.
2371bool
2372X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002373 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002374 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002375 bool isCalleeStructRet,
2376 bool isCallerStructRet,
Evan Chengb1712452010-01-27 06:25:16 +00002377 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002378 const SmallVectorImpl<SDValue> &OutVals,
Evan Chengb1712452010-01-27 06:25:16 +00002379 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002380 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002381 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002382 CalleeCC != CallingConv::C)
2383 return false;
2384
Evan Cheng7096ae42010-01-29 06:45:59 +00002385 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002386 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002387 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Cheng13617962010-04-30 01:12:32 +00002388 CallingConv::ID CallerCC = CallerF->getCallingConv();
2389 bool CCMatch = CallerCC == CalleeCC;
2390
Dan Gohman1797ed52010-02-08 20:27:50 +00002391 if (GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002392 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002393 return true;
2394 return false;
2395 }
2396
Dale Johannesen2f05cc02010-05-28 23:24:28 +00002397 // Look for obvious safe cases to perform tail call optimization that do not
2398 // require ABI changes. This is what gcc calls sibcall.
Evan Chengb2c92902010-02-02 02:22:50 +00002399
Evan Cheng2c12cb42010-03-26 16:26:03 +00002400 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2401 // emit a special epilogue.
2402 if (RegInfo->needsStackRealignment(MF))
2403 return false;
2404
Eric Christopher90eb4022010-07-22 00:26:08 +00002405 // Do not sibcall optimize vararg calls unless the call site is not passing
2406 // any arguments.
Evan Cheng3c262ee2010-03-26 02:13:13 +00002407 if (isVarArg && !Outs.empty())
Evan Cheng843bd692010-01-31 06:44:49 +00002408 return false;
2409
Evan Chenga375d472010-03-15 18:54:48 +00002410 // Also avoid sibcall optimization if either caller or callee uses struct
2411 // return semantics.
2412 if (isCalleeStructRet || isCallerStructRet)
2413 return false;
2414
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002415 // If the call result is in ST0 / ST1, it needs to be popped off the x87 stack.
2416 // Therefore if it's not used by the call it is not safe to optimize this into
2417 // a sibcall.
2418 bool Unused = false;
2419 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2420 if (!Ins[i].Used) {
2421 Unused = true;
2422 break;
2423 }
2424 }
2425 if (Unused) {
2426 SmallVector<CCValAssign, 16> RVLocs;
2427 CCState CCInfo(CalleeCC, false, getTargetMachine(),
2428 RVLocs, *DAG.getContext());
2429 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002430 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002431 CCValAssign &VA = RVLocs[i];
2432 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2433 return false;
2434 }
2435 }
2436
Evan Cheng13617962010-04-30 01:12:32 +00002437 // If the calling conventions do not match, then we'd better make sure the
2438 // results are returned in the same way as what the caller expects.
2439 if (!CCMatch) {
2440 SmallVector<CCValAssign, 16> RVLocs1;
2441 CCState CCInfo1(CalleeCC, false, getTargetMachine(),
2442 RVLocs1, *DAG.getContext());
2443 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2444
2445 SmallVector<CCValAssign, 16> RVLocs2;
2446 CCState CCInfo2(CallerCC, false, getTargetMachine(),
2447 RVLocs2, *DAG.getContext());
2448 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2449
2450 if (RVLocs1.size() != RVLocs2.size())
2451 return false;
2452 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2453 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2454 return false;
2455 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2456 return false;
2457 if (RVLocs1[i].isRegLoc()) {
2458 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2459 return false;
2460 } else {
2461 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2462 return false;
2463 }
2464 }
2465 }
2466
Evan Chenga6bff982010-01-30 01:22:00 +00002467 // If the callee takes no arguments then go on to check the results of the
2468 // call.
2469 if (!Outs.empty()) {
2470 // Check if stack adjustment is needed. For now, do not do this if any
2471 // argument is passed on the stack.
2472 SmallVector<CCValAssign, 16> ArgLocs;
2473 CCState CCInfo(CalleeCC, isVarArg, getTargetMachine(),
2474 ArgLocs, *DAG.getContext());
Duncan Sands45907662010-10-31 13:21:44 +00002475 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Evan Chengb2c92902010-02-02 02:22:50 +00002476 if (CCInfo.getNextStackOffset()) {
2477 MachineFunction &MF = DAG.getMachineFunction();
2478 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2479 return false;
Evan Chengb2c92902010-02-02 02:22:50 +00002480
2481 // Check if the arguments are already laid out in the right way as
2482 // the caller's fixed stack objects.
2483 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002484 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2485 const X86InstrInfo *TII =
2486 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002487 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2488 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00002489 SDValue Arg = OutVals[i];
Evan Chengb2c92902010-02-02 02:22:50 +00002490 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002491 if (VA.getLocInfo() == CCValAssign::Indirect)
2492 return false;
2493 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002494 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2495 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002496 return false;
2497 }
2498 }
2499 }
Evan Cheng9c044672010-05-29 01:35:22 +00002500
2501 // If the tailcall address may be in a register, then make sure it's
2502 // possible to register allocate for it. In 32-bit, the call address can
2503 // only target EAX, EDX, or ECX since the tail call must be scheduled after
Evan Chengdedd9742010-07-14 06:44:01 +00002504 // callee-saved registers are restored. These happen to be the same
2505 // registers used to pass 'inreg' arguments so watch out for those.
2506 if (!Subtarget->is64Bit() &&
2507 !isa<GlobalAddressSDNode>(Callee) &&
Evan Cheng9c044672010-05-29 01:35:22 +00002508 !isa<ExternalSymbolSDNode>(Callee)) {
Evan Cheng9c044672010-05-29 01:35:22 +00002509 unsigned NumInRegs = 0;
2510 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2511 CCValAssign &VA = ArgLocs[i];
Evan Chengdedd9742010-07-14 06:44:01 +00002512 if (!VA.isRegLoc())
2513 continue;
2514 unsigned Reg = VA.getLocReg();
2515 switch (Reg) {
2516 default: break;
2517 case X86::EAX: case X86::EDX: case X86::ECX:
2518 if (++NumInRegs == 3)
Evan Cheng9c044672010-05-29 01:35:22 +00002519 return false;
Evan Chengdedd9742010-07-14 06:44:01 +00002520 break;
Evan Cheng9c044672010-05-29 01:35:22 +00002521 }
2522 }
2523 }
Evan Chenga6bff982010-01-30 01:22:00 +00002524 }
Evan Chengb1712452010-01-27 06:25:16 +00002525
Dale Johannesend155d7e2010-10-25 22:17:05 +00002526 // An stdcall caller is expected to clean up its arguments; the callee
Dale Johannesen0e034562010-11-12 00:43:18 +00002527 // isn't going to do that.
Dale Johannesend155d7e2010-10-25 22:17:05 +00002528 if (!CCMatch && CallerCC==CallingConv::X86_StdCall)
2529 return false;
2530
Evan Cheng86809cc2010-02-03 03:28:02 +00002531 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002532}
2533
Dan Gohman3df24e62008-09-03 23:12:08 +00002534FastISel *
Dan Gohmana4160c32010-07-07 16:29:44 +00002535X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
2536 return X86::createFastISel(funcInfo);
Dan Gohmand9f3c482008-08-19 21:32:53 +00002537}
2538
2539
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002540//===----------------------------------------------------------------------===//
2541// Other Lowering Hooks
2542//===----------------------------------------------------------------------===//
2543
Bruno Cardoso Lopese654b562010-09-01 00:51:36 +00002544static bool MayFoldLoad(SDValue Op) {
2545 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
2546}
2547
2548static bool MayFoldIntoStore(SDValue Op) {
2549 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
2550}
2551
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002552static bool isTargetShuffle(unsigned Opcode) {
2553 switch(Opcode) {
2554 default: return false;
2555 case X86ISD::PSHUFD:
2556 case X86ISD::PSHUFHW:
2557 case X86ISD::PSHUFLW:
2558 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002559 case X86ISD::PALIGN:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002560 case X86ISD::SHUFPS:
2561 case X86ISD::MOVLHPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002562 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002563 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002564 case X86ISD::MOVLPS:
2565 case X86ISD::MOVLPD:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002566 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 Lopes27f12792010-08-28 02:46:39 +00002569 case X86ISD::MOVSS:
2570 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002571 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002572 case X86ISD::UNPCKLPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002573 case X86ISD::PUNPCKLWD:
2574 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002575 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002576 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002577 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002578 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002579 case X86ISD::PUNPCKHWD:
2580 case X86ISD::PUNPCKHBW:
2581 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002582 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002583 return true;
2584 }
2585 return false;
2586}
2587
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002588static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002589 SDValue V1, SelectionDAG &DAG) {
2590 switch(Opc) {
2591 default: llvm_unreachable("Unknown x86 shuffle node");
2592 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002593 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002594 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002595 return DAG.getNode(Opc, dl, VT, V1);
2596 }
2597
2598 return SDValue();
2599}
2600
2601static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00002602 SDValue V1, unsigned TargetMask, SelectionDAG &DAG) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002603 switch(Opc) {
2604 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002605 case X86ISD::PSHUFD:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002606 case X86ISD::PSHUFHW:
2607 case X86ISD::PSHUFLW:
2608 return DAG.getNode(Opc, dl, VT, V1, DAG.getConstant(TargetMask, MVT::i8));
2609 }
2610
2611 return SDValue();
2612}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002613
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002614static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2615 SDValue V1, SDValue V2, unsigned TargetMask, SelectionDAG &DAG) {
2616 switch(Opc) {
2617 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002618 case X86ISD::PALIGN:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002619 case X86ISD::SHUFPD:
2620 case X86ISD::SHUFPS:
2621 return DAG.getNode(Opc, dl, VT, V1, V2,
2622 DAG.getConstant(TargetMask, MVT::i8));
2623 }
2624 return SDValue();
2625}
2626
2627static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2628 SDValue V1, SDValue V2, SelectionDAG &DAG) {
2629 switch(Opc) {
2630 default: llvm_unreachable("Unknown x86 shuffle node");
2631 case X86ISD::MOVLHPS:
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00002632 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002633 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002634 case X86ISD::MOVLPS:
2635 case X86ISD::MOVLPD:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002636 case X86ISD::MOVSS:
2637 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002638 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002639 case X86ISD::UNPCKLPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002640 case X86ISD::PUNPCKLWD:
2641 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002642 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002643 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002644 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002645 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002646 case X86ISD::PUNPCKHWD:
2647 case X86ISD::PUNPCKHBW:
2648 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002649 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002650 return DAG.getNode(Opc, dl, VT, V1, V2);
2651 }
2652 return SDValue();
2653}
2654
Dan Gohmand858e902010-04-17 15:26:15 +00002655SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002656 MachineFunction &MF = DAG.getMachineFunction();
2657 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2658 int ReturnAddrIndex = FuncInfo->getRAIndex();
2659
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002660 if (ReturnAddrIndex == 0) {
2661 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002662 uint64_t SlotSize = TD->getPointerSize();
David Greene3f2bf852009-11-12 20:49:22 +00002663 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00002664 false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002665 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002666 }
2667
Evan Cheng25ab6902006-09-08 06:48:29 +00002668 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002669}
2670
2671
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002672bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
2673 bool hasSymbolicDisplacement) {
2674 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00002675 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002676 return false;
2677
2678 // If we don't have a symbolic displacement - we don't have any extra
2679 // restrictions.
2680 if (!hasSymbolicDisplacement)
2681 return true;
2682
2683 // FIXME: Some tweaks might be needed for medium code model.
2684 if (M != CodeModel::Small && M != CodeModel::Kernel)
2685 return false;
2686
2687 // For small code model we assume that latest object is 16MB before end of 31
2688 // bits boundary. We may also accept pretty large negative constants knowing
2689 // that all objects are in the positive half of address space.
2690 if (M == CodeModel::Small && Offset < 16*1024*1024)
2691 return true;
2692
2693 // For kernel code model we know that all object resist in the negative half
2694 // of 32bits address space. We may not accept negative offsets, since they may
2695 // be just off and we may accept pretty large positive ones.
2696 if (M == CodeModel::Kernel && Offset > 0)
2697 return true;
2698
2699 return false;
2700}
2701
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002702/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
2703/// specific condition code, returning the condition code and the LHS/RHS of the
2704/// comparison to make.
2705static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2706 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00002707 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002708 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2709 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2710 // X > -1 -> X == 0, jump !sign.
2711 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002712 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002713 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2714 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002715 return X86::COND_S;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002716 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00002717 // X < 1 -> X <= 0
2718 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002719 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002720 }
Chris Lattnerf9570512006-09-13 03:22:10 +00002721 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002722
Evan Chengd9558e02006-01-06 00:43:03 +00002723 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002724 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002725 case ISD::SETEQ: return X86::COND_E;
2726 case ISD::SETGT: return X86::COND_G;
2727 case ISD::SETGE: return X86::COND_GE;
2728 case ISD::SETLT: return X86::COND_L;
2729 case ISD::SETLE: return X86::COND_LE;
2730 case ISD::SETNE: return X86::COND_NE;
2731 case ISD::SETULT: return X86::COND_B;
2732 case ISD::SETUGT: return X86::COND_A;
2733 case ISD::SETULE: return X86::COND_BE;
2734 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00002735 }
Chris Lattner4c78e022008-12-23 23:42:27 +00002736 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002737
Chris Lattner4c78e022008-12-23 23:42:27 +00002738 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00002739
Chris Lattner4c78e022008-12-23 23:42:27 +00002740 // If LHS is a foldable load, but RHS is not, flip the condition.
2741 if ((ISD::isNON_EXTLoad(LHS.getNode()) && LHS.hasOneUse()) &&
2742 !(ISD::isNON_EXTLoad(RHS.getNode()) && RHS.hasOneUse())) {
2743 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
2744 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00002745 }
2746
Chris Lattner4c78e022008-12-23 23:42:27 +00002747 switch (SetCCOpcode) {
2748 default: break;
2749 case ISD::SETOLT:
2750 case ISD::SETOLE:
2751 case ISD::SETUGT:
2752 case ISD::SETUGE:
2753 std::swap(LHS, RHS);
2754 break;
2755 }
2756
2757 // On a floating point condition, the flags are set as follows:
2758 // ZF PF CF op
2759 // 0 | 0 | 0 | X > Y
2760 // 0 | 0 | 1 | X < Y
2761 // 1 | 0 | 0 | X == Y
2762 // 1 | 1 | 1 | unordered
2763 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002764 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00002765 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002766 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00002767 case ISD::SETOLT: // flipped
2768 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002769 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00002770 case ISD::SETOLE: // flipped
2771 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002772 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002773 case ISD::SETUGT: // flipped
2774 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002775 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00002776 case ISD::SETUGE: // flipped
2777 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002778 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002779 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002780 case ISD::SETNE: return X86::COND_NE;
2781 case ISD::SETUO: return X86::COND_P;
2782 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00002783 case ISD::SETOEQ:
2784 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00002785 }
Evan Chengd9558e02006-01-06 00:43:03 +00002786}
2787
Evan Cheng4a460802006-01-11 00:33:36 +00002788/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2789/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00002790/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00002791static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00002792 switch (X86CC) {
2793 default:
2794 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00002795 case X86::COND_B:
2796 case X86::COND_BE:
2797 case X86::COND_E:
2798 case X86::COND_P:
2799 case X86::COND_A:
2800 case X86::COND_AE:
2801 case X86::COND_NE:
2802 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00002803 return true;
2804 }
2805}
2806
Evan Chengeb2f9692009-10-27 19:56:55 +00002807/// isFPImmLegal - Returns true if the target can instruction select the
2808/// specified FP immediate natively. If false, the legalizer will
2809/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00002810bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00002811 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
2812 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
2813 return true;
2814 }
2815 return false;
2816}
2817
Nate Begeman9008ca62009-04-27 18:41:29 +00002818/// isUndefOrInRange - Return true if Val is undef or if its value falls within
2819/// the specified range (L, H].
2820static bool isUndefOrInRange(int Val, int Low, int Hi) {
2821 return (Val < 0) || (Val >= Low && Val < Hi);
2822}
2823
2824/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
2825/// specified value.
2826static bool isUndefOrEqual(int Val, int CmpVal) {
2827 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00002828 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00002829 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00002830}
2831
Nate Begeman9008ca62009-04-27 18:41:29 +00002832/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
2833/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
2834/// the second operand.
Owen Andersone50ed302009-08-10 22:56:29 +00002835static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00002836 if (VT == MVT::v4f32 || VT == MVT::v4i32 )
Nate Begeman9008ca62009-04-27 18:41:29 +00002837 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002838 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00002839 return (Mask[0] < 2 && Mask[1] < 2);
2840 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002841}
2842
Nate Begeman9008ca62009-04-27 18:41:29 +00002843bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002844 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002845 N->getMask(M);
2846 return ::isPSHUFDMask(M, N->getValueType(0));
2847}
Evan Cheng0188ecb2006-03-22 18:59:22 +00002848
Nate Begeman9008ca62009-04-27 18:41:29 +00002849/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
2850/// is suitable for input to PSHUFHW.
Owen Andersone50ed302009-08-10 22:56:29 +00002851static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002852 if (VT != MVT::v8i16)
Evan Cheng0188ecb2006-03-22 18:59:22 +00002853 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002854
Nate Begeman9008ca62009-04-27 18:41:29 +00002855 // Lower quadword copied in order or undef.
2856 for (int i = 0; i != 4; ++i)
2857 if (Mask[i] >= 0 && Mask[i] != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00002858 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002859
Evan Cheng506d3df2006-03-29 23:07:14 +00002860 // Upper quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002861 for (int i = 4; i != 8; ++i)
2862 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Evan Cheng506d3df2006-03-29 23:07:14 +00002863 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002864
Evan Cheng506d3df2006-03-29 23:07:14 +00002865 return true;
2866}
2867
Nate Begeman9008ca62009-04-27 18:41:29 +00002868bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002869 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002870 N->getMask(M);
2871 return ::isPSHUFHWMask(M, N->getValueType(0));
2872}
Evan Cheng506d3df2006-03-29 23:07:14 +00002873
Nate Begeman9008ca62009-04-27 18:41:29 +00002874/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
2875/// is suitable for input to PSHUFLW.
Owen Andersone50ed302009-08-10 22:56:29 +00002876static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002877 if (VT != MVT::v8i16)
Evan Cheng506d3df2006-03-29 23:07:14 +00002878 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002879
Rafael Espindola15684b22009-04-24 12:40:33 +00002880 // Upper quadword copied in order.
Nate Begeman9008ca62009-04-27 18:41:29 +00002881 for (int i = 4; i != 8; ++i)
2882 if (Mask[i] >= 0 && Mask[i] != i)
Rafael Espindola15684b22009-04-24 12:40:33 +00002883 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002884
Rafael Espindola15684b22009-04-24 12:40:33 +00002885 // Lower quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002886 for (int i = 0; i != 4; ++i)
2887 if (Mask[i] >= 4)
Rafael Espindola15684b22009-04-24 12:40:33 +00002888 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002889
Rafael Espindola15684b22009-04-24 12:40:33 +00002890 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002891}
2892
Nate Begeman9008ca62009-04-27 18:41:29 +00002893bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002894 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002895 N->getMask(M);
2896 return ::isPSHUFLWMask(M, N->getValueType(0));
2897}
2898
Nate Begemana09008b2009-10-19 02:17:23 +00002899/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
2900/// is suitable for input to PALIGNR.
2901static bool isPALIGNRMask(const SmallVectorImpl<int> &Mask, EVT VT,
2902 bool hasSSSE3) {
2903 int i, e = VT.getVectorNumElements();
Michael J. Spencerec38de22010-10-10 22:04:20 +00002904
Nate Begemana09008b2009-10-19 02:17:23 +00002905 // Do not handle v2i64 / v2f64 shuffles with palignr.
2906 if (e < 4 || !hasSSSE3)
2907 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00002908
Nate Begemana09008b2009-10-19 02:17:23 +00002909 for (i = 0; i != e; ++i)
2910 if (Mask[i] >= 0)
2911 break;
Michael J. Spencerec38de22010-10-10 22:04:20 +00002912
Nate Begemana09008b2009-10-19 02:17:23 +00002913 // All undef, not a palignr.
2914 if (i == e)
2915 return false;
2916
2917 // Determine if it's ok to perform a palignr with only the LHS, since we
2918 // don't have access to the actual shuffle elements to see if RHS is undef.
2919 bool Unary = Mask[i] < (int)e;
2920 bool NeedsUnary = false;
2921
2922 int s = Mask[i] - i;
Michael J. Spencerec38de22010-10-10 22:04:20 +00002923
Nate Begemana09008b2009-10-19 02:17:23 +00002924 // Check the rest of the elements to see if they are consecutive.
2925 for (++i; i != e; ++i) {
2926 int m = Mask[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00002927 if (m < 0)
Nate Begemana09008b2009-10-19 02:17:23 +00002928 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00002929
Nate Begemana09008b2009-10-19 02:17:23 +00002930 Unary = Unary && (m < (int)e);
2931 NeedsUnary = NeedsUnary || (m < s);
2932
2933 if (NeedsUnary && !Unary)
2934 return false;
2935 if (Unary && m != ((s+i) & (e-1)))
2936 return false;
2937 if (!Unary && m != (s+i))
2938 return false;
2939 }
2940 return true;
2941}
2942
2943bool X86::isPALIGNRMask(ShuffleVectorSDNode *N) {
2944 SmallVector<int, 8> M;
2945 N->getMask(M);
2946 return ::isPALIGNRMask(M, N->getValueType(0), true);
2947}
2948
Evan Cheng14aed5e2006-03-24 01:18:28 +00002949/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2950/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Owen Andersone50ed302009-08-10 22:56:29 +00002951static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002952 int NumElems = VT.getVectorNumElements();
2953 if (NumElems != 2 && NumElems != 4)
2954 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002955
Nate Begeman9008ca62009-04-27 18:41:29 +00002956 int Half = NumElems / 2;
2957 for (int i = 0; i < Half; ++i)
2958 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002959 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002960 for (int i = Half; i < NumElems; ++i)
2961 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002962 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002963
Evan Cheng14aed5e2006-03-24 01:18:28 +00002964 return true;
2965}
2966
Nate Begeman9008ca62009-04-27 18:41:29 +00002967bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
2968 SmallVector<int, 8> M;
2969 N->getMask(M);
2970 return ::isSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002971}
2972
Evan Cheng213d2cf2007-05-17 18:45:50 +00002973/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00002974/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2975/// half elements to come from vector 1 (which would equal the dest.) and
2976/// the upper half to come from vector 2.
Owen Andersone50ed302009-08-10 22:56:29 +00002977static bool isCommutedSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002978 int NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00002979
2980 if (NumElems != 2 && NumElems != 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00002981 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002982
Nate Begeman9008ca62009-04-27 18:41:29 +00002983 int Half = NumElems / 2;
2984 for (int i = 0; i < Half; ++i)
2985 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002986 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002987 for (int i = Half; i < NumElems; ++i)
2988 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002989 return false;
2990 return true;
2991}
2992
Nate Begeman9008ca62009-04-27 18:41:29 +00002993static bool isCommutedSHUFP(ShuffleVectorSDNode *N) {
2994 SmallVector<int, 8> M;
2995 N->getMask(M);
2996 return isCommutedSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002997}
2998
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002999/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
3000/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00003001bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
3002 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003003 return false;
3004
Evan Cheng2064a2b2006-03-28 06:50:32 +00003005 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman9008ca62009-04-27 18:41:29 +00003006 return isUndefOrEqual(N->getMaskElt(0), 6) &&
3007 isUndefOrEqual(N->getMaskElt(1), 7) &&
3008 isUndefOrEqual(N->getMaskElt(2), 2) &&
3009 isUndefOrEqual(N->getMaskElt(3), 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00003010}
3011
Nate Begeman0b10b912009-11-07 23:17:15 +00003012/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
3013/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
3014/// <2, 3, 2, 3>
3015bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
3016 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Michael J. Spencerec38de22010-10-10 22:04:20 +00003017
Nate Begeman0b10b912009-11-07 23:17:15 +00003018 if (NumElems != 4)
3019 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003020
Nate Begeman0b10b912009-11-07 23:17:15 +00003021 return isUndefOrEqual(N->getMaskElt(0), 2) &&
3022 isUndefOrEqual(N->getMaskElt(1), 3) &&
3023 isUndefOrEqual(N->getMaskElt(2), 2) &&
3024 isUndefOrEqual(N->getMaskElt(3), 3);
3025}
3026
Evan Cheng5ced1d82006-04-06 23:23:56 +00003027/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
3028/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman9008ca62009-04-27 18:41:29 +00003029bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
3030 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003031
Evan Cheng5ced1d82006-04-06 23:23:56 +00003032 if (NumElems != 2 && NumElems != 4)
3033 return false;
3034
Evan Chengc5cdff22006-04-07 21:53:05 +00003035 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003036 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003037 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003038
Evan Chengc5cdff22006-04-07 21:53:05 +00003039 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003040 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003041 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003042
3043 return true;
3044}
3045
Nate Begeman0b10b912009-11-07 23:17:15 +00003046/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
3047/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
3048bool X86::isMOVLHPSMask(ShuffleVectorSDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003049 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003050
Evan Cheng5ced1d82006-04-06 23:23:56 +00003051 if (NumElems != 2 && NumElems != 4)
3052 return false;
3053
Evan Chengc5cdff22006-04-07 21:53:05 +00003054 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003055 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003056 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003057
Nate Begeman9008ca62009-04-27 18:41:29 +00003058 for (unsigned i = 0; i < NumElems/2; ++i)
3059 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003060 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003061
3062 return true;
3063}
3064
Evan Cheng0038e592006-03-28 00:39:58 +00003065/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
3066/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Owen Andersone50ed302009-08-10 22:56:29 +00003067static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003068 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003069 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003070 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng0038e592006-03-28 00:39:58 +00003071 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003072
Nate Begeman9008ca62009-04-27 18:41:29 +00003073 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
3074 int BitI = Mask[i];
3075 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00003076 if (!isUndefOrEqual(BitI, j))
3077 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00003078 if (V2IsSplat) {
Mon P Wang7bcaefa2009-02-04 01:16:59 +00003079 if (!isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003080 return false;
3081 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00003082 if (!isUndefOrEqual(BitI1, j + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003083 return false;
3084 }
Evan Cheng0038e592006-03-28 00:39:58 +00003085 }
Evan Cheng0038e592006-03-28 00:39:58 +00003086 return true;
3087}
3088
Nate Begeman9008ca62009-04-27 18:41:29 +00003089bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3090 SmallVector<int, 8> M;
3091 N->getMask(M);
3092 return ::isUNPCKLMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003093}
3094
Evan Cheng4fcb9222006-03-28 02:43:26 +00003095/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3096/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Eric Christopherfd179292009-08-27 18:07:15 +00003097static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003098 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003099 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003100 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng4fcb9222006-03-28 02:43:26 +00003101 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003102
Nate Begeman9008ca62009-04-27 18:41:29 +00003103 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
3104 int BitI = Mask[i];
3105 int BitI1 = Mask[i+1];
Chris Lattner5a88b832007-02-25 07:10:00 +00003106 if (!isUndefOrEqual(BitI, j + NumElts/2))
Evan Chengc5cdff22006-04-07 21:53:05 +00003107 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00003108 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00003109 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003110 return false;
3111 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00003112 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003113 return false;
3114 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003115 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003116 return true;
3117}
3118
Nate Begeman9008ca62009-04-27 18:41:29 +00003119bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3120 SmallVector<int, 8> M;
3121 N->getMask(M);
3122 return ::isUNPCKHMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003123}
3124
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003125/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3126/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3127/// <0, 0, 1, 1>
Owen Andersone50ed302009-08-10 22:56:29 +00003128static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003129 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003130 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003131 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003132
Nate Begeman9008ca62009-04-27 18:41:29 +00003133 for (int i = 0, j = 0; i != NumElems; i += 2, ++j) {
3134 int BitI = Mask[i];
3135 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00003136 if (!isUndefOrEqual(BitI, j))
3137 return false;
3138 if (!isUndefOrEqual(BitI1, j))
3139 return false;
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003140 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003141 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003142}
3143
Nate Begeman9008ca62009-04-27 18:41:29 +00003144bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
3145 SmallVector<int, 8> M;
3146 N->getMask(M);
3147 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
3148}
3149
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003150/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3151/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3152/// <2, 2, 3, 3>
Owen Andersone50ed302009-08-10 22:56:29 +00003153static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003154 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003155 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
3156 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003157
Nate Begeman9008ca62009-04-27 18:41:29 +00003158 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
3159 int BitI = Mask[i];
3160 int BitI1 = Mask[i+1];
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003161 if (!isUndefOrEqual(BitI, j))
3162 return false;
3163 if (!isUndefOrEqual(BitI1, j))
3164 return false;
3165 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003166 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003167}
3168
Nate Begeman9008ca62009-04-27 18:41:29 +00003169bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
3170 SmallVector<int, 8> M;
3171 N->getMask(M);
3172 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
3173}
3174
Evan Cheng017dcc62006-04-21 01:05:10 +00003175/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3176/// specifies a shuffle of elements that is suitable for input to MOVSS,
3177/// MOVSD, and MOVD, i.e. setting the lowest element.
Owen Andersone50ed302009-08-10 22:56:29 +00003178static bool isMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003179 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003180 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003181
3182 int NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003183
Nate Begeman9008ca62009-04-27 18:41:29 +00003184 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003185 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003186
Nate Begeman9008ca62009-04-27 18:41:29 +00003187 for (int i = 1; i < NumElts; ++i)
3188 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003189 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003190
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003191 return true;
3192}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003193
Nate Begeman9008ca62009-04-27 18:41:29 +00003194bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
3195 SmallVector<int, 8> M;
3196 N->getMask(M);
3197 return ::isMOVLMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003198}
3199
Evan Cheng017dcc62006-04-21 01:05:10 +00003200/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
3201/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003202/// element of vector 2 and the other elements to come from vector 1 in order.
Owen Andersone50ed302009-08-10 22:56:29 +00003203static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003204 bool V2IsSplat = false, bool V2IsUndef = false) {
3205 int NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003206 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003207 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003208
Nate Begeman9008ca62009-04-27 18:41:29 +00003209 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003210 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003211
Nate Begeman9008ca62009-04-27 18:41:29 +00003212 for (int i = 1; i < NumOps; ++i)
3213 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3214 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3215 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003216 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003217
Evan Cheng39623da2006-04-20 08:58:49 +00003218 return true;
3219}
3220
Nate Begeman9008ca62009-04-27 18:41:29 +00003221static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00003222 bool V2IsUndef = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003223 SmallVector<int, 8> M;
3224 N->getMask(M);
3225 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00003226}
3227
Evan Chengd9539472006-04-14 21:59:03 +00003228/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3229/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003230bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N) {
3231 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003232 return false;
3233
3234 // Expect 1, 1, 3, 3
Rafael Espindola15684b22009-04-24 12:40:33 +00003235 for (unsigned i = 0; i < 2; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003236 int Elt = N->getMaskElt(i);
3237 if (Elt >= 0 && Elt != 1)
3238 return false;
Rafael Espindola15684b22009-04-24 12:40:33 +00003239 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003240
3241 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003242 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003243 int Elt = N->getMaskElt(i);
3244 if (Elt >= 0 && Elt != 3)
3245 return false;
3246 if (Elt == 3)
3247 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003248 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003249 // Don't use movshdup if it can be done with a shufps.
Nate Begeman9008ca62009-04-27 18:41:29 +00003250 // FIXME: verify that matching u, u, 3, 3 is what we want.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003251 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003252}
3253
3254/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3255/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003256bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N) {
3257 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003258 return false;
3259
3260 // Expect 0, 0, 2, 2
Nate Begeman9008ca62009-04-27 18:41:29 +00003261 for (unsigned i = 0; i < 2; ++i)
3262 if (N->getMaskElt(i) > 0)
3263 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003264
3265 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003266 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003267 int Elt = N->getMaskElt(i);
3268 if (Elt >= 0 && Elt != 2)
3269 return false;
3270 if (Elt == 2)
3271 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003272 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003273 // Don't use movsldup if it can be done with a shufps.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003274 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003275}
3276
Evan Cheng0b457f02008-09-25 20:50:48 +00003277/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3278/// specifies a shuffle of elements that is suitable for input to MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003279bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
3280 int e = N->getValueType(0).getVectorNumElements() / 2;
Eric Christopherfd179292009-08-27 18:07:15 +00003281
Nate Begeman9008ca62009-04-27 18:41:29 +00003282 for (int i = 0; i < e; ++i)
3283 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003284 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003285 for (int i = 0; i < e; ++i)
3286 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003287 return false;
3288 return true;
3289}
3290
Evan Cheng63d33002006-03-22 08:01:21 +00003291/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003292/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Evan Cheng63d33002006-03-22 08:01:21 +00003293unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003294 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3295 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
3296
Evan Chengb9df0ca2006-03-22 02:53:00 +00003297 unsigned Shift = (NumOperands == 4) ? 2 : 1;
3298 unsigned Mask = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00003299 for (int i = 0; i < NumOperands; ++i) {
3300 int Val = SVOp->getMaskElt(NumOperands-i-1);
3301 if (Val < 0) Val = 0;
Evan Cheng14aed5e2006-03-24 01:18:28 +00003302 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00003303 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00003304 if (i != NumOperands - 1)
3305 Mask <<= Shift;
3306 }
Evan Cheng63d33002006-03-22 08:01:21 +00003307 return Mask;
3308}
3309
Evan Cheng506d3df2006-03-29 23:07:14 +00003310/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003311/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003312unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003313 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003314 unsigned Mask = 0;
3315 // 8 nodes, but we only care about the last 4.
3316 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003317 int Val = SVOp->getMaskElt(i);
3318 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00003319 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00003320 if (i != 4)
3321 Mask <<= 2;
3322 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003323 return Mask;
3324}
3325
3326/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003327/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003328unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003329 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003330 unsigned Mask = 0;
3331 // 8 nodes, but we only care about the first 4.
3332 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003333 int Val = SVOp->getMaskElt(i);
3334 if (Val >= 0)
3335 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00003336 if (i != 0)
3337 Mask <<= 2;
3338 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003339 return Mask;
3340}
3341
Nate Begemana09008b2009-10-19 02:17:23 +00003342/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
3343/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
3344unsigned X86::getShufflePALIGNRImmediate(SDNode *N) {
3345 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3346 EVT VVT = N->getValueType(0);
3347 unsigned EltSize = VVT.getVectorElementType().getSizeInBits() >> 3;
3348 int Val = 0;
3349
3350 unsigned i, e;
3351 for (i = 0, e = VVT.getVectorNumElements(); i != e; ++i) {
3352 Val = SVOp->getMaskElt(i);
3353 if (Val >= 0)
3354 break;
3355 }
3356 return (Val - i) * EltSize;
3357}
3358
Evan Cheng37b73872009-07-30 08:33:02 +00003359/// isZeroNode - Returns true if Elt is a constant zero or a floating point
3360/// constant +0.0.
3361bool X86::isZeroNode(SDValue Elt) {
3362 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmane368b462010-06-18 14:22:04 +00003363 cast<ConstantSDNode>(Elt)->isNullValue()) ||
Evan Cheng37b73872009-07-30 08:33:02 +00003364 (isa<ConstantFPSDNode>(Elt) &&
3365 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
3366}
3367
Nate Begeman9008ca62009-04-27 18:41:29 +00003368/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
3369/// their permute mask.
3370static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
3371 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003372 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003373 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00003374 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00003375
Nate Begeman5a5ca152009-04-29 05:20:52 +00003376 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003377 int idx = SVOp->getMaskElt(i);
3378 if (idx < 0)
3379 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003380 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003381 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003382 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003383 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003384 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003385 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
3386 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003387}
3388
Evan Cheng779ccea2007-12-07 21:30:01 +00003389/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3390/// the two vector operands have swapped position.
Owen Andersone50ed302009-08-10 22:56:29 +00003391static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00003392 unsigned NumElems = VT.getVectorNumElements();
3393 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003394 int idx = Mask[i];
3395 if (idx < 0)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003396 continue;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003397 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003398 Mask[i] = idx + NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003399 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003400 Mask[i] = idx - NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003401 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003402}
3403
Evan Cheng533a0aa2006-04-19 20:35:22 +00003404/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
3405/// match movhlps. The lower half elements should come from upper half of
3406/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003407/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00003408static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
3409 if (Op->getValueType(0).getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00003410 return false;
3411 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003412 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003413 return false;
3414 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003415 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003416 return false;
3417 return true;
3418}
3419
Evan Cheng5ced1d82006-04-06 23:23:56 +00003420/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00003421/// is promoted to a vector. It also returns the LoadSDNode by reference if
3422/// required.
3423static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00003424 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
3425 return false;
3426 N = N->getOperand(0).getNode();
3427 if (!ISD::isNON_EXTLoad(N))
3428 return false;
3429 if (LD)
3430 *LD = cast<LoadSDNode>(N);
3431 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003432}
3433
Evan Cheng533a0aa2006-04-19 20:35:22 +00003434/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
3435/// match movlp{s|d}. The lower half elements should come from lower half of
3436/// V1 (and in order), and the upper half elements should come from the upper
3437/// half of V2 (and in order). And since V1 will become the source of the
3438/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003439static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
3440 ShuffleVectorSDNode *Op) {
Evan Cheng466685d2006-10-09 20:57:25 +00003441 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003442 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00003443 // Is V2 is a vector load, don't do this transformation. We will try to use
3444 // load folding shufps op.
3445 if (ISD::isNON_EXTLoad(V2))
3446 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003447
Nate Begeman5a5ca152009-04-29 05:20:52 +00003448 unsigned NumElems = Op->getValueType(0).getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003449
Evan Cheng533a0aa2006-04-19 20:35:22 +00003450 if (NumElems != 2 && NumElems != 4)
3451 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003452 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003453 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003454 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003455 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003456 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003457 return false;
3458 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003459}
3460
Evan Cheng39623da2006-04-20 08:58:49 +00003461/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
3462/// all the same.
3463static bool isSplatVector(SDNode *N) {
3464 if (N->getOpcode() != ISD::BUILD_VECTOR)
3465 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003466
Dan Gohman475871a2008-07-27 21:46:04 +00003467 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00003468 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
3469 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003470 return false;
3471 return true;
3472}
3473
Evan Cheng213d2cf2007-05-17 18:45:50 +00003474/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00003475/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00003476/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00003477static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00003478 SDValue V1 = N->getOperand(0);
3479 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003480 unsigned NumElems = N->getValueType(0).getVectorNumElements();
3481 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003482 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003483 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003484 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00003485 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
3486 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003487 if (Opc != ISD::BUILD_VECTOR ||
3488 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00003489 return false;
3490 } else if (Idx >= 0) {
3491 unsigned Opc = V1.getOpcode();
3492 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
3493 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003494 if (Opc != ISD::BUILD_VECTOR ||
3495 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00003496 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00003497 }
3498 }
3499 return true;
3500}
3501
3502/// getZeroVector - Returns a vector of specified type with all zero elements.
3503///
Owen Andersone50ed302009-08-10 22:56:29 +00003504static SDValue getZeroVector(EVT VT, bool HasSSE2, SelectionDAG &DAG,
Dale Johannesenace16102009-02-03 19:33:06 +00003505 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003506 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003507
Dale Johannesen0488fb62010-09-30 23:57:10 +00003508 // Always build SSE zero vectors as <4 x i32> bitcasted
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003509 // to their dest type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00003510 SDValue Vec;
Dale Johannesen0488fb62010-09-30 23:57:10 +00003511 if (VT.getSizeInBits() == 128) { // SSE
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003512 if (HasSSE2) { // SSE2
3513 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3514 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
3515 } else { // SSE1
3516 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
3517 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
3518 }
3519 } else if (VT.getSizeInBits() == 256) { // AVX
3520 // 256-bit logic and arithmetic instructions in AVX are
3521 // all floating-point, no support for integer ops. Default
3522 // to emitting fp zeroed vectors then.
Owen Anderson825b72b2009-08-11 20:47:22 +00003523 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003524 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
3525 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops, 8);
Evan Chengf0df0312008-05-15 08:39:06 +00003526 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003527 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00003528}
3529
Chris Lattner8a594482007-11-25 00:24:49 +00003530/// getOnesVector - Returns a vector of specified type with all bits set.
3531///
Owen Andersone50ed302009-08-10 22:56:29 +00003532static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003533 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003534
Chris Lattner8a594482007-11-25 00:24:49 +00003535 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
3536 // type. This ensures they get CSE'd.
Owen Anderson825b72b2009-08-11 20:47:22 +00003537 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00003538 SDValue Vec;
Dale Johannesen0488fb62010-09-30 23:57:10 +00003539 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003540 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00003541}
3542
3543
Evan Cheng39623da2006-04-20 08:58:49 +00003544/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
3545/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00003546static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003547 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003548 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003549
Evan Cheng39623da2006-04-20 08:58:49 +00003550 bool Changed = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003551 SmallVector<int, 8> MaskVec;
3552 SVOp->getMask(MaskVec);
Eric Christopherfd179292009-08-27 18:07:15 +00003553
Nate Begeman5a5ca152009-04-29 05:20:52 +00003554 for (unsigned i = 0; i != NumElems; ++i) {
3555 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003556 MaskVec[i] = NumElems;
3557 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00003558 }
Evan Cheng39623da2006-04-20 08:58:49 +00003559 }
Evan Cheng39623da2006-04-20 08:58:49 +00003560 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00003561 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
3562 SVOp->getOperand(1), &MaskVec[0]);
3563 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00003564}
3565
Evan Cheng017dcc62006-04-21 01:05:10 +00003566/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
3567/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00003568static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003569 SDValue V2) {
3570 unsigned NumElems = VT.getVectorNumElements();
3571 SmallVector<int, 8> Mask;
3572 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00003573 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003574 Mask.push_back(i);
3575 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00003576}
3577
Nate Begeman9008ca62009-04-27 18:41:29 +00003578/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003579static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003580 SDValue V2) {
3581 unsigned NumElems = VT.getVectorNumElements();
3582 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00003583 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003584 Mask.push_back(i);
3585 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00003586 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003587 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00003588}
3589
Nate Begeman9008ca62009-04-27 18:41:29 +00003590/// getUnpackhMask - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003591static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003592 SDValue V2) {
3593 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00003594 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00003595 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00003596 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003597 Mask.push_back(i + Half);
3598 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00003599 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003600 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00003601}
3602
Bruno Cardoso Lopesbb0a9482010-08-13 17:50:47 +00003603/// PromoteSplat - Promote a splat of v4i32, v8i16 or v16i8 to v4f32.
3604static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003605 EVT PVT = MVT::v4f32;
Owen Andersone50ed302009-08-10 22:56:29 +00003606 EVT VT = SV->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00003607 DebugLoc dl = SV->getDebugLoc();
3608 SDValue V1 = SV->getOperand(0);
3609 int NumElems = VT.getVectorNumElements();
3610 int EltNo = SV->getSplatIndex();
Rafael Espindola15684b22009-04-24 12:40:33 +00003611
Nate Begeman9008ca62009-04-27 18:41:29 +00003612 // unpack elements to the correct location
3613 while (NumElems > 4) {
3614 if (EltNo < NumElems/2) {
3615 V1 = getUnpackl(DAG, dl, VT, V1, V1);
3616 } else {
3617 V1 = getUnpackh(DAG, dl, VT, V1, V1);
3618 EltNo -= NumElems/2;
3619 }
3620 NumElems >>= 1;
3621 }
Eric Christopherfd179292009-08-27 18:07:15 +00003622
Nate Begeman9008ca62009-04-27 18:41:29 +00003623 // Perform the splat.
3624 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003625 V1 = DAG.getNode(ISD::BITCAST, dl, PVT, V1);
Nate Begeman9008ca62009-04-27 18:41:29 +00003626 V1 = DAG.getVectorShuffle(PVT, dl, V1, DAG.getUNDEF(PVT), &SplatMask[0]);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003627 return DAG.getNode(ISD::BITCAST, dl, VT, V1);
Evan Chengc575ca22006-04-17 20:43:08 +00003628}
3629
Evan Chengba05f722006-04-21 23:03:30 +00003630/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00003631/// vector of zero or undef vector. This produces a shuffle where the low
3632/// element of V2 is swizzled into the zero/undef vector, landing at element
3633/// 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 +00003634static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Chengf0df0312008-05-15 08:39:06 +00003635 bool isZero, bool HasSSE2,
3636 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003637 EVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00003638 SDValue V1 = isZero
Nate Begeman9008ca62009-04-27 18:41:29 +00003639 ? getZeroVector(VT, HasSSE2, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
3640 unsigned NumElems = VT.getVectorNumElements();
3641 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00003642 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003643 // If this is the insertion idx, put the low elt of V2 here.
3644 MaskVec.push_back(i == Idx ? NumElems : i);
3645 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00003646}
3647
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003648/// getShuffleScalarElt - Returns the scalar element that will make up the ith
3649/// element of the result of the vector shuffle.
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003650SDValue getShuffleScalarElt(SDNode *N, int Index, SelectionDAG &DAG,
3651 unsigned Depth) {
3652 if (Depth == 6)
3653 return SDValue(); // Limit search depth.
3654
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003655 SDValue V = SDValue(N, 0);
3656 EVT VT = V.getValueType();
3657 unsigned Opcode = V.getOpcode();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003658
3659 // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
3660 if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
3661 Index = SV->getMaskElt(Index);
3662
3663 if (Index < 0)
3664 return DAG.getUNDEF(VT.getVectorElementType());
3665
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003666 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003667 SDValue NewV = (Index < NumElems) ? SV->getOperand(0) : SV->getOperand(1);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003668 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG, Depth+1);
Evan Chengf26ffe92008-05-29 08:22:04 +00003669 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003670
3671 // Recurse into target specific vector shuffles to find scalars.
3672 if (isTargetShuffle(Opcode)) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003673 int NumElems = VT.getVectorNumElements();
3674 SmallVector<unsigned, 16> ShuffleMask;
3675 SDValue ImmN;
3676
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003677 switch(Opcode) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003678 case X86ISD::SHUFPS:
3679 case X86ISD::SHUFPD:
3680 ImmN = N->getOperand(N->getNumOperands()-1);
3681 DecodeSHUFPSMask(NumElems,
3682 cast<ConstantSDNode>(ImmN)->getZExtValue(),
3683 ShuffleMask);
3684 break;
3685 case X86ISD::PUNPCKHBW:
3686 case X86ISD::PUNPCKHWD:
3687 case X86ISD::PUNPCKHDQ:
3688 case X86ISD::PUNPCKHQDQ:
3689 DecodePUNPCKHMask(NumElems, ShuffleMask);
3690 break;
3691 case X86ISD::UNPCKHPS:
3692 case X86ISD::UNPCKHPD:
3693 DecodeUNPCKHPMask(NumElems, ShuffleMask);
3694 break;
3695 case X86ISD::PUNPCKLBW:
3696 case X86ISD::PUNPCKLWD:
3697 case X86ISD::PUNPCKLDQ:
3698 case X86ISD::PUNPCKLQDQ:
3699 DecodePUNPCKLMask(NumElems, ShuffleMask);
3700 break;
3701 case X86ISD::UNPCKLPS:
3702 case X86ISD::UNPCKLPD:
3703 DecodeUNPCKLPMask(NumElems, ShuffleMask);
3704 break;
3705 case X86ISD::MOVHLPS:
3706 DecodeMOVHLPSMask(NumElems, ShuffleMask);
3707 break;
3708 case X86ISD::MOVLHPS:
3709 DecodeMOVLHPSMask(NumElems, ShuffleMask);
3710 break;
3711 case X86ISD::PSHUFD:
3712 ImmN = N->getOperand(N->getNumOperands()-1);
3713 DecodePSHUFMask(NumElems,
3714 cast<ConstantSDNode>(ImmN)->getZExtValue(),
3715 ShuffleMask);
3716 break;
3717 case X86ISD::PSHUFHW:
3718 ImmN = N->getOperand(N->getNumOperands()-1);
3719 DecodePSHUFHWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
3720 ShuffleMask);
3721 break;
3722 case X86ISD::PSHUFLW:
3723 ImmN = N->getOperand(N->getNumOperands()-1);
3724 DecodePSHUFLWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
3725 ShuffleMask);
3726 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003727 case X86ISD::MOVSS:
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00003728 case X86ISD::MOVSD: {
3729 // The index 0 always comes from the first element of the second source,
3730 // this is why MOVSS and MOVSD are used in the first place. The other
3731 // elements come from the other positions of the first source vector.
3732 unsigned OpNum = (Index == 0) ? 1 : 0;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003733 return getShuffleScalarElt(V.getOperand(OpNum).getNode(), Index, DAG,
3734 Depth+1);
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00003735 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003736 default:
3737 assert("not implemented for target shuffle node");
3738 return SDValue();
3739 }
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003740
3741 Index = ShuffleMask[Index];
3742 if (Index < 0)
3743 return DAG.getUNDEF(VT.getVectorElementType());
3744
3745 SDValue NewV = (Index < NumElems) ? N->getOperand(0) : N->getOperand(1);
3746 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG,
3747 Depth+1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003748 }
3749
3750 // Actual nodes that may contain scalar elements
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003751 if (Opcode == ISD::BITCAST) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003752 V = V.getOperand(0);
3753 EVT SrcVT = V.getValueType();
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003754 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003755
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003756 if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003757 return SDValue();
3758 }
3759
3760 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
3761 return (Index == 0) ? V.getOperand(0)
3762 : DAG.getUNDEF(VT.getVectorElementType());
3763
3764 if (V.getOpcode() == ISD::BUILD_VECTOR)
3765 return V.getOperand(Index);
3766
3767 return SDValue();
3768}
3769
3770/// getNumOfConsecutiveZeros - Return the number of elements of a vector
3771/// shuffle operation which come from a consecutively from a zero. The
3772/// search can start in two diferent directions, from left or right.
3773static
3774unsigned getNumOfConsecutiveZeros(SDNode *N, int NumElems,
3775 bool ZerosFromLeft, SelectionDAG &DAG) {
3776 int i = 0;
3777
3778 while (i < NumElems) {
3779 unsigned Index = ZerosFromLeft ? i : NumElems-i-1;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003780 SDValue Elt = getShuffleScalarElt(N, Index, DAG, 0);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003781 if (!(Elt.getNode() &&
3782 (Elt.getOpcode() == ISD::UNDEF || X86::isZeroNode(Elt))))
3783 break;
3784 ++i;
3785 }
3786
3787 return i;
3788}
3789
3790/// isShuffleMaskConsecutive - Check if the shuffle mask indicies from MaskI to
3791/// MaskE correspond consecutively to elements from one of the vector operands,
3792/// starting from its index OpIdx. Also tell OpNum which source vector operand.
3793static
3794bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp, int MaskI, int MaskE,
3795 int OpIdx, int NumElems, unsigned &OpNum) {
3796 bool SeenV1 = false;
3797 bool SeenV2 = false;
3798
3799 for (int i = MaskI; i <= MaskE; ++i, ++OpIdx) {
3800 int Idx = SVOp->getMaskElt(i);
3801 // Ignore undef indicies
3802 if (Idx < 0)
3803 continue;
3804
3805 if (Idx < NumElems)
3806 SeenV1 = true;
3807 else
3808 SeenV2 = true;
3809
3810 // Only accept consecutive elements from the same vector
3811 if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
3812 return false;
3813 }
3814
3815 OpNum = SeenV1 ? 0 : 1;
3816 return true;
3817}
3818
3819/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
3820/// logical left shift of a vector.
3821static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
3822 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
3823 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
3824 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
3825 false /* check zeros from right */, DAG);
3826 unsigned OpSrc;
3827
3828 if (!NumZeros)
3829 return false;
3830
3831 // Considering the elements in the mask that are not consecutive zeros,
3832 // check if they consecutively come from only one of the source vectors.
3833 //
3834 // V1 = {X, A, B, C} 0
3835 // \ \ \ /
3836 // vector_shuffle V1, V2 <1, 2, 3, X>
3837 //
3838 if (!isShuffleMaskConsecutive(SVOp,
3839 0, // Mask Start Index
3840 NumElems-NumZeros-1, // Mask End Index
3841 NumZeros, // Where to start looking in the src vector
3842 NumElems, // Number of elements in vector
3843 OpSrc)) // Which source operand ?
3844 return false;
3845
3846 isLeft = false;
3847 ShAmt = NumZeros;
3848 ShVal = SVOp->getOperand(OpSrc);
3849 return true;
3850}
3851
3852/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
3853/// logical left shift of a vector.
3854static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
3855 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
3856 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
3857 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
3858 true /* check zeros from left */, DAG);
3859 unsigned OpSrc;
3860
3861 if (!NumZeros)
3862 return false;
3863
3864 // Considering the elements in the mask that are not consecutive zeros,
3865 // check if they consecutively come from only one of the source vectors.
3866 //
3867 // 0 { A, B, X, X } = V2
3868 // / \ / /
3869 // vector_shuffle V1, V2 <X, X, 4, 5>
3870 //
3871 if (!isShuffleMaskConsecutive(SVOp,
3872 NumZeros, // Mask Start Index
3873 NumElems-1, // Mask End Index
3874 0, // Where to start looking in the src vector
3875 NumElems, // Number of elements in vector
3876 OpSrc)) // Which source operand ?
3877 return false;
3878
3879 isLeft = true;
3880 ShAmt = NumZeros;
3881 ShVal = SVOp->getOperand(OpSrc);
3882 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00003883}
3884
3885/// isVectorShift - Returns true if the shuffle can be implemented as a
3886/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003887static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00003888 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003889 if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
3890 isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
3891 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00003892
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003893 return false;
Evan Chengf26ffe92008-05-29 08:22:04 +00003894}
3895
Evan Chengc78d3b42006-04-24 18:01:45 +00003896/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
3897///
Dan Gohman475871a2008-07-27 21:46:04 +00003898static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00003899 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00003900 SelectionDAG &DAG,
3901 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003902 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00003903 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00003904
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003905 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003906 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003907 bool First = true;
3908 for (unsigned i = 0; i < 16; ++i) {
3909 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
3910 if (ThisIsNonZero && First) {
3911 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003912 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00003913 else
Owen Anderson825b72b2009-08-11 20:47:22 +00003914 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00003915 First = false;
3916 }
3917
3918 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00003919 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003920 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
3921 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003922 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003923 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00003924 }
3925 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003926 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
3927 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
3928 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00003929 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003930 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00003931 } else
3932 ThisElt = LastElt;
3933
Gabor Greifba36cb52008-08-28 21:40:38 +00003934 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00003935 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00003936 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00003937 }
3938 }
3939
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003940 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00003941}
3942
Bill Wendlinga348c562007-03-22 18:42:45 +00003943/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00003944///
Dan Gohman475871a2008-07-27 21:46:04 +00003945static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00003946 unsigned NumNonZero, unsigned NumZero,
3947 SelectionDAG &DAG,
3948 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003949 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00003950 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00003951
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003952 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003953 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003954 bool First = true;
3955 for (unsigned i = 0; i < 8; ++i) {
3956 bool isNonZero = (NonZeros & (1 << i)) != 0;
3957 if (isNonZero) {
3958 if (First) {
3959 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003960 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00003961 else
Owen Anderson825b72b2009-08-11 20:47:22 +00003962 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00003963 First = false;
3964 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003965 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003966 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00003967 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00003968 }
3969 }
3970
3971 return V;
3972}
3973
Evan Chengf26ffe92008-05-29 08:22:04 +00003974/// getVShift - Return a vector logical shift node.
3975///
Owen Andersone50ed302009-08-10 22:56:29 +00003976static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00003977 unsigned NumBits, SelectionDAG &DAG,
3978 const TargetLowering &TLI, DebugLoc dl) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00003979 EVT ShVT = MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00003980 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003981 SrcOp = DAG.getNode(ISD::BITCAST, dl, ShVT, SrcOp);
3982 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00003983 DAG.getNode(Opc, dl, ShVT, SrcOp,
Gabor Greif327ef032008-08-28 23:19:51 +00003984 DAG.getConstant(NumBits, TLI.getShiftAmountTy())));
Evan Chengf26ffe92008-05-29 08:22:04 +00003985}
3986
Dan Gohman475871a2008-07-27 21:46:04 +00003987SDValue
Evan Chengc3630942009-12-09 21:00:30 +00003988X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00003989 SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00003990
Evan Chengc3630942009-12-09 21:00:30 +00003991 // Check if the scalar load can be widened into a vector load. And if
3992 // the address is "base + cst" see if the cst can be "absorbed" into
3993 // the shuffle mask.
3994 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
3995 SDValue Ptr = LD->getBasePtr();
3996 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
3997 return SDValue();
3998 EVT PVT = LD->getValueType(0);
3999 if (PVT != MVT::i32 && PVT != MVT::f32)
4000 return SDValue();
4001
4002 int FI = -1;
4003 int64_t Offset = 0;
4004 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
4005 FI = FINode->getIndex();
4006 Offset = 0;
4007 } else if (Ptr.getOpcode() == ISD::ADD &&
4008 isa<ConstantSDNode>(Ptr.getOperand(1)) &&
4009 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4010 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4011 Offset = Ptr.getConstantOperandVal(1);
4012 Ptr = Ptr.getOperand(0);
4013 } else {
4014 return SDValue();
4015 }
4016
4017 SDValue Chain = LD->getChain();
4018 // Make sure the stack object alignment is at least 16.
4019 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
4020 if (DAG.InferPtrAlignment(Ptr) < 16) {
4021 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00004022 // Can't change the alignment. FIXME: It's possible to compute
4023 // the exact stack offset and reference FI + adjust offset instead.
4024 // If someone *really* cares about this. That's the way to implement it.
4025 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004026 } else {
4027 MFI->setObjectAlignment(FI, 16);
4028 }
4029 }
4030
4031 // (Offset % 16) must be multiple of 4. Then address is then
4032 // Ptr + (Offset & ~15).
4033 if (Offset < 0)
4034 return SDValue();
4035 if ((Offset % 16) & 3)
4036 return SDValue();
4037 int64_t StartOffset = Offset & ~15;
4038 if (StartOffset)
4039 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
4040 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
4041
4042 int EltNo = (Offset - StartOffset) >> 2;
4043 int Mask[4] = { EltNo, EltNo, EltNo, EltNo };
4044 EVT VT = (PVT == MVT::i32) ? MVT::v4i32 : MVT::v4f32;
Chris Lattner51abfe42010-09-21 06:02:19 +00004045 SDValue V1 = DAG.getLoad(VT, dl, Chain, Ptr,
4046 LD->getPointerInfo().getWithOffset(StartOffset),
David Greene67c9d422010-02-15 16:53:33 +00004047 false, false, 0);
Evan Chengc3630942009-12-09 21:00:30 +00004048 // Canonicalize it to a v4i32 shuffle.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004049 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, V1);
4050 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Chengc3630942009-12-09 21:00:30 +00004051 DAG.getVectorShuffle(MVT::v4i32, dl, V1,
Chris Lattner51abfe42010-09-21 06:02:19 +00004052 DAG.getUNDEF(MVT::v4i32),&Mask[0]));
Evan Chengc3630942009-12-09 21:00:30 +00004053 }
4054
4055 return SDValue();
4056}
4057
Michael J. Spencerec38de22010-10-10 22:04:20 +00004058/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
4059/// vector of type 'VT', see if the elements can be replaced by a single large
Nate Begeman1449f292010-03-24 22:19:06 +00004060/// load which has the same value as a build_vector whose operands are 'elts'.
4061///
4062/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
Michael J. Spencerec38de22010-10-10 22:04:20 +00004063///
Nate Begeman1449f292010-03-24 22:19:06 +00004064/// FIXME: we'd also like to handle the case where the last elements are zero
4065/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
4066/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004067static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
Chris Lattner88641552010-09-22 00:34:38 +00004068 DebugLoc &DL, SelectionDAG &DAG) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00004069 EVT EltVT = VT.getVectorElementType();
4070 unsigned NumElems = Elts.size();
Michael J. Spencerec38de22010-10-10 22:04:20 +00004071
Nate Begemanfdea31a2010-03-24 20:49:50 +00004072 LoadSDNode *LDBase = NULL;
4073 unsigned LastLoadedElt = -1U;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004074
Nate Begeman1449f292010-03-24 22:19:06 +00004075 // For each element in the initializer, see if we've found a load or an undef.
Michael J. Spencerec38de22010-10-10 22:04:20 +00004076 // If we don't find an initial load element, or later load elements are
Nate Begeman1449f292010-03-24 22:19:06 +00004077 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004078 for (unsigned i = 0; i < NumElems; ++i) {
4079 SDValue Elt = Elts[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00004080
Nate Begemanfdea31a2010-03-24 20:49:50 +00004081 if (!Elt.getNode() ||
4082 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
4083 return SDValue();
4084 if (!LDBase) {
4085 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
4086 return SDValue();
4087 LDBase = cast<LoadSDNode>(Elt.getNode());
4088 LastLoadedElt = i;
4089 continue;
4090 }
4091 if (Elt.getOpcode() == ISD::UNDEF)
4092 continue;
4093
4094 LoadSDNode *LD = cast<LoadSDNode>(Elt);
4095 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
4096 return SDValue();
4097 LastLoadedElt = i;
4098 }
Nate Begeman1449f292010-03-24 22:19:06 +00004099
4100 // If we have found an entire vector of loads and undefs, then return a large
4101 // load of the entire vector width starting at the base pointer. If we found
4102 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004103 if (LastLoadedElt == NumElems - 1) {
4104 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
Chris Lattner88641552010-09-22 00:34:38 +00004105 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004106 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004107 LDBase->isVolatile(), LDBase->isNonTemporal(), 0);
Chris Lattner88641552010-09-22 00:34:38 +00004108 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004109 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004110 LDBase->isVolatile(), LDBase->isNonTemporal(),
4111 LDBase->getAlignment());
4112 } else if (NumElems == 4 && LastLoadedElt == 1) {
4113 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
4114 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
Chris Lattner88641552010-09-22 00:34:38 +00004115 SDValue ResNode = DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys,
4116 Ops, 2, MVT::i32,
4117 LDBase->getMemOperand());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004118 return DAG.getNode(ISD::BITCAST, DL, VT, ResNode);
Nate Begemanfdea31a2010-03-24 20:49:50 +00004119 }
4120 return SDValue();
4121}
4122
Evan Chengc3630942009-12-09 21:00:30 +00004123SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004124X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004125 DebugLoc dl = Op.getDebugLoc();
Chris Lattner6e80e442010-08-28 17:15:43 +00004126 // All zero's are handled with pxor in SSE2 and above, xorps in SSE1.
4127 // All one's are handled with pcmpeqd. In AVX, zero's are handled with
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004128 // vpxor in 128-bit and xor{pd,ps} in 256-bit, but no 256 version of pcmpeqd
4129 // is present, so AllOnes is ignored.
4130 if (ISD::isBuildVectorAllZeros(Op.getNode()) ||
4131 (Op.getValueType().getSizeInBits() != 256 &&
4132 ISD::isBuildVectorAllOnes(Op.getNode()))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004133 // Canonicalize this to <4 x i32> (SSE) to
Chris Lattner8a594482007-11-25 00:24:49 +00004134 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
4135 // eliminated on x86-32 hosts.
Dale Johannesen0488fb62010-09-30 23:57:10 +00004136 if (Op.getValueType() == MVT::v4i32)
Chris Lattner8a594482007-11-25 00:24:49 +00004137 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004138
Gabor Greifba36cb52008-08-28 21:40:38 +00004139 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00004140 return getOnesVector(Op.getValueType(), DAG, dl);
4141 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00004142 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004143
Owen Andersone50ed302009-08-10 22:56:29 +00004144 EVT VT = Op.getValueType();
4145 EVT ExtVT = VT.getVectorElementType();
4146 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004147
4148 unsigned NumElems = Op.getNumOperands();
4149 unsigned NumZero = 0;
4150 unsigned NumNonZero = 0;
4151 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004152 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00004153 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004154 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00004155 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00004156 if (Elt.getOpcode() == ISD::UNDEF)
4157 continue;
4158 Values.insert(Elt);
4159 if (Elt.getOpcode() != ISD::Constant &&
4160 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004161 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00004162 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00004163 NumZero++;
4164 else {
4165 NonZeros |= (1 << i);
4166 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004167 }
4168 }
4169
Chris Lattner97a2a562010-08-26 05:24:29 +00004170 // All undef vector. Return an UNDEF. All zero vectors were handled above.
4171 if (NumNonZero == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00004172 return DAG.getUNDEF(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004173
Chris Lattner67f453a2008-03-09 05:42:06 +00004174 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00004175 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004176 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00004177 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00004178
Chris Lattner62098042008-03-09 01:05:04 +00004179 // If this is an insertion of an i64 value on x86-32, and if the top bits of
4180 // the value are obviously zero, truncate the value to i32 and do the
4181 // insertion that way. Only do this if the value is non-constant or if the
4182 // value is a constant being inserted into element 0. It is cheaper to do
4183 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00004184 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00004185 (!IsAllConstants || Idx == 0)) {
4186 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004187 // Handle SSE only.
4188 assert(VT == MVT::v2i64 && "Expected an SSE value type!");
4189 EVT VecVT = MVT::v4i32;
4190 unsigned VecElts = 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00004191
Chris Lattner62098042008-03-09 01:05:04 +00004192 // Truncate the value (which may itself be a constant) to i32, and
4193 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00004194 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00004195 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00004196 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
4197 Subtarget->hasSSE2(), DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00004198
Chris Lattner62098042008-03-09 01:05:04 +00004199 // Now we have our 32-bit value zero extended in the low element of
4200 // a vector. If Idx != 0, swizzle it into place.
4201 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004202 SmallVector<int, 4> Mask;
4203 Mask.push_back(Idx);
4204 for (unsigned i = 1; i != VecElts; ++i)
4205 Mask.push_back(i);
4206 Item = DAG.getVectorShuffle(VecVT, dl, Item,
Eric Christopherfd179292009-08-27 18:07:15 +00004207 DAG.getUNDEF(Item.getValueType()),
Nate Begeman9008ca62009-04-27 18:41:29 +00004208 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00004209 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004210 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00004211 }
4212 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004213
Chris Lattner19f79692008-03-08 22:59:52 +00004214 // If we have a constant or non-constant insertion into the low element of
4215 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
4216 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00004217 // depending on what the source datatype is.
4218 if (Idx == 0) {
4219 if (NumZero == 0) {
4220 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Owen Anderson825b72b2009-08-11 20:47:22 +00004221 } else if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
4222 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Eli Friedman10415532009-06-06 06:05:10 +00004223 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
4224 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
4225 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget->hasSSE2(),
4226 DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00004227 } else if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
4228 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
Dale Johannesen0488fb62010-09-30 23:57:10 +00004229 assert(VT.getSizeInBits() == 128 && "Expected an SSE value type!");
4230 EVT MiddleVT = MVT::v4i32;
Eli Friedman10415532009-06-06 06:05:10 +00004231 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MiddleVT, Item);
4232 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
4233 Subtarget->hasSSE2(), DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004234 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Eli Friedman10415532009-06-06 06:05:10 +00004235 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004236 }
Evan Chengf26ffe92008-05-29 08:22:04 +00004237
4238 // Is it a vector logical left shift?
4239 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00004240 X86::isZeroNode(Op.getOperand(0)) &&
4241 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004242 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00004243 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00004244 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004245 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00004246 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004247 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004248
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004249 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00004250 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004251
Chris Lattner19f79692008-03-08 22:59:52 +00004252 // Otherwise, if this is a vector with i32 or f32 elements, and the element
4253 // is a non-constant being inserted into an element other than the low one,
4254 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
4255 // movd/movss) to move this into the low element, then shuffle it into
4256 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004257 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00004258 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00004259
Evan Cheng0db9fe62006-04-25 20:13:52 +00004260 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00004261 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
4262 Subtarget->hasSSE2(), DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00004263 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004264 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00004265 MaskVec.push_back(i == Idx ? 0 : 1);
4266 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004267 }
4268 }
4269
Chris Lattner67f453a2008-03-09 05:42:06 +00004270 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00004271 if (Values.size() == 1) {
4272 if (EVTBits == 32) {
4273 // Instead of a shuffle like this:
4274 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
4275 // Check if it's possible to issue this instead.
4276 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
4277 unsigned Idx = CountTrailingZeros_32(NonZeros);
4278 SDValue Item = Op.getOperand(Idx);
4279 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
4280 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
4281 }
Dan Gohman475871a2008-07-27 21:46:04 +00004282 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004283 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004284
Dan Gohmana3941172007-07-24 22:55:08 +00004285 // A vector full of immediates; various special cases are already
4286 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004287 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00004288 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00004289
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00004290 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00004291 if (EVTBits == 64) {
4292 if (NumNonZero == 1) {
4293 // One half is zero or undef.
4294 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00004295 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00004296 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00004297 return getShuffleVectorZeroOrUndef(V2, Idx, true,
4298 Subtarget->hasSSE2(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00004299 }
Dan Gohman475871a2008-07-27 21:46:04 +00004300 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00004301 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004302
4303 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00004304 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00004305 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00004306 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004307 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004308 }
4309
Bill Wendling826f36f2007-03-28 00:57:11 +00004310 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00004311 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Chris Lattner97a2a562010-08-26 05:24:29 +00004312 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004313 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004314 }
4315
4316 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00004317 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00004318 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004319 if (NumElems == 4 && NumZero > 0) {
4320 for (unsigned i = 0; i < 4; ++i) {
4321 bool isZero = !(NonZeros & (1 << i));
4322 if (isZero)
Dale Johannesenace16102009-02-03 19:33:06 +00004323 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004324 else
Dale Johannesenace16102009-02-03 19:33:06 +00004325 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004326 }
4327
4328 for (unsigned i = 0; i < 2; ++i) {
4329 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
4330 default: break;
4331 case 0:
4332 V[i] = V[i*2]; // Must be a zero vector.
4333 break;
4334 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00004335 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004336 break;
4337 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00004338 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004339 break;
4340 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00004341 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004342 break;
4343 }
4344 }
4345
Nate Begeman9008ca62009-04-27 18:41:29 +00004346 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004347 bool Reverse = (NonZeros & 0x3) == 2;
4348 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004349 MaskVec.push_back(Reverse ? 1-i : i);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004350 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
4351 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004352 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
4353 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004354 }
4355
Nate Begemanfdea31a2010-03-24 20:49:50 +00004356 if (Values.size() > 1 && VT.getSizeInBits() == 128) {
4357 // Check for a build vector of consecutive loads.
4358 for (unsigned i = 0; i < NumElems; ++i)
4359 V[i] = Op.getOperand(i);
Michael J. Spencerec38de22010-10-10 22:04:20 +00004360
Nate Begemanfdea31a2010-03-24 20:49:50 +00004361 // Check for elements which are consecutive loads.
4362 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
4363 if (LD.getNode())
4364 return LD;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004365
4366 // For SSE 4.1, use insertps to put the high elements into the low element.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004367 if (getSubtarget()->hasSSE41()) {
Chris Lattner24faf612010-08-28 17:59:08 +00004368 SDValue Result;
4369 if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
4370 Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
4371 else
4372 Result = DAG.getUNDEF(VT);
Michael J. Spencerec38de22010-10-10 22:04:20 +00004373
Chris Lattner24faf612010-08-28 17:59:08 +00004374 for (unsigned i = 1; i < NumElems; ++i) {
4375 if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
4376 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
Nate Begeman9008ca62009-04-27 18:41:29 +00004377 Op.getOperand(i), DAG.getIntPtrConstant(i));
Chris Lattner24faf612010-08-28 17:59:08 +00004378 }
4379 return Result;
Nate Begeman9008ca62009-04-27 18:41:29 +00004380 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00004381
Chris Lattner6e80e442010-08-28 17:15:43 +00004382 // Otherwise, expand into a number of unpckl*, start by extending each of
4383 // our (non-undef) elements to the full vector width with the element in the
4384 // bottom slot of the vector (which generates no code for SSE).
4385 for (unsigned i = 0; i < NumElems; ++i) {
4386 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
4387 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
4388 else
4389 V[i] = DAG.getUNDEF(VT);
4390 }
4391
4392 // Next, we iteratively mix elements, e.g. for v4f32:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004393 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
4394 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
4395 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Chris Lattner6e80e442010-08-28 17:15:43 +00004396 unsigned EltStride = NumElems >> 1;
4397 while (EltStride != 0) {
Chris Lattner3ddcc432010-08-28 17:28:30 +00004398 for (unsigned i = 0; i < EltStride; ++i) {
4399 // If V[i+EltStride] is undef and this is the first round of mixing,
4400 // then it is safe to just drop this shuffle: V[i] is already in the
4401 // right place, the one element (since it's the first round) being
4402 // inserted as undef can be dropped. This isn't safe for successive
4403 // rounds because they will permute elements within both vectors.
4404 if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
4405 EltStride == NumElems/2)
4406 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004407
Chris Lattner6e80e442010-08-28 17:15:43 +00004408 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
Chris Lattner3ddcc432010-08-28 17:28:30 +00004409 }
Chris Lattner6e80e442010-08-28 17:15:43 +00004410 EltStride >>= 1;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004411 }
4412 return V[0];
4413 }
Dan Gohman475871a2008-07-27 21:46:04 +00004414 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004415}
4416
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004417SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004418X86TargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const {
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004419 // We support concatenate two MMX registers and place them in a MMX
4420 // register. This is better than doing a stack convert.
4421 DebugLoc dl = Op.getDebugLoc();
4422 EVT ResVT = Op.getValueType();
4423 assert(Op.getNumOperands() == 2);
4424 assert(ResVT == MVT::v2i64 || ResVT == MVT::v4i32 ||
4425 ResVT == MVT::v8i16 || ResVT == MVT::v16i8);
4426 int Mask[2];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004427 SDValue InVec = DAG.getNode(ISD::BITCAST,dl, MVT::v1i64, Op.getOperand(0));
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004428 SDValue VecOp = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
4429 InVec = Op.getOperand(1);
4430 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4431 unsigned NumElts = ResVT.getVectorNumElements();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004432 VecOp = DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004433 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, ResVT, VecOp,
4434 InVec.getOperand(0), DAG.getIntPtrConstant(NumElts/2+1));
4435 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004436 InVec = DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, InVec);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004437 SDValue VecOp2 = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
4438 Mask[0] = 0; Mask[1] = 2;
4439 VecOp = DAG.getVectorShuffle(MVT::v2i64, dl, VecOp, VecOp2, Mask);
4440 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004441 return DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004442}
4443
Nate Begemanb9a47b82009-02-23 08:49:38 +00004444// v8i16 shuffles - Prefer shuffles in the following order:
4445// 1. [all] pshuflw, pshufhw, optional move
4446// 2. [ssse3] 1 x pshufb
4447// 3. [ssse3] 2 x pshufb + 1 x por
4448// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004449SDValue
4450X86TargetLowering::LowerVECTOR_SHUFFLEv8i16(SDValue Op,
4451 SelectionDAG &DAG) const {
4452 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00004453 SDValue V1 = SVOp->getOperand(0);
4454 SDValue V2 = SVOp->getOperand(1);
4455 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00004456 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00004457
Nate Begemanb9a47b82009-02-23 08:49:38 +00004458 // Determine if more than 1 of the words in each of the low and high quadwords
4459 // of the result come from the same quadword of one of the two inputs. Undef
4460 // mask values count as coming from any quadword, for better codegen.
4461 SmallVector<unsigned, 4> LoQuad(4);
4462 SmallVector<unsigned, 4> HiQuad(4);
4463 BitVector InputQuads(4);
4464 for (unsigned i = 0; i < 8; ++i) {
4465 SmallVectorImpl<unsigned> &Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00004466 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004467 MaskVals.push_back(EltIdx);
4468 if (EltIdx < 0) {
4469 ++Quad[0];
4470 ++Quad[1];
4471 ++Quad[2];
4472 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00004473 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004474 }
4475 ++Quad[EltIdx / 4];
4476 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00004477 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004478
Nate Begemanb9a47b82009-02-23 08:49:38 +00004479 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00004480 unsigned MaxQuad = 1;
4481 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004482 if (LoQuad[i] > MaxQuad) {
4483 BestLoQuad = i;
4484 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00004485 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004486 }
4487
Nate Begemanb9a47b82009-02-23 08:49:38 +00004488 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00004489 MaxQuad = 1;
4490 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004491 if (HiQuad[i] > MaxQuad) {
4492 BestHiQuad = i;
4493 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00004494 }
4495 }
4496
Nate Begemanb9a47b82009-02-23 08:49:38 +00004497 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00004498 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00004499 // single pshufb instruction is necessary. If There are more than 2 input
4500 // quads, disable the next transformation since it does not help SSSE3.
4501 bool V1Used = InputQuads[0] || InputQuads[1];
4502 bool V2Used = InputQuads[2] || InputQuads[3];
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004503 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004504 if (InputQuads.count() == 2 && V1Used && V2Used) {
4505 BestLoQuad = InputQuads.find_first();
4506 BestHiQuad = InputQuads.find_next(BestLoQuad);
4507 }
4508 if (InputQuads.count() > 2) {
4509 BestLoQuad = -1;
4510 BestHiQuad = -1;
4511 }
4512 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004513
Nate Begemanb9a47b82009-02-23 08:49:38 +00004514 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
4515 // the shuffle mask. If a quad is scored as -1, that means that it contains
4516 // words from all 4 input quadwords.
4517 SDValue NewV;
4518 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004519 SmallVector<int, 8> MaskV;
4520 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
4521 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
Eric Christopherfd179292009-08-27 18:07:15 +00004522 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004523 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V1),
4524 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V2), &MaskV[0]);
4525 NewV = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00004526
Nate Begemanb9a47b82009-02-23 08:49:38 +00004527 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
4528 // source words for the shuffle, to aid later transformations.
4529 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00004530 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00004531 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004532 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00004533 if (idx != (int)i)
4534 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004535 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00004536 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004537 AllWordsInNewV = false;
4538 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00004539 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004540
Nate Begemanb9a47b82009-02-23 08:49:38 +00004541 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
4542 if (AllWordsInNewV) {
4543 for (int i = 0; i != 8; ++i) {
4544 int idx = MaskVals[i];
4545 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00004546 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004547 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004548 if ((idx != i) && idx < 4)
4549 pshufhw = false;
4550 if ((idx != i) && idx > 3)
4551 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00004552 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00004553 V1 = NewV;
4554 V2Used = false;
4555 BestLoQuad = 0;
4556 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004557 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004558
Nate Begemanb9a47b82009-02-23 08:49:38 +00004559 // If we've eliminated the use of V2, and the new mask is a pshuflw or
4560 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00004561 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00004562 unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
4563 unsigned TargetMask = 0;
4564 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00004565 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00004566 TargetMask = pshufhw ? X86::getShufflePSHUFHWImmediate(NewV.getNode()):
4567 X86::getShufflePSHUFLWImmediate(NewV.getNode());
4568 V1 = NewV.getOperand(0);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00004569 return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
Evan Cheng14b32e12007-12-11 01:46:18 +00004570 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004571 }
Eric Christopherfd179292009-08-27 18:07:15 +00004572
Nate Begemanb9a47b82009-02-23 08:49:38 +00004573 // If we have SSSE3, and all words of the result are from 1 input vector,
4574 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
4575 // is present, fall back to case 4.
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004576 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004577 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00004578
Nate Begemanb9a47b82009-02-23 08:49:38 +00004579 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00004580 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00004581 // mask, and elements that come from V1 in the V2 mask, so that the two
4582 // results can be OR'd together.
4583 bool TwoInputs = V1Used && V2Used;
4584 for (unsigned i = 0; i != 8; ++i) {
4585 int EltIdx = MaskVals[i] * 2;
4586 if (TwoInputs && (EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004587 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4588 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004589 continue;
4590 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004591 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
4592 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004593 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004594 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004595 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00004596 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004597 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004598 if (!TwoInputs)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004599 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004600
Nate Begemanb9a47b82009-02-23 08:49:38 +00004601 // Calculate the shuffle mask for the second input, shuffle it, and
4602 // OR it with the first shuffled input.
4603 pshufbMask.clear();
4604 for (unsigned i = 0; i != 8; ++i) {
4605 int EltIdx = MaskVals[i] * 2;
4606 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004607 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4608 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004609 continue;
4610 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004611 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
4612 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004613 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004614 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00004615 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00004616 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004617 MVT::v16i8, &pshufbMask[0], 16));
4618 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004619 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004620 }
4621
4622 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
4623 // and update MaskVals with new element order.
4624 BitVector InOrder(8);
4625 if (BestLoQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004626 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004627 for (int i = 0; i != 4; ++i) {
4628 int idx = MaskVals[i];
4629 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004630 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004631 InOrder.set(i);
4632 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004633 MaskV.push_back(idx & 3);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004634 InOrder.set(i);
4635 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004636 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004637 }
4638 }
4639 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004640 MaskV.push_back(i);
Owen Anderson825b72b2009-08-11 20:47:22 +00004641 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00004642 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00004643
4644 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3())
4645 NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
4646 NewV.getOperand(0),
4647 X86::getShufflePSHUFLWImmediate(NewV.getNode()),
4648 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004649 }
Eric Christopherfd179292009-08-27 18:07:15 +00004650
Nate Begemanb9a47b82009-02-23 08:49:38 +00004651 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
4652 // and update MaskVals with the new element order.
4653 if (BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004654 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004655 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004656 MaskV.push_back(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004657 for (unsigned i = 4; i != 8; ++i) {
4658 int idx = MaskVals[i];
4659 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004660 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004661 InOrder.set(i);
4662 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004663 MaskV.push_back((idx & 3) + 4);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004664 InOrder.set(i);
4665 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004666 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004667 }
4668 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004669 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00004670 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00004671
4672 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3())
4673 NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
4674 NewV.getOperand(0),
4675 X86::getShufflePSHUFHWImmediate(NewV.getNode()),
4676 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004677 }
Eric Christopherfd179292009-08-27 18:07:15 +00004678
Nate Begemanb9a47b82009-02-23 08:49:38 +00004679 // In case BestHi & BestLo were both -1, which means each quadword has a word
4680 // from each of the four input quadwords, calculate the InOrder bitvector now
4681 // before falling through to the insert/extract cleanup.
4682 if (BestLoQuad == -1 && BestHiQuad == -1) {
4683 NewV = V1;
4684 for (int i = 0; i != 8; ++i)
4685 if (MaskVals[i] < 0 || MaskVals[i] == i)
4686 InOrder.set(i);
4687 }
Eric Christopherfd179292009-08-27 18:07:15 +00004688
Nate Begemanb9a47b82009-02-23 08:49:38 +00004689 // The other elements are put in the right place using pextrw and pinsrw.
4690 for (unsigned i = 0; i != 8; ++i) {
4691 if (InOrder[i])
4692 continue;
4693 int EltIdx = MaskVals[i];
4694 if (EltIdx < 0)
4695 continue;
4696 SDValue ExtOp = (EltIdx < 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00004697 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004698 DAG.getIntPtrConstant(EltIdx))
Owen Anderson825b72b2009-08-11 20:47:22 +00004699 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004700 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00004701 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004702 DAG.getIntPtrConstant(i));
4703 }
4704 return NewV;
4705}
4706
4707// v16i8 shuffles - Prefer shuffles in the following order:
4708// 1. [ssse3] 1 x pshufb
4709// 2. [ssse3] 2 x pshufb + 1 x por
4710// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
4711static
Nate Begeman9008ca62009-04-27 18:41:29 +00004712SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00004713 SelectionDAG &DAG,
4714 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004715 SDValue V1 = SVOp->getOperand(0);
4716 SDValue V2 = SVOp->getOperand(1);
4717 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00004718 SmallVector<int, 16> MaskVals;
Nate Begeman9008ca62009-04-27 18:41:29 +00004719 SVOp->getMask(MaskVals);
Eric Christopherfd179292009-08-27 18:07:15 +00004720
Nate Begemanb9a47b82009-02-23 08:49:38 +00004721 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00004722 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00004723 // present, fall back to case 3.
4724 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
4725 bool V1Only = true;
4726 bool V2Only = true;
4727 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004728 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00004729 if (EltIdx < 0)
4730 continue;
4731 if (EltIdx < 16)
4732 V2Only = false;
4733 else
4734 V1Only = false;
4735 }
Eric Christopherfd179292009-08-27 18:07:15 +00004736
Nate Begemanb9a47b82009-02-23 08:49:38 +00004737 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
4738 if (TLI.getSubtarget()->hasSSSE3()) {
4739 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00004740
Nate Begemanb9a47b82009-02-23 08:49:38 +00004741 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00004742 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00004743 //
4744 // Otherwise, we have elements from both input vectors, and must zero out
4745 // elements that come from V2 in the first mask, and V1 in the second mask
4746 // so that we can OR them together.
4747 bool TwoInputs = !(V1Only || V2Only);
4748 for (unsigned i = 0; i != 16; ++i) {
4749 int EltIdx = MaskVals[i];
4750 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004751 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004752 continue;
4753 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004754 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004755 }
4756 // If all the elements are from V2, assign it to V1 and return after
4757 // building the first pshufb.
4758 if (V2Only)
4759 V1 = V2;
Owen Anderson825b72b2009-08-11 20:47:22 +00004760 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00004761 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004762 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004763 if (!TwoInputs)
4764 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00004765
Nate Begemanb9a47b82009-02-23 08:49:38 +00004766 // Calculate the shuffle mask for the second input, shuffle it, and
4767 // OR it with the first shuffled input.
4768 pshufbMask.clear();
4769 for (unsigned i = 0; i != 16; ++i) {
4770 int EltIdx = MaskVals[i];
4771 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004772 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004773 continue;
4774 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004775 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004776 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004777 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00004778 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004779 MVT::v16i8, &pshufbMask[0], 16));
4780 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004781 }
Eric Christopherfd179292009-08-27 18:07:15 +00004782
Nate Begemanb9a47b82009-02-23 08:49:38 +00004783 // No SSSE3 - Calculate in place words and then fix all out of place words
4784 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
4785 // the 16 different words that comprise the two doublequadword input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004786 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
4787 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004788 SDValue NewV = V2Only ? V2 : V1;
4789 for (int i = 0; i != 8; ++i) {
4790 int Elt0 = MaskVals[i*2];
4791 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00004792
Nate Begemanb9a47b82009-02-23 08:49:38 +00004793 // This word of the result is all undef, skip it.
4794 if (Elt0 < 0 && Elt1 < 0)
4795 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004796
Nate Begemanb9a47b82009-02-23 08:49:38 +00004797 // This word of the result is already in the correct place, skip it.
4798 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
4799 continue;
4800 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
4801 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004802
Nate Begemanb9a47b82009-02-23 08:49:38 +00004803 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
4804 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
4805 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00004806
4807 // If Elt0 and Elt1 are defined, are consecutive, and can be load
4808 // using a single extract together, load it and store it.
4809 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004810 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00004811 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00004812 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00004813 DAG.getIntPtrConstant(i));
4814 continue;
4815 }
4816
Nate Begemanb9a47b82009-02-23 08:49:38 +00004817 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00004818 // source byte is not also odd, shift the extracted word left 8 bits
4819 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00004820 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004821 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004822 DAG.getIntPtrConstant(Elt1 / 2));
4823 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004824 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004825 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00004826 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004827 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
4828 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004829 }
4830 // If Elt0 is defined, extract it from the appropriate source. If the
4831 // source byte is not also even, shift the extracted word right 8 bits. If
4832 // Elt1 was also defined, OR the extracted values together before
4833 // inserting them in the result.
4834 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004835 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004836 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
4837 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004838 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004839 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00004840 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004841 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
4842 DAG.getConstant(0x00FF, MVT::i16));
4843 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00004844 : InsElt0;
4845 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004846 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004847 DAG.getIntPtrConstant(i));
4848 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004849 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00004850}
4851
Evan Cheng7a831ce2007-12-15 03:00:47 +00004852/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00004853/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00004854/// done when every pair / quad of shuffle mask elements point to elements in
4855/// the right sequence. e.g.
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00004856/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
Evan Cheng14b32e12007-12-11 01:46:18 +00004857static
Nate Begeman9008ca62009-04-27 18:41:29 +00004858SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00004859 SelectionDAG &DAG, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00004860 EVT VT = SVOp->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00004861 SDValue V1 = SVOp->getOperand(0);
4862 SDValue V2 = SVOp->getOperand(1);
4863 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00004864 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00004865 EVT NewVT;
Owen Anderson825b72b2009-08-11 20:47:22 +00004866 switch (VT.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004867 default: assert(false && "Unexpected!");
Owen Anderson825b72b2009-08-11 20:47:22 +00004868 case MVT::v4f32: NewVT = MVT::v2f64; break;
4869 case MVT::v4i32: NewVT = MVT::v2i64; break;
4870 case MVT::v8i16: NewVT = MVT::v4i32; break;
4871 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00004872 }
4873
Nate Begeman9008ca62009-04-27 18:41:29 +00004874 int Scale = NumElems / NewWidth;
4875 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00004876 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004877 int StartIdx = -1;
4878 for (int j = 0; j < Scale; ++j) {
4879 int EltIdx = SVOp->getMaskElt(i+j);
4880 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00004881 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00004882 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00004883 StartIdx = EltIdx - (EltIdx % Scale);
4884 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00004885 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00004886 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004887 if (StartIdx == -1)
4888 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00004889 else
Nate Begeman9008ca62009-04-27 18:41:29 +00004890 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004891 }
4892
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004893 V1 = DAG.getNode(ISD::BITCAST, dl, NewVT, V1);
4894 V2 = DAG.getNode(ISD::BITCAST, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00004895 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004896}
4897
Evan Chengd880b972008-05-09 21:53:03 +00004898/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00004899///
Owen Andersone50ed302009-08-10 22:56:29 +00004900static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00004901 SDValue SrcOp, SelectionDAG &DAG,
4902 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004903 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004904 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00004905 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00004906 LD = dyn_cast<LoadSDNode>(SrcOp);
4907 if (!LD) {
4908 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
4909 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00004910 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Duncan Sandscdfad362010-11-03 12:17:33 +00004911 if ((ExtVT != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00004912 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004913 SrcOp.getOperand(0).getOpcode() == ISD::BITCAST &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00004914 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004915 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00004916 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004917 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004918 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
4919 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
4920 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00004921 SrcOp.getOperand(0)
4922 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00004923 }
4924 }
4925 }
4926
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004927 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004928 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004929 DAG.getNode(ISD::BITCAST, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00004930 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00004931}
4932
Evan Chengace3c172008-07-22 21:13:36 +00004933/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
4934/// shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00004935static SDValue
Nate Begeman9008ca62009-04-27 18:41:29 +00004936LowerVECTOR_SHUFFLE_4wide(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
4937 SDValue V1 = SVOp->getOperand(0);
4938 SDValue V2 = SVOp->getOperand(1);
4939 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00004940 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00004941
Evan Chengace3c172008-07-22 21:13:36 +00004942 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00004943 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00004944 SmallVector<int, 8> Mask1(4U, -1);
4945 SmallVector<int, 8> PermMask;
4946 SVOp->getMask(PermMask);
4947
Evan Chengace3c172008-07-22 21:13:36 +00004948 unsigned NumHi = 0;
4949 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00004950 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004951 int Idx = PermMask[i];
4952 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00004953 Locs[i] = std::make_pair(-1, -1);
4954 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004955 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
4956 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00004957 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00004958 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004959 NumLo++;
4960 } else {
4961 Locs[i] = std::make_pair(1, NumHi);
4962 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00004963 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004964 NumHi++;
4965 }
4966 }
4967 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004968
Evan Chengace3c172008-07-22 21:13:36 +00004969 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004970 // If no more than two elements come from either vector. This can be
4971 // implemented with two shuffles. First shuffle gather the elements.
4972 // The second shuffle, which takes the first shuffle as both of its
4973 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00004974 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004975
Nate Begeman9008ca62009-04-27 18:41:29 +00004976 SmallVector<int, 8> Mask2(4U, -1);
Eric Christopherfd179292009-08-27 18:07:15 +00004977
Evan Chengace3c172008-07-22 21:13:36 +00004978 for (unsigned i = 0; i != 4; ++i) {
4979 if (Locs[i].first == -1)
4980 continue;
4981 else {
4982 unsigned Idx = (i < 2) ? 0 : 4;
4983 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00004984 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004985 }
4986 }
4987
Nate Begeman9008ca62009-04-27 18:41:29 +00004988 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004989 } else if (NumLo == 3 || NumHi == 3) {
4990 // Otherwise, we must have three elements from one vector, call it X, and
4991 // one element from the other, call it Y. First, use a shufps to build an
4992 // intermediate vector with the one element from Y and the element from X
4993 // that will be in the same half in the final destination (the indexes don't
4994 // matter). Then, use a shufps to build the final vector, taking the half
4995 // containing the element from Y from the intermediate, and the other half
4996 // from X.
4997 if (NumHi == 3) {
4998 // Normalize it so the 3 elements come from V1.
Nate Begeman9008ca62009-04-27 18:41:29 +00004999 CommuteVectorShuffleMask(PermMask, VT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005000 std::swap(V1, V2);
5001 }
5002
5003 // Find the element from V2.
5004 unsigned HiIndex;
5005 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005006 int Val = PermMask[HiIndex];
5007 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005008 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005009 if (Val >= 4)
5010 break;
5011 }
5012
Nate Begeman9008ca62009-04-27 18:41:29 +00005013 Mask1[0] = PermMask[HiIndex];
5014 Mask1[1] = -1;
5015 Mask1[2] = PermMask[HiIndex^1];
5016 Mask1[3] = -1;
5017 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005018
5019 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005020 Mask1[0] = PermMask[0];
5021 Mask1[1] = PermMask[1];
5022 Mask1[2] = HiIndex & 1 ? 6 : 4;
5023 Mask1[3] = HiIndex & 1 ? 4 : 6;
5024 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005025 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005026 Mask1[0] = HiIndex & 1 ? 2 : 0;
5027 Mask1[1] = HiIndex & 1 ? 0 : 2;
5028 Mask1[2] = PermMask[2];
5029 Mask1[3] = PermMask[3];
5030 if (Mask1[2] >= 0)
5031 Mask1[2] += 4;
5032 if (Mask1[3] >= 0)
5033 Mask1[3] += 4;
5034 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005035 }
Evan Chengace3c172008-07-22 21:13:36 +00005036 }
5037
5038 // Break it into (shuffle shuffle_hi, shuffle_lo).
5039 Locs.clear();
Nate Begeman9008ca62009-04-27 18:41:29 +00005040 SmallVector<int,8> LoMask(4U, -1);
5041 SmallVector<int,8> HiMask(4U, -1);
5042
5043 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00005044 unsigned MaskIdx = 0;
5045 unsigned LoIdx = 0;
5046 unsigned HiIdx = 2;
5047 for (unsigned i = 0; i != 4; ++i) {
5048 if (i == 2) {
5049 MaskPtr = &HiMask;
5050 MaskIdx = 1;
5051 LoIdx = 0;
5052 HiIdx = 2;
5053 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005054 int Idx = PermMask[i];
5055 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00005056 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00005057 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00005058 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00005059 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005060 LoIdx++;
5061 } else {
5062 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00005063 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005064 HiIdx++;
5065 }
5066 }
5067
Nate Begeman9008ca62009-04-27 18:41:29 +00005068 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
5069 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
5070 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00005071 for (unsigned i = 0; i != 4; ++i) {
5072 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005073 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00005074 } else {
5075 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00005076 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00005077 }
5078 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005079 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00005080}
5081
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005082static bool MayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005083 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005084 V = V.getOperand(0);
5085 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
5086 V = V.getOperand(0);
5087 if (MayFoldLoad(V))
5088 return true;
5089 return false;
5090}
5091
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005092// FIXME: the version above should always be used. Since there's
5093// a bug where several vector shuffles can't be folded because the
5094// DAG is not updated during lowering and a node claims to have two
5095// uses while it only has one, use this version, and let isel match
5096// another instruction if the load really happens to have more than
5097// one use. Remove this version after this bug get fixed.
Evan Cheng835580f2010-10-07 20:50:20 +00005098// rdar://8434668, PR8156
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005099static bool RelaxedMayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005100 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005101 V = V.getOperand(0);
5102 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
5103 V = V.getOperand(0);
5104 if (ISD::isNormalLoad(V.getNode()))
5105 return true;
5106 return false;
5107}
5108
5109/// CanFoldShuffleIntoVExtract - Check if the current shuffle is used by
5110/// a vector extract, and if both can be later optimized into a single load.
5111/// This is done in visitEXTRACT_VECTOR_ELT and the conditions are checked
5112/// here because otherwise a target specific shuffle node is going to be
5113/// emitted for this shuffle, and the optimization not done.
5114/// FIXME: This is probably not the best approach, but fix the problem
5115/// until the right path is decided.
5116static
5117bool CanXFormVExtractWithShuffleIntoLoad(SDValue V, SelectionDAG &DAG,
5118 const TargetLowering &TLI) {
5119 EVT VT = V.getValueType();
5120 ShuffleVectorSDNode *SVOp = dyn_cast<ShuffleVectorSDNode>(V);
5121
5122 // Be sure that the vector shuffle is present in a pattern like this:
5123 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), c) -> (f32 load $addr)
5124 if (!V.hasOneUse())
5125 return false;
5126
5127 SDNode *N = *V.getNode()->use_begin();
5128 if (N->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
5129 return false;
5130
5131 SDValue EltNo = N->getOperand(1);
5132 if (!isa<ConstantSDNode>(EltNo))
5133 return false;
5134
5135 // If the bit convert changed the number of elements, it is unsafe
5136 // to examine the mask.
5137 bool HasShuffleIntoBitcast = false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005138 if (V.getOpcode() == ISD::BITCAST) {
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005139 EVT SrcVT = V.getOperand(0).getValueType();
5140 if (SrcVT.getVectorNumElements() != VT.getVectorNumElements())
5141 return false;
5142 V = V.getOperand(0);
5143 HasShuffleIntoBitcast = true;
5144 }
5145
5146 // Select the input vector, guarding against out of range extract vector.
5147 unsigned NumElems = VT.getVectorNumElements();
5148 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
5149 int Idx = (Elt > NumElems) ? -1 : SVOp->getMaskElt(Elt);
5150 V = (Idx < (int)NumElems) ? V.getOperand(0) : V.getOperand(1);
5151
5152 // Skip one more bit_convert if necessary
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005153 if (V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005154 V = V.getOperand(0);
5155
5156 if (ISD::isNormalLoad(V.getNode())) {
5157 // Is the original load suitable?
5158 LoadSDNode *LN0 = cast<LoadSDNode>(V);
5159
5160 // FIXME: avoid the multi-use bug that is preventing lots of
5161 // of foldings to be detected, this is still wrong of course, but
5162 // give the temporary desired behavior, and if it happens that
5163 // the load has real more uses, during isel it will not fold, and
5164 // will generate poor code.
5165 if (!LN0 || LN0->isVolatile()) // || !LN0->hasOneUse()
5166 return false;
5167
5168 if (!HasShuffleIntoBitcast)
5169 return true;
5170
5171 // If there's a bitcast before the shuffle, check if the load type and
5172 // alignment is valid.
5173 unsigned Align = LN0->getAlignment();
5174 unsigned NewAlign =
5175 TLI.getTargetData()->getABITypeAlignment(
5176 VT.getTypeForEVT(*DAG.getContext()));
5177
5178 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
5179 return false;
5180 }
5181
5182 return true;
5183}
5184
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005185static
Evan Cheng835580f2010-10-07 20:50:20 +00005186SDValue getMOVDDup(SDValue &Op, DebugLoc &dl, SDValue V1, SelectionDAG &DAG) {
5187 EVT VT = Op.getValueType();
5188
5189 // Canonizalize to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005190 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1);
5191 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng835580f2010-10-07 20:50:20 +00005192 getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64,
5193 V1, DAG));
5194}
5195
5196static
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005197SDValue getMOVLowToHigh(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG,
5198 bool HasSSE2) {
5199 SDValue V1 = Op.getOperand(0);
5200 SDValue V2 = Op.getOperand(1);
5201 EVT VT = Op.getValueType();
5202
5203 assert(VT != MVT::v2i64 && "unsupported shuffle type");
5204
5205 if (HasSSE2 && VT == MVT::v2f64)
5206 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
5207
5208 // v4f32 or v4i32
5209 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V2, DAG);
5210}
5211
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00005212static
5213SDValue getMOVHighToLow(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG) {
5214 SDValue V1 = Op.getOperand(0);
5215 SDValue V2 = Op.getOperand(1);
5216 EVT VT = Op.getValueType();
5217
5218 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
5219 "unsupported shuffle type");
5220
5221 if (V2.getOpcode() == ISD::UNDEF)
5222 V2 = V1;
5223
5224 // v4i32 or v4f32
5225 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
5226}
5227
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005228static
5229SDValue getMOVLP(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG, bool HasSSE2) {
5230 SDValue V1 = Op.getOperand(0);
5231 SDValue V2 = Op.getOperand(1);
5232 EVT VT = Op.getValueType();
5233 unsigned NumElems = VT.getVectorNumElements();
5234
5235 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
5236 // operand of these instructions is only memory, so check if there's a
5237 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
5238 // same masks.
5239 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005240
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00005241 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005242 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005243 CanFoldLoad = true;
5244
5245 // When V1 is a load, it can be folded later into a store in isel, example:
5246 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
5247 // turns into:
5248 // (MOVLPSmr addr:$src1, VR128:$src2)
5249 // So, recognize this potential and also use MOVLPS or MOVLPD
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005250 if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005251 CanFoldLoad = true;
5252
5253 if (CanFoldLoad) {
5254 if (HasSSE2 && NumElems == 2)
5255 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
5256
5257 if (NumElems == 4)
5258 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
5259 }
5260
5261 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5262 // movl and movlp will both match v2i64, but v2i64 is never matched by
5263 // movl earlier because we make it strict to avoid messing with the movlp load
5264 // folding logic (see the code above getMOVLP call). Match it here then,
5265 // this is horrible, but will stay like this until we move all shuffle
5266 // matching to x86 specific nodes. Note that for the 1st condition all
5267 // types are matched with movsd.
5268 if ((HasSSE2 && NumElems == 2) || !X86::isMOVLMask(SVOp))
5269 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
5270 else if (HasSSE2)
5271 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
5272
5273
5274 assert(VT != MVT::v4i32 && "unsupported shuffle type");
5275
5276 // Invert the operand order and use SHUFPS to match it.
5277 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V2, V1,
5278 X86::getShuffleSHUFImmediate(SVOp), DAG);
5279}
5280
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005281static inline unsigned getUNPCKLOpcode(EVT VT) {
5282 switch(VT.getSimpleVT().SimpleTy) {
5283 case MVT::v4i32: return X86ISD::PUNPCKLDQ;
5284 case MVT::v2i64: return X86ISD::PUNPCKLQDQ;
5285 case MVT::v4f32: return X86ISD::UNPCKLPS;
5286 case MVT::v2f64: return X86ISD::UNPCKLPD;
5287 case MVT::v16i8: return X86ISD::PUNPCKLBW;
5288 case MVT::v8i16: return X86ISD::PUNPCKLWD;
5289 default:
5290 llvm_unreachable("Unknow type for unpckl");
5291 }
5292 return 0;
5293}
5294
5295static inline unsigned getUNPCKHOpcode(EVT VT) {
5296 switch(VT.getSimpleVT().SimpleTy) {
5297 case MVT::v4i32: return X86ISD::PUNPCKHDQ;
5298 case MVT::v2i64: return X86ISD::PUNPCKHQDQ;
5299 case MVT::v4f32: return X86ISD::UNPCKHPS;
5300 case MVT::v2f64: return X86ISD::UNPCKHPD;
5301 case MVT::v16i8: return X86ISD::PUNPCKHBW;
5302 case MVT::v8i16: return X86ISD::PUNPCKHWD;
5303 default:
5304 llvm_unreachable("Unknow type for unpckh");
5305 }
5306 return 0;
5307}
5308
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005309static
5310SDValue NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG,
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005311 const TargetLowering &TLI,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005312 const X86Subtarget *Subtarget) {
5313 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5314 EVT VT = Op.getValueType();
5315 DebugLoc dl = Op.getDebugLoc();
5316 SDValue V1 = Op.getOperand(0);
5317 SDValue V2 = Op.getOperand(1);
5318
5319 if (isZeroShuffle(SVOp))
5320 return getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
5321
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005322 // Handle splat operations
5323 if (SVOp->isSplat()) {
5324 // Special case, this is the only place now where it's
5325 // allowed to return a vector_shuffle operation without
5326 // using a target specific node, because *hopefully* it
5327 // will be optimized away by the dag combiner.
5328 if (VT.getVectorNumElements() <= 4 &&
5329 CanXFormVExtractWithShuffleIntoLoad(Op, DAG, TLI))
5330 return Op;
5331
5332 // Handle splats by matching through known masks
5333 if (VT.getVectorNumElements() <= 4)
5334 return SDValue();
5335
Evan Cheng835580f2010-10-07 20:50:20 +00005336 // Canonicalize all of the remaining to v4f32.
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005337 return PromoteSplat(SVOp, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005338 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005339
5340 // If the shuffle can be profitably rewritten as a narrower shuffle, then
5341 // do it!
5342 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
5343 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5344 if (NewOp.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005345 return DAG.getNode(ISD::BITCAST, dl, VT, NewOp);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005346 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
5347 // FIXME: Figure out a cleaner way to do this.
5348 // Try to make use of movq to zero out the top part.
5349 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
5350 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5351 if (NewOp.getNode()) {
5352 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
5353 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
5354 DAG, Subtarget, dl);
5355 }
5356 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
5357 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5358 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
5359 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
5360 DAG, Subtarget, dl);
5361 }
5362 }
5363 return SDValue();
5364}
5365
Dan Gohman475871a2008-07-27 21:46:04 +00005366SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005367X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00005368 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00005369 SDValue V1 = Op.getOperand(0);
5370 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00005371 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005372 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00005373 unsigned NumElems = VT.getVectorNumElements();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005374 bool isMMX = VT.getSizeInBits() == 64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005375 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
5376 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00005377 bool V1IsSplat = false;
5378 bool V2IsSplat = false;
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005379 bool HasSSE2 = Subtarget->hasSSE2() || Subtarget->hasAVX();
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00005380 bool HasSSE3 = Subtarget->hasSSE3() || Subtarget->hasAVX();
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00005381 bool HasSSSE3 = Subtarget->hasSSSE3() || Subtarget->hasAVX();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005382 MachineFunction &MF = DAG.getMachineFunction();
5383 bool OptForSize = MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005384
Dale Johannesen0488fb62010-09-30 23:57:10 +00005385 // Shuffle operations on MMX not supported.
5386 if (isMMX)
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00005387 return Op;
5388
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005389 // Vector shuffle lowering takes 3 steps:
5390 //
5391 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
5392 // narrowing and commutation of operands should be handled.
5393 // 2) Matching of shuffles with known shuffle masks to x86 target specific
5394 // shuffle nodes.
5395 // 3) Rewriting of unmatched masks into new generic shuffle operations,
5396 // so the shuffle can be broken into other shuffles and the legalizer can
5397 // try the lowering again.
5398 //
5399 // The general ideia is that no vector_shuffle operation should be left to
5400 // be matched during isel, all of them must be converted to a target specific
5401 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00005402
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005403 // Normalize the input vectors. Here splats, zeroed vectors, profitable
5404 // narrowing and commutation of operands should be handled. The actual code
5405 // doesn't include all of those, work in progress...
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005406 SDValue NewOp = NormalizeVectorShuffle(Op, DAG, *this, Subtarget);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005407 if (NewOp.getNode())
5408 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00005409
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005410 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
5411 // unpckh_undef). Only use pshufd if speed is more important than size.
5412 if (OptForSize && X86::isUNPCKL_v_undef_Mask(SVOp))
5413 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5414 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V1, DAG);
5415 if (OptForSize && X86::isUNPCKH_v_undef_Mask(SVOp))
5416 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5417 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00005418
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005419 if (X86::isMOVDDUPMask(SVOp) && HasSSE3 && V2IsUndef &&
Dale Johannesen0488fb62010-09-30 23:57:10 +00005420 RelaxedMayFoldVectorLoad(V1))
Evan Cheng835580f2010-10-07 20:50:20 +00005421 return getMOVDDup(Op, dl, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005422
Dale Johannesen0488fb62010-09-30 23:57:10 +00005423 if (X86::isMOVHLPS_v_undef_Mask(SVOp))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005424 return getMOVHighToLow(Op, dl, DAG);
5425
5426 // Use to match splats
5427 if (HasSSE2 && X86::isUNPCKHMask(SVOp) && V2IsUndef &&
5428 (VT == MVT::v2f64 || VT == MVT::v2i64))
5429 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
5430
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005431 if (X86::isPSHUFDMask(SVOp)) {
5432 // The actual implementation will match the mask in the if above and then
5433 // during isel it can match several different instructions, not only pshufd
5434 // as its name says, sad but true, emulate the behavior for now...
5435 if (X86::isMOVDDUPMask(SVOp) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
5436 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
5437
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005438 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
5439
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005440 if (HasSSE2 && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005441 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
5442
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005443 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005444 return getTargetShuffleNode(X86ISD::SHUFPD, dl, VT, V1, V1,
5445 TargetMask, DAG);
5446
5447 if (VT == MVT::v4f32)
5448 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V1, V1,
5449 TargetMask, DAG);
5450 }
Eric Christopherfd179292009-08-27 18:07:15 +00005451
Evan Chengf26ffe92008-05-29 08:22:04 +00005452 // Check if this can be converted into a logical shift.
5453 bool isLeft = false;
5454 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00005455 SDValue ShVal;
Nate Begeman9008ca62009-04-27 18:41:29 +00005456 bool isShift = getSubtarget()->hasSSE2() &&
Evan Chengc3630942009-12-09 21:00:30 +00005457 isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00005458 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00005459 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00005460 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00005461 EVT EltVT = VT.getVectorElementType();
5462 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00005463 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005464 }
Eric Christopherfd179292009-08-27 18:07:15 +00005465
Nate Begeman9008ca62009-04-27 18:41:29 +00005466 if (X86::isMOVLMask(SVOp)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005467 if (V1IsUndef)
5468 return V2;
Gabor Greifba36cb52008-08-28 21:40:38 +00005469 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00005470 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Dale Johannesen0488fb62010-09-30 23:57:10 +00005471 if (!X86::isMOVLPMask(SVOp)) {
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005472 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00005473 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
5474
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005475 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00005476 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
5477 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00005478 }
Eric Christopherfd179292009-08-27 18:07:15 +00005479
Nate Begeman9008ca62009-04-27 18:41:29 +00005480 // FIXME: fold these into legal mask.
Dale Johannesen0488fb62010-09-30 23:57:10 +00005481 if (X86::isMOVLHPSMask(SVOp) && !X86::isUNPCKLMask(SVOp))
5482 return getMOVLowToHigh(Op, dl, DAG, HasSSE2);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005483
Dale Johannesen0488fb62010-09-30 23:57:10 +00005484 if (X86::isMOVHLPSMask(SVOp))
5485 return getMOVHighToLow(Op, dl, DAG);
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00005486
Dale Johannesen0488fb62010-09-30 23:57:10 +00005487 if (X86::isMOVSHDUPMask(SVOp) && HasSSE3 && V2IsUndef && NumElems == 4)
5488 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00005489
Dale Johannesen0488fb62010-09-30 23:57:10 +00005490 if (X86::isMOVSLDUPMask(SVOp) && HasSSE3 && V2IsUndef && NumElems == 4)
5491 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00005492
Dale Johannesen0488fb62010-09-30 23:57:10 +00005493 if (X86::isMOVLPMask(SVOp))
5494 return getMOVLP(Op, dl, DAG, HasSSE2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005495
Nate Begeman9008ca62009-04-27 18:41:29 +00005496 if (ShouldXformToMOVHLPS(SVOp) ||
5497 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
5498 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005499
Evan Chengf26ffe92008-05-29 08:22:04 +00005500 if (isShift) {
5501 // No better options. Use a vshl / vsrl.
Dan Gohman8a55ce42009-09-23 21:02:20 +00005502 EVT EltVT = VT.getVectorElementType();
5503 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00005504 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005505 }
Eric Christopherfd179292009-08-27 18:07:15 +00005506
Evan Cheng9eca5e82006-10-25 21:49:50 +00005507 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00005508 // FIXME: This should also accept a bitcast of a splat? Be careful, not
5509 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00005510 V1IsSplat = isSplatVector(V1.getNode());
5511 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00005512
Chris Lattner8a594482007-11-25 00:24:49 +00005513 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00005514 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005515 Op = CommuteVectorShuffle(SVOp, DAG);
5516 SVOp = cast<ShuffleVectorSDNode>(Op);
5517 V1 = SVOp->getOperand(0);
5518 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00005519 std::swap(V1IsSplat, V2IsSplat);
5520 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00005521 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00005522 }
5523
Nate Begeman9008ca62009-04-27 18:41:29 +00005524 if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) {
5525 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00005526 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00005527 return V1;
5528 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
5529 // the instruction selector will not match, so get a canonical MOVL with
5530 // swapped operands to undo the commute.
5531 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00005532 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005533
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005534 if (X86::isUNPCKLMask(SVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005535 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005536
5537 if (X86::isUNPCKHMask(SVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005538 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V2, DAG);
Evan Chenge1113032006-10-04 18:33:38 +00005539
Evan Cheng9bbbb982006-10-25 20:48:19 +00005540 if (V2IsSplat) {
5541 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005542 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00005543 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00005544 SDValue NewMask = NormalizeMask(SVOp, DAG);
5545 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
5546 if (NSVOp != SVOp) {
5547 if (X86::isUNPCKLMask(NSVOp, true)) {
5548 return NewMask;
5549 } else if (X86::isUNPCKHMask(NSVOp, true)) {
5550 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005551 }
5552 }
5553 }
5554
Evan Cheng9eca5e82006-10-25 21:49:50 +00005555 if (Commuted) {
5556 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00005557 // FIXME: this seems wrong.
5558 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
5559 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005560
5561 if (X86::isUNPCKLMask(NewSVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005562 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V2, V1, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005563
5564 if (X86::isUNPCKHMask(NewSVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005565 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V2, V1, DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00005566 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005567
Nate Begeman9008ca62009-04-27 18:41:29 +00005568 // Normalize the node to match x86 shuffle ops if needed
Dale Johannesen0488fb62010-09-30 23:57:10 +00005569 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp))
Nate Begeman9008ca62009-04-27 18:41:29 +00005570 return CommuteVectorShuffle(SVOp, DAG);
5571
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00005572 // The checks below are all present in isShuffleMaskLegal, but they are
5573 // inlined here right now to enable us to directly emit target specific
5574 // nodes, and remove one by one until they don't return Op anymore.
5575 SmallVector<int, 16> M;
5576 SVOp->getMask(M);
5577
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00005578 if (isPALIGNRMask(M, VT, HasSSSE3))
5579 return getTargetShuffleNode(X86ISD::PALIGN, dl, VT, V1, V2,
5580 X86::getShufflePALIGNRImmediate(SVOp),
5581 DAG);
5582
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00005583 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
5584 SVOp->getSplatIndex() == 0 && V2IsUndef) {
5585 if (VT == MVT::v2f64)
5586 return getTargetShuffleNode(X86ISD::UNPCKLPD, dl, VT, V1, V1, DAG);
5587 if (VT == MVT::v2i64)
5588 return getTargetShuffleNode(X86ISD::PUNPCKLQDQ, dl, VT, V1, V1, DAG);
5589 }
5590
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00005591 if (isPSHUFHWMask(M, VT))
5592 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
5593 X86::getShufflePSHUFHWImmediate(SVOp),
5594 DAG);
5595
5596 if (isPSHUFLWMask(M, VT))
5597 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
5598 X86::getShufflePSHUFLWImmediate(SVOp),
5599 DAG);
5600
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00005601 if (isSHUFPMask(M, VT)) {
5602 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
5603 if (VT == MVT::v4f32 || VT == MVT::v4i32)
5604 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V1, V2,
5605 TargetMask, DAG);
5606 if (VT == MVT::v2f64 || VT == MVT::v2i64)
5607 return getTargetShuffleNode(X86ISD::SHUFPD, dl, VT, V1, V2,
5608 TargetMask, DAG);
5609 }
5610
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005611 if (X86::isUNPCKL_v_undef_Mask(SVOp))
5612 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5613 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V1, DAG);
5614 if (X86::isUNPCKH_v_undef_Mask(SVOp))
5615 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5616 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
5617
Evan Cheng14b32e12007-12-11 01:46:18 +00005618 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
Owen Anderson825b72b2009-08-11 20:47:22 +00005619 if (VT == MVT::v8i16) {
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005620 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00005621 if (NewOp.getNode())
Evan Cheng14b32e12007-12-11 01:46:18 +00005622 return NewOp;
5623 }
5624
Owen Anderson825b72b2009-08-11 20:47:22 +00005625 if (VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005626 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005627 if (NewOp.getNode())
5628 return NewOp;
5629 }
Eric Christopherfd179292009-08-27 18:07:15 +00005630
Dale Johannesen0488fb62010-09-30 23:57:10 +00005631 // Handle all 4 wide cases with a number of shuffles.
5632 if (NumElems == 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00005633 return LowerVECTOR_SHUFFLE_4wide(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005634
Dan Gohman475871a2008-07-27 21:46:04 +00005635 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005636}
5637
Dan Gohman475871a2008-07-27 21:46:04 +00005638SDValue
5639X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00005640 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005641 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005642 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005643 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005644 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005645 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00005646 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005647 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00005648 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005649 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00005650 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
5651 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
5652 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005653 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
5654 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005655 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005656 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00005657 Op.getOperand(0)),
5658 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00005659 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005660 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00005661 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005662 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00005663 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Owen Anderson825b72b2009-08-11 20:47:22 +00005664 } else if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00005665 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
5666 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00005667 // result has a single use which is a store or a bitcast to i32. And in
5668 // the case of a store, it's not worth it if the index is a constant 0,
5669 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00005670 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00005671 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00005672 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00005673 if ((User->getOpcode() != ISD::STORE ||
5674 (isa<ConstantSDNode>(Op.getOperand(1)) &&
5675 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005676 (User->getOpcode() != ISD::BITCAST ||
Owen Anderson825b72b2009-08-11 20:47:22 +00005677 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00005678 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00005679 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005680 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00005681 Op.getOperand(0)),
5682 Op.getOperand(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005683 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Extract);
Owen Anderson825b72b2009-08-11 20:47:22 +00005684 } else if (VT == MVT::i32) {
Mon P Wangf0fcdd82009-01-15 21:10:20 +00005685 // ExtractPS works with constant index.
5686 if (isa<ConstantSDNode>(Op.getOperand(1)))
5687 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00005688 }
Dan Gohman475871a2008-07-27 21:46:04 +00005689 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00005690}
5691
5692
Dan Gohman475871a2008-07-27 21:46:04 +00005693SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005694X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
5695 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005696 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00005697 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005698
Evan Cheng62a3f152008-03-24 21:52:23 +00005699 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005700 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00005701 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00005702 return Res;
5703 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00005704
Owen Andersone50ed302009-08-10 22:56:29 +00005705 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005706 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005707 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005708 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00005709 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005710 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00005711 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005712 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
5713 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005714 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005715 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00005716 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005717 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00005718 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00005719 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00005720 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00005721 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00005722 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00005723 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005724 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005725 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005726 if (Idx == 0)
5727 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00005728
Evan Cheng0db9fe62006-04-25 20:13:52 +00005729 // SHUFPS the element to the lowest double word, then movss.
Nate Begeman9008ca62009-04-27 18:41:29 +00005730 int Mask[4] = { Idx, -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00005731 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00005732 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00005733 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00005734 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00005735 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00005736 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00005737 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
5738 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
5739 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005740 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005741 if (Idx == 0)
5742 return Op;
5743
5744 // UNPCKHPD the element to the lowest double word, then movsd.
5745 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
5746 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00005747 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00005748 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00005749 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00005750 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00005751 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00005752 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005753 }
5754
Dan Gohman475871a2008-07-27 21:46:04 +00005755 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005756}
5757
Dan Gohman475871a2008-07-27 21:46:04 +00005758SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005759X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op,
5760 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005761 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00005762 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005763 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00005764
Dan Gohman475871a2008-07-27 21:46:04 +00005765 SDValue N0 = Op.getOperand(0);
5766 SDValue N1 = Op.getOperand(1);
5767 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00005768
Dan Gohman8a55ce42009-09-23 21:02:20 +00005769 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00005770 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00005771 unsigned Opc;
5772 if (VT == MVT::v8i16)
5773 Opc = X86ISD::PINSRW;
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00005774 else if (VT == MVT::v16i8)
5775 Opc = X86ISD::PINSRB;
5776 else
5777 Opc = X86ISD::PINSRB;
5778
Nate Begeman14d12ca2008-02-11 04:19:36 +00005779 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
5780 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00005781 if (N1.getValueType() != MVT::i32)
5782 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
5783 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005784 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00005785 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00005786 } else if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00005787 // Bits [7:6] of the constant are the source select. This will always be
5788 // zero here. The DAG Combiner may combine an extract_elt index into these
5789 // bits. For example (insert (extract, 3), 2) could be matched by putting
5790 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00005791 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00005792 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00005793 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00005794 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005795 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00005796 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00005797 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00005798 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00005799 } else if (EltVT == MVT::i32 && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00005800 // PINSR* works with constant index.
5801 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00005802 }
Dan Gohman475871a2008-07-27 21:46:04 +00005803 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00005804}
5805
Dan Gohman475871a2008-07-27 21:46:04 +00005806SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005807X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005808 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00005809 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00005810
5811 if (Subtarget->hasSSE41())
5812 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
5813
Dan Gohman8a55ce42009-09-23 21:02:20 +00005814 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00005815 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00005816
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005817 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00005818 SDValue N0 = Op.getOperand(0);
5819 SDValue N1 = Op.getOperand(1);
5820 SDValue N2 = Op.getOperand(2);
Evan Cheng794405e2007-12-12 07:55:34 +00005821
Dan Gohman8a55ce42009-09-23 21:02:20 +00005822 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00005823 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
5824 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00005825 if (N1.getValueType() != MVT::i32)
5826 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
5827 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005828 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesen0488fb62010-09-30 23:57:10 +00005829 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005830 }
Dan Gohman475871a2008-07-27 21:46:04 +00005831 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005832}
5833
Dan Gohman475871a2008-07-27 21:46:04 +00005834SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005835X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005836 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00005837
Chris Lattnerf172ecd2010-07-04 23:07:25 +00005838 if (Op.getValueType() == MVT::v1i64 &&
5839 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00005840 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00005841
Owen Anderson825b72b2009-08-11 20:47:22 +00005842 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Dale Johannesen0488fb62010-09-30 23:57:10 +00005843 assert(Op.getValueType().getSimpleVT().getSizeInBits() == 128 &&
5844 "Expected an SSE type!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005845 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(),
Dale Johannesen0488fb62010-09-30 23:57:10 +00005846 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005847}
5848
David Greene91585092011-01-26 15:38:49 +00005849// Lower a node with an EXTRACT_SUBVECTOR opcode. This may result in
5850// a simple subregister reference or explicit instructions to grab
5851// upper bits of a vector.
5852SDValue
5853X86TargetLowering::LowerEXTRACT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
5854 if (Subtarget->hasAVX()) {
5855 // TODO
5856 }
5857 return SDValue();
5858}
5859
David Greenecfe33c42011-01-26 19:13:22 +00005860// Lower a node with an INSERT_SUBVECTOR opcode. This may result in a
5861// simple superregister reference or explicit instructions to insert
5862// the upper bits of a vector.
5863SDValue
5864X86TargetLowering::LowerINSERT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
5865 if (Subtarget->hasAVX()) {
5866 DebugLoc dl = Op.getNode()->getDebugLoc();
5867 SDValue Vec = Op.getNode()->getOperand(0);
5868 SDValue SubVec = Op.getNode()->getOperand(1);
5869 SDValue Idx = Op.getNode()->getOperand(2);
5870
5871 if (Op.getNode()->getValueType(0).getSizeInBits() == 256
5872 && SubVec.getNode()->getValueType(0).getSizeInBits() == 128) {
5873 // TODO
5874 }
5875 }
5876 return SDValue();
5877}
5878
Bill Wendling056292f2008-09-16 21:48:12 +00005879// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
5880// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
5881// one of the above mentioned nodes. It has to be wrapped because otherwise
5882// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
5883// be used to form addressing mode. These wrapped nodes will be selected
5884// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00005885SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005886X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005887 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00005888
Chris Lattner41621a22009-06-26 19:22:52 +00005889 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
5890 // global base reg.
5891 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00005892 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005893 CodeModel::Model M = getTargetMachine().getCodeModel();
5894
Chris Lattner4f066492009-07-11 20:29:19 +00005895 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005896 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00005897 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00005898 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005899 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00005900 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005901 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00005902
Evan Cheng1606e8e2009-03-13 07:51:59 +00005903 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00005904 CP->getAlignment(),
5905 CP->getOffset(), OpFlag);
5906 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00005907 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00005908 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00005909 if (OpFlag) {
5910 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00005911 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00005912 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00005913 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005914 }
5915
5916 return Result;
5917}
5918
Dan Gohmand858e902010-04-17 15:26:15 +00005919SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00005920 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00005921
Chris Lattner18c59872009-06-27 04:16:01 +00005922 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
5923 // global base reg.
5924 unsigned char OpFlag = 0;
5925 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005926 CodeModel::Model M = getTargetMachine().getCodeModel();
5927
Chris Lattner4f066492009-07-11 20:29:19 +00005928 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005929 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00005930 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00005931 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005932 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00005933 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005934 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00005935
Chris Lattner18c59872009-06-27 04:16:01 +00005936 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
5937 OpFlag);
5938 DebugLoc DL = JT->getDebugLoc();
5939 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00005940
Chris Lattner18c59872009-06-27 04:16:01 +00005941 // With PIC, the address is actually $g + Offset.
Chris Lattner1e61e692010-11-15 02:46:57 +00005942 if (OpFlag)
Chris Lattner18c59872009-06-27 04:16:01 +00005943 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
5944 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00005945 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00005946 Result);
Eric Christopherfd179292009-08-27 18:07:15 +00005947
Chris Lattner18c59872009-06-27 04:16:01 +00005948 return Result;
5949}
5950
5951SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005952X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00005953 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00005954
Chris Lattner18c59872009-06-27 04:16:01 +00005955 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
5956 // global base reg.
5957 unsigned char OpFlag = 0;
5958 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005959 CodeModel::Model M = getTargetMachine().getCodeModel();
5960
Chris Lattner4f066492009-07-11 20:29:19 +00005961 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005962 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00005963 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00005964 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005965 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00005966 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005967 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00005968
Chris Lattner18c59872009-06-27 04:16:01 +00005969 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00005970
Chris Lattner18c59872009-06-27 04:16:01 +00005971 DebugLoc DL = Op.getDebugLoc();
5972 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00005973
5974
Chris Lattner18c59872009-06-27 04:16:01 +00005975 // With PIC, the address is actually $g + Offset.
5976 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00005977 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00005978 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
5979 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00005980 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00005981 Result);
5982 }
Eric Christopherfd179292009-08-27 18:07:15 +00005983
Chris Lattner18c59872009-06-27 04:16:01 +00005984 return Result;
5985}
5986
Dan Gohman475871a2008-07-27 21:46:04 +00005987SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005988X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00005989 // Create the TargetBlockAddressAddress node.
5990 unsigned char OpFlags =
5991 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00005992 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00005993 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Dan Gohman29cbade2009-11-20 23:18:13 +00005994 DebugLoc dl = Op.getDebugLoc();
5995 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(),
5996 /*isTarget=*/true, OpFlags);
5997
Dan Gohmanf705adb2009-10-30 01:28:02 +00005998 if (Subtarget->isPICStyleRIPRel() &&
5999 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00006000 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
6001 else
6002 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00006003
Dan Gohman29cbade2009-11-20 23:18:13 +00006004 // With PIC, the address is actually $g + Offset.
6005 if (isGlobalRelativeToPICBase(OpFlags)) {
6006 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
6007 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
6008 Result);
6009 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00006010
6011 return Result;
6012}
6013
6014SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00006015X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00006016 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00006017 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00006018 // Create the TargetGlobalAddress node, folding in the constant
6019 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00006020 unsigned char OpFlags =
6021 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006022 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00006023 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006024 if (OpFlags == X86II::MO_NO_FLAG &&
6025 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00006026 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00006027 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00006028 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00006029 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00006030 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00006031 }
Eric Christopherfd179292009-08-27 18:07:15 +00006032
Chris Lattner4f066492009-07-11 20:29:19 +00006033 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006034 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00006035 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
6036 else
6037 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00006038
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006039 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00006040 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00006041 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
6042 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006043 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006044 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006045
Chris Lattner36c25012009-07-10 07:34:39 +00006046 // For globals that require a load from a stub to get the address, emit the
6047 // load.
6048 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00006049 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00006050 MachinePointerInfo::getGOT(), false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006051
Dan Gohman6520e202008-10-18 02:06:02 +00006052 // If there was a non-zero offset that we didn't fold, create an explicit
6053 // addition for it.
6054 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00006055 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00006056 DAG.getConstant(Offset, getPointerTy()));
6057
Evan Cheng0db9fe62006-04-25 20:13:52 +00006058 return Result;
6059}
6060
Evan Chengda43bcf2008-09-24 00:05:32 +00006061SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006062X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00006063 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00006064 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006065 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00006066}
6067
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006068static SDValue
6069GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00006070 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Chris Lattnerb903bed2009-06-26 21:20:29 +00006071 unsigned char OperandFlags) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006072 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006073 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006074 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00006075 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006076 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00006077 GA->getOffset(),
6078 OperandFlags);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006079 if (InFlag) {
6080 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00006081 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006082 } else {
6083 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00006084 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006085 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006086
6087 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00006088 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006089
Rafael Espindola15f1b662009-04-24 12:59:40 +00006090 SDValue Flag = Chain.getValue(1);
6091 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006092}
6093
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006094// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00006095static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006096LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006097 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00006098 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00006099 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
6100 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006101 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006102 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006103 InFlag = Chain.getValue(1);
6104
Chris Lattnerb903bed2009-06-26 21:20:29 +00006105 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006106}
6107
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006108// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00006109static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006110LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006111 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00006112 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
6113 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006114}
6115
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006116// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
6117// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00006118static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006119 const EVT PtrVT, TLSModel::Model model,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00006120 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00006121 DebugLoc dl = GA->getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00006122
Chris Lattnerf93b90c2010-09-22 04:39:11 +00006123 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
6124 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
6125 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00006126
Michael J. Spencerec38de22010-10-10 22:04:20 +00006127 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Chris Lattnerf93b90c2010-09-22 04:39:11 +00006128 DAG.getIntPtrConstant(0),
6129 MachinePointerInfo(Ptr), false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00006130
Chris Lattnerb903bed2009-06-26 21:20:29 +00006131 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00006132 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
6133 // initialexec.
6134 unsigned WrapperKind = X86ISD::Wrapper;
6135 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00006136 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Chris Lattner18c59872009-06-27 04:16:01 +00006137 } else if (is64Bit) {
6138 assert(model == TLSModel::InitialExec);
6139 OperandFlags = X86II::MO_GOTTPOFF;
6140 WrapperKind = X86ISD::WrapperRIP;
6141 } else {
6142 assert(model == TLSModel::InitialExec);
6143 OperandFlags = X86II::MO_INDNTPOFF;
Chris Lattnerb903bed2009-06-26 21:20:29 +00006144 }
Eric Christopherfd179292009-08-27 18:07:15 +00006145
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006146 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
6147 // exec)
Michael J. Spencerec38de22010-10-10 22:04:20 +00006148 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Devang Patel0d881da2010-07-06 22:08:15 +00006149 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00006150 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00006151 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00006152
Rafael Espindola9a580232009-02-27 13:37:18 +00006153 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00006154 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00006155 MachinePointerInfo::getGOT(), false, false, 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00006156
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006157 // The address of the thread local variable is the add of the thread
6158 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00006159 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006160}
6161
Dan Gohman475871a2008-07-27 21:46:04 +00006162SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006163X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00006164
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006165 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00006166 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00006167
Eric Christopher30ef0e52010-06-03 04:07:48 +00006168 if (Subtarget->isTargetELF()) {
6169 // TODO: implement the "local dynamic" model
6170 // TODO: implement the "initial exec"model for pic executables
Michael J. Spencerec38de22010-10-10 22:04:20 +00006171
Eric Christopher30ef0e52010-06-03 04:07:48 +00006172 // If GV is an alias then use the aliasee for determining
6173 // thread-localness.
6174 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
6175 GV = GA->resolveAliasedGlobal(false);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006176
6177 TLSModel::Model model
Eric Christopher30ef0e52010-06-03 04:07:48 +00006178 = getTLSModel(GV, getTargetMachine().getRelocationModel());
Michael J. Spencerec38de22010-10-10 22:04:20 +00006179
Eric Christopher30ef0e52010-06-03 04:07:48 +00006180 switch (model) {
6181 case TLSModel::GeneralDynamic:
6182 case TLSModel::LocalDynamic: // not implemented
6183 if (Subtarget->is64Bit())
6184 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
6185 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Michael J. Spencerec38de22010-10-10 22:04:20 +00006186
Eric Christopher30ef0e52010-06-03 04:07:48 +00006187 case TLSModel::InitialExec:
6188 case TLSModel::LocalExec:
6189 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
6190 Subtarget->is64Bit());
6191 }
6192 } else if (Subtarget->isTargetDarwin()) {
6193 // Darwin only has one model of TLS. Lower to that.
6194 unsigned char OpFlag = 0;
6195 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
6196 X86ISD::WrapperRIP : X86ISD::Wrapper;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006197
Eric Christopher30ef0e52010-06-03 04:07:48 +00006198 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6199 // global base reg.
6200 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
6201 !Subtarget->is64Bit();
6202 if (PIC32)
6203 OpFlag = X86II::MO_TLVP_PIC_BASE;
6204 else
6205 OpFlag = X86II::MO_TLVP;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006206 DebugLoc DL = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00006207 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopherd8c05362010-12-09 06:25:53 +00006208 GA->getValueType(0),
Eric Christopher30ef0e52010-06-03 04:07:48 +00006209 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00006210 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006211
Eric Christopher30ef0e52010-06-03 04:07:48 +00006212 // With PIC32, the address is actually $g + Offset.
6213 if (PIC32)
6214 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
6215 DAG.getNode(X86ISD::GlobalBaseReg,
6216 DebugLoc(), getPointerTy()),
6217 Offset);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006218
Eric Christopher30ef0e52010-06-03 04:07:48 +00006219 // Lowering the machine isd will make sure everything is in the right
6220 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00006221 SDValue Chain = DAG.getEntryNode();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006222 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Eric Christopherd8c05362010-12-09 06:25:53 +00006223 SDValue Args[] = { Chain, Offset };
6224 Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006225
Eric Christopher30ef0e52010-06-03 04:07:48 +00006226 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
6227 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
6228 MFI->setAdjustsStack(true);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00006229
Eric Christopher30ef0e52010-06-03 04:07:48 +00006230 // And our return value (tls address) is in the standard call return value
6231 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00006232 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
6233 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy());
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006234 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00006235
Eric Christopher30ef0e52010-06-03 04:07:48 +00006236 assert(false &&
6237 "TLS not implemented for this target.");
Eric Christopherfd179292009-08-27 18:07:15 +00006238
Torok Edwinc23197a2009-07-14 16:55:14 +00006239 llvm_unreachable("Unreachable");
Chris Lattner5867de12009-04-01 22:14:45 +00006240 return SDValue();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006241}
6242
Evan Cheng0db9fe62006-04-25 20:13:52 +00006243
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006244/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michelfdc40a02009-02-17 22:15:04 +00006245/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00006246SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman4c1fa612008-03-03 22:22:09 +00006247 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00006248 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006249 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006250 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006251 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00006252 SDValue ShOpLo = Op.getOperand(0);
6253 SDValue ShOpHi = Op.getOperand(1);
6254 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00006255 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00006256 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00006257 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00006258
Dan Gohman475871a2008-07-27 21:46:04 +00006259 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006260 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00006261 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
6262 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006263 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00006264 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
6265 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006266 }
Evan Chenge3413162006-01-09 18:33:28 +00006267
Owen Anderson825b72b2009-08-11 20:47:22 +00006268 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
6269 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00006270 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00006271 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00006272
Dan Gohman475871a2008-07-27 21:46:04 +00006273 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00006274 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00006275 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
6276 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00006277
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006278 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00006279 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
6280 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006281 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00006282 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
6283 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006284 }
6285
Dan Gohman475871a2008-07-27 21:46:04 +00006286 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00006287 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006288}
Evan Chenga3195e82006-01-12 22:54:21 +00006289
Dan Gohmand858e902010-04-17 15:26:15 +00006290SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
6291 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006292 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00006293
Dale Johannesen0488fb62010-09-30 23:57:10 +00006294 if (SrcVT.isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00006295 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00006296
Owen Anderson825b72b2009-08-11 20:47:22 +00006297 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00006298 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00006299
Eli Friedman36df4992009-05-27 00:47:34 +00006300 // These are really Legal; return the operand so the caller accepts it as
6301 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00006302 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00006303 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00006304 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00006305 Subtarget->is64Bit()) {
6306 return Op;
6307 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006308
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006309 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006310 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006311 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00006312 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006313 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00006314 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00006315 StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006316 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006317 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00006318 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
6319}
Evan Cheng0db9fe62006-04-25 20:13:52 +00006320
Owen Andersone50ed302009-08-10 22:56:29 +00006321SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Michael J. Spencerec38de22010-10-10 22:04:20 +00006322 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00006323 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006324 // Build the FILD
Chris Lattner492a43e2010-09-22 01:28:21 +00006325 DebugLoc DL = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00006326 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00006327 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006328 if (useSSE)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006329 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Glue);
Chris Lattner5a88b832007-02-25 07:10:00 +00006330 else
Owen Anderson825b72b2009-08-11 20:47:22 +00006331 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006332
Chris Lattner492a43e2010-09-22 01:28:21 +00006333 unsigned ByteSize = SrcVT.getSizeInBits()/8;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006334
Chris Lattner492a43e2010-09-22 01:28:21 +00006335 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
6336 MachineMemOperand *MMO =
6337 DAG.getMachineFunction()
6338 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6339 MachineMemOperand::MOLoad, ByteSize, ByteSize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006340
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006341 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Chris Lattner492a43e2010-09-22 01:28:21 +00006342 SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
6343 X86ISD::FILD, DL,
6344 Tys, Ops, array_lengthof(Ops),
6345 SrcVT, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006346
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006347 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006348 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00006349 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006350
6351 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
6352 // shouldn't be necessary except that RFP cannot be live across
6353 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006354 MachineFunction &MF = DAG.getMachineFunction();
Bob Wilsoneafca4e2010-09-22 17:35:14 +00006355 unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
6356 int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006357 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00006358 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006359 SDValue Ops[] = {
6360 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
6361 };
Chris Lattner492a43e2010-09-22 01:28:21 +00006362 MachineMemOperand *MMO =
6363 DAG.getMachineFunction()
6364 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
Bob Wilsoneafca4e2010-09-22 17:35:14 +00006365 MachineMemOperand::MOStore, SSFISize, SSFISize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006366
Chris Lattner492a43e2010-09-22 01:28:21 +00006367 Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
6368 Ops, array_lengthof(Ops),
6369 Op.getValueType(), MMO);
6370 Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006371 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006372 false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006373 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006374
Evan Cheng0db9fe62006-04-25 20:13:52 +00006375 return Result;
6376}
6377
Bill Wendling8b8a6362009-01-17 03:56:04 +00006378// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00006379SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
6380 SelectionDAG &DAG) const {
Bill Wendling8b8a6362009-01-17 03:56:04 +00006381 // This algorithm is not obvious. Here it is in C code, more or less:
6382 /*
6383 double uint64_to_double( uint32_t hi, uint32_t lo ) {
6384 static const __m128i exp = { 0x4330000045300000ULL, 0 };
6385 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00006386
Bill Wendling8b8a6362009-01-17 03:56:04 +00006387 // Copy ints to xmm registers.
6388 __m128i xh = _mm_cvtsi32_si128( hi );
6389 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00006390
Bill Wendling8b8a6362009-01-17 03:56:04 +00006391 // Combine into low half of a single xmm register.
6392 __m128i x = _mm_unpacklo_epi32( xh, xl );
6393 __m128d d;
6394 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00006395
Bill Wendling8b8a6362009-01-17 03:56:04 +00006396 // Merge in appropriate exponents to give the integer bits the right
6397 // magnitude.
6398 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00006399
Bill Wendling8b8a6362009-01-17 03:56:04 +00006400 // Subtract away the biases to deal with the IEEE-754 double precision
6401 // implicit 1.
6402 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00006403
Bill Wendling8b8a6362009-01-17 03:56:04 +00006404 // All conversions up to here are exact. The correctly rounded result is
6405 // calculated using the current rounding mode using the following
6406 // horizontal add.
6407 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
6408 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
6409 // store doesn't really need to be here (except
6410 // maybe to zero the other double)
6411 return sd;
6412 }
6413 */
Dale Johannesen040225f2008-10-21 23:07:49 +00006414
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006415 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00006416 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00006417
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006418 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00006419 std::vector<Constant*> CV0;
Owen Andersoneed707b2009-07-24 23:12:02 +00006420 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x45300000)));
6421 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x43300000)));
6422 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
6423 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
Owen Andersonaf7ec972009-07-28 21:19:26 +00006424 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006425 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006426
Bill Wendling8b8a6362009-01-17 03:56:04 +00006427 std::vector<Constant*> CV1;
Owen Andersona90b3dc2009-07-15 21:51:10 +00006428 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006429 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
Owen Andersona90b3dc2009-07-15 21:51:10 +00006430 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006431 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Owen Andersonaf7ec972009-07-28 21:19:26 +00006432 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006433 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006434
Owen Anderson825b72b2009-08-11 20:47:22 +00006435 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6436 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00006437 Op.getOperand(0),
6438 DAG.getIntPtrConstant(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006439 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6440 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00006441 Op.getOperand(0),
6442 DAG.getIntPtrConstant(0)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006443 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
6444 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Chris Lattnere8639032010-09-21 06:22:23 +00006445 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006446 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00006447 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006448 SDValue XR2F = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Unpck2);
Owen Anderson825b72b2009-08-11 20:47:22 +00006449 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00006450 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006451 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00006452 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006453
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006454 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman9008ca62009-04-27 18:41:29 +00006455 int ShufMask[2] = { 1, -1 };
Owen Anderson825b72b2009-08-11 20:47:22 +00006456 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
6457 DAG.getUNDEF(MVT::v2f64), ShufMask);
6458 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
6459 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006460 DAG.getIntPtrConstant(0));
6461}
6462
Bill Wendling8b8a6362009-01-17 03:56:04 +00006463// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00006464SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
6465 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006466 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00006467 // FP constant to bias correct the final result.
6468 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00006469 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006470
6471 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00006472 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6473 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Bill Wendling8b8a6362009-01-17 03:56:04 +00006474 Op.getOperand(0),
6475 DAG.getIntPtrConstant(0)));
6476
Owen Anderson825b72b2009-08-11 20:47:22 +00006477 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006478 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00006479 DAG.getIntPtrConstant(0));
6480
6481 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00006482 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006483 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00006484 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006485 MVT::v2f64, Load)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006486 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00006487 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006488 MVT::v2f64, Bias)));
6489 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006490 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00006491 DAG.getIntPtrConstant(0));
6492
6493 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00006494 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006495
6496 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00006497 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00006498
Owen Anderson825b72b2009-08-11 20:47:22 +00006499 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006500 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00006501 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00006502 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006503 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00006504 }
6505
6506 // Handle final rounding.
6507 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00006508}
6509
Dan Gohmand858e902010-04-17 15:26:15 +00006510SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
6511 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00006512 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006513 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00006514
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006515 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00006516 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
6517 // the optimization here.
6518 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00006519 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00006520
Owen Andersone50ed302009-08-10 22:56:29 +00006521 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006522 EVT DstVT = Op.getValueType();
6523 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00006524 return LowerUINT_TO_FP_i64(Op, DAG);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006525 else if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00006526 return LowerUINT_TO_FP_i32(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00006527
6528 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00006529 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006530 if (SrcVT == MVT::i32) {
6531 SDValue WordOff = DAG.getConstant(4, getPointerTy());
6532 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
6533 getPointerTy(), StackSlot, WordOff);
6534 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006535 StackSlot, MachinePointerInfo(),
6536 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006537 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006538 OffsetSlot, MachinePointerInfo(),
6539 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006540 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
6541 return Fild;
6542 }
6543
6544 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
6545 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006546 StackSlot, MachinePointerInfo(),
6547 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006548 // For i64 source, we need to add the appropriate power of 2 if the input
6549 // was negative. This is the same as the optimization in
6550 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
6551 // we must be careful to do the computation in x87 extended precision, not
6552 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00006553 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
6554 MachineMemOperand *MMO =
6555 DAG.getMachineFunction()
6556 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6557 MachineMemOperand::MOLoad, 8, 8);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006558
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006559 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
6560 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Chris Lattner492a43e2010-09-22 01:28:21 +00006561 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, 3,
6562 MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006563
6564 APInt FF(32, 0x5F800000ULL);
6565
6566 // Check whether the sign bit is set.
6567 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
6568 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
6569 ISD::SETLT);
6570
6571 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
6572 SDValue FudgePtr = DAG.getConstantPool(
6573 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
6574 getPointerTy());
6575
6576 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
6577 SDValue Zero = DAG.getIntPtrConstant(0);
6578 SDValue Four = DAG.getIntPtrConstant(4);
6579 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
6580 Zero, Four);
6581 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
6582
6583 // Load the value out, extending it from f32 to f80.
6584 // FIXME: Avoid the extend by constructing the right constant pool?
Evan Chengbcc80172010-07-07 22:15:37 +00006585 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, MVT::f80, dl, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00006586 FudgePtr, MachinePointerInfo::getConstantPool(),
6587 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006588 // Extend everything to 80 bits to force it to be done on x87.
6589 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
6590 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00006591}
6592
Dan Gohman475871a2008-07-27 21:46:04 +00006593std::pair<SDValue,SDValue> X86TargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00006594FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) const {
Chris Lattner07290932010-09-22 01:05:16 +00006595 DebugLoc DL = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00006596
Owen Andersone50ed302009-08-10 22:56:29 +00006597 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00006598
6599 if (!IsSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006600 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
6601 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00006602 }
6603
Owen Anderson825b72b2009-08-11 20:47:22 +00006604 assert(DstTy.getSimpleVT() <= MVT::i64 &&
6605 DstTy.getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00006606 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00006607
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006608 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00006609 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00006610 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00006611 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00006612 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00006613 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00006614 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00006615 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006616
Evan Cheng87c89352007-10-15 20:11:21 +00006617 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
6618 // stack slot.
6619 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00006620 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00006621 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006622 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00006623
Michael J. Spencerec38de22010-10-10 22:04:20 +00006624
6625
Evan Cheng0db9fe62006-04-25 20:13:52 +00006626 unsigned Opc;
Owen Anderson825b72b2009-08-11 20:47:22 +00006627 switch (DstTy.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00006628 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
Owen Anderson825b72b2009-08-11 20:47:22 +00006629 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
6630 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
6631 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006632 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006633
Dan Gohman475871a2008-07-27 21:46:04 +00006634 SDValue Chain = DAG.getEntryNode();
6635 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00006636 EVT TheVT = Op.getOperand(0).getValueType();
6637 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006638 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00006639 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006640 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006641 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00006642 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00006643 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00006644 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00006645 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00006646
Chris Lattner492a43e2010-09-22 01:28:21 +00006647 MachineMemOperand *MMO =
6648 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6649 MachineMemOperand::MOLoad, MemSize, MemSize);
6650 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, 3,
6651 DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006652 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00006653 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006654 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
6655 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00006656
Chris Lattner07290932010-09-22 01:05:16 +00006657 MachineMemOperand *MMO =
6658 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6659 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006660
Evan Cheng0db9fe62006-04-25 20:13:52 +00006661 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00006662 SDValue Ops[] = { Chain, Value, StackSlot };
Chris Lattner07290932010-09-22 01:05:16 +00006663 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
6664 Ops, 3, DstTy, MMO);
Evan Chengd9558e02006-01-06 00:43:03 +00006665
Chris Lattner27a6c732007-11-24 07:07:01 +00006666 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006667}
6668
Dan Gohmand858e902010-04-17 15:26:15 +00006669SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
6670 SelectionDAG &DAG) const {
Dale Johannesen0488fb62010-09-30 23:57:10 +00006671 if (Op.getValueType().isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00006672 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00006673
Eli Friedman948e95a2009-05-23 09:59:16 +00006674 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00006675 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00006676 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
6677 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00006678
Chris Lattner27a6c732007-11-24 07:07:01 +00006679 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006680 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00006681 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00006682}
6683
Dan Gohmand858e902010-04-17 15:26:15 +00006684SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
6685 SelectionDAG &DAG) const {
Eli Friedman948e95a2009-05-23 09:59:16 +00006686 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
6687 SDValue FIST = Vals.first, StackSlot = Vals.second;
6688 assert(FIST.getNode() && "Unexpected failure");
6689
6690 // Load the result.
6691 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00006692 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00006693}
6694
Dan Gohmand858e902010-04-17 15:26:15 +00006695SDValue X86TargetLowering::LowerFABS(SDValue Op,
6696 SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00006697 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006698 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00006699 EVT VT = Op.getValueType();
6700 EVT EltVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00006701 if (VT.isVector())
6702 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006703 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00006704 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006705 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00006706 CV.push_back(C);
6707 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006708 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006709 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00006710 CV.push_back(C);
6711 CV.push_back(C);
6712 CV.push_back(C);
6713 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006714 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00006715 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006716 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006717 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00006718 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006719 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006720 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006721}
6722
Dan Gohmand858e902010-04-17 15:26:15 +00006723SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00006724 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006725 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00006726 EVT VT = Op.getValueType();
6727 EVT EltVT = VT;
Duncan Sandsda9ad382009-09-06 19:29:07 +00006728 if (VT.isVector())
Duncan Sands83ec4b62008-06-06 12:08:01 +00006729 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006730 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00006731 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006732 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00006733 CV.push_back(C);
6734 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006735 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006736 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00006737 CV.push_back(C);
6738 CV.push_back(C);
6739 CV.push_back(C);
6740 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006741 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00006742 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006743 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006744 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00006745 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006746 false, false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006747 if (VT.isVector()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006748 return DAG.getNode(ISD::BITCAST, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00006749 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006750 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00006751 Op.getOperand(0)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006752 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00006753 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00006754 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00006755 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006756}
6757
Dan Gohmand858e902010-04-17 15:26:15 +00006758SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00006759 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00006760 SDValue Op0 = Op.getOperand(0);
6761 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006762 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00006763 EVT VT = Op.getValueType();
6764 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00006765
6766 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00006767 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006768 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00006769 SrcVT = VT;
6770 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00006771 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00006772 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006773 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00006774 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00006775 }
6776
6777 // At this point the operands and the result should have the same
6778 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00006779
Evan Cheng68c47cb2007-01-05 07:55:56 +00006780 // First get the sign bit of second operand.
6781 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00006782 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006783 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
6784 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00006785 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006786 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
6787 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
6788 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
6789 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00006790 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00006791 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006792 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006793 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00006794 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006795 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006796 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00006797
6798 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00006799 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006800 // Op0 is MVT::f32, Op1 is MVT::f64.
6801 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
6802 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
6803 DAG.getConstant(32, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006804 SignBit = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, SignBit);
Owen Anderson825b72b2009-08-11 20:47:22 +00006805 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00006806 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00006807 }
6808
Evan Cheng73d6cf12007-01-05 21:37:56 +00006809 // Clear first operand sign bit.
6810 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00006811 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006812 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
6813 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00006814 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006815 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
6816 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
6817 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
6818 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00006819 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00006820 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006821 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006822 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00006823 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006824 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006825 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00006826
6827 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00006828 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00006829}
6830
Dan Gohman076aee32009-03-04 19:44:21 +00006831/// Emit nodes that will be selected as "test Op0,Op0", or something
6832/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00006833SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00006834 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00006835 DebugLoc dl = Op.getDebugLoc();
6836
Dan Gohman31125812009-03-07 01:58:32 +00006837 // CF and OF aren't always set the way we want. Determine which
6838 // of these we need.
6839 bool NeedCF = false;
6840 bool NeedOF = false;
6841 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006842 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00006843 case X86::COND_A: case X86::COND_AE:
6844 case X86::COND_B: case X86::COND_BE:
6845 NeedCF = true;
6846 break;
6847 case X86::COND_G: case X86::COND_GE:
6848 case X86::COND_L: case X86::COND_LE:
6849 case X86::COND_O: case X86::COND_NO:
6850 NeedOF = true;
6851 break;
Dan Gohman31125812009-03-07 01:58:32 +00006852 }
6853
Dan Gohman076aee32009-03-04 19:44:21 +00006854 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00006855 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
6856 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006857 if (Op.getResNo() != 0 || NeedOF || NeedCF)
6858 // Emit a CMP with 0, which is the TEST pattern.
6859 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
6860 DAG.getConstant(0, Op.getValueType()));
6861
6862 unsigned Opcode = 0;
6863 unsigned NumOperands = 0;
6864 switch (Op.getNode()->getOpcode()) {
6865 case ISD::ADD:
6866 // Due to an isel shortcoming, be conservative if this add is likely to be
6867 // selected as part of a load-modify-store instruction. When the root node
6868 // in a match is a store, isel doesn't know how to remap non-chain non-flag
6869 // uses of other nodes in the match, such as the ADD in this case. This
6870 // leads to the ADD being left around and reselected, with the result being
6871 // two adds in the output. Alas, even if none our users are stores, that
6872 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
6873 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
6874 // climbing the DAG back to the root, and it doesn't seem to be worth the
6875 // effort.
6876 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Dan Gohman076aee32009-03-04 19:44:21 +00006877 UE = Op.getNode()->use_end(); UI != UE; ++UI)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006878 if (UI->getOpcode() != ISD::CopyToReg && UI->getOpcode() != ISD::SETCC)
6879 goto default_case;
6880
6881 if (ConstantSDNode *C =
6882 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
6883 // An add of one will be selected as an INC.
6884 if (C->getAPIntValue() == 1) {
6885 Opcode = X86ISD::INC;
6886 NumOperands = 1;
6887 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00006888 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006889
6890 // An add of negative one (subtract of one) will be selected as a DEC.
6891 if (C->getAPIntValue().isAllOnesValue()) {
6892 Opcode = X86ISD::DEC;
6893 NumOperands = 1;
6894 break;
6895 }
Dan Gohman076aee32009-03-04 19:44:21 +00006896 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006897
6898 // Otherwise use a regular EFLAGS-setting add.
6899 Opcode = X86ISD::ADD;
6900 NumOperands = 2;
6901 break;
6902 case ISD::AND: {
6903 // If the primary and result isn't used, don't bother using X86ISD::AND,
6904 // because a TEST instruction will be better.
6905 bool NonFlagUse = false;
6906 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
6907 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
6908 SDNode *User = *UI;
6909 unsigned UOpNo = UI.getOperandNo();
6910 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
6911 // Look pass truncate.
6912 UOpNo = User->use_begin().getOperandNo();
6913 User = *User->use_begin();
6914 }
6915
6916 if (User->getOpcode() != ISD::BRCOND &&
6917 User->getOpcode() != ISD::SETCC &&
6918 (User->getOpcode() != ISD::SELECT || UOpNo != 0)) {
6919 NonFlagUse = true;
6920 break;
6921 }
Dan Gohman076aee32009-03-04 19:44:21 +00006922 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006923
6924 if (!NonFlagUse)
6925 break;
6926 }
6927 // FALL THROUGH
6928 case ISD::SUB:
6929 case ISD::OR:
6930 case ISD::XOR:
6931 // Due to the ISEL shortcoming noted above, be conservative if this op is
6932 // likely to be selected as part of a load-modify-store instruction.
6933 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
6934 UE = Op.getNode()->use_end(); UI != UE; ++UI)
6935 if (UI->getOpcode() == ISD::STORE)
6936 goto default_case;
6937
6938 // Otherwise use a regular EFLAGS-setting instruction.
6939 switch (Op.getNode()->getOpcode()) {
6940 default: llvm_unreachable("unexpected operator!");
6941 case ISD::SUB: Opcode = X86ISD::SUB; break;
6942 case ISD::OR: Opcode = X86ISD::OR; break;
6943 case ISD::XOR: Opcode = X86ISD::XOR; break;
6944 case ISD::AND: Opcode = X86ISD::AND; break;
6945 }
6946
6947 NumOperands = 2;
6948 break;
6949 case X86ISD::ADD:
6950 case X86ISD::SUB:
6951 case X86ISD::INC:
6952 case X86ISD::DEC:
6953 case X86ISD::OR:
6954 case X86ISD::XOR:
6955 case X86ISD::AND:
6956 return SDValue(Op.getNode(), 1);
6957 default:
6958 default_case:
6959 break;
Dan Gohman076aee32009-03-04 19:44:21 +00006960 }
6961
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006962 if (Opcode == 0)
6963 // Emit a CMP with 0, which is the TEST pattern.
6964 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
6965 DAG.getConstant(0, Op.getValueType()));
6966
6967 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
6968 SmallVector<SDValue, 4> Ops;
6969 for (unsigned i = 0; i != NumOperands; ++i)
6970 Ops.push_back(Op.getOperand(i));
6971
6972 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
6973 DAG.ReplaceAllUsesWith(Op, New);
6974 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00006975}
6976
6977/// Emit nodes that will be selected as "cmp Op0,Op1", or something
6978/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00006979SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00006980 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00006981 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
6982 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00006983 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00006984
6985 DebugLoc dl = Op0.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00006986 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00006987}
6988
Evan Chengd40d03e2010-01-06 19:38:29 +00006989/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
6990/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00006991SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
6992 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00006993 SDValue Op0 = And.getOperand(0);
6994 SDValue Op1 = And.getOperand(1);
6995 if (Op0.getOpcode() == ISD::TRUNCATE)
6996 Op0 = Op0.getOperand(0);
6997 if (Op1.getOpcode() == ISD::TRUNCATE)
6998 Op1 = Op1.getOperand(0);
6999
Evan Chengd40d03e2010-01-06 19:38:29 +00007000 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00007001 if (Op1.getOpcode() == ISD::SHL)
7002 std::swap(Op0, Op1);
7003 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00007004 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
7005 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00007006 // If we looked past a truncate, check that it's only truncating away
7007 // known zeros.
7008 unsigned BitWidth = Op0.getValueSizeInBits();
7009 unsigned AndBitWidth = And.getValueSizeInBits();
7010 if (BitWidth > AndBitWidth) {
7011 APInt Mask = APInt::getAllOnesValue(BitWidth), Zeros, Ones;
7012 DAG.ComputeMaskedBits(Op0, Mask, Zeros, Ones);
7013 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
7014 return SDValue();
7015 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00007016 LHS = Op1;
7017 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00007018 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00007019 } else if (Op1.getOpcode() == ISD::Constant) {
7020 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
7021 SDValue AndLHS = Op0;
Evan Chengd40d03e2010-01-06 19:38:29 +00007022 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
7023 LHS = AndLHS.getOperand(0);
7024 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00007025 }
Evan Chengd40d03e2010-01-06 19:38:29 +00007026 }
Evan Cheng0488db92007-09-25 01:57:46 +00007027
Evan Chengd40d03e2010-01-06 19:38:29 +00007028 if (LHS.getNode()) {
Evan Chenge5b51ac2010-04-17 06:13:15 +00007029 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00007030 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00007031 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00007032 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00007033 // Also promote i16 to i32 for performance / code size reason.
7034 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00007035 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00007036 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00007037
Evan Chengd40d03e2010-01-06 19:38:29 +00007038 // If the operand types disagree, extend the shift amount to match. Since
7039 // BT ignores high bits (like shifts) we can use anyextend.
7040 if (LHS.getValueType() != RHS.getValueType())
7041 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00007042
Evan Chengd40d03e2010-01-06 19:38:29 +00007043 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
7044 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
7045 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
7046 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00007047 }
7048
Evan Cheng54de3ea2010-01-05 06:52:31 +00007049 return SDValue();
7050}
7051
Dan Gohmand858e902010-04-17 15:26:15 +00007052SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng54de3ea2010-01-05 06:52:31 +00007053 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
7054 SDValue Op0 = Op.getOperand(0);
7055 SDValue Op1 = Op.getOperand(1);
7056 DebugLoc dl = Op.getDebugLoc();
7057 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
7058
7059 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00007060 // Lower (X & (1 << N)) == 0 to BT(X, N).
7061 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
7062 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Chris Lattner481eebc2010-12-19 21:23:48 +00007063 if (Op0.getOpcode() == ISD::AND && Op0.hasOneUse() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00007064 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane368b462010-06-18 14:22:04 +00007065 cast<ConstantSDNode>(Op1)->isNullValue() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00007066 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
7067 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
7068 if (NewSetCC.getNode())
7069 return NewSetCC;
7070 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00007071
Chris Lattner481eebc2010-12-19 21:23:48 +00007072 // Look for X == 0, X == 1, X != 0, or X != 1. We can simplify some forms of
7073 // these.
7074 if (Op1.getOpcode() == ISD::Constant &&
Evan Cheng2c755ba2010-02-27 07:36:59 +00007075 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
7076 cast<ConstantSDNode>(Op1)->isNullValue()) &&
7077 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007078
Chris Lattner481eebc2010-12-19 21:23:48 +00007079 // If the input is a setcc, then reuse the input setcc or use a new one with
7080 // the inverted condition.
7081 if (Op0.getOpcode() == X86ISD::SETCC) {
7082 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
7083 bool Invert = (CC == ISD::SETNE) ^
7084 cast<ConstantSDNode>(Op1)->isNullValue();
7085 if (!Invert) return Op0;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007086
Evan Cheng2c755ba2010-02-27 07:36:59 +00007087 CCode = X86::GetOppositeBranchCondition(CCode);
Chris Lattner481eebc2010-12-19 21:23:48 +00007088 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
7089 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
7090 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00007091 }
7092
Evan Chenge5b51ac2010-04-17 06:13:15 +00007093 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00007094 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00007095 if (X86CC == X86::COND_INVALID)
7096 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00007097
Chris Lattnerc19d1c32010-12-19 22:08:31 +00007098 SDValue EFLAGS = EmitCmp(Op0, Op1, X86CC, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00007099 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattnerc19d1c32010-12-19 22:08:31 +00007100 DAG.getConstant(X86CC, MVT::i8), EFLAGS);
Evan Cheng0488db92007-09-25 01:57:46 +00007101}
7102
Dan Gohmand858e902010-04-17 15:26:15 +00007103SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00007104 SDValue Cond;
7105 SDValue Op0 = Op.getOperand(0);
7106 SDValue Op1 = Op.getOperand(1);
7107 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00007108 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00007109 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
7110 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007111 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00007112
7113 if (isFP) {
7114 unsigned SSECC = 8;
Owen Andersone50ed302009-08-10 22:56:29 +00007115 EVT VT0 = Op0.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00007116 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
7117 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00007118 bool Swap = false;
7119
7120 switch (SetCCOpcode) {
7121 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00007122 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00007123 case ISD::SETEQ: SSECC = 0; break;
Scott Michelfdc40a02009-02-17 22:15:04 +00007124 case ISD::SETOGT:
Nate Begeman30a0de92008-07-17 16:51:19 +00007125 case ISD::SETGT: Swap = true; // Fallthrough
7126 case ISD::SETLT:
7127 case ISD::SETOLT: SSECC = 1; break;
7128 case ISD::SETOGE:
7129 case ISD::SETGE: Swap = true; // Fallthrough
7130 case ISD::SETLE:
7131 case ISD::SETOLE: SSECC = 2; break;
7132 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00007133 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00007134 case ISD::SETNE: SSECC = 4; break;
7135 case ISD::SETULE: Swap = true;
7136 case ISD::SETUGE: SSECC = 5; break;
7137 case ISD::SETULT: Swap = true;
7138 case ISD::SETUGT: SSECC = 6; break;
7139 case ISD::SETO: SSECC = 7; break;
7140 }
7141 if (Swap)
7142 std::swap(Op0, Op1);
7143
Nate Begemanfb8ead02008-07-25 19:05:58 +00007144 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00007145 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00007146 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00007147 SDValue UNORD, EQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00007148 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
7149 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00007150 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00007151 }
7152 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00007153 SDValue ORD, NEQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00007154 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
7155 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00007156 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00007157 }
Torok Edwinc23197a2009-07-14 16:55:14 +00007158 llvm_unreachable("Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00007159 }
7160 // Handle all other FP comparisons here.
Owen Anderson825b72b2009-08-11 20:47:22 +00007161 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00007162 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007163
Nate Begeman30a0de92008-07-17 16:51:19 +00007164 // We are handling one of the integer comparisons here. Since SSE only has
7165 // GT and EQ comparisons for integer, swapping operands and multiple
7166 // operations may be required for some comparisons.
7167 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
7168 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00007169
Owen Anderson825b72b2009-08-11 20:47:22 +00007170 switch (VT.getSimpleVT().SimpleTy) {
Nate Begeman30a0de92008-07-17 16:51:19 +00007171 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007172 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007173 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007174 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
7175 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00007176 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007177
Nate Begeman30a0de92008-07-17 16:51:19 +00007178 switch (SetCCOpcode) {
7179 default: break;
7180 case ISD::SETNE: Invert = true;
7181 case ISD::SETEQ: Opc = EQOpc; break;
7182 case ISD::SETLT: Swap = true;
7183 case ISD::SETGT: Opc = GTOpc; break;
7184 case ISD::SETGE: Swap = true;
7185 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
7186 case ISD::SETULT: Swap = true;
7187 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
7188 case ISD::SETUGE: Swap = true;
7189 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
7190 }
7191 if (Swap)
7192 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007193
Nate Begeman30a0de92008-07-17 16:51:19 +00007194 // Since SSE has no unsigned integer comparisons, we need to flip the sign
7195 // bits of the inputs before performing those operations.
7196 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00007197 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00007198 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
7199 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00007200 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00007201 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
7202 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00007203 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
7204 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00007205 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007206
Dale Johannesenace16102009-02-03 19:33:06 +00007207 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00007208
7209 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00007210 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00007211 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00007212
Nate Begeman30a0de92008-07-17 16:51:19 +00007213 return Result;
7214}
Evan Cheng0488db92007-09-25 01:57:46 +00007215
Evan Cheng370e5342008-12-03 08:38:43 +00007216// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00007217static bool isX86LogicalCmp(SDValue Op) {
7218 unsigned Opc = Op.getNode()->getOpcode();
7219 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
7220 return true;
7221 if (Op.getResNo() == 1 &&
7222 (Opc == X86ISD::ADD ||
7223 Opc == X86ISD::SUB ||
Chris Lattner5b856542010-12-20 00:59:46 +00007224 Opc == X86ISD::ADC ||
7225 Opc == X86ISD::SBB ||
Dan Gohman076aee32009-03-04 19:44:21 +00007226 Opc == X86ISD::SMUL ||
7227 Opc == X86ISD::UMUL ||
7228 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00007229 Opc == X86ISD::DEC ||
7230 Opc == X86ISD::OR ||
7231 Opc == X86ISD::XOR ||
7232 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00007233 return true;
7234
Chris Lattner9637d5b2010-12-05 07:49:54 +00007235 if (Op.getResNo() == 2 && Opc == X86ISD::UMUL)
7236 return true;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007237
Dan Gohman076aee32009-03-04 19:44:21 +00007238 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00007239}
7240
Chris Lattnera2b56002010-12-05 01:23:24 +00007241static bool isZero(SDValue V) {
7242 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
7243 return C && C->isNullValue();
7244}
7245
Chris Lattner96908b12010-12-05 02:00:51 +00007246static bool isAllOnes(SDValue V) {
7247 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
7248 return C && C->isAllOnesValue();
7249}
7250
Dan Gohmand858e902010-04-17 15:26:15 +00007251SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00007252 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00007253 SDValue Cond = Op.getOperand(0);
Chris Lattnera2b56002010-12-05 01:23:24 +00007254 SDValue Op1 = Op.getOperand(1);
7255 SDValue Op2 = Op.getOperand(2);
7256 DebugLoc DL = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00007257 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00007258
Dan Gohman1a492952009-10-20 16:22:37 +00007259 if (Cond.getOpcode() == ISD::SETCC) {
7260 SDValue NewCond = LowerSETCC(Cond, DAG);
7261 if (NewCond.getNode())
7262 Cond = NewCond;
7263 }
Evan Cheng734503b2006-09-11 02:19:56 +00007264
Chris Lattnera2b56002010-12-05 01:23:24 +00007265 // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00007266 // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
Chris Lattnera2b56002010-12-05 01:23:24 +00007267 // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00007268 // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007269 if (Cond.getOpcode() == X86ISD::SETCC &&
Chris Lattner96908b12010-12-05 02:00:51 +00007270 Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
7271 isZero(Cond.getOperand(1).getOperand(1))) {
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007272 SDValue Cmp = Cond.getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007273
Chris Lattnera2b56002010-12-05 01:23:24 +00007274 unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007275
7276 if ((isAllOnes(Op1) || isAllOnes(Op2)) &&
Chris Lattner96908b12010-12-05 02:00:51 +00007277 (CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
7278 SDValue Y = isAllOnes(Op2) ? Op1 : Op2;
Chris Lattnera2b56002010-12-05 01:23:24 +00007279
7280 SDValue CmpOp0 = Cmp.getOperand(0);
7281 Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32,
7282 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007283
Chris Lattner96908b12010-12-05 02:00:51 +00007284 SDValue Res = // Res = 0 or -1.
Chris Lattnera2b56002010-12-05 01:23:24 +00007285 DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
7286 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007287
Chris Lattner96908b12010-12-05 02:00:51 +00007288 if (isAllOnes(Op1) != (CondCode == X86::COND_E))
7289 Res = DAG.getNOT(DL, Res, Res.getValueType());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007290
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007291 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
Chris Lattnera2b56002010-12-05 01:23:24 +00007292 if (N2C == 0 || !N2C->isNullValue())
7293 Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
7294 return Res;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007295 }
7296 }
7297
Chris Lattnera2b56002010-12-05 01:23:24 +00007298 // Look past (and (setcc_carry (cmp ...)), 1).
Evan Chengad9c0a32009-12-15 00:53:42 +00007299 if (Cond.getOpcode() == ISD::AND &&
7300 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
7301 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00007302 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00007303 Cond = Cond.getOperand(0);
7304 }
7305
Evan Cheng3f41d662007-10-08 22:16:29 +00007306 // If condition flag is set by a X86ISD::CMP, then use it as the condition
7307 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00007308 if (Cond.getOpcode() == X86ISD::SETCC ||
7309 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00007310 CC = Cond.getOperand(0);
7311
Dan Gohman475871a2008-07-27 21:46:04 +00007312 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00007313 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00007314 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00007315
Evan Cheng3f41d662007-10-08 22:16:29 +00007316 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007317 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00007318 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00007319 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00007320
Chris Lattnerd1980a52009-03-12 06:52:53 +00007321 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
7322 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00007323 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00007324 addTest = false;
7325 }
7326 }
7327
7328 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007329 // Look pass the truncate.
7330 if (Cond.getOpcode() == ISD::TRUNCATE)
7331 Cond = Cond.getOperand(0);
7332
7333 // We know the result of AND is compared against zero. Try to match
7334 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00007335 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Chris Lattnera2b56002010-12-05 01:23:24 +00007336 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG);
Evan Chengd40d03e2010-01-06 19:38:29 +00007337 if (NewSetCC.getNode()) {
7338 CC = NewSetCC.getOperand(0);
7339 Cond = NewSetCC.getOperand(1);
7340 addTest = false;
7341 }
7342 }
7343 }
7344
7345 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007346 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00007347 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00007348 }
7349
Benjamin Kramere915ff32010-12-22 23:09:28 +00007350 // a < b ? -1 : 0 -> RES = ~setcc_carry
7351 // a < b ? 0 : -1 -> RES = setcc_carry
7352 // a >= b ? -1 : 0 -> RES = setcc_carry
7353 // a >= b ? 0 : -1 -> RES = ~setcc_carry
7354 if (Cond.getOpcode() == X86ISD::CMP) {
7355 unsigned CondCode = cast<ConstantSDNode>(CC)->getZExtValue();
7356
7357 if ((CondCode == X86::COND_AE || CondCode == X86::COND_B) &&
7358 (isAllOnes(Op1) || isAllOnes(Op2)) && (isZero(Op1) || isZero(Op2))) {
7359 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
7360 DAG.getConstant(X86::COND_B, MVT::i8), Cond);
7361 if (isAllOnes(Op1) != (CondCode == X86::COND_B))
7362 return DAG.getNOT(DL, Res, Res.getValueType());
7363 return Res;
7364 }
7365 }
7366
Evan Cheng0488db92007-09-25 01:57:46 +00007367 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
7368 // condition is true.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007369 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007370 SDValue Ops[] = { Op2, Op1, CC, Cond };
Chris Lattnera2b56002010-12-05 01:23:24 +00007371 return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00007372}
7373
Evan Cheng370e5342008-12-03 08:38:43 +00007374// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
7375// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
7376// from the AND / OR.
7377static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
7378 Opc = Op.getOpcode();
7379 if (Opc != ISD::OR && Opc != ISD::AND)
7380 return false;
7381 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
7382 Op.getOperand(0).hasOneUse() &&
7383 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
7384 Op.getOperand(1).hasOneUse());
7385}
7386
Evan Cheng961d6d42009-02-02 08:19:07 +00007387// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
7388// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00007389static bool isXor1OfSetCC(SDValue Op) {
7390 if (Op.getOpcode() != ISD::XOR)
7391 return false;
7392 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
7393 if (N1C && N1C->getAPIntValue() == 1) {
7394 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
7395 Op.getOperand(0).hasOneUse();
7396 }
7397 return false;
7398}
7399
Dan Gohmand858e902010-04-17 15:26:15 +00007400SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00007401 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00007402 SDValue Chain = Op.getOperand(0);
7403 SDValue Cond = Op.getOperand(1);
7404 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007405 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00007406 SDValue CC;
Evan Cheng734503b2006-09-11 02:19:56 +00007407
Dan Gohman1a492952009-10-20 16:22:37 +00007408 if (Cond.getOpcode() == ISD::SETCC) {
7409 SDValue NewCond = LowerSETCC(Cond, DAG);
7410 if (NewCond.getNode())
7411 Cond = NewCond;
7412 }
Chris Lattnere55484e2008-12-25 05:34:37 +00007413#if 0
7414 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00007415 else if (Cond.getOpcode() == X86ISD::ADD ||
7416 Cond.getOpcode() == X86ISD::SUB ||
7417 Cond.getOpcode() == X86ISD::SMUL ||
7418 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00007419 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00007420#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00007421
Evan Chengad9c0a32009-12-15 00:53:42 +00007422 // Look pass (and (setcc_carry (cmp ...)), 1).
7423 if (Cond.getOpcode() == ISD::AND &&
7424 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
7425 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00007426 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00007427 Cond = Cond.getOperand(0);
7428 }
7429
Evan Cheng3f41d662007-10-08 22:16:29 +00007430 // If condition flag is set by a X86ISD::CMP, then use it as the condition
7431 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00007432 if (Cond.getOpcode() == X86ISD::SETCC ||
7433 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00007434 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007435
Dan Gohman475871a2008-07-27 21:46:04 +00007436 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00007437 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00007438 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00007439 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00007440 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00007441 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00007442 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00007443 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00007444 default: break;
7445 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00007446 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00007447 // These can only come from an arithmetic instruction with overflow,
7448 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00007449 Cond = Cond.getNode()->getOperand(1);
7450 addTest = false;
7451 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00007452 }
Evan Cheng0488db92007-09-25 01:57:46 +00007453 }
Evan Cheng370e5342008-12-03 08:38:43 +00007454 } else {
7455 unsigned CondOpc;
7456 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
7457 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00007458 if (CondOpc == ISD::OR) {
7459 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
7460 // two branches instead of an explicit OR instruction with a
7461 // separate test.
7462 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00007463 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00007464 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007465 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00007466 Chain, Dest, CC, Cmp);
7467 CC = Cond.getOperand(1).getOperand(0);
7468 Cond = Cmp;
7469 addTest = false;
7470 }
7471 } else { // ISD::AND
7472 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
7473 // two branches instead of an explicit AND instruction with a
7474 // separate test. However, we only do this if this block doesn't
7475 // have a fall-through edge, because this requires an explicit
7476 // jmp when the condition is false.
7477 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00007478 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00007479 Op.getNode()->hasOneUse()) {
7480 X86::CondCode CCode =
7481 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
7482 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007483 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00007484 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00007485 // Look for an unconditional branch following this conditional branch.
7486 // We need this because we need to reverse the successors in order
7487 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00007488 if (User->getOpcode() == ISD::BR) {
7489 SDValue FalseBB = User->getOperand(1);
7490 SDNode *NewBR =
7491 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00007492 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00007493 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00007494 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00007495
Dale Johannesene4d209d2009-02-03 20:21:25 +00007496 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00007497 Chain, Dest, CC, Cmp);
7498 X86::CondCode CCode =
7499 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
7500 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007501 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00007502 Cond = Cmp;
7503 addTest = false;
7504 }
7505 }
Dan Gohman279c22e2008-10-21 03:29:32 +00007506 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00007507 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
7508 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
7509 // It should be transformed during dag combiner except when the condition
7510 // is set by a arithmetics with overflow node.
7511 X86::CondCode CCode =
7512 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
7513 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007514 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00007515 Cond = Cond.getOperand(0).getOperand(1);
7516 addTest = false;
Dan Gohman279c22e2008-10-21 03:29:32 +00007517 }
Evan Cheng0488db92007-09-25 01:57:46 +00007518 }
7519
7520 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007521 // Look pass the truncate.
7522 if (Cond.getOpcode() == ISD::TRUNCATE)
7523 Cond = Cond.getOperand(0);
7524
7525 // We know the result of AND is compared against zero. Try to match
7526 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00007527 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007528 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
7529 if (NewSetCC.getNode()) {
7530 CC = NewSetCC.getOperand(0);
7531 Cond = NewSetCC.getOperand(1);
7532 addTest = false;
7533 }
7534 }
7535 }
7536
7537 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007538 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00007539 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00007540 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00007541 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00007542 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00007543}
7544
Anton Korobeynikove060b532007-04-17 19:34:00 +00007545
7546// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
7547// Calls to _alloca is needed to probe the stack when allocating more than 4k
7548// bytes in one go. Touching the stack at 4K increments is necessary to ensure
7549// that the guard pages used by the OS virtual memory manager are allocated in
7550// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00007551SDValue
7552X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00007553 SelectionDAG &DAG) const {
Duncan Sands1e1ca0b2010-10-21 16:02:12 +00007554 assert((Subtarget->isTargetCygMing() || Subtarget->isTargetWindows()) &&
Michael J. Spencere9c253e2010-10-21 01:41:01 +00007555 "This should be used only on Windows targets");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007556 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007557
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007558 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00007559 SDValue Chain = Op.getOperand(0);
7560 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007561 // FIXME: Ensure alignment here
7562
Dan Gohman475871a2008-07-27 21:46:04 +00007563 SDValue Flag;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007564
Owen Anderson825b72b2009-08-11 20:47:22 +00007565 EVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007566
Dale Johannesendd64c412009-02-04 00:33:20 +00007567 Chain = DAG.getCopyToReg(Chain, dl, X86::EAX, Size, Flag);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00007568 Flag = Chain.getValue(1);
7569
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007570 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00007571
Michael J. Spencere9c253e2010-10-21 01:41:01 +00007572 Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag);
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00007573 Flag = Chain.getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007574
Dale Johannesendd64c412009-02-04 00:33:20 +00007575 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007576
Dan Gohman475871a2008-07-27 21:46:04 +00007577 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Dale Johannesene4d209d2009-02-03 20:21:25 +00007578 return DAG.getMergeValues(Ops1, 2, dl);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007579}
7580
Dan Gohmand858e902010-04-17 15:26:15 +00007581SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00007582 MachineFunction &MF = DAG.getMachineFunction();
7583 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
7584
Dan Gohman69de1932008-02-06 22:27:42 +00007585 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00007586 DebugLoc DL = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00007587
Anton Korobeynikove7beda12010-10-03 22:52:07 +00007588 if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00007589 // vastart just stores the address of the VarArgsFrameIndex slot into the
7590 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +00007591 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
7592 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00007593 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
7594 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007595 }
7596
7597 // __va_list_tag:
7598 // gp_offset (0 - 6 * 8)
7599 // fp_offset (48 - 48 + 8 * 16)
7600 // overflow_arg_area (point to parameters coming in memory).
7601 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00007602 SmallVector<SDValue, 8> MemOps;
7603 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00007604 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00007605 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00007606 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
7607 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007608 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007609 MemOps.push_back(Store);
7610
7611 // Store fp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00007612 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007613 FIN, DAG.getIntPtrConstant(4));
Chris Lattner8026a9d2010-09-21 17:50:43 +00007614 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00007615 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
7616 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007617 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007618 MemOps.push_back(Store);
7619
7620 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +00007621 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007622 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +00007623 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
7624 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00007625 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
7626 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +00007627 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007628 MemOps.push_back(Store);
7629
7630 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +00007631 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007632 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +00007633 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
7634 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00007635 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
7636 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007637 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +00007638 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00007639 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00007640}
7641
Dan Gohmand858e902010-04-17 15:26:15 +00007642SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman320afb82010-10-12 18:00:49 +00007643 assert(Subtarget->is64Bit() &&
7644 "LowerVAARG only handles 64-bit va_arg!");
7645 assert((Subtarget->isTargetLinux() ||
7646 Subtarget->isTargetDarwin()) &&
7647 "Unhandled target in LowerVAARG");
7648 assert(Op.getNode()->getNumOperands() == 4);
7649 SDValue Chain = Op.getOperand(0);
7650 SDValue SrcPtr = Op.getOperand(1);
7651 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
7652 unsigned Align = Op.getConstantOperandVal(3);
7653 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9018e832008-05-10 01:26:14 +00007654
Dan Gohman320afb82010-10-12 18:00:49 +00007655 EVT ArgVT = Op.getNode()->getValueType(0);
7656 const Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
7657 uint32_t ArgSize = getTargetData()->getTypeAllocSize(ArgTy);
7658 uint8_t ArgMode;
7659
7660 // Decide which area this value should be read from.
7661 // TODO: Implement the AMD64 ABI in its entirety. This simple
7662 // selection mechanism works only for the basic types.
7663 if (ArgVT == MVT::f80) {
7664 llvm_unreachable("va_arg for f80 not yet implemented");
7665 } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) {
7666 ArgMode = 2; // Argument passed in XMM register. Use fp_offset.
7667 } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) {
7668 ArgMode = 1; // Argument passed in GPR64 register(s). Use gp_offset.
7669 } else {
7670 llvm_unreachable("Unhandled argument type in LowerVAARG");
7671 }
7672
7673 if (ArgMode == 2) {
7674 // Sanity Check: Make sure using fp_offset makes sense.
Michael J. Spencer87b86652010-10-19 07:32:42 +00007675 assert(!UseSoftFloat &&
Eric Christopher52b45052010-10-12 19:44:17 +00007676 !(DAG.getMachineFunction()
7677 .getFunction()->hasFnAttr(Attribute::NoImplicitFloat)) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00007678 Subtarget->hasXMM());
Dan Gohman320afb82010-10-12 18:00:49 +00007679 }
7680
7681 // Insert VAARG_64 node into the DAG
7682 // VAARG_64 returns two values: Variable Argument Address, Chain
7683 SmallVector<SDValue, 11> InstOps;
7684 InstOps.push_back(Chain);
7685 InstOps.push_back(SrcPtr);
7686 InstOps.push_back(DAG.getConstant(ArgSize, MVT::i32));
7687 InstOps.push_back(DAG.getConstant(ArgMode, MVT::i8));
7688 InstOps.push_back(DAG.getConstant(Align, MVT::i32));
7689 SDVTList VTs = DAG.getVTList(getPointerTy(), MVT::Other);
7690 SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl,
7691 VTs, &InstOps[0], InstOps.size(),
7692 MVT::i64,
7693 MachinePointerInfo(SV),
7694 /*Align=*/0,
7695 /*Volatile=*/false,
7696 /*ReadMem=*/true,
7697 /*WriteMem=*/true);
7698 Chain = VAARG.getValue(1);
7699
7700 // Load the next argument and return it
7701 return DAG.getLoad(ArgVT, dl,
7702 Chain,
7703 VAARG,
7704 MachinePointerInfo(),
7705 false, false, 0);
Dan Gohman9018e832008-05-10 01:26:14 +00007706}
7707
Dan Gohmand858e902010-04-17 15:26:15 +00007708SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) const {
Evan Chengae642192007-03-02 23:16:35 +00007709 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00007710 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00007711 SDValue Chain = Op.getOperand(0);
7712 SDValue DstPtr = Op.getOperand(1);
7713 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00007714 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
7715 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Chris Lattnere72f2022010-09-21 05:40:29 +00007716 DebugLoc DL = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00007717
Chris Lattnere72f2022010-09-21 05:40:29 +00007718 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +00007719 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Michael J. Spencerec38de22010-10-10 22:04:20 +00007720 false,
Chris Lattnere72f2022010-09-21 05:40:29 +00007721 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +00007722}
7723
Dan Gohman475871a2008-07-27 21:46:04 +00007724SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007725X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007726 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007727 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007728 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00007729 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00007730 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00007731 case Intrinsic::x86_sse_comieq_ss:
7732 case Intrinsic::x86_sse_comilt_ss:
7733 case Intrinsic::x86_sse_comile_ss:
7734 case Intrinsic::x86_sse_comigt_ss:
7735 case Intrinsic::x86_sse_comige_ss:
7736 case Intrinsic::x86_sse_comineq_ss:
7737 case Intrinsic::x86_sse_ucomieq_ss:
7738 case Intrinsic::x86_sse_ucomilt_ss:
7739 case Intrinsic::x86_sse_ucomile_ss:
7740 case Intrinsic::x86_sse_ucomigt_ss:
7741 case Intrinsic::x86_sse_ucomige_ss:
7742 case Intrinsic::x86_sse_ucomineq_ss:
7743 case Intrinsic::x86_sse2_comieq_sd:
7744 case Intrinsic::x86_sse2_comilt_sd:
7745 case Intrinsic::x86_sse2_comile_sd:
7746 case Intrinsic::x86_sse2_comigt_sd:
7747 case Intrinsic::x86_sse2_comige_sd:
7748 case Intrinsic::x86_sse2_comineq_sd:
7749 case Intrinsic::x86_sse2_ucomieq_sd:
7750 case Intrinsic::x86_sse2_ucomilt_sd:
7751 case Intrinsic::x86_sse2_ucomile_sd:
7752 case Intrinsic::x86_sse2_ucomigt_sd:
7753 case Intrinsic::x86_sse2_ucomige_sd:
7754 case Intrinsic::x86_sse2_ucomineq_sd: {
7755 unsigned Opc = 0;
7756 ISD::CondCode CC = ISD::SETCC_INVALID;
7757 switch (IntNo) {
7758 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007759 case Intrinsic::x86_sse_comieq_ss:
7760 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007761 Opc = X86ISD::COMI;
7762 CC = ISD::SETEQ;
7763 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00007764 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007765 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007766 Opc = X86ISD::COMI;
7767 CC = ISD::SETLT;
7768 break;
7769 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007770 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007771 Opc = X86ISD::COMI;
7772 CC = ISD::SETLE;
7773 break;
7774 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007775 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007776 Opc = X86ISD::COMI;
7777 CC = ISD::SETGT;
7778 break;
7779 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007780 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007781 Opc = X86ISD::COMI;
7782 CC = ISD::SETGE;
7783 break;
7784 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007785 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007786 Opc = X86ISD::COMI;
7787 CC = ISD::SETNE;
7788 break;
7789 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007790 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007791 Opc = X86ISD::UCOMI;
7792 CC = ISD::SETEQ;
7793 break;
7794 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007795 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007796 Opc = X86ISD::UCOMI;
7797 CC = ISD::SETLT;
7798 break;
7799 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007800 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007801 Opc = X86ISD::UCOMI;
7802 CC = ISD::SETLE;
7803 break;
7804 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007805 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007806 Opc = X86ISD::UCOMI;
7807 CC = ISD::SETGT;
7808 break;
7809 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007810 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007811 Opc = X86ISD::UCOMI;
7812 CC = ISD::SETGE;
7813 break;
7814 case Intrinsic::x86_sse_ucomineq_ss:
7815 case Intrinsic::x86_sse2_ucomineq_sd:
7816 Opc = X86ISD::UCOMI;
7817 CC = ISD::SETNE;
7818 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00007819 }
Evan Cheng734503b2006-09-11 02:19:56 +00007820
Dan Gohman475871a2008-07-27 21:46:04 +00007821 SDValue LHS = Op.getOperand(1);
7822 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00007823 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00007824 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00007825 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
7826 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
7827 DAG.getConstant(X86CC, MVT::i8), Cond);
7828 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00007829 }
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007830 // ptest and testp intrinsics. The intrinsic these come from are designed to
7831 // return an integer value, not just an instruction so lower it to the ptest
7832 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00007833 case Intrinsic::x86_sse41_ptestz:
7834 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007835 case Intrinsic::x86_sse41_ptestnzc:
7836 case Intrinsic::x86_avx_ptestz_256:
7837 case Intrinsic::x86_avx_ptestc_256:
7838 case Intrinsic::x86_avx_ptestnzc_256:
7839 case Intrinsic::x86_avx_vtestz_ps:
7840 case Intrinsic::x86_avx_vtestc_ps:
7841 case Intrinsic::x86_avx_vtestnzc_ps:
7842 case Intrinsic::x86_avx_vtestz_pd:
7843 case Intrinsic::x86_avx_vtestc_pd:
7844 case Intrinsic::x86_avx_vtestnzc_pd:
7845 case Intrinsic::x86_avx_vtestz_ps_256:
7846 case Intrinsic::x86_avx_vtestc_ps_256:
7847 case Intrinsic::x86_avx_vtestnzc_ps_256:
7848 case Intrinsic::x86_avx_vtestz_pd_256:
7849 case Intrinsic::x86_avx_vtestc_pd_256:
7850 case Intrinsic::x86_avx_vtestnzc_pd_256: {
7851 bool IsTestPacked = false;
Eric Christopher71c67532009-07-29 00:28:05 +00007852 unsigned X86CC = 0;
7853 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00007854 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007855 case Intrinsic::x86_avx_vtestz_ps:
7856 case Intrinsic::x86_avx_vtestz_pd:
7857 case Intrinsic::x86_avx_vtestz_ps_256:
7858 case Intrinsic::x86_avx_vtestz_pd_256:
7859 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00007860 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007861 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +00007862 // ZF = 1
7863 X86CC = X86::COND_E;
7864 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007865 case Intrinsic::x86_avx_vtestc_ps:
7866 case Intrinsic::x86_avx_vtestc_pd:
7867 case Intrinsic::x86_avx_vtestc_ps_256:
7868 case Intrinsic::x86_avx_vtestc_pd_256:
7869 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00007870 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007871 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00007872 // CF = 1
7873 X86CC = X86::COND_B;
7874 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007875 case Intrinsic::x86_avx_vtestnzc_ps:
7876 case Intrinsic::x86_avx_vtestnzc_pd:
7877 case Intrinsic::x86_avx_vtestnzc_ps_256:
7878 case Intrinsic::x86_avx_vtestnzc_pd_256:
7879 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +00007880 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007881 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00007882 // ZF and CF = 0
7883 X86CC = X86::COND_A;
7884 break;
7885 }
Eric Christopherfd179292009-08-27 18:07:15 +00007886
Eric Christopher71c67532009-07-29 00:28:05 +00007887 SDValue LHS = Op.getOperand(1);
7888 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007889 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
7890 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00007891 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
7892 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
7893 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +00007894 }
Evan Cheng5759f972008-05-04 09:15:50 +00007895
7896 // Fix vector shift instructions where the last operand is a non-immediate
7897 // i32 value.
7898 case Intrinsic::x86_sse2_pslli_w:
7899 case Intrinsic::x86_sse2_pslli_d:
7900 case Intrinsic::x86_sse2_pslli_q:
7901 case Intrinsic::x86_sse2_psrli_w:
7902 case Intrinsic::x86_sse2_psrli_d:
7903 case Intrinsic::x86_sse2_psrli_q:
7904 case Intrinsic::x86_sse2_psrai_w:
7905 case Intrinsic::x86_sse2_psrai_d:
7906 case Intrinsic::x86_mmx_pslli_w:
7907 case Intrinsic::x86_mmx_pslli_d:
7908 case Intrinsic::x86_mmx_pslli_q:
7909 case Intrinsic::x86_mmx_psrli_w:
7910 case Intrinsic::x86_mmx_psrli_d:
7911 case Intrinsic::x86_mmx_psrli_q:
7912 case Intrinsic::x86_mmx_psrai_w:
7913 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00007914 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00007915 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00007916 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00007917
7918 unsigned NewIntNo = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00007919 EVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00007920 switch (IntNo) {
7921 case Intrinsic::x86_sse2_pslli_w:
7922 NewIntNo = Intrinsic::x86_sse2_psll_w;
7923 break;
7924 case Intrinsic::x86_sse2_pslli_d:
7925 NewIntNo = Intrinsic::x86_sse2_psll_d;
7926 break;
7927 case Intrinsic::x86_sse2_pslli_q:
7928 NewIntNo = Intrinsic::x86_sse2_psll_q;
7929 break;
7930 case Intrinsic::x86_sse2_psrli_w:
7931 NewIntNo = Intrinsic::x86_sse2_psrl_w;
7932 break;
7933 case Intrinsic::x86_sse2_psrli_d:
7934 NewIntNo = Intrinsic::x86_sse2_psrl_d;
7935 break;
7936 case Intrinsic::x86_sse2_psrli_q:
7937 NewIntNo = Intrinsic::x86_sse2_psrl_q;
7938 break;
7939 case Intrinsic::x86_sse2_psrai_w:
7940 NewIntNo = Intrinsic::x86_sse2_psra_w;
7941 break;
7942 case Intrinsic::x86_sse2_psrai_d:
7943 NewIntNo = Intrinsic::x86_sse2_psra_d;
7944 break;
7945 default: {
Owen Anderson825b72b2009-08-11 20:47:22 +00007946 ShAmtVT = MVT::v2i32;
Evan Cheng5759f972008-05-04 09:15:50 +00007947 switch (IntNo) {
7948 case Intrinsic::x86_mmx_pslli_w:
7949 NewIntNo = Intrinsic::x86_mmx_psll_w;
7950 break;
7951 case Intrinsic::x86_mmx_pslli_d:
7952 NewIntNo = Intrinsic::x86_mmx_psll_d;
7953 break;
7954 case Intrinsic::x86_mmx_pslli_q:
7955 NewIntNo = Intrinsic::x86_mmx_psll_q;
7956 break;
7957 case Intrinsic::x86_mmx_psrli_w:
7958 NewIntNo = Intrinsic::x86_mmx_psrl_w;
7959 break;
7960 case Intrinsic::x86_mmx_psrli_d:
7961 NewIntNo = Intrinsic::x86_mmx_psrl_d;
7962 break;
7963 case Intrinsic::x86_mmx_psrli_q:
7964 NewIntNo = Intrinsic::x86_mmx_psrl_q;
7965 break;
7966 case Intrinsic::x86_mmx_psrai_w:
7967 NewIntNo = Intrinsic::x86_mmx_psra_w;
7968 break;
7969 case Intrinsic::x86_mmx_psrai_d:
7970 NewIntNo = Intrinsic::x86_mmx_psra_d;
7971 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00007972 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng5759f972008-05-04 09:15:50 +00007973 }
7974 break;
7975 }
7976 }
Mon P Wangefa42202009-09-03 19:56:25 +00007977
7978 // The vector shift intrinsics with scalars uses 32b shift amounts but
7979 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
7980 // to be zero.
7981 SDValue ShOps[4];
7982 ShOps[0] = ShAmt;
7983 ShOps[1] = DAG.getConstant(0, MVT::i32);
7984 if (ShAmtVT == MVT::v4i32) {
7985 ShOps[2] = DAG.getUNDEF(MVT::i32);
7986 ShOps[3] = DAG.getUNDEF(MVT::i32);
7987 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 4);
7988 } else {
7989 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 2);
Dale Johannesen0488fb62010-09-30 23:57:10 +00007990// FIXME this must be lowered to get rid of the invalid type.
Mon P Wangefa42202009-09-03 19:56:25 +00007991 }
7992
Owen Andersone50ed302009-08-10 22:56:29 +00007993 EVT VT = Op.getValueType();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007994 ShAmt = DAG.getNode(ISD::BITCAST, dl, VT, ShAmt);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007995 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007996 DAG.getConstant(NewIntNo, MVT::i32),
Evan Cheng5759f972008-05-04 09:15:50 +00007997 Op.getOperand(1), ShAmt);
7998 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00007999 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008000}
Evan Cheng72261582005-12-20 06:22:03 +00008001
Dan Gohmand858e902010-04-17 15:26:15 +00008002SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
8003 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +00008004 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
8005 MFI->setReturnAddressIsTaken(true);
8006
Bill Wendling64e87322009-01-16 19:25:27 +00008007 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008008 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00008009
8010 if (Depth > 0) {
8011 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
8012 SDValue Offset =
8013 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +00008014 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008015 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00008016 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008017 FrameAddr, Offset),
Chris Lattner51abfe42010-09-21 06:02:19 +00008018 MachinePointerInfo(), false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +00008019 }
8020
8021 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00008022 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00008023 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Chris Lattner51abfe42010-09-21 06:02:19 +00008024 RetAddrFI, MachinePointerInfo(), false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00008025}
8026
Dan Gohmand858e902010-04-17 15:26:15 +00008027SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +00008028 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
8029 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00008030
Owen Andersone50ed302009-08-10 22:56:29 +00008031 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008032 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00008033 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
8034 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00008035 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00008036 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +00008037 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
8038 MachinePointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +00008039 false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00008040 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00008041}
8042
Dan Gohman475871a2008-07-27 21:46:04 +00008043SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008044 SelectionDAG &DAG) const {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00008045 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008046}
8047
Dan Gohmand858e902010-04-17 15:26:15 +00008048SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008049 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00008050 SDValue Chain = Op.getOperand(0);
8051 SDValue Offset = Op.getOperand(1);
8052 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008053 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008054
Dan Gohmand8816272010-08-11 18:14:00 +00008055 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
8056 Subtarget->is64Bit() ? X86::RBP : X86::EBP,
8057 getPointerTy());
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008058 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008059
Dan Gohmand8816272010-08-11 18:14:00 +00008060 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Frame,
8061 DAG.getIntPtrConstant(TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008062 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +00008063 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
8064 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00008065 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008066 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008067
Dale Johannesene4d209d2009-02-03 20:21:25 +00008068 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008069 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008070 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008071}
8072
Dan Gohman475871a2008-07-27 21:46:04 +00008073SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008074 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00008075 SDValue Root = Op.getOperand(0);
8076 SDValue Trmp = Op.getOperand(1); // trampoline
8077 SDValue FPtr = Op.getOperand(2); // nested function
8078 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008079 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008080
Dan Gohman69de1932008-02-06 22:27:42 +00008081 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008082
8083 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00008084 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00008085
8086 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +00008087 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
8088 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +00008089
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00008090 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
8091 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00008092
8093 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
8094
8095 // Load the pointer to the nested function into R11.
8096 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00008097 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00008098 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008099 Addr, MachinePointerInfo(TrmpAddr),
8100 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008101
Owen Anderson825b72b2009-08-11 20:47:22 +00008102 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8103 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008104 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
8105 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +00008106 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00008107
8108 // Load the 'nest' parameter value into R10.
8109 // R10 is specified in X86CallingConv.td
8110 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +00008111 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8112 DAG.getConstant(10, MVT::i64));
8113 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008114 Addr, MachinePointerInfo(TrmpAddr, 10),
8115 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008116
Owen Anderson825b72b2009-08-11 20:47:22 +00008117 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8118 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008119 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
8120 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +00008121 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00008122
8123 // Jump to the nested function.
8124 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +00008125 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8126 DAG.getConstant(20, MVT::i64));
8127 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008128 Addr, MachinePointerInfo(TrmpAddr, 20),
8129 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008130
8131 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +00008132 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8133 DAG.getConstant(22, MVT::i64));
8134 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00008135 MachinePointerInfo(TrmpAddr, 22),
8136 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008137
Dan Gohman475871a2008-07-27 21:46:04 +00008138 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00008139 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008140 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008141 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00008142 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00008143 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00008144 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00008145 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008146
8147 switch (CC) {
8148 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00008149 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +00008150 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00008151 case CallingConv::X86_StdCall: {
8152 // Pass 'nest' parameter in ECX.
8153 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00008154 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008155
8156 // Check that ECX wasn't needed by an 'inreg' parameter.
8157 const FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00008158 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008159
Chris Lattner58d74912008-03-12 17:45:29 +00008160 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00008161 unsigned InRegCount = 0;
8162 unsigned Idx = 1;
8163
8164 for (FunctionType::param_iterator I = FTy->param_begin(),
8165 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00008166 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00008167 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00008168 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008169
8170 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +00008171 report_fatal_error("Nest register in use - reduce number of inreg"
8172 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +00008173 }
8174 }
8175 break;
8176 }
8177 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +00008178 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00008179 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00008180 // Pass 'nest' parameter in EAX.
8181 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00008182 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008183 break;
8184 }
8185
Dan Gohman475871a2008-07-27 21:46:04 +00008186 SDValue OutChains[4];
8187 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008188
Owen Anderson825b72b2009-08-11 20:47:22 +00008189 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8190 DAG.getConstant(10, MVT::i32));
8191 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008192
Chris Lattnera62fe662010-02-05 19:20:30 +00008193 // This is storing the opcode for MOV32ri.
8194 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00008195 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00008196 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008197 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008198 Trmp, MachinePointerInfo(TrmpAddr),
8199 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008200
Owen Anderson825b72b2009-08-11 20:47:22 +00008201 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8202 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008203 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
8204 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +00008205 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008206
Chris Lattnera62fe662010-02-05 19:20:30 +00008207 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +00008208 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8209 DAG.getConstant(5, MVT::i32));
8210 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00008211 MachinePointerInfo(TrmpAddr, 5),
8212 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008213
Owen Anderson825b72b2009-08-11 20:47:22 +00008214 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8215 DAG.getConstant(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008216 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
8217 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +00008218 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008219
Dan Gohman475871a2008-07-27 21:46:04 +00008220 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00008221 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008222 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008223 }
8224}
8225
Dan Gohmand858e902010-04-17 15:26:15 +00008226SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
8227 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008228 /*
8229 The rounding mode is in bits 11:10 of FPSR, and has the following
8230 settings:
8231 00 Round to nearest
8232 01 Round to -inf
8233 10 Round to +inf
8234 11 Round to 0
8235
8236 FLT_ROUNDS, on the other hand, expects the following:
8237 -1 Undefined
8238 0 Round to 0
8239 1 Round to nearest
8240 2 Round to +inf
8241 3 Round to -inf
8242
8243 To perform the conversion, we do:
8244 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
8245 */
8246
8247 MachineFunction &MF = DAG.getMachineFunction();
8248 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00008249 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008250 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00008251 EVT VT = Op.getValueType();
Chris Lattner2156b792010-09-22 01:11:26 +00008252 DebugLoc DL = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008253
8254 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +00008255 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +00008256 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008257
Michael J. Spencerec38de22010-10-10 22:04:20 +00008258
Chris Lattner2156b792010-09-22 01:11:26 +00008259 MachineMemOperand *MMO =
8260 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8261 MachineMemOperand::MOStore, 2, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008262
Chris Lattner2156b792010-09-22 01:11:26 +00008263 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
8264 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
8265 DAG.getVTList(MVT::Other),
8266 Ops, 2, MVT::i16, MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008267
8268 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +00008269 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Chris Lattner51abfe42010-09-21 06:02:19 +00008270 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008271
8272 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00008273 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +00008274 DAG.getNode(ISD::SRL, DL, MVT::i16,
8275 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00008276 CWD, DAG.getConstant(0x800, MVT::i16)),
8277 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00008278 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +00008279 DAG.getNode(ISD::SRL, DL, MVT::i16,
8280 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00008281 CWD, DAG.getConstant(0x400, MVT::i16)),
8282 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008283
Dan Gohman475871a2008-07-27 21:46:04 +00008284 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +00008285 DAG.getNode(ISD::AND, DL, MVT::i16,
8286 DAG.getNode(ISD::ADD, DL, MVT::i16,
8287 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +00008288 DAG.getConstant(1, MVT::i16)),
8289 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008290
8291
Duncan Sands83ec4b62008-06-06 12:08:01 +00008292 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +00008293 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008294}
8295
Dan Gohmand858e902010-04-17 15:26:15 +00008296SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008297 EVT VT = Op.getValueType();
8298 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008299 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008300 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00008301
8302 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008303 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00008304 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +00008305 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00008306 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008307 }
Evan Cheng18efe262007-12-14 02:13:44 +00008308
Evan Cheng152804e2007-12-14 08:30:15 +00008309 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008310 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008311 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00008312
8313 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00008314 SDValue Ops[] = {
8315 Op,
8316 DAG.getConstant(NumBits+NumBits-1, OpVT),
8317 DAG.getConstant(X86::COND_E, MVT::i8),
8318 Op.getValue(1)
8319 };
8320 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00008321
8322 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +00008323 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +00008324
Owen Anderson825b72b2009-08-11 20:47:22 +00008325 if (VT == MVT::i8)
8326 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008327 return Op;
8328}
8329
Dan Gohmand858e902010-04-17 15:26:15 +00008330SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008331 EVT VT = Op.getValueType();
8332 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008333 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008334 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00008335
8336 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008337 if (VT == MVT::i8) {
8338 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00008339 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008340 }
Evan Cheng152804e2007-12-14 08:30:15 +00008341
8342 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008343 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008344 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00008345
8346 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00008347 SDValue Ops[] = {
8348 Op,
8349 DAG.getConstant(NumBits, OpVT),
8350 DAG.getConstant(X86::COND_E, MVT::i8),
8351 Op.getValue(1)
8352 };
8353 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00008354
Owen Anderson825b72b2009-08-11 20:47:22 +00008355 if (VT == MVT::i8)
8356 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008357 return Op;
8358}
8359
Dan Gohmand858e902010-04-17 15:26:15 +00008360SDValue X86TargetLowering::LowerMUL_V2I64(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008361 EVT VT = Op.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00008362 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008363 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00008364
Mon P Wangaf9b9522008-12-18 21:42:19 +00008365 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
8366 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
8367 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
8368 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
8369 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
8370 //
8371 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
8372 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
8373 // return AloBlo + AloBhi + AhiBlo;
8374
8375 SDValue A = Op.getOperand(0);
8376 SDValue B = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008377
Dale Johannesene4d209d2009-02-03 20:21:25 +00008378 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008379 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
8380 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008381 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008382 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
8383 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008384 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008385 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008386 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008387 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008388 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008389 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008390 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008391 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008392 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008393 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008394 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
8395 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008396 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008397 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
8398 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008399 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
8400 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +00008401 return Res;
8402}
8403
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008404SDValue X86TargetLowering::LowerSHL(SDValue Op, SelectionDAG &DAG) const {
8405 EVT VT = Op.getValueType();
8406 DebugLoc dl = Op.getDebugLoc();
8407 SDValue R = Op.getOperand(0);
8408
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008409 LLVMContext *Context = DAG.getContext();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008410
Nate Begeman51409212010-07-28 00:21:48 +00008411 assert(Subtarget->hasSSE41() && "Cannot lower SHL without SSE4.1 or later");
8412
8413 if (VT == MVT::v4i32) {
8414 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8415 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
8416 Op.getOperand(1), DAG.getConstant(23, MVT::i32));
8417
8418 ConstantInt *CI = ConstantInt::get(*Context, APInt(32, 0x3f800000U));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008419
Nate Begeman51409212010-07-28 00:21:48 +00008420 std::vector<Constant*> CV(4, CI);
8421 Constant *C = ConstantVector::get(CV);
8422 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8423 SDValue Addend = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008424 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +00008425 false, false, 16);
8426
8427 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Addend);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008428 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op);
Nate Begeman51409212010-07-28 00:21:48 +00008429 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
8430 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
8431 }
8432 if (VT == MVT::v16i8) {
8433 // a = a << 5;
8434 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8435 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
8436 Op.getOperand(1), DAG.getConstant(5, MVT::i32));
8437
8438 ConstantInt *CM1 = ConstantInt::get(*Context, APInt(8, 15));
8439 ConstantInt *CM2 = ConstantInt::get(*Context, APInt(8, 63));
8440
8441 std::vector<Constant*> CVM1(16, CM1);
8442 std::vector<Constant*> CVM2(16, CM2);
8443 Constant *C = ConstantVector::get(CVM1);
8444 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8445 SDValue M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008446 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +00008447 false, false, 16);
8448
8449 // r = pblendv(r, psllw(r & (char16)15, 4), a);
8450 M = DAG.getNode(ISD::AND, dl, VT, R, M);
8451 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8452 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
8453 DAG.getConstant(4, MVT::i32));
Nate Begeman672fb622010-12-20 22:04:24 +00008454 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT, R, M, Op);
Nate Begeman51409212010-07-28 00:21:48 +00008455 // a += a
8456 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008457
Nate Begeman51409212010-07-28 00:21:48 +00008458 C = ConstantVector::get(CVM2);
8459 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8460 M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008461 MachinePointerInfo::getConstantPool(),
Chris Lattner51abfe42010-09-21 06:02:19 +00008462 false, false, 16);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008463
Nate Begeman51409212010-07-28 00:21:48 +00008464 // r = pblendv(r, psllw(r & (char16)63, 2), a);
8465 M = DAG.getNode(ISD::AND, dl, VT, R, M);
8466 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8467 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
8468 DAG.getConstant(2, MVT::i32));
Nate Begeman672fb622010-12-20 22:04:24 +00008469 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT, R, M, Op);
Nate Begeman51409212010-07-28 00:21:48 +00008470 // a += a
8471 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008472
Nate Begeman51409212010-07-28 00:21:48 +00008473 // return pblendv(r, r+r, a);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008474 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT,
Nate Begeman51409212010-07-28 00:21:48 +00008475 R, DAG.getNode(ISD::ADD, dl, VT, R, R), Op);
8476 return R;
8477 }
8478 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008479}
Mon P Wangaf9b9522008-12-18 21:42:19 +00008480
Dan Gohmand858e902010-04-17 15:26:15 +00008481SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
Bill Wendling74c37652008-12-09 22:08:41 +00008482 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
8483 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +00008484 // looks for this combo and may remove the "setcc" instruction if the "setcc"
8485 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +00008486 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +00008487 SDValue LHS = N->getOperand(0);
8488 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +00008489 unsigned BaseOp = 0;
8490 unsigned Cond = 0;
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008491 DebugLoc DL = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +00008492 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008493 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +00008494 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +00008495 // A subtract of one will be selected as a INC. Note that INC doesn't
8496 // set CF, so we can't do this for UADDO.
8497 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
8498 if (C->getAPIntValue() == 1) {
8499 BaseOp = X86ISD::INC;
8500 Cond = X86::COND_O;
8501 break;
8502 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008503 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +00008504 Cond = X86::COND_O;
8505 break;
8506 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008507 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +00008508 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00008509 break;
8510 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +00008511 // A subtract of one will be selected as a DEC. Note that DEC doesn't
8512 // set CF, so we can't do this for USUBO.
8513 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
8514 if (C->getAPIntValue() == 1) {
8515 BaseOp = X86ISD::DEC;
8516 Cond = X86::COND_O;
8517 break;
8518 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008519 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +00008520 Cond = X86::COND_O;
8521 break;
8522 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008523 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +00008524 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00008525 break;
8526 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00008527 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +00008528 Cond = X86::COND_O;
8529 break;
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008530 case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs
8531 SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0),
8532 MVT::i32);
8533 SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008534
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008535 SDValue SetCC =
8536 DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
8537 DAG.getConstant(X86::COND_O, MVT::i32),
8538 SDValue(Sum.getNode(), 2));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008539
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008540 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
8541 return Sum;
8542 }
Bill Wendling74c37652008-12-09 22:08:41 +00008543 }
Bill Wendling3fafd932008-11-26 22:37:40 +00008544
Bill Wendling61edeb52008-12-02 01:06:39 +00008545 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008546 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008547 SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +00008548
Bill Wendling61edeb52008-12-02 01:06:39 +00008549 SDValue SetCC =
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008550 DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1),
8551 DAG.getConstant(Cond, MVT::i32),
8552 SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +00008553
Bill Wendling61edeb52008-12-02 01:06:39 +00008554 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
8555 return Sum;
Bill Wendling41ea7e72008-11-24 19:21:46 +00008556}
8557
Eric Christopher9a9d2752010-07-22 02:48:34 +00008558SDValue X86TargetLowering::LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG) const{
8559 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00008560
Eric Christopherb6729dc2010-08-04 23:03:04 +00008561 if (!Subtarget->hasSSE2()) {
Eric Christopherc0b2a202010-08-14 21:51:50 +00008562 SDValue Chain = Op.getOperand(0);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008563 SDValue Zero = DAG.getConstant(0,
Eric Christopherb6729dc2010-08-04 23:03:04 +00008564 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Eric Christopherc0b2a202010-08-14 21:51:50 +00008565 SDValue Ops[] = {
8566 DAG.getRegister(X86::ESP, MVT::i32), // Base
8567 DAG.getTargetConstant(1, MVT::i8), // Scale
8568 DAG.getRegister(0, MVT::i32), // Index
8569 DAG.getTargetConstant(0, MVT::i32), // Disp
8570 DAG.getRegister(0, MVT::i32), // Segment.
8571 Zero,
8572 Chain
8573 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00008574 SDNode *Res =
Eric Christopherc0b2a202010-08-14 21:51:50 +00008575 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
8576 array_lengthof(Ops));
8577 return SDValue(Res, 0);
Eric Christopherb6729dc2010-08-04 23:03:04 +00008578 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00008579
Eric Christopher9a9d2752010-07-22 02:48:34 +00008580 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
Chris Lattner132929a2010-08-14 17:26:09 +00008581 if (!isDev)
Eric Christopher9a9d2752010-07-22 02:48:34 +00008582 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008583
Chris Lattner132929a2010-08-14 17:26:09 +00008584 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
8585 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
8586 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
8587 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
Michael J. Spencerec38de22010-10-10 22:04:20 +00008588
Chris Lattner132929a2010-08-14 17:26:09 +00008589 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
8590 if (!Op1 && !Op2 && !Op3 && Op4)
8591 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008592
Chris Lattner132929a2010-08-14 17:26:09 +00008593 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
8594 if (Op1 && !Op2 && !Op3 && !Op4)
8595 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008596
8597 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
Chris Lattner132929a2010-08-14 17:26:09 +00008598 // (MFENCE)>;
8599 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
Eric Christopher9a9d2752010-07-22 02:48:34 +00008600}
8601
Dan Gohmand858e902010-04-17 15:26:15 +00008602SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008603 EVT T = Op.getValueType();
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008604 DebugLoc DL = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +00008605 unsigned Reg = 0;
8606 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00008607 switch(T.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00008608 default:
8609 assert(false && "Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +00008610 case MVT::i8: Reg = X86::AL; size = 1; break;
8611 case MVT::i16: Reg = X86::AX; size = 2; break;
8612 case MVT::i32: Reg = X86::EAX; size = 4; break;
8613 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +00008614 assert(Subtarget->is64Bit() && "Node not type legal!");
8615 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +00008616 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00008617 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008618 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +00008619 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +00008620 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +00008621 Op.getOperand(1),
8622 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +00008623 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +00008624 cpIn.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00008625 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008626 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
8627 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
8628 Ops, 5, T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +00008629 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008630 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +00008631 return cpOut;
8632}
8633
Duncan Sands1607f052008-12-01 11:39:25 +00008634SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008635 SelectionDAG &DAG) const {
Duncan Sands1607f052008-12-01 11:39:25 +00008636 assert(Subtarget->is64Bit() && "Result not type legalized?");
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00008637 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +00008638 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008639 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +00008640 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00008641 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
8642 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +00008643 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +00008644 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
8645 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +00008646 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +00008647 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +00008648 rdx.getValue(1)
8649 };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008650 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008651}
8652
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008653SDValue X86TargetLowering::LowerBITCAST(SDValue Op,
Dale Johannesen7d07b482010-05-21 00:52:33 +00008654 SelectionDAG &DAG) const {
8655 EVT SrcVT = Op.getOperand(0).getValueType();
8656 EVT DstVT = Op.getValueType();
Chris Lattner2a786eb2010-12-19 20:19:20 +00008657 assert(Subtarget->is64Bit() && !Subtarget->hasSSE2() &&
8658 Subtarget->hasMMX() && "Unexpected custom BITCAST");
Michael J. Spencerec38de22010-10-10 22:04:20 +00008659 assert((DstVT == MVT::i64 ||
Dale Johannesen7d07b482010-05-21 00:52:33 +00008660 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008661 "Unexpected custom BITCAST");
Dale Johannesen7d07b482010-05-21 00:52:33 +00008662 // i64 <=> MMX conversions are Legal.
8663 if (SrcVT==MVT::i64 && DstVT.isVector())
8664 return Op;
8665 if (DstVT==MVT::i64 && SrcVT.isVector())
8666 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +00008667 // MMX <=> MMX conversions are Legal.
8668 if (SrcVT.isVector() && DstVT.isVector())
8669 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +00008670 // All other conversions need to be expanded.
8671 return SDValue();
8672}
Chris Lattner5b856542010-12-20 00:59:46 +00008673
Dan Gohmand858e902010-04-17 15:26:15 +00008674SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen71d1bf52008-09-29 22:25:26 +00008675 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +00008676 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008677 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008678 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +00008679 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008680 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008681 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +00008682 Node->getOperand(0),
8683 Node->getOperand(1), negOp,
8684 cast<AtomicSDNode>(Node)->getSrcValue(),
8685 cast<AtomicSDNode>(Node)->getAlignment());
Mon P Wang63307c32008-05-05 19:05:59 +00008686}
8687
Chris Lattner5b856542010-12-20 00:59:46 +00008688static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
8689 EVT VT = Op.getNode()->getValueType(0);
8690
8691 // Let legalize expand this if it isn't a legal type yet.
8692 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
8693 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008694
Chris Lattner5b856542010-12-20 00:59:46 +00008695 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008696
Chris Lattner5b856542010-12-20 00:59:46 +00008697 unsigned Opc;
8698 bool ExtraOp = false;
8699 switch (Op.getOpcode()) {
8700 default: assert(0 && "Invalid code");
8701 case ISD::ADDC: Opc = X86ISD::ADD; break;
8702 case ISD::ADDE: Opc = X86ISD::ADC; ExtraOp = true; break;
8703 case ISD::SUBC: Opc = X86ISD::SUB; break;
8704 case ISD::SUBE: Opc = X86ISD::SBB; ExtraOp = true; break;
8705 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008706
Chris Lattner5b856542010-12-20 00:59:46 +00008707 if (!ExtraOp)
8708 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
8709 Op.getOperand(1));
8710 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
8711 Op.getOperand(1), Op.getOperand(2));
8712}
8713
Evan Cheng0db9fe62006-04-25 20:13:52 +00008714/// LowerOperation - Provide custom lowering hooks for some operations.
8715///
Dan Gohmand858e902010-04-17 15:26:15 +00008716SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00008717 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008718 default: llvm_unreachable("Should not custom lower this!");
Eric Christopher9a9d2752010-07-22 02:48:34 +00008719 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op,DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008720 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
8721 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008722 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00008723 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008724 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
8725 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
8726 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
David Greene91585092011-01-26 15:38:49 +00008727 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op, DAG);
David Greenecfe33c42011-01-26 19:13:22 +00008728 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008729 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
8730 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
8731 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00008732 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +00008733 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +00008734 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008735 case ISD::SHL_PARTS:
8736 case ISD::SRA_PARTS:
8737 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
8738 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00008739 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008740 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00008741 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008742 case ISD::FABS: return LowerFABS(Op, DAG);
8743 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008744 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00008745 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman30a0de92008-07-17 16:51:19 +00008746 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00008747 case ISD::SELECT: return LowerSELECT(Op, DAG);
8748 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008749 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008750 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +00008751 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +00008752 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008753 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00008754 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
8755 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008756 case ISD::FRAME_TO_ARGS_OFFSET:
8757 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008758 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008759 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008760 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +00008761 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +00008762 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
8763 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Mon P Wangaf9b9522008-12-18 21:42:19 +00008764 case ISD::MUL: return LowerMUL_V2I64(Op, DAG);
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008765 case ISD::SHL: return LowerSHL(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +00008766 case ISD::SADDO:
8767 case ISD::UADDO:
8768 case ISD::SSUBO:
8769 case ISD::USUBO:
8770 case ISD::SMULO:
8771 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +00008772 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008773 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Chris Lattner5b856542010-12-20 00:59:46 +00008774 case ISD::ADDC:
8775 case ISD::ADDE:
8776 case ISD::SUBC:
8777 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008778 }
Chris Lattner27a6c732007-11-24 07:07:01 +00008779}
8780
Duncan Sands1607f052008-12-01 11:39:25 +00008781void X86TargetLowering::
8782ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00008783 SelectionDAG &DAG, unsigned NewOp) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008784 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008785 DebugLoc dl = Node->getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00008786 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +00008787
8788 SDValue Chain = Node->getOperand(0);
8789 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00008790 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00008791 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00008792 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00008793 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +00008794 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +00008795 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +00008796 SDValue Result =
8797 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
8798 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +00008799 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +00008800 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00008801 Results.push_back(Result.getValue(2));
8802}
8803
Duncan Sands126d9072008-07-04 11:47:58 +00008804/// ReplaceNodeResults - Replace a node with an illegal result type
8805/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00008806void X86TargetLowering::ReplaceNodeResults(SDNode *N,
8807 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00008808 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +00008809 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +00008810 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +00008811 default:
Duncan Sands1607f052008-12-01 11:39:25 +00008812 assert(false && "Do not know how to custom type legalize this operation!");
8813 return;
Chris Lattner5b856542010-12-20 00:59:46 +00008814 case ISD::ADDC:
8815 case ISD::ADDE:
8816 case ISD::SUBC:
8817 case ISD::SUBE:
8818 // We don't want to expand or promote these.
8819 return;
Duncan Sands1607f052008-12-01 11:39:25 +00008820 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +00008821 std::pair<SDValue,SDValue> Vals =
8822 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +00008823 SDValue FIST = Vals.first, StackSlot = Vals.second;
8824 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +00008825 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +00008826 // Return a load from the stack slot.
Chris Lattner51abfe42010-09-21 06:02:19 +00008827 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
8828 MachinePointerInfo(), false, false, 0));
Duncan Sands1607f052008-12-01 11:39:25 +00008829 }
8830 return;
8831 }
8832 case ISD::READCYCLECOUNTER: {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00008833 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +00008834 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008835 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00008836 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +00008837 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00008838 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00008839 eax.getValue(2));
8840 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
8841 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +00008842 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00008843 Results.push_back(edx.getValue(1));
8844 return;
8845 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008846 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +00008847 EVT T = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008848 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
Duncan Sands1607f052008-12-01 11:39:25 +00008849 SDValue cpInL, cpInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00008850 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
8851 DAG.getConstant(0, MVT::i32));
8852 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
8853 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00008854 cpInL = DAG.getCopyToReg(N->getOperand(0), dl, X86::EAX, cpInL, SDValue());
8855 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl, X86::EDX, cpInH,
Duncan Sands1607f052008-12-01 11:39:25 +00008856 cpInL.getValue(1));
8857 SDValue swapInL, swapInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00008858 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
8859 DAG.getConstant(0, MVT::i32));
8860 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
8861 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00008862 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl, X86::EBX, swapInL,
Duncan Sands1607f052008-12-01 11:39:25 +00008863 cpInH.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00008864 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl, X86::ECX, swapInH,
Duncan Sands1607f052008-12-01 11:39:25 +00008865 swapInL.getValue(1));
8866 SDValue Ops[] = { swapInH.getValue(0),
8867 N->getOperand(1),
8868 swapInH.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00008869 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Andrew Trick1a2cf3b2010-10-11 19:02:04 +00008870 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
8871 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG8_DAG, dl, Tys,
8872 Ops, 3, T, MMO);
Dale Johannesendd64c412009-02-04 00:33:20 +00008873 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl, X86::EAX,
Owen Anderson825b72b2009-08-11 20:47:22 +00008874 MVT::i32, Result.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00008875 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl, X86::EDX,
Owen Anderson825b72b2009-08-11 20:47:22 +00008876 MVT::i32, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +00008877 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Owen Anderson825b72b2009-08-11 20:47:22 +00008878 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00008879 Results.push_back(cpOutH.getValue(1));
8880 return;
8881 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008882 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +00008883 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
8884 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008885 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +00008886 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
8887 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008888 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +00008889 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
8890 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008891 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +00008892 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
8893 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008894 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +00008895 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
8896 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008897 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +00008898 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
8899 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008900 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +00008901 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
8902 return;
Chris Lattner27a6c732007-11-24 07:07:01 +00008903 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00008904}
8905
Evan Cheng72261582005-12-20 06:22:03 +00008906const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
8907 switch (Opcode) {
8908 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +00008909 case X86ISD::BSF: return "X86ISD::BSF";
8910 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +00008911 case X86ISD::SHLD: return "X86ISD::SHLD";
8912 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00008913 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +00008914 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +00008915 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +00008916 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +00008917 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00008918 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00008919 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
8920 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
8921 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00008922 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00008923 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +00008924 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +00008925 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +00008926 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +00008927 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +00008928 case X86ISD::COMI: return "X86ISD::COMI";
8929 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +00008930 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +00008931 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Evan Cheng72261582005-12-20 06:22:03 +00008932 case X86ISD::CMOV: return "X86ISD::CMOV";
8933 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00008934 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00008935 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
8936 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +00008937 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00008938 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +00008939 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +00008940 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +00008941 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +00008942 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
8943 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +00008944 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +00008945 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Nate Begemanb65c1752010-12-17 22:55:37 +00008946 case X86ISD::PANDN: return "X86ISD::PANDN";
8947 case X86ISD::PSIGNB: return "X86ISD::PSIGNB";
8948 case X86ISD::PSIGNW: return "X86ISD::PSIGNW";
8949 case X86ISD::PSIGND: return "X86ISD::PSIGND";
Nate Begeman672fb622010-12-20 22:04:24 +00008950 case X86ISD::PBLENDVB: return "X86ISD::PBLENDVB";
Evan Cheng8ca29322006-11-10 21:43:37 +00008951 case X86ISD::FMAX: return "X86ISD::FMAX";
8952 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +00008953 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
8954 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00008955 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +00008956 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008957 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00008958 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008959 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +00008960 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
8961 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008962 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
8963 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
8964 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
8965 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
8966 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
8967 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +00008968 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
8969 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +00008970 case X86ISD::VSHL: return "X86ISD::VSHL";
8971 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +00008972 case X86ISD::CMPPD: return "X86ISD::CMPPD";
8973 case X86ISD::CMPPS: return "X86ISD::CMPPS";
8974 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
8975 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
8976 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
8977 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
8978 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
8979 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
8980 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
8981 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008982 case X86ISD::ADD: return "X86ISD::ADD";
8983 case X86ISD::SUB: return "X86ISD::SUB";
Chris Lattner5b856542010-12-20 00:59:46 +00008984 case X86ISD::ADC: return "X86ISD::ADC";
8985 case X86ISD::SBB: return "X86ISD::SBB";
Bill Wendlingd350e022008-12-12 21:15:41 +00008986 case X86ISD::SMUL: return "X86ISD::SMUL";
8987 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +00008988 case X86ISD::INC: return "X86ISD::INC";
8989 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +00008990 case X86ISD::OR: return "X86ISD::OR";
8991 case X86ISD::XOR: return "X86ISD::XOR";
8992 case X86ISD::AND: return "X86ISD::AND";
Evan Cheng73f24c92009-03-30 21:36:47 +00008993 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +00008994 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008995 case X86ISD::TESTP: return "X86ISD::TESTP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00008996 case X86ISD::PALIGN: return "X86ISD::PALIGN";
8997 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
8998 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
8999 case X86ISD::PSHUFHW_LD: return "X86ISD::PSHUFHW_LD";
9000 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
9001 case X86ISD::PSHUFLW_LD: return "X86ISD::PSHUFLW_LD";
9002 case X86ISD::SHUFPS: return "X86ISD::SHUFPS";
9003 case X86ISD::SHUFPD: return "X86ISD::SHUFPD";
9004 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009005 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00009006 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009007 case X86ISD::MOVHLPD: return "X86ISD::MOVHLPD";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00009008 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
9009 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009010 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
9011 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
9012 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
9013 case X86ISD::MOVSHDUP_LD: return "X86ISD::MOVSHDUP_LD";
9014 case X86ISD::MOVSLDUP_LD: return "X86ISD::MOVSLDUP_LD";
9015 case X86ISD::MOVSD: return "X86ISD::MOVSD";
9016 case X86ISD::MOVSS: return "X86ISD::MOVSS";
9017 case X86ISD::UNPCKLPS: return "X86ISD::UNPCKLPS";
9018 case X86ISD::UNPCKLPD: return "X86ISD::UNPCKLPD";
9019 case X86ISD::UNPCKHPS: return "X86ISD::UNPCKHPS";
9020 case X86ISD::UNPCKHPD: return "X86ISD::UNPCKHPD";
9021 case X86ISD::PUNPCKLBW: return "X86ISD::PUNPCKLBW";
9022 case X86ISD::PUNPCKLWD: return "X86ISD::PUNPCKLWD";
9023 case X86ISD::PUNPCKLDQ: return "X86ISD::PUNPCKLDQ";
9024 case X86ISD::PUNPCKLQDQ: return "X86ISD::PUNPCKLQDQ";
9025 case X86ISD::PUNPCKHBW: return "X86ISD::PUNPCKHBW";
9026 case X86ISD::PUNPCKHWD: return "X86ISD::PUNPCKHWD";
9027 case X86ISD::PUNPCKHDQ: return "X86ISD::PUNPCKHDQ";
9028 case X86ISD::PUNPCKHQDQ: return "X86ISD::PUNPCKHQDQ";
Dan Gohmand6708ea2009-08-15 01:38:56 +00009029 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Dan Gohman320afb82010-10-12 18:00:49 +00009030 case X86ISD::VAARG_64: return "X86ISD::VAARG_64";
Michael J. Spencere9c253e2010-10-21 01:41:01 +00009031 case X86ISD::WIN_ALLOCA: return "X86ISD::WIN_ALLOCA";
Evan Cheng72261582005-12-20 06:22:03 +00009032 }
9033}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00009034
Chris Lattnerc9addb72007-03-30 23:15:24 +00009035// isLegalAddressingMode - Return true if the addressing mode represented
9036// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +00009037bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerc9addb72007-03-30 23:15:24 +00009038 const Type *Ty) const {
9039 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009040 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +00009041 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +00009042
Chris Lattnerc9addb72007-03-30 23:15:24 +00009043 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009044 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +00009045 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00009046
Chris Lattnerc9addb72007-03-30 23:15:24 +00009047 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +00009048 unsigned GVFlags =
9049 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009050
Chris Lattnerdfed4132009-07-10 07:38:24 +00009051 // If a reference to this global requires an extra load, we can't fold it.
9052 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +00009053 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009054
Chris Lattnerdfed4132009-07-10 07:38:24 +00009055 // If BaseGV requires a register for the PIC base, we cannot also have a
9056 // BaseReg specified.
9057 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +00009058 return false;
Evan Cheng52787842007-08-01 23:46:47 +00009059
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009060 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +00009061 if ((M != CodeModel::Small || R != Reloc::Static) &&
9062 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009063 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +00009064 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009065
Chris Lattnerc9addb72007-03-30 23:15:24 +00009066 switch (AM.Scale) {
9067 case 0:
9068 case 1:
9069 case 2:
9070 case 4:
9071 case 8:
9072 // These scales always work.
9073 break;
9074 case 3:
9075 case 5:
9076 case 9:
9077 // These scales are formed with basereg+scalereg. Only accept if there is
9078 // no basereg yet.
9079 if (AM.HasBaseReg)
9080 return false;
9081 break;
9082 default: // Other stuff never works.
9083 return false;
9084 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009085
Chris Lattnerc9addb72007-03-30 23:15:24 +00009086 return true;
9087}
9088
9089
Evan Cheng2bd122c2007-10-26 01:56:11 +00009090bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00009091 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +00009092 return false;
Evan Chenge127a732007-10-29 07:57:50 +00009093 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
9094 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00009095 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +00009096 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00009097 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +00009098}
9099
Owen Andersone50ed302009-08-10 22:56:29 +00009100bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +00009101 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009102 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009103 unsigned NumBits1 = VT1.getSizeInBits();
9104 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00009105 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009106 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00009107 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009108}
Evan Cheng2bd122c2007-10-26 01:56:11 +00009109
Dan Gohman97121ba2009-04-08 00:15:30 +00009110bool X86TargetLowering::isZExtFree(const Type *Ty1, const Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00009111 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00009112 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00009113}
9114
Owen Andersone50ed302009-08-10 22:56:29 +00009115bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00009116 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00009117 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00009118}
9119
Owen Andersone50ed302009-08-10 22:56:29 +00009120bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +00009121 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +00009122 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +00009123}
9124
Evan Cheng60c07e12006-07-05 22:17:51 +00009125/// isShuffleMaskLegal - Targets can use this to indicate that they only
9126/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
9127/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
9128/// are assumed to be legal.
9129bool
Eric Christopherfd179292009-08-27 18:07:15 +00009130X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +00009131 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +00009132 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +00009133 if (VT.getSizeInBits() == 64)
Eric Christophercff6f852010-04-15 01:40:20 +00009134 return isPALIGNRMask(M, VT, Subtarget->hasSSSE3());
Nate Begeman9008ca62009-04-27 18:41:29 +00009135
Nate Begemana09008b2009-10-19 02:17:23 +00009136 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +00009137 return (VT.getVectorNumElements() == 2 ||
9138 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
9139 isMOVLMask(M, VT) ||
9140 isSHUFPMask(M, VT) ||
9141 isPSHUFDMask(M, VT) ||
9142 isPSHUFHWMask(M, VT) ||
9143 isPSHUFLWMask(M, VT) ||
Nate Begemana09008b2009-10-19 02:17:23 +00009144 isPALIGNRMask(M, VT, Subtarget->hasSSSE3()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +00009145 isUNPCKLMask(M, VT) ||
9146 isUNPCKHMask(M, VT) ||
9147 isUNPCKL_v_undef_Mask(M, VT) ||
9148 isUNPCKH_v_undef_Mask(M, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00009149}
9150
Dan Gohman7d8143f2008-04-09 20:09:42 +00009151bool
Nate Begeman5a5ca152009-04-29 05:20:52 +00009152X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +00009153 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00009154 unsigned NumElts = VT.getVectorNumElements();
9155 // FIXME: This collection of masks seems suspect.
9156 if (NumElts == 2)
9157 return true;
9158 if (NumElts == 4 && VT.getSizeInBits() == 128) {
9159 return (isMOVLMask(Mask, VT) ||
9160 isCommutedMOVLMask(Mask, VT, true) ||
9161 isSHUFPMask(Mask, VT) ||
9162 isCommutedSHUFPMask(Mask, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00009163 }
9164 return false;
9165}
9166
9167//===----------------------------------------------------------------------===//
9168// X86 Scheduler Hooks
9169//===----------------------------------------------------------------------===//
9170
Mon P Wang63307c32008-05-05 19:05:59 +00009171// private utility function
9172MachineBasicBlock *
9173X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
9174 MachineBasicBlock *MBB,
9175 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009176 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009177 unsigned LoadOpc,
9178 unsigned CXchgOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009179 unsigned notOpc,
9180 unsigned EAXreg,
9181 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009182 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00009183 // For the atomic bitwise operator, we generate
9184 // thisMBB:
9185 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00009186 // ld t1 = [bitinstr.addr]
9187 // op t2 = t1, [bitinstr.val]
9188 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00009189 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
9190 // bz newMBB
9191 // fallthrough -->nextMBB
9192 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9193 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009194 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00009195 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009196
Mon P Wang63307c32008-05-05 19:05:59 +00009197 /// First build the CFG
9198 MachineFunction *F = MBB->getParent();
9199 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009200 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9201 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9202 F->insert(MBBIter, newMBB);
9203 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009204
Dan Gohman14152b42010-07-06 20:24:04 +00009205 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9206 nextMBB->splice(nextMBB->begin(), thisMBB,
9207 llvm::next(MachineBasicBlock::iterator(bInstr)),
9208 thisMBB->end());
9209 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009210
Mon P Wang63307c32008-05-05 19:05:59 +00009211 // Update thisMBB to fall through to newMBB
9212 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009213
Mon P Wang63307c32008-05-05 19:05:59 +00009214 // newMBB jumps to itself and fall through to nextMBB
9215 newMBB->addSuccessor(nextMBB);
9216 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009217
Mon P Wang63307c32008-05-05 19:05:59 +00009218 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009219 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009220 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +00009221 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00009222 MachineOperand& destOper = bInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009223 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00009224 int numArgs = bInstr->getNumOperands() - 1;
9225 for (int i=0; i < numArgs; ++i)
9226 argOpers[i] = &bInstr->getOperand(i+1);
9227
9228 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009229 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009230 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00009231
Dale Johannesen140be2d2008-08-19 18:47:28 +00009232 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009233 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00009234 for (int i=0; i <= lastAddrIndx; ++i)
9235 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009236
Dale Johannesen140be2d2008-08-19 18:47:28 +00009237 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009238 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00009239 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009240 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009241 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009242 tt = t1;
9243
Dale Johannesen140be2d2008-08-19 18:47:28 +00009244 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +00009245 assert((argOpers[valArgIndx]->isReg() ||
9246 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00009247 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +00009248 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009249 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00009250 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009251 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009252 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +00009253 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009254
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009255 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +00009256 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009257
Dale Johannesene4d209d2009-02-03 20:21:25 +00009258 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +00009259 for (int i=0; i <= lastAddrIndx; ++i)
9260 (*MIB).addOperand(*argOpers[i]);
9261 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +00009262 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00009263 (*MIB).setMemRefs(bInstr->memoperands_begin(),
9264 bInstr->memoperands_end());
Mon P Wangf5952662008-07-17 04:54:06 +00009265
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009266 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +00009267 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +00009268
Mon P Wang63307c32008-05-05 19:05:59 +00009269 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009270 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00009271
Dan Gohman14152b42010-07-06 20:24:04 +00009272 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00009273 return nextMBB;
9274}
9275
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00009276// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +00009277MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009278X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
9279 MachineBasicBlock *MBB,
9280 unsigned regOpcL,
9281 unsigned regOpcH,
9282 unsigned immOpcL,
9283 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009284 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009285 // For the atomic bitwise operator, we generate
9286 // thisMBB (instructions are in pairs, except cmpxchg8b)
9287 // ld t1,t2 = [bitinstr.addr]
9288 // newMBB:
9289 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
9290 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +00009291 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009292 // mov ECX, EBX <- t5, t6
9293 // mov EAX, EDX <- t1, t2
9294 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
9295 // mov t3, t4 <- EAX, EDX
9296 // bz newMBB
9297 // result in out1, out2
9298 // fallthrough -->nextMBB
9299
9300 const TargetRegisterClass *RC = X86::GR32RegisterClass;
9301 const unsigned LoadOpc = X86::MOV32rm;
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009302 const unsigned NotOpc = X86::NOT32r;
9303 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9304 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
9305 MachineFunction::iterator MBBIter = MBB;
9306 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009307
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009308 /// First build the CFG
9309 MachineFunction *F = MBB->getParent();
9310 MachineBasicBlock *thisMBB = MBB;
9311 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9312 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9313 F->insert(MBBIter, newMBB);
9314 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009315
Dan Gohman14152b42010-07-06 20:24:04 +00009316 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9317 nextMBB->splice(nextMBB->begin(), thisMBB,
9318 llvm::next(MachineBasicBlock::iterator(bInstr)),
9319 thisMBB->end());
9320 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009321
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009322 // Update thisMBB to fall through to newMBB
9323 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009324
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009325 // newMBB jumps to itself and fall through to nextMBB
9326 newMBB->addSuccessor(nextMBB);
9327 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009328
Dale Johannesene4d209d2009-02-03 20:21:25 +00009329 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009330 // Insert instructions into newMBB based on incoming instruction
9331 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009332 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009333 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009334 MachineOperand& dest1Oper = bInstr->getOperand(0);
9335 MachineOperand& dest2Oper = bInstr->getOperand(1);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009336 MachineOperand* argOpers[2 + X86::AddrNumOperands];
9337 for (int i=0; i < 2 + X86::AddrNumOperands; ++i) {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009338 argOpers[i] = &bInstr->getOperand(i+2);
9339
Dan Gohman71ea4e52010-05-14 21:01:44 +00009340 // We use some of the operands multiple times, so conservatively just
9341 // clear any kill flags that might be present.
9342 if (argOpers[i]->isReg() && argOpers[i]->isUse())
9343 argOpers[i]->setIsKill(false);
9344 }
9345
Evan Chengad5b52f2010-01-08 19:14:57 +00009346 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009347 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +00009348
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009349 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009350 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009351 for (int i=0; i <= lastAddrIndx; ++i)
9352 (*MIB).addOperand(*argOpers[i]);
9353 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009354 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +00009355 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +00009356 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009357 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +00009358 MachineOperand newOp3 = *(argOpers[3]);
9359 if (newOp3.isImm())
9360 newOp3.setImm(newOp3.getImm()+4);
9361 else
9362 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009363 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +00009364 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009365
9366 // t3/4 are defined later, at the bottom of the loop
9367 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
9368 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009369 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009370 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009371 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009372 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
9373
Evan Cheng306b4ca2010-01-08 23:41:50 +00009374 // The subsequent operations should be using the destination registers of
9375 //the PHI instructions.
Scott Michelfdc40a02009-02-17 22:15:04 +00009376 if (invSrc) {
Evan Cheng306b4ca2010-01-08 23:41:50 +00009377 t1 = F->getRegInfo().createVirtualRegister(RC);
9378 t2 = F->getRegInfo().createVirtualRegister(RC);
9379 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t1).addReg(dest1Oper.getReg());
9380 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t2).addReg(dest2Oper.getReg());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009381 } else {
Evan Cheng306b4ca2010-01-08 23:41:50 +00009382 t1 = dest1Oper.getReg();
9383 t2 = dest2Oper.getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009384 }
9385
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009386 int valArgIndx = lastAddrIndx + 1;
9387 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +00009388 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009389 "invalid operand");
9390 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
9391 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009392 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009393 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009394 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009395 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +00009396 if (regOpcL != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00009397 MIB.addReg(t1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009398 (*MIB).addOperand(*argOpers[valArgIndx]);
9399 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00009400 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009401 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00009402 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009403 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009404 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009405 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009406 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +00009407 if (regOpcH != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00009408 MIB.addReg(t2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009409 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009410
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009411 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009412 MIB.addReg(t1);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009413 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009414 MIB.addReg(t2);
9415
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009416 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009417 MIB.addReg(t5);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009418 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009419 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +00009420
Dale Johannesene4d209d2009-02-03 20:21:25 +00009421 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009422 for (int i=0; i <= lastAddrIndx; ++i)
9423 (*MIB).addOperand(*argOpers[i]);
9424
9425 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00009426 (*MIB).setMemRefs(bInstr->memoperands_begin(),
9427 bInstr->memoperands_end());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009428
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009429 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009430 MIB.addReg(X86::EAX);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009431 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009432 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +00009433
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009434 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009435 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009436
Dan Gohman14152b42010-07-06 20:24:04 +00009437 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009438 return nextMBB;
9439}
9440
9441// private utility function
9442MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +00009443X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
9444 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009445 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00009446 // For the atomic min/max operator, we generate
9447 // thisMBB:
9448 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00009449 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +00009450 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +00009451 // cmp t1, t2
9452 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +00009453 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00009454 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
9455 // bz newMBB
9456 // fallthrough -->nextMBB
9457 //
9458 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9459 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009460 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00009461 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009462
Mon P Wang63307c32008-05-05 19:05:59 +00009463 /// First build the CFG
9464 MachineFunction *F = MBB->getParent();
9465 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009466 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9467 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9468 F->insert(MBBIter, newMBB);
9469 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009470
Dan Gohman14152b42010-07-06 20:24:04 +00009471 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9472 nextMBB->splice(nextMBB->begin(), thisMBB,
9473 llvm::next(MachineBasicBlock::iterator(mInstr)),
9474 thisMBB->end());
9475 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009476
Mon P Wang63307c32008-05-05 19:05:59 +00009477 // Update thisMBB to fall through to newMBB
9478 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009479
Mon P Wang63307c32008-05-05 19:05:59 +00009480 // newMBB jumps to newMBB and fall through to nextMBB
9481 newMBB->addSuccessor(nextMBB);
9482 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009483
Dale Johannesene4d209d2009-02-03 20:21:25 +00009484 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00009485 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009486 assert(mInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009487 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +00009488 MachineOperand& destOper = mInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009489 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00009490 int numArgs = mInstr->getNumOperands() - 1;
9491 for (int i=0; i < numArgs; ++i)
9492 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009493
Mon P Wang63307c32008-05-05 19:05:59 +00009494 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009495 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009496 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00009497
Mon P Wangab3e7472008-05-05 22:56:23 +00009498 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009499 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00009500 for (int i=0; i <= lastAddrIndx; ++i)
9501 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +00009502
Mon P Wang63307c32008-05-05 19:05:59 +00009503 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +00009504 assert((argOpers[valArgIndx]->isReg() ||
9505 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00009506 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +00009507
9508 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +00009509 if (argOpers[valArgIndx]->isReg())
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009510 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +00009511 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009512 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00009513 (*MIB).addOperand(*argOpers[valArgIndx]);
9514
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009515 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +00009516 MIB.addReg(t1);
9517
Dale Johannesene4d209d2009-02-03 20:21:25 +00009518 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +00009519 MIB.addReg(t1);
9520 MIB.addReg(t2);
9521
9522 // Generate movc
9523 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009524 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +00009525 MIB.addReg(t2);
9526 MIB.addReg(t1);
9527
9528 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +00009529 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +00009530 for (int i=0; i <= lastAddrIndx; ++i)
9531 (*MIB).addOperand(*argOpers[i]);
9532 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +00009533 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00009534 (*MIB).setMemRefs(mInstr->memoperands_begin(),
9535 mInstr->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +00009536
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009537 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +00009538 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +00009539
Mon P Wang63307c32008-05-05 19:05:59 +00009540 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009541 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00009542
Dan Gohman14152b42010-07-06 20:24:04 +00009543 mInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00009544 return nextMBB;
9545}
9546
Eric Christopherf83a5de2009-08-27 18:08:16 +00009547// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00009548// or XMM0_V32I8 in AVX all of this code can be replaced with that
9549// in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +00009550MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +00009551X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +00009552 unsigned numArgs, bool memArg) const {
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00009553 assert((Subtarget->hasSSE42() || Subtarget->hasAVX()) &&
9554 "Target must have SSE4.2 or AVX features enabled");
9555
Eric Christopherb120ab42009-08-18 22:50:32 +00009556 DebugLoc dl = MI->getDebugLoc();
9557 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Eric Christopherb120ab42009-08-18 22:50:32 +00009558 unsigned Opc;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00009559 if (!Subtarget->hasAVX()) {
9560 if (memArg)
9561 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
9562 else
9563 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
9564 } else {
9565 if (memArg)
9566 Opc = numArgs == 3 ? X86::VPCMPISTRM128rm : X86::VPCMPESTRM128rm;
9567 else
9568 Opc = numArgs == 3 ? X86::VPCMPISTRM128rr : X86::VPCMPESTRM128rr;
9569 }
Eric Christopherb120ab42009-08-18 22:50:32 +00009570
Eric Christopher41c902f2010-11-30 08:20:21 +00009571 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Eric Christopherb120ab42009-08-18 22:50:32 +00009572 for (unsigned i = 0; i < numArgs; ++i) {
9573 MachineOperand &Op = MI->getOperand(i+1);
Eric Christopherb120ab42009-08-18 22:50:32 +00009574 if (!(Op.isReg() && Op.isImplicit()))
9575 MIB.addOperand(Op);
9576 }
Eric Christopher41c902f2010-11-30 08:20:21 +00009577 BuildMI(*BB, MI, dl, TII->get(X86::MOVAPSrr), MI->getOperand(0).getReg())
Eric Christopherb120ab42009-08-18 22:50:32 +00009578 .addReg(X86::XMM0);
9579
Dan Gohman14152b42010-07-06 20:24:04 +00009580 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +00009581 return BB;
9582}
9583
9584MachineBasicBlock *
Eric Christopher228232b2010-11-30 07:20:12 +00009585X86TargetLowering::EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +00009586 DebugLoc dl = MI->getDebugLoc();
9587 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009588
Eric Christopher228232b2010-11-30 07:20:12 +00009589 // Address into RAX/EAX, other two args into ECX, EDX.
9590 unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
9591 unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
9592 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg);
9593 for (int i = 0; i < X86::AddrNumOperands; ++i)
Eric Christopher82be2202010-11-30 08:10:28 +00009594 MIB.addOperand(MI->getOperand(i));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009595
Eric Christopher228232b2010-11-30 07:20:12 +00009596 unsigned ValOps = X86::AddrNumOperands;
9597 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
9598 .addReg(MI->getOperand(ValOps).getReg());
9599 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX)
9600 .addReg(MI->getOperand(ValOps+1).getReg());
9601
9602 // The instruction doesn't actually take any operands though.
9603 BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009604
Eric Christopher228232b2010-11-30 07:20:12 +00009605 MI->eraseFromParent(); // The pseudo is gone now.
9606 return BB;
9607}
9608
9609MachineBasicBlock *
9610X86TargetLowering::EmitMwait(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +00009611 DebugLoc dl = MI->getDebugLoc();
9612 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009613
Eric Christopher228232b2010-11-30 07:20:12 +00009614 // First arg in ECX, the second in EAX.
9615 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
9616 .addReg(MI->getOperand(0).getReg());
9617 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EAX)
9618 .addReg(MI->getOperand(1).getReg());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009619
Eric Christopher228232b2010-11-30 07:20:12 +00009620 // The instruction doesn't actually take any operands though.
9621 BuildMI(*BB, MI, dl, TII->get(X86::MWAITrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009622
Eric Christopher228232b2010-11-30 07:20:12 +00009623 MI->eraseFromParent(); // The pseudo is gone now.
9624 return BB;
9625}
9626
9627MachineBasicBlock *
Dan Gohman320afb82010-10-12 18:00:49 +00009628X86TargetLowering::EmitVAARG64WithCustomInserter(
9629 MachineInstr *MI,
9630 MachineBasicBlock *MBB) const {
9631 // Emit va_arg instruction on X86-64.
9632
9633 // Operands to this pseudo-instruction:
9634 // 0 ) Output : destination address (reg)
9635 // 1-5) Input : va_list address (addr, i64mem)
9636 // 6 ) ArgSize : Size (in bytes) of vararg type
9637 // 7 ) ArgMode : 0=overflow only, 1=use gp_offset, 2=use fp_offset
9638 // 8 ) Align : Alignment of type
9639 // 9 ) EFLAGS (implicit-def)
9640
9641 assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
9642 assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands");
9643
9644 unsigned DestReg = MI->getOperand(0).getReg();
9645 MachineOperand &Base = MI->getOperand(1);
9646 MachineOperand &Scale = MI->getOperand(2);
9647 MachineOperand &Index = MI->getOperand(3);
9648 MachineOperand &Disp = MI->getOperand(4);
9649 MachineOperand &Segment = MI->getOperand(5);
9650 unsigned ArgSize = MI->getOperand(6).getImm();
9651 unsigned ArgMode = MI->getOperand(7).getImm();
9652 unsigned Align = MI->getOperand(8).getImm();
9653
9654 // Memory Reference
9655 assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand");
9656 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
9657 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
9658
9659 // Machine Information
9660 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9661 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
9662 const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64);
9663 const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32);
9664 DebugLoc DL = MI->getDebugLoc();
9665
9666 // struct va_list {
9667 // i32 gp_offset
9668 // i32 fp_offset
9669 // i64 overflow_area (address)
9670 // i64 reg_save_area (address)
9671 // }
9672 // sizeof(va_list) = 24
9673 // alignment(va_list) = 8
9674
9675 unsigned TotalNumIntRegs = 6;
9676 unsigned TotalNumXMMRegs = 8;
9677 bool UseGPOffset = (ArgMode == 1);
9678 bool UseFPOffset = (ArgMode == 2);
9679 unsigned MaxOffset = TotalNumIntRegs * 8 +
9680 (UseFPOffset ? TotalNumXMMRegs * 16 : 0);
9681
9682 /* Align ArgSize to a multiple of 8 */
9683 unsigned ArgSizeA8 = (ArgSize + 7) & ~7;
9684 bool NeedsAlign = (Align > 8);
9685
9686 MachineBasicBlock *thisMBB = MBB;
9687 MachineBasicBlock *overflowMBB;
9688 MachineBasicBlock *offsetMBB;
9689 MachineBasicBlock *endMBB;
9690
9691 unsigned OffsetDestReg = 0; // Argument address computed by offsetMBB
9692 unsigned OverflowDestReg = 0; // Argument address computed by overflowMBB
9693 unsigned OffsetReg = 0;
9694
9695 if (!UseGPOffset && !UseFPOffset) {
9696 // If we only pull from the overflow region, we don't create a branch.
9697 // We don't need to alter control flow.
9698 OffsetDestReg = 0; // unused
9699 OverflowDestReg = DestReg;
9700
9701 offsetMBB = NULL;
9702 overflowMBB = thisMBB;
9703 endMBB = thisMBB;
9704 } else {
9705 // First emit code to check if gp_offset (or fp_offset) is below the bound.
9706 // If so, pull the argument from reg_save_area. (branch to offsetMBB)
9707 // If not, pull from overflow_area. (branch to overflowMBB)
9708 //
9709 // thisMBB
9710 // | .
9711 // | .
9712 // offsetMBB overflowMBB
9713 // | .
9714 // | .
9715 // endMBB
9716
9717 // Registers for the PHI in endMBB
9718 OffsetDestReg = MRI.createVirtualRegister(AddrRegClass);
9719 OverflowDestReg = MRI.createVirtualRegister(AddrRegClass);
9720
9721 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
9722 MachineFunction *MF = MBB->getParent();
9723 overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB);
9724 offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB);
9725 endMBB = MF->CreateMachineBasicBlock(LLVM_BB);
9726
9727 MachineFunction::iterator MBBIter = MBB;
9728 ++MBBIter;
9729
9730 // Insert the new basic blocks
9731 MF->insert(MBBIter, offsetMBB);
9732 MF->insert(MBBIter, overflowMBB);
9733 MF->insert(MBBIter, endMBB);
9734
9735 // Transfer the remainder of MBB and its successor edges to endMBB.
9736 endMBB->splice(endMBB->begin(), thisMBB,
9737 llvm::next(MachineBasicBlock::iterator(MI)),
9738 thisMBB->end());
9739 endMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
9740
9741 // Make offsetMBB and overflowMBB successors of thisMBB
9742 thisMBB->addSuccessor(offsetMBB);
9743 thisMBB->addSuccessor(overflowMBB);
9744
9745 // endMBB is a successor of both offsetMBB and overflowMBB
9746 offsetMBB->addSuccessor(endMBB);
9747 overflowMBB->addSuccessor(endMBB);
9748
9749 // Load the offset value into a register
9750 OffsetReg = MRI.createVirtualRegister(OffsetRegClass);
9751 BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg)
9752 .addOperand(Base)
9753 .addOperand(Scale)
9754 .addOperand(Index)
9755 .addDisp(Disp, UseFPOffset ? 4 : 0)
9756 .addOperand(Segment)
9757 .setMemRefs(MMOBegin, MMOEnd);
9758
9759 // Check if there is enough room left to pull this argument.
9760 BuildMI(thisMBB, DL, TII->get(X86::CMP32ri))
9761 .addReg(OffsetReg)
9762 .addImm(MaxOffset + 8 - ArgSizeA8);
9763
9764 // Branch to "overflowMBB" if offset >= max
9765 // Fall through to "offsetMBB" otherwise
9766 BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE)))
9767 .addMBB(overflowMBB);
9768 }
9769
9770 // In offsetMBB, emit code to use the reg_save_area.
9771 if (offsetMBB) {
9772 assert(OffsetReg != 0);
9773
9774 // Read the reg_save_area address.
9775 unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass);
9776 BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg)
9777 .addOperand(Base)
9778 .addOperand(Scale)
9779 .addOperand(Index)
9780 .addDisp(Disp, 16)
9781 .addOperand(Segment)
9782 .setMemRefs(MMOBegin, MMOEnd);
9783
9784 // Zero-extend the offset
9785 unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass);
9786 BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64)
9787 .addImm(0)
9788 .addReg(OffsetReg)
9789 .addImm(X86::sub_32bit);
9790
9791 // Add the offset to the reg_save_area to get the final address.
9792 BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg)
9793 .addReg(OffsetReg64)
9794 .addReg(RegSaveReg);
9795
9796 // Compute the offset for the next argument
9797 unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass);
9798 BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg)
9799 .addReg(OffsetReg)
9800 .addImm(UseFPOffset ? 16 : 8);
9801
9802 // Store it back into the va_list.
9803 BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr))
9804 .addOperand(Base)
9805 .addOperand(Scale)
9806 .addOperand(Index)
9807 .addDisp(Disp, UseFPOffset ? 4 : 0)
9808 .addOperand(Segment)
9809 .addReg(NextOffsetReg)
9810 .setMemRefs(MMOBegin, MMOEnd);
9811
9812 // Jump to endMBB
9813 BuildMI(offsetMBB, DL, TII->get(X86::JMP_4))
9814 .addMBB(endMBB);
9815 }
9816
9817 //
9818 // Emit code to use overflow area
9819 //
9820
9821 // Load the overflow_area address into a register.
9822 unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass);
9823 BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg)
9824 .addOperand(Base)
9825 .addOperand(Scale)
9826 .addOperand(Index)
9827 .addDisp(Disp, 8)
9828 .addOperand(Segment)
9829 .setMemRefs(MMOBegin, MMOEnd);
9830
9831 // If we need to align it, do so. Otherwise, just copy the address
9832 // to OverflowDestReg.
9833 if (NeedsAlign) {
9834 // Align the overflow address
9835 assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2");
9836 unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass);
9837
9838 // aligned_addr = (addr + (align-1)) & ~(align-1)
9839 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg)
9840 .addReg(OverflowAddrReg)
9841 .addImm(Align-1);
9842
9843 BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg)
9844 .addReg(TmpReg)
9845 .addImm(~(uint64_t)(Align-1));
9846 } else {
9847 BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg)
9848 .addReg(OverflowAddrReg);
9849 }
9850
9851 // Compute the next overflow address after this argument.
9852 // (the overflow address should be kept 8-byte aligned)
9853 unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass);
9854 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg)
9855 .addReg(OverflowDestReg)
9856 .addImm(ArgSizeA8);
9857
9858 // Store the new overflow address.
9859 BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr))
9860 .addOperand(Base)
9861 .addOperand(Scale)
9862 .addOperand(Index)
9863 .addDisp(Disp, 8)
9864 .addOperand(Segment)
9865 .addReg(NextAddrReg)
9866 .setMemRefs(MMOBegin, MMOEnd);
9867
9868 // If we branched, emit the PHI to the front of endMBB.
9869 if (offsetMBB) {
9870 BuildMI(*endMBB, endMBB->begin(), DL,
9871 TII->get(X86::PHI), DestReg)
9872 .addReg(OffsetDestReg).addMBB(offsetMBB)
9873 .addReg(OverflowDestReg).addMBB(overflowMBB);
9874 }
9875
9876 // Erase the pseudo instruction
9877 MI->eraseFromParent();
9878
9879 return endMBB;
9880}
9881
9882MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +00009883X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
9884 MachineInstr *MI,
9885 MachineBasicBlock *MBB) const {
9886 // Emit code to save XMM registers to the stack. The ABI says that the
9887 // number of registers to save is given in %al, so it's theoretically
9888 // possible to do an indirect jump trick to avoid saving all of them,
9889 // however this code takes a simpler approach and just executes all
9890 // of the stores if %al is non-zero. It's less code, and it's probably
9891 // easier on the hardware branch predictor, and stores aren't all that
9892 // expensive anyway.
9893
9894 // Create the new basic blocks. One block contains all the XMM stores,
9895 // and one block is the final destination regardless of whether any
9896 // stores were performed.
9897 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
9898 MachineFunction *F = MBB->getParent();
9899 MachineFunction::iterator MBBIter = MBB;
9900 ++MBBIter;
9901 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
9902 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
9903 F->insert(MBBIter, XMMSaveMBB);
9904 F->insert(MBBIter, EndMBB);
9905
Dan Gohman14152b42010-07-06 20:24:04 +00009906 // Transfer the remainder of MBB and its successor edges to EndMBB.
9907 EndMBB->splice(EndMBB->begin(), MBB,
9908 llvm::next(MachineBasicBlock::iterator(MI)),
9909 MBB->end());
9910 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
9911
Dan Gohmand6708ea2009-08-15 01:38:56 +00009912 // The original block will now fall through to the XMM save block.
9913 MBB->addSuccessor(XMMSaveMBB);
9914 // The XMMSaveMBB will fall through to the end block.
9915 XMMSaveMBB->addSuccessor(EndMBB);
9916
9917 // Now add the instructions.
9918 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9919 DebugLoc DL = MI->getDebugLoc();
9920
9921 unsigned CountReg = MI->getOperand(0).getReg();
9922 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
9923 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
9924
9925 if (!Subtarget->isTargetWin64()) {
9926 // If %al is 0, branch around the XMM save block.
9927 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009928 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +00009929 MBB->addSuccessor(EndMBB);
9930 }
9931
9932 // In the XMM save block, save all the XMM argument registers.
9933 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
9934 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +00009935 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +00009936 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +00009937 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +00009938 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +00009939 /*Size=*/16, /*Align=*/16);
Dan Gohmand6708ea2009-08-15 01:38:56 +00009940 BuildMI(XMMSaveMBB, DL, TII->get(X86::MOVAPSmr))
9941 .addFrameIndex(RegSaveFrameIndex)
9942 .addImm(/*Scale=*/1)
9943 .addReg(/*IndexReg=*/0)
9944 .addImm(/*Disp=*/Offset)
9945 .addReg(/*Segment=*/0)
9946 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +00009947 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +00009948 }
9949
Dan Gohman14152b42010-07-06 20:24:04 +00009950 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +00009951
9952 return EndMBB;
9953}
Mon P Wang63307c32008-05-05 19:05:59 +00009954
Evan Cheng60c07e12006-07-05 22:17:51 +00009955MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +00009956X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00009957 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +00009958 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9959 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +00009960
Chris Lattner52600972009-09-02 05:57:00 +00009961 // To "insert" a SELECT_CC instruction, we actually have to insert the
9962 // diamond control-flow pattern. The incoming instruction knows the
9963 // destination vreg to set, the condition code register to branch on, the
9964 // true/false values to select between, and a branch opcode to use.
9965 const BasicBlock *LLVM_BB = BB->getBasicBlock();
9966 MachineFunction::iterator It = BB;
9967 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +00009968
Chris Lattner52600972009-09-02 05:57:00 +00009969 // thisMBB:
9970 // ...
9971 // TrueVal = ...
9972 // cmpTY ccX, r1, r2
9973 // bCC copy1MBB
9974 // fallthrough --> copy0MBB
9975 MachineBasicBlock *thisMBB = BB;
9976 MachineFunction *F = BB->getParent();
9977 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
9978 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +00009979 F->insert(It, copy0MBB);
9980 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +00009981
Bill Wendling730c07e2010-06-25 20:48:10 +00009982 // If the EFLAGS register isn't dead in the terminator, then claim that it's
9983 // live into the sink and copy blocks.
9984 const MachineFunction *MF = BB->getParent();
9985 const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo();
9986 BitVector ReservedRegs = TRI->getReservedRegs(*MF);
Bill Wendling730c07e2010-06-25 20:48:10 +00009987
Dan Gohman14152b42010-07-06 20:24:04 +00009988 for (unsigned I = 0, E = MI->getNumOperands(); I != E; ++I) {
9989 const MachineOperand &MO = MI->getOperand(I);
9990 if (!MO.isReg() || !MO.isUse() || MO.isKill()) continue;
Bill Wendling730c07e2010-06-25 20:48:10 +00009991 unsigned Reg = MO.getReg();
9992 if (Reg != X86::EFLAGS) continue;
9993 copy0MBB->addLiveIn(Reg);
9994 sinkMBB->addLiveIn(Reg);
9995 }
9996
Dan Gohman14152b42010-07-06 20:24:04 +00009997 // Transfer the remainder of BB and its successor edges to sinkMBB.
9998 sinkMBB->splice(sinkMBB->begin(), BB,
9999 llvm::next(MachineBasicBlock::iterator(MI)),
10000 BB->end());
10001 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
10002
10003 // Add the true and fallthrough blocks as its successors.
10004 BB->addSuccessor(copy0MBB);
10005 BB->addSuccessor(sinkMBB);
10006
10007 // Create the conditional branch instruction.
10008 unsigned Opc =
10009 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
10010 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
10011
Chris Lattner52600972009-09-02 05:57:00 +000010012 // copy0MBB:
10013 // %FalseValue = ...
10014 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +000010015 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +000010016
Chris Lattner52600972009-09-02 05:57:00 +000010017 // sinkMBB:
10018 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
10019 // ...
Dan Gohman14152b42010-07-06 20:24:04 +000010020 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
10021 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +000010022 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
10023 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
10024
Dan Gohman14152b42010-07-06 20:24:04 +000010025 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +000010026 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +000010027}
10028
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010029MachineBasicBlock *
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010030X86TargetLowering::EmitLoweredWinAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010031 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010032 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10033 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010034
10035 // The lowering is pretty easy: we're just emitting the call to _alloca. The
10036 // non-trivial part is impdef of ESP.
10037 // FIXME: The code should be tweaked as soon as we'll try to do codegen for
10038 // mingw-w64.
10039
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010040 const char *StackProbeSymbol =
10041 Subtarget->isTargetWindows() ? "_chkstk" : "_alloca";
10042
Dan Gohman14152b42010-07-06 20:24:04 +000010043 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010044 .addExternalSymbol(StackProbeSymbol)
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010045 .addReg(X86::EAX, RegState::Implicit)
10046 .addReg(X86::ESP, RegState::Implicit)
10047 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
Anton Korobeynikov9f7f83b2010-08-25 07:50:11 +000010048 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
10049 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010050
Dan Gohman14152b42010-07-06 20:24:04 +000010051 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010052 return BB;
10053}
Chris Lattner52600972009-09-02 05:57:00 +000010054
10055MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +000010056X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
10057 MachineBasicBlock *BB) const {
10058 // This is pretty easy. We're taking the value that we received from
10059 // our load from the relocation, sticking it in either RDI (x86-64)
10060 // or EAX and doing an indirect call. The return value will then
10061 // be in the normal return register.
Michael J. Spencerec38de22010-10-10 22:04:20 +000010062 const X86InstrInfo *TII
Eric Christopher54415362010-06-08 22:04:25 +000010063 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +000010064 DebugLoc DL = MI->getDebugLoc();
10065 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +000010066
10067 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +000010068 assert(MI->getOperand(3).isGlobal() && "This should be a global");
Michael J. Spencerec38de22010-10-10 22:04:20 +000010069
Eric Christopher30ef0e52010-06-03 04:07:48 +000010070 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +000010071 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
10072 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +000010073 .addReg(X86::RIP)
10074 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000010075 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000010076 MI->getOperand(3).getTargetFlags())
10077 .addReg(0);
Eric Christopher722d3152010-09-27 06:01:51 +000010078 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +000010079 addDirectMem(MIB, X86::RDI);
Eric Christopher61025492010-06-15 23:08:42 +000010080 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +000010081 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
10082 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +000010083 .addReg(0)
10084 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000010085 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher61025492010-06-15 23:08:42 +000010086 MI->getOperand(3).getTargetFlags())
10087 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000010088 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000010089 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010090 } else {
Dan Gohman14152b42010-07-06 20:24:04 +000010091 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
10092 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +000010093 .addReg(TII->getGlobalBaseReg(F))
10094 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000010095 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000010096 MI->getOperand(3).getTargetFlags())
10097 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000010098 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000010099 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010100 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000010101
Dan Gohman14152b42010-07-06 20:24:04 +000010102 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +000010103 return BB;
10104}
10105
10106MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +000010107X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010108 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +000010109 switch (MI->getOpcode()) {
10110 default: assert(false && "Unexpected instr type to insert");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000010111 case X86::TAILJMPd64:
10112 case X86::TAILJMPr64:
10113 case X86::TAILJMPm64:
10114 assert(!"TAILJMP64 would not be touched here.");
10115 case X86::TCRETURNdi64:
10116 case X86::TCRETURNri64:
10117 case X86::TCRETURNmi64:
10118 // Defs of TCRETURNxx64 has Win64's callee-saved registers, as subset.
10119 // On AMD64, additional defs should be added before register allocation.
10120 if (!Subtarget->isTargetWin64()) {
10121 MI->addRegisterDefined(X86::RSI);
10122 MI->addRegisterDefined(X86::RDI);
10123 MI->addRegisterDefined(X86::XMM6);
10124 MI->addRegisterDefined(X86::XMM7);
10125 MI->addRegisterDefined(X86::XMM8);
10126 MI->addRegisterDefined(X86::XMM9);
10127 MI->addRegisterDefined(X86::XMM10);
10128 MI->addRegisterDefined(X86::XMM11);
10129 MI->addRegisterDefined(X86::XMM12);
10130 MI->addRegisterDefined(X86::XMM13);
10131 MI->addRegisterDefined(X86::XMM14);
10132 MI->addRegisterDefined(X86::XMM15);
10133 }
10134 return BB;
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010135 case X86::WIN_ALLOCA:
10136 return EmitLoweredWinAlloca(MI, BB);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010137 case X86::TLSCall_32:
10138 case X86::TLSCall_64:
10139 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +000010140 case X86::CMOV_GR8:
Evan Cheng60c07e12006-07-05 22:17:51 +000010141 case X86::CMOV_FR32:
10142 case X86::CMOV_FR64:
10143 case X86::CMOV_V4F32:
10144 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +000010145 case X86::CMOV_V2I64:
Chris Lattner314a1132010-03-14 18:31:44 +000010146 case X86::CMOV_GR16:
10147 case X86::CMOV_GR32:
10148 case X86::CMOV_RFP32:
10149 case X86::CMOV_RFP64:
10150 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010151 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000010152
Dale Johannesen849f2142007-07-03 00:53:03 +000010153 case X86::FP32_TO_INT16_IN_MEM:
10154 case X86::FP32_TO_INT32_IN_MEM:
10155 case X86::FP32_TO_INT64_IN_MEM:
10156 case X86::FP64_TO_INT16_IN_MEM:
10157 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +000010158 case X86::FP64_TO_INT64_IN_MEM:
10159 case X86::FP80_TO_INT16_IN_MEM:
10160 case X86::FP80_TO_INT32_IN_MEM:
10161 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +000010162 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10163 DebugLoc DL = MI->getDebugLoc();
10164
Evan Cheng60c07e12006-07-05 22:17:51 +000010165 // Change the floating point control register to use "round towards zero"
10166 // mode when truncating to an integer value.
10167 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +000010168 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +000010169 addFrameReference(BuildMI(*BB, MI, DL,
10170 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010171
10172 // Load the old value of the high byte of the control word...
10173 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +000010174 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohman14152b42010-07-06 20:24:04 +000010175 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +000010176 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010177
10178 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +000010179 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000010180 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +000010181
10182 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +000010183 addFrameReference(BuildMI(*BB, MI, DL,
10184 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010185
10186 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +000010187 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000010188 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +000010189
10190 // Get the X86 opcode to use.
10191 unsigned Opc;
10192 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000010193 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +000010194 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
10195 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
10196 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
10197 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
10198 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
10199 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +000010200 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
10201 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
10202 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +000010203 }
10204
10205 X86AddressMode AM;
10206 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +000010207 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000010208 AM.BaseType = X86AddressMode::RegBase;
10209 AM.Base.Reg = Op.getReg();
10210 } else {
10211 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +000010212 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +000010213 }
10214 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +000010215 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000010216 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010217 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +000010218 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000010219 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010220 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +000010221 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000010222 AM.GV = Op.getGlobal();
10223 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +000010224 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010225 }
Dan Gohman14152b42010-07-06 20:24:04 +000010226 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010227 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +000010228
10229 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +000010230 addFrameReference(BuildMI(*BB, MI, DL,
10231 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010232
Dan Gohman14152b42010-07-06 20:24:04 +000010233 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +000010234 return BB;
10235 }
Eric Christopherb120ab42009-08-18 22:50:32 +000010236 // String/text processing lowering.
10237 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010238 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000010239 return EmitPCMP(MI, BB, 3, false /* in-mem */);
10240 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010241 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000010242 return EmitPCMP(MI, BB, 3, true /* in-mem */);
10243 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010244 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000010245 return EmitPCMP(MI, BB, 5, false /* in mem */);
10246 case X86::PCMPESTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010247 case X86::VPCMPESTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000010248 return EmitPCMP(MI, BB, 5, true /* in mem */);
10249
Eric Christopher228232b2010-11-30 07:20:12 +000010250 // Thread synchronization.
10251 case X86::MONITOR:
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010252 return EmitMonitor(MI, BB);
Eric Christopher228232b2010-11-30 07:20:12 +000010253 case X86::MWAIT:
10254 return EmitMwait(MI, BB);
10255
Eric Christopherb120ab42009-08-18 22:50:32 +000010256 // Atomic Lowering.
Mon P Wang63307c32008-05-05 19:05:59 +000010257 case X86::ATOMAND32:
10258 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010259 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010260 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010261 X86::NOT32r, X86::EAX,
10262 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000010263 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +000010264 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
10265 X86::OR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010266 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010267 X86::NOT32r, X86::EAX,
10268 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000010269 case X86::ATOMXOR32:
10270 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010271 X86::XOR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010272 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010273 X86::NOT32r, X86::EAX,
10274 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010275 case X86::ATOMNAND32:
10276 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010277 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010278 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010279 X86::NOT32r, X86::EAX,
10280 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +000010281 case X86::ATOMMIN32:
10282 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
10283 case X86::ATOMMAX32:
10284 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
10285 case X86::ATOMUMIN32:
10286 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
10287 case X86::ATOMUMAX32:
10288 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +000010289
10290 case X86::ATOMAND16:
10291 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
10292 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010293 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010294 X86::NOT16r, X86::AX,
10295 X86::GR16RegisterClass);
10296 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +000010297 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010298 X86::OR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010299 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010300 X86::NOT16r, X86::AX,
10301 X86::GR16RegisterClass);
10302 case X86::ATOMXOR16:
10303 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
10304 X86::XOR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010305 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010306 X86::NOT16r, X86::AX,
10307 X86::GR16RegisterClass);
10308 case X86::ATOMNAND16:
10309 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
10310 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010311 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010312 X86::NOT16r, X86::AX,
10313 X86::GR16RegisterClass, true);
10314 case X86::ATOMMIN16:
10315 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
10316 case X86::ATOMMAX16:
10317 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
10318 case X86::ATOMUMIN16:
10319 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
10320 case X86::ATOMUMAX16:
10321 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
10322
10323 case X86::ATOMAND8:
10324 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
10325 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010326 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010327 X86::NOT8r, X86::AL,
10328 X86::GR8RegisterClass);
10329 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +000010330 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010331 X86::OR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010332 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010333 X86::NOT8r, X86::AL,
10334 X86::GR8RegisterClass);
10335 case X86::ATOMXOR8:
10336 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
10337 X86::XOR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010338 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010339 X86::NOT8r, X86::AL,
10340 X86::GR8RegisterClass);
10341 case X86::ATOMNAND8:
10342 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
10343 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010344 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010345 X86::NOT8r, X86::AL,
10346 X86::GR8RegisterClass, true);
10347 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010348 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +000010349 case X86::ATOMAND64:
10350 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010351 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010352 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010353 X86::NOT64r, X86::RAX,
10354 X86::GR64RegisterClass);
10355 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +000010356 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
10357 X86::OR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010358 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010359 X86::NOT64r, X86::RAX,
10360 X86::GR64RegisterClass);
10361 case X86::ATOMXOR64:
10362 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010363 X86::XOR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010364 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010365 X86::NOT64r, X86::RAX,
10366 X86::GR64RegisterClass);
10367 case X86::ATOMNAND64:
10368 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
10369 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010370 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010371 X86::NOT64r, X86::RAX,
10372 X86::GR64RegisterClass, true);
10373 case X86::ATOMMIN64:
10374 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
10375 case X86::ATOMMAX64:
10376 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
10377 case X86::ATOMUMIN64:
10378 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
10379 case X86::ATOMUMAX64:
10380 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010381
10382 // This group does 64-bit operations on a 32-bit host.
10383 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010384 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010385 X86::AND32rr, X86::AND32rr,
10386 X86::AND32ri, X86::AND32ri,
10387 false);
10388 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010389 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010390 X86::OR32rr, X86::OR32rr,
10391 X86::OR32ri, X86::OR32ri,
10392 false);
10393 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010394 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010395 X86::XOR32rr, X86::XOR32rr,
10396 X86::XOR32ri, X86::XOR32ri,
10397 false);
10398 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010399 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010400 X86::AND32rr, X86::AND32rr,
10401 X86::AND32ri, X86::AND32ri,
10402 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010403 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010404 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010405 X86::ADD32rr, X86::ADC32rr,
10406 X86::ADD32ri, X86::ADC32ri,
10407 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010408 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010409 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010410 X86::SUB32rr, X86::SBB32rr,
10411 X86::SUB32ri, X86::SBB32ri,
10412 false);
Dale Johannesen880ae362008-10-03 22:25:52 +000010413 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010414 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +000010415 X86::MOV32rr, X86::MOV32rr,
10416 X86::MOV32ri, X86::MOV32ri,
10417 false);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010418 case X86::VASTART_SAVE_XMM_REGS:
10419 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Dan Gohman320afb82010-10-12 18:00:49 +000010420
10421 case X86::VAARG_64:
10422 return EmitVAARG64WithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000010423 }
10424}
10425
10426//===----------------------------------------------------------------------===//
10427// X86 Optimization Hooks
10428//===----------------------------------------------------------------------===//
10429
Dan Gohman475871a2008-07-27 21:46:04 +000010430void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +000010431 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000010432 APInt &KnownZero,
10433 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +000010434 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +000010435 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010436 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +000010437 assert((Opc >= ISD::BUILTIN_OP_END ||
10438 Opc == ISD::INTRINSIC_WO_CHAIN ||
10439 Opc == ISD::INTRINSIC_W_CHAIN ||
10440 Opc == ISD::INTRINSIC_VOID) &&
10441 "Should use MaskedValueIsZero if you don't know whether Op"
10442 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010443
Dan Gohmanf4f92f52008-02-13 23:07:24 +000010444 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010445 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +000010446 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +000010447 case X86ISD::ADD:
10448 case X86ISD::SUB:
Chris Lattner5b856542010-12-20 00:59:46 +000010449 case X86ISD::ADC:
10450 case X86ISD::SBB:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000010451 case X86ISD::SMUL:
10452 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +000010453 case X86ISD::INC:
10454 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +000010455 case X86ISD::OR:
10456 case X86ISD::XOR:
10457 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000010458 // These nodes' second result is a boolean.
10459 if (Op.getResNo() == 0)
10460 break;
10461 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010462 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000010463 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
10464 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +000010465 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010466 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010467}
Chris Lattner259e97c2006-01-31 19:43:35 +000010468
Owen Andersonbc146b02010-09-21 20:42:50 +000010469unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
10470 unsigned Depth) const {
10471 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
10472 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
10473 return Op.getValueType().getScalarType().getSizeInBits();
Michael J. Spencerec38de22010-10-10 22:04:20 +000010474
Owen Andersonbc146b02010-09-21 20:42:50 +000010475 // Fallback case.
10476 return 1;
10477}
10478
Evan Cheng206ee9d2006-07-07 08:33:52 +000010479/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +000010480/// node is a GlobalAddress + offset.
10481bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +000010482 const GlobalValue* &GA,
10483 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +000010484 if (N->getOpcode() == X86ISD::Wrapper) {
10485 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +000010486 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +000010487 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +000010488 return true;
10489 }
Evan Cheng206ee9d2006-07-07 08:33:52 +000010490 }
Evan Chengad4196b2008-05-12 19:56:52 +000010491 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +000010492}
10493
Evan Cheng206ee9d2006-07-07 08:33:52 +000010494/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
10495/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
10496/// if the load addresses are consecutive, non-overlapping, and in the right
Nate Begemanfdea31a2010-03-24 20:49:50 +000010497/// order.
Dan Gohman475871a2008-07-27 21:46:04 +000010498static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Mon P Wanga0fd0d52010-12-19 23:55:53 +000010499 TargetLowering::DAGCombinerInfo &DCI) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000010500 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000010501 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +000010502
Eli Friedman7a5e5552009-06-07 06:52:44 +000010503 if (VT.getSizeInBits() != 128)
10504 return SDValue();
10505
Mon P Wanga0fd0d52010-12-19 23:55:53 +000010506 // Don't create instructions with illegal types after legalize types has run.
10507 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10508 if (!DCI.isBeforeLegalize() && !TLI.isTypeLegal(VT.getVectorElementType()))
10509 return SDValue();
10510
Nate Begemanfdea31a2010-03-24 20:49:50 +000010511 SmallVector<SDValue, 16> Elts;
10512 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000010513 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +000010514
Nate Begemanfdea31a2010-03-24 20:49:50 +000010515 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000010516}
Evan Chengd880b972008-05-09 21:53:03 +000010517
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +000010518/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
10519/// generation and convert it from being a bunch of shuffles and extracts
10520/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010521static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
10522 const TargetLowering &TLI) {
10523 SDValue InputVector = N->getOperand(0);
10524
10525 // Only operate on vectors of 4 elements, where the alternative shuffling
10526 // gets to be more expensive.
10527 if (InputVector.getValueType() != MVT::v4i32)
10528 return SDValue();
10529
10530 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
10531 // single use which is a sign-extend or zero-extend, and all elements are
10532 // used.
10533 SmallVector<SDNode *, 4> Uses;
10534 unsigned ExtractedElements = 0;
10535 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
10536 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
10537 if (UI.getUse().getResNo() != InputVector.getResNo())
10538 return SDValue();
10539
10540 SDNode *Extract = *UI;
10541 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
10542 return SDValue();
10543
10544 if (Extract->getValueType(0) != MVT::i32)
10545 return SDValue();
10546 if (!Extract->hasOneUse())
10547 return SDValue();
10548 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
10549 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
10550 return SDValue();
10551 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
10552 return SDValue();
10553
10554 // Record which element was extracted.
10555 ExtractedElements |=
10556 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
10557
10558 Uses.push_back(Extract);
10559 }
10560
10561 // If not all the elements were used, this may not be worthwhile.
10562 if (ExtractedElements != 15)
10563 return SDValue();
10564
10565 // Ok, we've now decided to do the transformation.
10566 DebugLoc dl = InputVector.getDebugLoc();
10567
10568 // Store the value to a temporary stack slot.
10569 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000010570 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
10571 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010572
10573 // Replace each use (extract) with a load of the appropriate element.
10574 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
10575 UE = Uses.end(); UI != UE; ++UI) {
10576 SDNode *Extract = *UI;
10577
10578 // Compute the element's address.
10579 SDValue Idx = Extract->getOperand(1);
10580 unsigned EltSize =
10581 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
10582 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
10583 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
10584
Eric Christopher90eb4022010-07-22 00:26:08 +000010585 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, Idx.getValueType(),
Chris Lattner51abfe42010-09-21 06:02:19 +000010586 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010587
10588 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000010589 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000010590 ScalarAddr, MachinePointerInfo(),
10591 false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010592
10593 // Replace the exact with the load.
10594 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
10595 }
10596
10597 // The replacement was made in place; don't return anything.
10598 return SDValue();
10599}
10600
Chris Lattner83e6c992006-10-04 06:57:07 +000010601/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000010602static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner47b4ce82009-03-11 05:48:52 +000010603 const X86Subtarget *Subtarget) {
10604 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +000010605 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000010606 // Get the LHS/RHS of the select.
10607 SDValue LHS = N->getOperand(1);
10608 SDValue RHS = N->getOperand(2);
Eric Christopherfd179292009-08-27 18:07:15 +000010609
Dan Gohman670e5392009-09-21 18:03:22 +000010610 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000010611 // instructions match the semantics of the common C idiom x<y?x:y but not
10612 // x<=y?x:y, because of how they handle negative zero (which can be
10613 // ignored in unsafe-math mode).
Chris Lattner83e6c992006-10-04 06:57:07 +000010614 if (Subtarget->hasSSE2() &&
Owen Anderson825b72b2009-08-11 20:47:22 +000010615 (LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64) &&
Chris Lattner47b4ce82009-03-11 05:48:52 +000010616 Cond.getOpcode() == ISD::SETCC) {
10617 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010618
Chris Lattner47b4ce82009-03-11 05:48:52 +000010619 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000010620 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000010621 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
10622 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000010623 switch (CC) {
10624 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000010625 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000010626 // Converting this to a min would handle NaNs incorrectly, and swapping
10627 // the operands would cause it to handle comparisons between positive
10628 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000010629 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000010630 if (!UnsafeFPMath &&
10631 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
10632 break;
10633 std::swap(LHS, RHS);
10634 }
Dan Gohman670e5392009-09-21 18:03:22 +000010635 Opcode = X86ISD::FMIN;
10636 break;
10637 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000010638 // Converting this to a min would handle comparisons between positive
10639 // and negative zero incorrectly.
10640 if (!UnsafeFPMath &&
10641 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
10642 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010643 Opcode = X86ISD::FMIN;
10644 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000010645 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000010646 // Converting this to a min would handle both negative zeros and NaNs
10647 // incorrectly, but we can swap the operands to fix both.
10648 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000010649 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010650 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000010651 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010652 Opcode = X86ISD::FMIN;
10653 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010654
Dan Gohman670e5392009-09-21 18:03:22 +000010655 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000010656 // Converting this to a max would handle comparisons between positive
10657 // and negative zero incorrectly.
10658 if (!UnsafeFPMath &&
10659 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(LHS))
10660 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010661 Opcode = X86ISD::FMAX;
10662 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000010663 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000010664 // Converting this to a max would handle NaNs incorrectly, and swapping
10665 // the operands would cause it to handle comparisons between positive
10666 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000010667 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000010668 if (!UnsafeFPMath &&
10669 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
10670 break;
10671 std::swap(LHS, RHS);
10672 }
Dan Gohman670e5392009-09-21 18:03:22 +000010673 Opcode = X86ISD::FMAX;
10674 break;
10675 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000010676 // Converting this to a max would handle both negative zeros and NaNs
10677 // incorrectly, but we can swap the operands to fix both.
10678 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000010679 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010680 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010681 case ISD::SETGE:
10682 Opcode = X86ISD::FMAX;
10683 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000010684 }
Dan Gohman670e5392009-09-21 18:03:22 +000010685 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000010686 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
10687 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000010688 switch (CC) {
10689 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000010690 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000010691 // Converting this to a min would handle comparisons between positive
10692 // and negative zero incorrectly, and swapping the operands would
10693 // cause it to handle NaNs incorrectly.
10694 if (!UnsafeFPMath &&
10695 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000010696 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000010697 break;
10698 std::swap(LHS, RHS);
10699 }
Dan Gohman670e5392009-09-21 18:03:22 +000010700 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000010701 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010702 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000010703 // Converting this to a min would handle NaNs incorrectly.
10704 if (!UnsafeFPMath &&
10705 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
10706 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010707 Opcode = X86ISD::FMIN;
10708 break;
10709 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000010710 // Converting this to a min would handle both negative zeros and NaNs
10711 // incorrectly, but we can swap the operands to fix both.
10712 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000010713 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010714 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010715 case ISD::SETGE:
10716 Opcode = X86ISD::FMIN;
10717 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010718
Dan Gohman670e5392009-09-21 18:03:22 +000010719 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000010720 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000010721 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000010722 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010723 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000010724 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010725 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000010726 // Converting this to a max would handle comparisons between positive
10727 // and negative zero incorrectly, and swapping the operands would
10728 // cause it to handle NaNs incorrectly.
10729 if (!UnsafeFPMath &&
10730 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000010731 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000010732 break;
10733 std::swap(LHS, RHS);
10734 }
Dan Gohman670e5392009-09-21 18:03:22 +000010735 Opcode = X86ISD::FMAX;
10736 break;
10737 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000010738 // Converting this to a max would handle both negative zeros and NaNs
10739 // incorrectly, but we can swap the operands to fix both.
10740 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000010741 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010742 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000010743 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010744 Opcode = X86ISD::FMAX;
10745 break;
10746 }
Chris Lattner83e6c992006-10-04 06:57:07 +000010747 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010748
Chris Lattner47b4ce82009-03-11 05:48:52 +000010749 if (Opcode)
10750 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000010751 }
Eric Christopherfd179292009-08-27 18:07:15 +000010752
Chris Lattnerd1980a52009-03-12 06:52:53 +000010753 // If this is a select between two integer constants, try to do some
10754 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000010755 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
10756 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000010757 // Don't do this for crazy integer types.
10758 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
10759 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000010760 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000010761 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000010762
Chris Lattnercee56e72009-03-13 05:53:31 +000010763 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000010764 // Efficiently invertible.
10765 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
10766 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
10767 isa<ConstantSDNode>(Cond.getOperand(1))))) {
10768 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000010769 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000010770 }
Eric Christopherfd179292009-08-27 18:07:15 +000010771
Chris Lattnerd1980a52009-03-12 06:52:53 +000010772 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000010773 if (FalseC->getAPIntValue() == 0 &&
10774 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000010775 if (NeedsCondInvert) // Invert the condition if needed.
10776 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
10777 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000010778
Chris Lattnerd1980a52009-03-12 06:52:53 +000010779 // Zero extend the condition if needed.
10780 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000010781
Chris Lattnercee56e72009-03-13 05:53:31 +000010782 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000010783 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000010784 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000010785 }
Eric Christopherfd179292009-08-27 18:07:15 +000010786
Chris Lattner97a29a52009-03-13 05:22:11 +000010787 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000010788 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +000010789 if (NeedsCondInvert) // Invert the condition if needed.
10790 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
10791 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000010792
Chris Lattner97a29a52009-03-13 05:22:11 +000010793 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000010794 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
10795 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000010796 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000010797 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000010798 }
Eric Christopherfd179292009-08-27 18:07:15 +000010799
Chris Lattnercee56e72009-03-13 05:53:31 +000010800 // Optimize cases that will turn into an LEA instruction. This requires
10801 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000010802 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000010803 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000010804 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000010805
Chris Lattnercee56e72009-03-13 05:53:31 +000010806 bool isFastMultiplier = false;
10807 if (Diff < 10) {
10808 switch ((unsigned char)Diff) {
10809 default: break;
10810 case 1: // result = add base, cond
10811 case 2: // result = lea base( , cond*2)
10812 case 3: // result = lea base(cond, cond*2)
10813 case 4: // result = lea base( , cond*4)
10814 case 5: // result = lea base(cond, cond*4)
10815 case 8: // result = lea base( , cond*8)
10816 case 9: // result = lea base(cond, cond*8)
10817 isFastMultiplier = true;
10818 break;
10819 }
10820 }
Eric Christopherfd179292009-08-27 18:07:15 +000010821
Chris Lattnercee56e72009-03-13 05:53:31 +000010822 if (isFastMultiplier) {
10823 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
10824 if (NeedsCondInvert) // Invert the condition if needed.
10825 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
10826 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000010827
Chris Lattnercee56e72009-03-13 05:53:31 +000010828 // Zero extend the condition if needed.
10829 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
10830 Cond);
10831 // Scale the condition by the difference.
10832 if (Diff != 1)
10833 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
10834 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000010835
Chris Lattnercee56e72009-03-13 05:53:31 +000010836 // Add the base if non-zero.
10837 if (FalseC->getAPIntValue() != 0)
10838 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
10839 SDValue(FalseC, 0));
10840 return Cond;
10841 }
Eric Christopherfd179292009-08-27 18:07:15 +000010842 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000010843 }
10844 }
Eric Christopherfd179292009-08-27 18:07:15 +000010845
Dan Gohman475871a2008-07-27 21:46:04 +000010846 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000010847}
10848
Chris Lattnerd1980a52009-03-12 06:52:53 +000010849/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
10850static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
10851 TargetLowering::DAGCombinerInfo &DCI) {
10852 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +000010853
Chris Lattnerd1980a52009-03-12 06:52:53 +000010854 // If the flag operand isn't dead, don't touch this CMOV.
10855 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
10856 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +000010857
Chris Lattnerd1980a52009-03-12 06:52:53 +000010858 // If this is a select between two integer constants, try to do some
10859 // optimizations. Note that the operands are ordered the opposite of SELECT
10860 // operands.
10861 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
10862 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
10863 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
10864 // larger than FalseC (the false value).
10865 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
Eric Christopherfd179292009-08-27 18:07:15 +000010866
Chris Lattnerd1980a52009-03-12 06:52:53 +000010867 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
10868 CC = X86::GetOppositeBranchCondition(CC);
10869 std::swap(TrueC, FalseC);
10870 }
Eric Christopherfd179292009-08-27 18:07:15 +000010871
Chris Lattnerd1980a52009-03-12 06:52:53 +000010872 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000010873 // This is efficient for any integer data type (including i8/i16) and
10874 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000010875 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
10876 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +000010877 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
10878 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000010879
Chris Lattnerd1980a52009-03-12 06:52:53 +000010880 // Zero extend the condition if needed.
10881 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000010882
Chris Lattnerd1980a52009-03-12 06:52:53 +000010883 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
10884 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000010885 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000010886 if (N->getNumValues() == 2) // Dead flag value?
10887 return DCI.CombineTo(N, Cond, SDValue());
10888 return Cond;
10889 }
Eric Christopherfd179292009-08-27 18:07:15 +000010890
Chris Lattnercee56e72009-03-13 05:53:31 +000010891 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
10892 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000010893 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
10894 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +000010895 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
10896 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000010897
Chris Lattner97a29a52009-03-13 05:22:11 +000010898 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000010899 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
10900 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000010901 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
10902 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000010903
Chris Lattner97a29a52009-03-13 05:22:11 +000010904 if (N->getNumValues() == 2) // Dead flag value?
10905 return DCI.CombineTo(N, Cond, SDValue());
10906 return Cond;
10907 }
Eric Christopherfd179292009-08-27 18:07:15 +000010908
Chris Lattnercee56e72009-03-13 05:53:31 +000010909 // Optimize cases that will turn into an LEA instruction. This requires
10910 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000010911 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000010912 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000010913 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000010914
Chris Lattnercee56e72009-03-13 05:53:31 +000010915 bool isFastMultiplier = false;
10916 if (Diff < 10) {
10917 switch ((unsigned char)Diff) {
10918 default: break;
10919 case 1: // result = add base, cond
10920 case 2: // result = lea base( , cond*2)
10921 case 3: // result = lea base(cond, cond*2)
10922 case 4: // result = lea base( , cond*4)
10923 case 5: // result = lea base(cond, cond*4)
10924 case 8: // result = lea base( , cond*8)
10925 case 9: // result = lea base(cond, cond*8)
10926 isFastMultiplier = true;
10927 break;
10928 }
10929 }
Eric Christopherfd179292009-08-27 18:07:15 +000010930
Chris Lattnercee56e72009-03-13 05:53:31 +000010931 if (isFastMultiplier) {
10932 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
10933 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +000010934 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
10935 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000010936 // Zero extend the condition if needed.
10937 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
10938 Cond);
10939 // Scale the condition by the difference.
10940 if (Diff != 1)
10941 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
10942 DAG.getConstant(Diff, Cond.getValueType()));
10943
10944 // Add the base if non-zero.
10945 if (FalseC->getAPIntValue() != 0)
10946 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
10947 SDValue(FalseC, 0));
10948 if (N->getNumValues() == 2) // Dead flag value?
10949 return DCI.CombineTo(N, Cond, SDValue());
10950 return Cond;
10951 }
Eric Christopherfd179292009-08-27 18:07:15 +000010952 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000010953 }
10954 }
10955 return SDValue();
10956}
10957
10958
Evan Cheng0b0cd912009-03-28 05:57:29 +000010959/// PerformMulCombine - Optimize a single multiply with constant into two
10960/// in order to implement it with two cheaper instructions, e.g.
10961/// LEA + SHL, LEA + LEA.
10962static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
10963 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000010964 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
10965 return SDValue();
10966
Owen Andersone50ed302009-08-10 22:56:29 +000010967 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010968 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000010969 return SDValue();
10970
10971 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
10972 if (!C)
10973 return SDValue();
10974 uint64_t MulAmt = C->getZExtValue();
10975 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
10976 return SDValue();
10977
10978 uint64_t MulAmt1 = 0;
10979 uint64_t MulAmt2 = 0;
10980 if ((MulAmt % 9) == 0) {
10981 MulAmt1 = 9;
10982 MulAmt2 = MulAmt / 9;
10983 } else if ((MulAmt % 5) == 0) {
10984 MulAmt1 = 5;
10985 MulAmt2 = MulAmt / 5;
10986 } else if ((MulAmt % 3) == 0) {
10987 MulAmt1 = 3;
10988 MulAmt2 = MulAmt / 3;
10989 }
10990 if (MulAmt2 &&
10991 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
10992 DebugLoc DL = N->getDebugLoc();
10993
10994 if (isPowerOf2_64(MulAmt2) &&
10995 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
10996 // If second multiplifer is pow2, issue it first. We want the multiply by
10997 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
10998 // is an add.
10999 std::swap(MulAmt1, MulAmt2);
11000
11001 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000011002 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000011003 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000011004 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000011005 else
Evan Cheng73f24c92009-03-30 21:36:47 +000011006 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000011007 DAG.getConstant(MulAmt1, VT));
11008
Eric Christopherfd179292009-08-27 18:07:15 +000011009 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000011010 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000011011 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000011012 else
Evan Cheng73f24c92009-03-30 21:36:47 +000011013 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000011014 DAG.getConstant(MulAmt2, VT));
11015
11016 // Do not add new nodes to DAG combiner worklist.
11017 DCI.CombineTo(N, NewMul, false);
11018 }
11019 return SDValue();
11020}
11021
Evan Chengad9c0a32009-12-15 00:53:42 +000011022static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
11023 SDValue N0 = N->getOperand(0);
11024 SDValue N1 = N->getOperand(1);
11025 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
11026 EVT VT = N0.getValueType();
11027
11028 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
11029 // since the result of setcc_c is all zero's or all ones.
11030 if (N1C && N0.getOpcode() == ISD::AND &&
11031 N0.getOperand(1).getOpcode() == ISD::Constant) {
11032 SDValue N00 = N0.getOperand(0);
11033 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
11034 ((N00.getOpcode() == ISD::ANY_EXTEND ||
11035 N00.getOpcode() == ISD::ZERO_EXTEND) &&
11036 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
11037 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
11038 APInt ShAmt = N1C->getAPIntValue();
11039 Mask = Mask.shl(ShAmt);
11040 if (Mask != 0)
11041 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
11042 N00, DAG.getConstant(Mask, VT));
11043 }
11044 }
11045
11046 return SDValue();
11047}
Evan Cheng0b0cd912009-03-28 05:57:29 +000011048
Nate Begeman740ab032009-01-26 00:52:55 +000011049/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
11050/// when possible.
11051static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
11052 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +000011053 EVT VT = N->getValueType(0);
11054 if (!VT.isVector() && VT.isInteger() &&
11055 N->getOpcode() == ISD::SHL)
11056 return PerformSHLCombine(N, DAG);
11057
Nate Begeman740ab032009-01-26 00:52:55 +000011058 // On X86 with SSE2 support, we can transform this to a vector shift if
11059 // all elements are shifted by the same amount. We can't do this in legalize
11060 // because the a constant vector is typically transformed to a constant pool
11061 // so we have no knowledge of the shift amount.
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011062 if (!Subtarget->hasSSE2())
11063 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000011064
Owen Anderson825b72b2009-08-11 20:47:22 +000011065 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011066 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000011067
Mon P Wang3becd092009-01-28 08:12:05 +000011068 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +000011069 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +000011070 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +000011071 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +000011072 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
11073 unsigned NumElts = VT.getVectorNumElements();
11074 unsigned i = 0;
11075 for (; i != NumElts; ++i) {
11076 SDValue Arg = ShAmtOp.getOperand(i);
11077 if (Arg.getOpcode() == ISD::UNDEF) continue;
11078 BaseShAmt = Arg;
11079 break;
11080 }
11081 for (; i != NumElts; ++i) {
11082 SDValue Arg = ShAmtOp.getOperand(i);
11083 if (Arg.getOpcode() == ISD::UNDEF) continue;
11084 if (Arg != BaseShAmt) {
11085 return SDValue();
11086 }
11087 }
11088 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +000011089 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +000011090 SDValue InVec = ShAmtOp.getOperand(0);
11091 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
11092 unsigned NumElts = InVec.getValueType().getVectorNumElements();
11093 unsigned i = 0;
11094 for (; i != NumElts; ++i) {
11095 SDValue Arg = InVec.getOperand(i);
11096 if (Arg.getOpcode() == ISD::UNDEF) continue;
11097 BaseShAmt = Arg;
11098 break;
11099 }
11100 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
11101 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +000011102 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +000011103 if (C->getZExtValue() == SplatIdx)
11104 BaseShAmt = InVec.getOperand(1);
11105 }
11106 }
11107 if (BaseShAmt.getNode() == 0)
11108 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
11109 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +000011110 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011111 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000011112
Mon P Wangefa42202009-09-03 19:56:25 +000011113 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +000011114 if (EltVT.bitsGT(MVT::i32))
11115 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
11116 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +000011117 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +000011118
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011119 // The shift amount is identical so we can do a vector shift.
11120 SDValue ValOp = N->getOperand(0);
11121 switch (N->getOpcode()) {
11122 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000011123 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011124 break;
11125 case ISD::SHL:
Owen Anderson825b72b2009-08-11 20:47:22 +000011126 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011127 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011128 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011129 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011130 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011131 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011132 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011133 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011134 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011135 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011136 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011137 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011138 break;
11139 case ISD::SRA:
Owen Anderson825b72b2009-08-11 20:47:22 +000011140 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011141 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011142 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011143 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011144 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011145 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011146 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011147 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011148 break;
11149 case ISD::SRL:
Owen Anderson825b72b2009-08-11 20:47:22 +000011150 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011151 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011152 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011153 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011154 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011155 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011156 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011157 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011158 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011159 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011160 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011161 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011162 break;
Nate Begeman740ab032009-01-26 00:52:55 +000011163 }
11164 return SDValue();
11165}
11166
Nate Begemanb65c1752010-12-17 22:55:37 +000011167
11168static SDValue PerformAndCombine(SDNode *N, SelectionDAG &DAG,
11169 TargetLowering::DAGCombinerInfo &DCI,
11170 const X86Subtarget *Subtarget) {
11171 if (DCI.isBeforeLegalizeOps())
11172 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011173
Nate Begemanb65c1752010-12-17 22:55:37 +000011174 // Want to form PANDN nodes, in the hopes of then easily combining them with
11175 // OR and AND nodes to form PBLEND/PSIGN.
11176 EVT VT = N->getValueType(0);
11177 if (VT != MVT::v2i64)
11178 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011179
Nate Begemanb65c1752010-12-17 22:55:37 +000011180 SDValue N0 = N->getOperand(0);
11181 SDValue N1 = N->getOperand(1);
11182 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011183
Nate Begemanb65c1752010-12-17 22:55:37 +000011184 // Check LHS for vnot
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011185 if (N0.getOpcode() == ISD::XOR &&
Nate Begemanb65c1752010-12-17 22:55:37 +000011186 ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode()))
11187 return DAG.getNode(X86ISD::PANDN, DL, VT, N0.getOperand(0), N1);
11188
11189 // Check RHS for vnot
11190 if (N1.getOpcode() == ISD::XOR &&
11191 ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode()))
11192 return DAG.getNode(X86ISD::PANDN, DL, VT, N1.getOperand(0), N0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011193
Nate Begemanb65c1752010-12-17 22:55:37 +000011194 return SDValue();
11195}
11196
Evan Cheng760d1942010-01-04 21:22:48 +000011197static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000011198 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000011199 const X86Subtarget *Subtarget) {
Evan Cheng39cfeec2010-04-28 02:25:18 +000011200 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000011201 return SDValue();
11202
Evan Cheng760d1942010-01-04 21:22:48 +000011203 EVT VT = N->getValueType(0);
Nate Begemanb65c1752010-12-17 22:55:37 +000011204 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64 && VT != MVT::v2i64)
Evan Cheng760d1942010-01-04 21:22:48 +000011205 return SDValue();
11206
Evan Cheng760d1942010-01-04 21:22:48 +000011207 SDValue N0 = N->getOperand(0);
11208 SDValue N1 = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011209
Nate Begemanb65c1752010-12-17 22:55:37 +000011210 // look for psign/blend
11211 if (Subtarget->hasSSSE3()) {
11212 if (VT == MVT::v2i64) {
11213 // Canonicalize pandn to RHS
11214 if (N0.getOpcode() == X86ISD::PANDN)
11215 std::swap(N0, N1);
11216 // or (and (m, x), (pandn m, y))
11217 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::PANDN) {
11218 SDValue Mask = N1.getOperand(0);
11219 SDValue X = N1.getOperand(1);
11220 SDValue Y;
11221 if (N0.getOperand(0) == Mask)
11222 Y = N0.getOperand(1);
11223 if (N0.getOperand(1) == Mask)
11224 Y = N0.getOperand(0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011225
Nate Begemanb65c1752010-12-17 22:55:37 +000011226 // Check to see if the mask appeared in both the AND and PANDN and
11227 if (!Y.getNode())
11228 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011229
Nate Begemanb65c1752010-12-17 22:55:37 +000011230 // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them.
11231 if (Mask.getOpcode() != ISD::BITCAST ||
11232 X.getOpcode() != ISD::BITCAST ||
11233 Y.getOpcode() != ISD::BITCAST)
11234 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011235
Nate Begemanb65c1752010-12-17 22:55:37 +000011236 // Look through mask bitcast.
11237 Mask = Mask.getOperand(0);
11238 EVT MaskVT = Mask.getValueType();
11239
11240 // Validate that the Mask operand is a vector sra node. The sra node
11241 // will be an intrinsic.
11242 if (Mask.getOpcode() != ISD::INTRINSIC_WO_CHAIN)
11243 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011244
Nate Begemanb65c1752010-12-17 22:55:37 +000011245 // FIXME: what to do for bytes, since there is a psignb/pblendvb, but
11246 // there is no psrai.b
11247 switch (cast<ConstantSDNode>(Mask.getOperand(0))->getZExtValue()) {
11248 case Intrinsic::x86_sse2_psrai_w:
11249 case Intrinsic::x86_sse2_psrai_d:
11250 break;
11251 default: return SDValue();
11252 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011253
Nate Begemanb65c1752010-12-17 22:55:37 +000011254 // Check that the SRA is all signbits.
11255 SDValue SraC = Mask.getOperand(2);
11256 unsigned SraAmt = cast<ConstantSDNode>(SraC)->getZExtValue();
11257 unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits();
11258 if ((SraAmt + 1) != EltBits)
11259 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011260
Nate Begemanb65c1752010-12-17 22:55:37 +000011261 DebugLoc DL = N->getDebugLoc();
11262
11263 // Now we know we at least have a plendvb with the mask val. See if
11264 // we can form a psignb/w/d.
11265 // psign = x.type == y.type == mask.type && y = sub(0, x);
11266 X = X.getOperand(0);
11267 Y = Y.getOperand(0);
11268 if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X &&
11269 ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) &&
11270 X.getValueType() == MaskVT && X.getValueType() == Y.getValueType()){
11271 unsigned Opc = 0;
11272 switch (EltBits) {
11273 case 8: Opc = X86ISD::PSIGNB; break;
11274 case 16: Opc = X86ISD::PSIGNW; break;
11275 case 32: Opc = X86ISD::PSIGND; break;
11276 default: break;
11277 }
11278 if (Opc) {
11279 SDValue Sign = DAG.getNode(Opc, DL, MaskVT, X, Mask.getOperand(1));
11280 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Sign);
11281 }
11282 }
11283 // PBLENDVB only available on SSE 4.1
11284 if (!Subtarget->hasSSE41())
11285 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011286
Nate Begemanb65c1752010-12-17 22:55:37 +000011287 X = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, X);
11288 Y = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Y);
11289 Mask = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Mask);
Nate Begeman672fb622010-12-20 22:04:24 +000011290 Mask = DAG.getNode(X86ISD::PBLENDVB, DL, MVT::v16i8, X, Y, Mask);
Nate Begemanb65c1752010-12-17 22:55:37 +000011291 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Mask);
11292 }
11293 }
11294 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011295
Nate Begemanb65c1752010-12-17 22:55:37 +000011296 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
Evan Cheng760d1942010-01-04 21:22:48 +000011297 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
11298 std::swap(N0, N1);
11299 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
11300 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000011301 if (!N0.hasOneUse() || !N1.hasOneUse())
11302 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000011303
11304 SDValue ShAmt0 = N0.getOperand(1);
11305 if (ShAmt0.getValueType() != MVT::i8)
11306 return SDValue();
11307 SDValue ShAmt1 = N1.getOperand(1);
11308 if (ShAmt1.getValueType() != MVT::i8)
11309 return SDValue();
11310 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
11311 ShAmt0 = ShAmt0.getOperand(0);
11312 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
11313 ShAmt1 = ShAmt1.getOperand(0);
11314
11315 DebugLoc DL = N->getDebugLoc();
11316 unsigned Opc = X86ISD::SHLD;
11317 SDValue Op0 = N0.getOperand(0);
11318 SDValue Op1 = N1.getOperand(0);
11319 if (ShAmt0.getOpcode() == ISD::SUB) {
11320 Opc = X86ISD::SHRD;
11321 std::swap(Op0, Op1);
11322 std::swap(ShAmt0, ShAmt1);
11323 }
11324
Evan Cheng8b1190a2010-04-28 01:18:01 +000011325 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000011326 if (ShAmt1.getOpcode() == ISD::SUB) {
11327 SDValue Sum = ShAmt1.getOperand(0);
11328 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000011329 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
11330 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
11331 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
11332 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000011333 return DAG.getNode(Opc, DL, VT,
11334 Op0, Op1,
11335 DAG.getNode(ISD::TRUNCATE, DL,
11336 MVT::i8, ShAmt0));
11337 }
11338 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
11339 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
11340 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000011341 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000011342 return DAG.getNode(Opc, DL, VT,
11343 N0.getOperand(0), N1.getOperand(0),
11344 DAG.getNode(ISD::TRUNCATE, DL,
11345 MVT::i8, ShAmt0));
11346 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011347
Evan Cheng760d1942010-01-04 21:22:48 +000011348 return SDValue();
11349}
11350
Chris Lattner149a4e52008-02-22 02:09:43 +000011351/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000011352static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000011353 const X86Subtarget *Subtarget) {
Chris Lattner149a4e52008-02-22 02:09:43 +000011354 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
11355 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000011356 // A preferable solution to the general problem is to figure out the right
11357 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000011358
11359 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng7e2ff772008-05-08 00:57:18 +000011360 StoreSDNode *St = cast<StoreSDNode>(N);
Owen Andersone50ed302009-08-10 22:56:29 +000011361 EVT VT = St->getValue().getValueType();
Evan Cheng536e6672009-03-12 05:59:15 +000011362 if (VT.getSizeInBits() != 64)
11363 return SDValue();
11364
Devang Patel578efa92009-06-05 21:57:13 +000011365 const Function *F = DAG.getMachineFunction().getFunction();
11366 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Eric Christopherfd179292009-08-27 18:07:15 +000011367 bool F64IsLegal = !UseSoftFloat && !NoImplicitFloatOps
Devang Patel578efa92009-06-05 21:57:13 +000011368 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +000011369 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000011370 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000011371 isa<LoadSDNode>(St->getValue()) &&
11372 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
11373 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000011374 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000011375 LoadSDNode *Ld = 0;
11376 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000011377 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000011378 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000011379 // Must be a store of a load. We currently handle two cases: the load
11380 // is a direct child, and it's under an intervening TokenFactor. It is
11381 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000011382 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000011383 Ld = cast<LoadSDNode>(St->getChain());
11384 else if (St->getValue().hasOneUse() &&
11385 ChainVal->getOpcode() == ISD::TokenFactor) {
11386 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000011387 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000011388 TokenFactorIndex = i;
11389 Ld = cast<LoadSDNode>(St->getValue());
11390 } else
11391 Ops.push_back(ChainVal->getOperand(i));
11392 }
11393 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000011394
Evan Cheng536e6672009-03-12 05:59:15 +000011395 if (!Ld || !ISD::isNormalLoad(Ld))
11396 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000011397
Evan Cheng536e6672009-03-12 05:59:15 +000011398 // If this is not the MMX case, i.e. we are just turning i64 load/store
11399 // into f64 load/store, avoid the transformation if there are multiple
11400 // uses of the loaded value.
11401 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
11402 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000011403
Evan Cheng536e6672009-03-12 05:59:15 +000011404 DebugLoc LdDL = Ld->getDebugLoc();
11405 DebugLoc StDL = N->getDebugLoc();
11406 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
11407 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
11408 // pair instead.
11409 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000011410 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000011411 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
11412 Ld->getPointerInfo(), Ld->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000011413 Ld->isNonTemporal(), Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000011414 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000011415 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000011416 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000011417 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000011418 Ops.size());
11419 }
Evan Cheng536e6672009-03-12 05:59:15 +000011420 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000011421 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000011422 St->isVolatile(), St->isNonTemporal(),
11423 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000011424 }
Evan Cheng536e6672009-03-12 05:59:15 +000011425
11426 // Otherwise, lower to two pairs of 32-bit loads / stores.
11427 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000011428 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
11429 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000011430
Owen Anderson825b72b2009-08-11 20:47:22 +000011431 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000011432 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000011433 Ld->isVolatile(), Ld->isNonTemporal(),
11434 Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000011435 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000011436 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000011437 Ld->isVolatile(), Ld->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000011438 MinAlign(Ld->getAlignment(), 4));
11439
11440 SDValue NewChain = LoLd.getValue(1);
11441 if (TokenFactorIndex != -1) {
11442 Ops.push_back(LoLd);
11443 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000011444 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000011445 Ops.size());
11446 }
11447
11448 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000011449 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
11450 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000011451
11452 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000011453 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000011454 St->isVolatile(), St->isNonTemporal(),
11455 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000011456 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000011457 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000011458 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000011459 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000011460 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000011461 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000011462 }
Dan Gohman475871a2008-07-27 21:46:04 +000011463 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000011464}
11465
Chris Lattner6cf73262008-01-25 06:14:17 +000011466/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
11467/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000011468static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000011469 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
11470 // F[X]OR(0.0, x) -> x
11471 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000011472 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
11473 if (C->getValueAPF().isPosZero())
11474 return N->getOperand(1);
11475 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
11476 if (C->getValueAPF().isPosZero())
11477 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000011478 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000011479}
11480
11481/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000011482static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000011483 // FAND(0.0, x) -> 0.0
11484 // FAND(x, 0.0) -> 0.0
11485 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
11486 if (C->getValueAPF().isPosZero())
11487 return N->getOperand(0);
11488 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
11489 if (C->getValueAPF().isPosZero())
11490 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000011491 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000011492}
11493
Dan Gohmane5af2d32009-01-29 01:59:02 +000011494static SDValue PerformBTCombine(SDNode *N,
11495 SelectionDAG &DAG,
11496 TargetLowering::DAGCombinerInfo &DCI) {
11497 // BT ignores high bits in the bit index operand.
11498 SDValue Op1 = N->getOperand(1);
11499 if (Op1.hasOneUse()) {
11500 unsigned BitWidth = Op1.getValueSizeInBits();
11501 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
11502 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000011503 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
11504 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000011505 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000011506 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
11507 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
11508 DCI.CommitTargetLoweringOpt(TLO);
11509 }
11510 return SDValue();
11511}
Chris Lattner83e6c992006-10-04 06:57:07 +000011512
Eli Friedman7a5e5552009-06-07 06:52:44 +000011513static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
11514 SDValue Op = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011515 if (Op.getOpcode() == ISD::BITCAST)
Eli Friedman7a5e5552009-06-07 06:52:44 +000011516 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000011517 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000011518 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000011519 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000011520 OpVT.getVectorElementType().getSizeInBits()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011521 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Eli Friedman7a5e5552009-06-07 06:52:44 +000011522 }
11523 return SDValue();
11524}
11525
Evan Cheng2e489c42009-12-16 00:53:11 +000011526static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG) {
11527 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
11528 // (and (i32 x86isd::setcc_carry), 1)
11529 // This eliminates the zext. This transformation is necessary because
11530 // ISD::SETCC is always legalized to i8.
11531 DebugLoc dl = N->getDebugLoc();
11532 SDValue N0 = N->getOperand(0);
11533 EVT VT = N->getValueType(0);
11534 if (N0.getOpcode() == ISD::AND &&
11535 N0.hasOneUse() &&
11536 N0.getOperand(0).hasOneUse()) {
11537 SDValue N00 = N0.getOperand(0);
11538 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
11539 return SDValue();
11540 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
11541 if (!C || C->getZExtValue() != 1)
11542 return SDValue();
11543 return DAG.getNode(ISD::AND, dl, VT,
11544 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
11545 N00.getOperand(0), N00.getOperand(1)),
11546 DAG.getConstant(1, VT));
11547 }
11548
11549 return SDValue();
11550}
11551
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011552// Optimize RES = X86ISD::SETCC CONDCODE, EFLAG_INPUT
11553static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG &DAG) {
11554 unsigned X86CC = N->getConstantOperandVal(0);
11555 SDValue EFLAG = N->getOperand(1);
11556 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011557
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011558 // Materialize "setb reg" as "sbb reg,reg", since it can be extended without
11559 // a zext and produces an all-ones bit which is more useful than 0/1 in some
11560 // cases.
11561 if (X86CC == X86::COND_B)
11562 return DAG.getNode(ISD::AND, DL, MVT::i8,
11563 DAG.getNode(X86ISD::SETCC_CARRY, DL, MVT::i8,
11564 DAG.getConstant(X86CC, MVT::i8), EFLAG),
11565 DAG.getConstant(1, MVT::i8));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011566
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011567 return SDValue();
11568}
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011569
Chris Lattner23a01992010-12-20 01:37:09 +000011570// Optimize RES, EFLAGS = X86ISD::ADC LHS, RHS, EFLAGS
11571static SDValue PerformADCCombine(SDNode *N, SelectionDAG &DAG,
11572 X86TargetLowering::DAGCombinerInfo &DCI) {
11573 // If the LHS and RHS of the ADC node are zero, then it can't overflow and
11574 // the result is either zero or one (depending on the input carry bit).
11575 // Strength reduce this down to a "set on carry" aka SETCC_CARRY&1.
11576 if (X86::isZeroNode(N->getOperand(0)) &&
11577 X86::isZeroNode(N->getOperand(1)) &&
11578 // We don't have a good way to replace an EFLAGS use, so only do this when
11579 // dead right now.
11580 SDValue(N, 1).use_empty()) {
11581 DebugLoc DL = N->getDebugLoc();
11582 EVT VT = N->getValueType(0);
11583 SDValue CarryOut = DAG.getConstant(0, N->getValueType(1));
11584 SDValue Res1 = DAG.getNode(ISD::AND, DL, VT,
11585 DAG.getNode(X86ISD::SETCC_CARRY, DL, VT,
11586 DAG.getConstant(X86::COND_B,MVT::i8),
11587 N->getOperand(2)),
11588 DAG.getConstant(1, VT));
11589 return DCI.CombineTo(N, Res1, CarryOut);
11590 }
11591
11592 return SDValue();
11593}
11594
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000011595// fold (add Y, (sete X, 0)) -> adc 0, Y
11596// (add Y, (setne X, 0)) -> sbb -1, Y
11597// (sub (sete X, 0), Y) -> sbb 0, Y
11598// (sub (setne X, 0), Y) -> adc -1, Y
11599static SDValue OptimizeConditonalInDecrement(SDNode *N, SelectionDAG &DAG) {
11600 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011601
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000011602 // Look through ZExts.
11603 SDValue Ext = N->getOperand(N->getOpcode() == ISD::SUB ? 1 : 0);
11604 if (Ext.getOpcode() != ISD::ZERO_EXTEND || !Ext.hasOneUse())
11605 return SDValue();
11606
11607 SDValue SetCC = Ext.getOperand(0);
11608 if (SetCC.getOpcode() != X86ISD::SETCC || !SetCC.hasOneUse())
11609 return SDValue();
11610
11611 X86::CondCode CC = (X86::CondCode)SetCC.getConstantOperandVal(0);
11612 if (CC != X86::COND_E && CC != X86::COND_NE)
11613 return SDValue();
11614
11615 SDValue Cmp = SetCC.getOperand(1);
11616 if (Cmp.getOpcode() != X86ISD::CMP || !Cmp.hasOneUse() ||
Chris Lattner9cd3da42011-01-16 02:56:53 +000011617 !X86::isZeroNode(Cmp.getOperand(1)) ||
11618 !Cmp.getOperand(0).getValueType().isInteger())
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000011619 return SDValue();
11620
11621 SDValue CmpOp0 = Cmp.getOperand(0);
11622 SDValue NewCmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32, CmpOp0,
11623 DAG.getConstant(1, CmpOp0.getValueType()));
11624
11625 SDValue OtherVal = N->getOperand(N->getOpcode() == ISD::SUB ? 0 : 1);
11626 if (CC == X86::COND_NE)
11627 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::ADC : X86ISD::SBB,
11628 DL, OtherVal.getValueType(), OtherVal,
11629 DAG.getConstant(-1ULL, OtherVal.getValueType()), NewCmp);
11630 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::SBB : X86ISD::ADC,
11631 DL, OtherVal.getValueType(), OtherVal,
11632 DAG.getConstant(0, OtherVal.getValueType()), NewCmp);
11633}
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011634
Dan Gohman475871a2008-07-27 21:46:04 +000011635SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000011636 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000011637 SelectionDAG &DAG = DCI.DAG;
11638 switch (N->getOpcode()) {
11639 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000011640 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011641 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, *this);
Chris Lattneraf723b92008-01-25 05:46:26 +000011642 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +000011643 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000011644 case ISD::ADD:
11645 case ISD::SUB: return OptimizeConditonalInDecrement(N, DAG);
Chris Lattner23a01992010-12-20 01:37:09 +000011646 case X86ISD::ADC: return PerformADCCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000011647 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000011648 case ISD::SHL:
11649 case ISD::SRA:
11650 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Nate Begemanb65c1752010-12-17 22:55:37 +000011651 case ISD::AND: return PerformAndCombine(N, DAG, DCI, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000011652 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000011653 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +000011654 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000011655 case X86ISD::FOR: return PerformFORCombine(N, DAG);
11656 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000011657 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000011658 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Evan Cheng2e489c42009-12-16 00:53:11 +000011659 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG);
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011660 case X86ISD::SETCC: return PerformSETCCCombine(N, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000011661 case X86ISD::SHUFPS: // Handle all target specific shuffles
11662 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +000011663 case X86ISD::PALIGN:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000011664 case X86ISD::PUNPCKHBW:
11665 case X86ISD::PUNPCKHWD:
11666 case X86ISD::PUNPCKHDQ:
11667 case X86ISD::PUNPCKHQDQ:
11668 case X86ISD::UNPCKHPS:
11669 case X86ISD::UNPCKHPD:
11670 case X86ISD::PUNPCKLBW:
11671 case X86ISD::PUNPCKLWD:
11672 case X86ISD::PUNPCKLDQ:
11673 case X86ISD::PUNPCKLQDQ:
11674 case X86ISD::UNPCKLPS:
11675 case X86ISD::UNPCKLPD:
11676 case X86ISD::MOVHLPS:
11677 case X86ISD::MOVLHPS:
11678 case X86ISD::PSHUFD:
11679 case X86ISD::PSHUFHW:
11680 case X86ISD::PSHUFLW:
11681 case X86ISD::MOVSS:
11682 case X86ISD::MOVSD:
Mon P Wanga0fd0d52010-12-19 23:55:53 +000011683 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, DCI);
Evan Cheng206ee9d2006-07-07 08:33:52 +000011684 }
11685
Dan Gohman475871a2008-07-27 21:46:04 +000011686 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000011687}
11688
Evan Chenge5b51ac2010-04-17 06:13:15 +000011689/// isTypeDesirableForOp - Return true if the target has native support for
11690/// the specified value type and it is 'desirable' to use the type for the
11691/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
11692/// instruction encodings are longer and some i16 instructions are slow.
11693bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
11694 if (!isTypeLegal(VT))
11695 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000011696 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000011697 return true;
11698
11699 switch (Opc) {
11700 default:
11701 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000011702 case ISD::LOAD:
11703 case ISD::SIGN_EXTEND:
11704 case ISD::ZERO_EXTEND:
11705 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000011706 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000011707 case ISD::SRL:
11708 case ISD::SUB:
11709 case ISD::ADD:
11710 case ISD::MUL:
11711 case ISD::AND:
11712 case ISD::OR:
11713 case ISD::XOR:
11714 return false;
11715 }
11716}
11717
11718/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000011719/// beneficial for dag combiner to promote the specified node. If true, it
11720/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000011721bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000011722 EVT VT = Op.getValueType();
11723 if (VT != MVT::i16)
11724 return false;
11725
Evan Cheng4c26e932010-04-19 19:29:22 +000011726 bool Promote = false;
11727 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000011728 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000011729 default: break;
11730 case ISD::LOAD: {
11731 LoadSDNode *LD = cast<LoadSDNode>(Op);
11732 // If the non-extending load has a single use and it's not live out, then it
11733 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000011734 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
11735 Op.hasOneUse()*/) {
11736 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
11737 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
11738 // The only case where we'd want to promote LOAD (rather then it being
11739 // promoted as an operand is when it's only use is liveout.
11740 if (UI->getOpcode() != ISD::CopyToReg)
11741 return false;
11742 }
11743 }
Evan Cheng4c26e932010-04-19 19:29:22 +000011744 Promote = true;
11745 break;
11746 }
11747 case ISD::SIGN_EXTEND:
11748 case ISD::ZERO_EXTEND:
11749 case ISD::ANY_EXTEND:
11750 Promote = true;
11751 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000011752 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000011753 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000011754 SDValue N0 = Op.getOperand(0);
11755 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000011756 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000011757 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000011758 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000011759 break;
11760 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000011761 case ISD::ADD:
11762 case ISD::MUL:
11763 case ISD::AND:
11764 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000011765 case ISD::XOR:
11766 Commute = true;
11767 // fallthrough
11768 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000011769 SDValue N0 = Op.getOperand(0);
11770 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000011771 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000011772 return false;
11773 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000011774 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000011775 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000011776 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000011777 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000011778 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000011779 }
11780 }
11781
11782 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000011783 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000011784}
11785
Evan Cheng60c07e12006-07-05 22:17:51 +000011786//===----------------------------------------------------------------------===//
11787// X86 Inline Assembly Support
11788//===----------------------------------------------------------------------===//
11789
Chris Lattnerb8105652009-07-20 17:51:36 +000011790bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
11791 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattnerb8105652009-07-20 17:51:36 +000011792
11793 std::string AsmStr = IA->getAsmString();
11794
11795 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000011796 SmallVector<StringRef, 4> AsmPieces;
Peter Collingbourne98361182010-11-13 19:54:23 +000011797 SplitString(AsmStr, AsmPieces, ";\n");
Chris Lattnerb8105652009-07-20 17:51:36 +000011798
11799 switch (AsmPieces.size()) {
11800 default: return false;
11801 case 1:
11802 AsmStr = AsmPieces[0];
11803 AsmPieces.clear();
11804 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
11805
Evan Cheng55d42002011-01-08 01:24:27 +000011806 // FIXME: this should verify that we are targetting a 486 or better. If not,
11807 // we will turn this bswap into something that will be lowered to logical ops
11808 // instead of emitting the bswap asm. For now, we don't support 486 or lower
11809 // so don't worry about this.
Chris Lattnerb8105652009-07-20 17:51:36 +000011810 // bswap $0
11811 if (AsmPieces.size() == 2 &&
11812 (AsmPieces[0] == "bswap" ||
11813 AsmPieces[0] == "bswapq" ||
11814 AsmPieces[0] == "bswapl") &&
11815 (AsmPieces[1] == "$0" ||
11816 AsmPieces[1] == "${0:q}")) {
11817 // No need to check constraints, nothing other than the equivalent of
11818 // "=r,0" would be valid here.
Evan Cheng55d42002011-01-08 01:24:27 +000011819 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
11820 if (!Ty || Ty->getBitWidth() % 16 != 0)
11821 return false;
11822 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000011823 }
11824 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000011825 if (CI->getType()->isIntegerTy(16) &&
Chris Lattnerb8105652009-07-20 17:51:36 +000011826 AsmPieces.size() == 3 &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000011827 (AsmPieces[0] == "rorw" || AsmPieces[0] == "rolw") &&
Chris Lattnerb8105652009-07-20 17:51:36 +000011828 AsmPieces[1] == "$$8," &&
11829 AsmPieces[2] == "${0:w}" &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000011830 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
11831 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000011832 const std::string &ConstraintsStr = IA->getConstraintString();
11833 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000011834 std::sort(AsmPieces.begin(), AsmPieces.end());
11835 if (AsmPieces.size() == 4 &&
11836 AsmPieces[0] == "~{cc}" &&
11837 AsmPieces[1] == "~{dirflag}" &&
11838 AsmPieces[2] == "~{flags}" &&
11839 AsmPieces[3] == "~{fpsr}") {
Evan Cheng55d42002011-01-08 01:24:27 +000011840 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
11841 if (!Ty || Ty->getBitWidth() % 16 != 0)
11842 return false;
11843 return IntrinsicLowering::LowerToByteSwap(CI);
Dan Gohman0ef701e2010-03-04 19:58:08 +000011844 }
Chris Lattnerb8105652009-07-20 17:51:36 +000011845 }
11846 break;
11847 case 3:
Peter Collingbourne948cf022010-11-13 19:54:30 +000011848 if (CI->getType()->isIntegerTy(32) &&
11849 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
11850 SmallVector<StringRef, 4> Words;
11851 SplitString(AsmPieces[0], Words, " \t,");
11852 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
11853 Words[2] == "${0:w}") {
11854 Words.clear();
11855 SplitString(AsmPieces[1], Words, " \t,");
11856 if (Words.size() == 3 && Words[0] == "rorl" && Words[1] == "$$16" &&
11857 Words[2] == "$0") {
11858 Words.clear();
11859 SplitString(AsmPieces[2], Words, " \t,");
11860 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
11861 Words[2] == "${0:w}") {
11862 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000011863 const std::string &ConstraintsStr = IA->getConstraintString();
11864 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Peter Collingbourne948cf022010-11-13 19:54:30 +000011865 std::sort(AsmPieces.begin(), AsmPieces.end());
11866 if (AsmPieces.size() == 4 &&
11867 AsmPieces[0] == "~{cc}" &&
11868 AsmPieces[1] == "~{dirflag}" &&
11869 AsmPieces[2] == "~{flags}" &&
11870 AsmPieces[3] == "~{fpsr}") {
Evan Cheng55d42002011-01-08 01:24:27 +000011871 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
11872 if (!Ty || Ty->getBitWidth() % 16 != 0)
11873 return false;
11874 return IntrinsicLowering::LowerToByteSwap(CI);
Peter Collingbourne948cf022010-11-13 19:54:30 +000011875 }
11876 }
11877 }
11878 }
11879 }
Evan Cheng55d42002011-01-08 01:24:27 +000011880
11881 if (CI->getType()->isIntegerTy(64)) {
11882 InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
11883 if (Constraints.size() >= 2 &&
11884 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
11885 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
11886 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
11887 SmallVector<StringRef, 4> Words;
11888 SplitString(AsmPieces[0], Words, " \t");
11889 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
Chris Lattnerb8105652009-07-20 17:51:36 +000011890 Words.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000011891 SplitString(AsmPieces[1], Words, " \t");
11892 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
11893 Words.clear();
11894 SplitString(AsmPieces[2], Words, " \t,");
11895 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
11896 Words[2] == "%edx") {
11897 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
11898 if (!Ty || Ty->getBitWidth() % 16 != 0)
11899 return false;
11900 return IntrinsicLowering::LowerToByteSwap(CI);
11901 }
Chris Lattnerb8105652009-07-20 17:51:36 +000011902 }
11903 }
11904 }
11905 }
11906 break;
11907 }
11908 return false;
11909}
11910
11911
11912
Chris Lattnerf4dff842006-07-11 02:54:03 +000011913/// getConstraintType - Given a constraint letter, return the type of
11914/// constraint it is for this target.
11915X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000011916X86TargetLowering::getConstraintType(const std::string &Constraint) const {
11917 if (Constraint.size() == 1) {
11918 switch (Constraint[0]) {
Chris Lattner4234f572007-03-25 02:14:49 +000011919 case 'R':
Chris Lattner4234f572007-03-25 02:14:49 +000011920 case 'q':
11921 case 'Q':
John Thompson44ab89e2010-10-29 17:29:13 +000011922 case 'f':
11923 case 't':
11924 case 'u':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000011925 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +000011926 case 'x':
Chris Lattner4234f572007-03-25 02:14:49 +000011927 case 'Y':
11928 return C_RegisterClass;
John Thompson44ab89e2010-10-29 17:29:13 +000011929 case 'a':
11930 case 'b':
11931 case 'c':
11932 case 'd':
11933 case 'S':
11934 case 'D':
11935 case 'A':
11936 return C_Register;
11937 case 'I':
11938 case 'J':
11939 case 'K':
11940 case 'L':
11941 case 'M':
11942 case 'N':
11943 case 'G':
11944 case 'C':
Dale Johannesen78e3e522009-02-12 20:58:09 +000011945 case 'e':
11946 case 'Z':
11947 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000011948 default:
11949 break;
11950 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000011951 }
Chris Lattner4234f572007-03-25 02:14:49 +000011952 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000011953}
11954
John Thompson44ab89e2010-10-29 17:29:13 +000011955/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +000011956/// This object must already have been set up with the operand type
11957/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +000011958TargetLowering::ConstraintWeight
11959 X86TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +000011960 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +000011961 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +000011962 Value *CallOperandVal = info.CallOperandVal;
11963 // If we don't have a value, we can't do a match,
11964 // but allow it at the lowest weight.
11965 if (CallOperandVal == NULL)
John Thompson44ab89e2010-10-29 17:29:13 +000011966 return CW_Default;
11967 const Type *type = CallOperandVal->getType();
John Thompsoneac6e1d2010-09-13 18:15:37 +000011968 // Look at the constraint type.
11969 switch (*constraint) {
11970 default:
John Thompson44ab89e2010-10-29 17:29:13 +000011971 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
11972 case 'R':
11973 case 'q':
11974 case 'Q':
11975 case 'a':
11976 case 'b':
11977 case 'c':
11978 case 'd':
11979 case 'S':
11980 case 'D':
11981 case 'A':
11982 if (CallOperandVal->getType()->isIntegerTy())
11983 weight = CW_SpecificReg;
11984 break;
11985 case 'f':
11986 case 't':
11987 case 'u':
11988 if (type->isFloatingPointTy())
11989 weight = CW_SpecificReg;
11990 break;
11991 case 'y':
Chris Lattner2a786eb2010-12-19 20:19:20 +000011992 if (type->isX86_MMXTy() && Subtarget->hasMMX())
John Thompson44ab89e2010-10-29 17:29:13 +000011993 weight = CW_SpecificReg;
11994 break;
11995 case 'x':
11996 case 'Y':
Nate Begeman2ea8ee72010-12-10 00:26:57 +000011997 if ((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasXMM())
John Thompson44ab89e2010-10-29 17:29:13 +000011998 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +000011999 break;
12000 case 'I':
12001 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
12002 if (C->getZExtValue() <= 31)
John Thompson44ab89e2010-10-29 17:29:13 +000012003 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012004 }
12005 break;
John Thompson44ab89e2010-10-29 17:29:13 +000012006 case 'J':
12007 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12008 if (C->getZExtValue() <= 63)
12009 weight = CW_Constant;
12010 }
12011 break;
12012 case 'K':
12013 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12014 if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f))
12015 weight = CW_Constant;
12016 }
12017 break;
12018 case 'L':
12019 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12020 if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff))
12021 weight = CW_Constant;
12022 }
12023 break;
12024 case 'M':
12025 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12026 if (C->getZExtValue() <= 3)
12027 weight = CW_Constant;
12028 }
12029 break;
12030 case 'N':
12031 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12032 if (C->getZExtValue() <= 0xff)
12033 weight = CW_Constant;
12034 }
12035 break;
12036 case 'G':
12037 case 'C':
12038 if (dyn_cast<ConstantFP>(CallOperandVal)) {
12039 weight = CW_Constant;
12040 }
12041 break;
12042 case 'e':
12043 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12044 if ((C->getSExtValue() >= -0x80000000LL) &&
12045 (C->getSExtValue() <= 0x7fffffffLL))
12046 weight = CW_Constant;
12047 }
12048 break;
12049 case 'Z':
12050 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12051 if (C->getZExtValue() <= 0xffffffff)
12052 weight = CW_Constant;
12053 }
12054 break;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012055 }
12056 return weight;
12057}
12058
Dale Johannesenba2a0b92008-01-29 02:21:21 +000012059/// LowerXConstraint - try to replace an X constraint, which matches anything,
12060/// with another that has more specific requirements based on the type of the
12061/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000012062const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000012063LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000012064 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
12065 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000012066 if (ConstraintVT.isFloatingPoint()) {
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012067 if (Subtarget->hasXMMInt())
Chris Lattner5e764232008-04-26 23:02:14 +000012068 return "Y";
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012069 if (Subtarget->hasXMM())
Chris Lattner5e764232008-04-26 23:02:14 +000012070 return "x";
12071 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012072
Chris Lattner5e764232008-04-26 23:02:14 +000012073 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000012074}
12075
Chris Lattner48884cd2007-08-25 00:47:38 +000012076/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
12077/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000012078void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattner48884cd2007-08-25 00:47:38 +000012079 char Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000012080 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000012081 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000012082 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000012083
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012084 switch (Constraint) {
12085 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000012086 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000012087 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000012088 if (C->getZExtValue() <= 31) {
12089 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000012090 break;
12091 }
Devang Patel84f7fd22007-03-17 00:13:28 +000012092 }
Chris Lattner48884cd2007-08-25 00:47:38 +000012093 return;
Evan Cheng364091e2008-09-22 23:57:37 +000012094 case 'J':
12095 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000012096 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000012097 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
12098 break;
12099 }
12100 }
12101 return;
12102 case 'K':
12103 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000012104 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +000012105 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
12106 break;
12107 }
12108 }
12109 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000012110 case 'N':
12111 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000012112 if (C->getZExtValue() <= 255) {
12113 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000012114 break;
12115 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000012116 }
Chris Lattner48884cd2007-08-25 00:47:38 +000012117 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000012118 case 'e': {
12119 // 32-bit signed value
12120 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000012121 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
12122 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000012123 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000012124 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000012125 break;
12126 }
12127 // FIXME gcc accepts some relocatable values here too, but only in certain
12128 // memory models; it's complicated.
12129 }
12130 return;
12131 }
12132 case 'Z': {
12133 // 32-bit unsigned value
12134 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000012135 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
12136 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000012137 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
12138 break;
12139 }
12140 }
12141 // FIXME gcc accepts some relocatable values here too, but only in certain
12142 // memory models; it's complicated.
12143 return;
12144 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000012145 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012146 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000012147 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000012148 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000012149 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000012150 break;
12151 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012152
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000012153 // In any sort of PIC mode addresses need to be computed at runtime by
12154 // adding in a register or some sort of table lookup. These can't
12155 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000012156 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000012157 return;
12158
Chris Lattnerdc43a882007-05-03 16:52:29 +000012159 // If we are in non-pic codegen mode, we allow the address of a global (with
12160 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000012161 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000012162 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000012163
Chris Lattner49921962009-05-08 18:23:14 +000012164 // Match either (GA), (GA+C), (GA+C1+C2), etc.
12165 while (1) {
12166 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
12167 Offset += GA->getOffset();
12168 break;
12169 } else if (Op.getOpcode() == ISD::ADD) {
12170 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
12171 Offset += C->getZExtValue();
12172 Op = Op.getOperand(0);
12173 continue;
12174 }
12175 } else if (Op.getOpcode() == ISD::SUB) {
12176 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
12177 Offset += -C->getZExtValue();
12178 Op = Op.getOperand(0);
12179 continue;
12180 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000012181 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000012182
Chris Lattner49921962009-05-08 18:23:14 +000012183 // Otherwise, this isn't something we can handle, reject it.
12184 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000012185 }
Eric Christopherfd179292009-08-27 18:07:15 +000012186
Dan Gohman46510a72010-04-15 01:51:59 +000012187 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000012188 // If we require an extra load to get this address, as in PIC mode, we
12189 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000012190 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
12191 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000012192 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000012193
Devang Patel0d881da2010-07-06 22:08:15 +000012194 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
12195 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000012196 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012197 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000012198 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012199
Gabor Greifba36cb52008-08-28 21:40:38 +000012200 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000012201 Ops.push_back(Result);
12202 return;
12203 }
Dale Johannesen1784d162010-06-25 21:55:36 +000012204 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012205}
12206
Chris Lattner259e97c2006-01-31 19:43:35 +000012207std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +000012208getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000012209 EVT VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +000012210 if (Constraint.size() == 1) {
12211 // FIXME: not handling fp-stack yet!
Chris Lattner259e97c2006-01-31 19:43:35 +000012212 switch (Constraint[0]) { // GCC X86 Constraint Letters
Chris Lattnerf4dff842006-07-11 02:54:03 +000012213 default: break; // Unknown constraint letter
Evan Cheng47e9fab2009-07-17 22:13:25 +000012214 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
12215 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000012216 if (VT == MVT::i32)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012217 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX,
12218 X86::ESI, X86::EDI, X86::R8D, X86::R9D,
12219 X86::R10D,X86::R11D,X86::R12D,
12220 X86::R13D,X86::R14D,X86::R15D,
12221 X86::EBP, X86::ESP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012222 else if (VT == MVT::i16)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012223 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX,
12224 X86::SI, X86::DI, X86::R8W,X86::R9W,
12225 X86::R10W,X86::R11W,X86::R12W,
12226 X86::R13W,X86::R14W,X86::R15W,
12227 X86::BP, X86::SP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012228 else if (VT == MVT::i8)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012229 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL,
12230 X86::SIL, X86::DIL, X86::R8B,X86::R9B,
12231 X86::R10B,X86::R11B,X86::R12B,
12232 X86::R13B,X86::R14B,X86::R15B,
12233 X86::BPL, X86::SPL, 0);
12234
Owen Anderson825b72b2009-08-11 20:47:22 +000012235 else if (VT == MVT::i64)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012236 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX,
12237 X86::RSI, X86::RDI, X86::R8, X86::R9,
12238 X86::R10, X86::R11, X86::R12,
12239 X86::R13, X86::R14, X86::R15,
12240 X86::RBP, X86::RSP, 0);
12241
12242 break;
12243 }
Eric Christopherfd179292009-08-27 18:07:15 +000012244 // 32-bit fallthrough
Chris Lattner259e97c2006-01-31 19:43:35 +000012245 case 'Q': // Q_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +000012246 if (VT == MVT::i32)
Chris Lattner80a7ecc2006-05-06 00:29:37 +000012247 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012248 else if (VT == MVT::i16)
Chris Lattner80a7ecc2006-05-06 00:29:37 +000012249 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012250 else if (VT == MVT::i8)
Evan Cheng12914382007-08-13 23:27:11 +000012251 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012252 else if (VT == MVT::i64)
Chris Lattner03e6c702007-11-04 06:51:12 +000012253 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
12254 break;
Chris Lattner259e97c2006-01-31 19:43:35 +000012255 }
12256 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012257
Chris Lattner1efa40f2006-02-22 00:56:39 +000012258 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +000012259}
Chris Lattnerf76d1802006-07-31 23:26:50 +000012260
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012261std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000012262X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000012263 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000012264 // First, see if this is a constraint that directly corresponds to an LLVM
12265 // register class.
12266 if (Constraint.size() == 1) {
12267 // GCC Constraint Letters
12268 switch (Constraint[0]) {
12269 default: break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000012270 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000012271 case 'l': // INDEX_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +000012272 if (VT == MVT::i8)
Chris Lattner0f65cad2007-04-09 05:49:22 +000012273 return std::make_pair(0U, X86::GR8RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012274 if (VT == MVT::i16)
Chris Lattner1fa71982008-10-17 18:15:05 +000012275 return std::make_pair(0U, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012276 if (VT == MVT::i32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +000012277 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +000012278 return std::make_pair(0U, X86::GR64RegisterClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000012279 case 'R': // LEGACY_REGS
12280 if (VT == MVT::i8)
12281 return std::make_pair(0U, X86::GR8_NOREXRegisterClass);
12282 if (VT == MVT::i16)
12283 return std::make_pair(0U, X86::GR16_NOREXRegisterClass);
12284 if (VT == MVT::i32 || !Subtarget->is64Bit())
12285 return std::make_pair(0U, X86::GR32_NOREXRegisterClass);
12286 return std::make_pair(0U, X86::GR64_NOREXRegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000012287 case 'f': // FP Stack registers.
12288 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
12289 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000012290 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000012291 return std::make_pair(0U, X86::RFP32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012292 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000012293 return std::make_pair(0U, X86::RFP64RegisterClass);
12294 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000012295 case 'y': // MMX_REGS if MMX allowed.
12296 if (!Subtarget->hasMMX()) break;
12297 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000012298 case 'Y': // SSE_REGS if SSE2 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012299 if (!Subtarget->hasXMMInt()) break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000012300 // FALL THROUGH.
12301 case 'x': // SSE_REGS if SSE1 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012302 if (!Subtarget->hasXMM()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000012303
Owen Anderson825b72b2009-08-11 20:47:22 +000012304 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000012305 default: break;
12306 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000012307 case MVT::f32:
12308 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +000012309 return std::make_pair(0U, X86::FR32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012310 case MVT::f64:
12311 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +000012312 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000012313 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000012314 case MVT::v16i8:
12315 case MVT::v8i16:
12316 case MVT::v4i32:
12317 case MVT::v2i64:
12318 case MVT::v4f32:
12319 case MVT::v2f64:
Chris Lattner0f65cad2007-04-09 05:49:22 +000012320 return std::make_pair(0U, X86::VR128RegisterClass);
12321 }
Chris Lattnerad043e82007-04-09 05:11:28 +000012322 break;
12323 }
12324 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012325
Chris Lattnerf76d1802006-07-31 23:26:50 +000012326 // Use the default implementation in TargetLowering to convert the register
12327 // constraint into a member of a register class.
12328 std::pair<unsigned, const TargetRegisterClass*> Res;
12329 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000012330
12331 // Not found as a standard register?
12332 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000012333 // Map st(0) -> st(7) -> ST0
12334 if (Constraint.size() == 7 && Constraint[0] == '{' &&
12335 tolower(Constraint[1]) == 's' &&
12336 tolower(Constraint[2]) == 't' &&
12337 Constraint[3] == '(' &&
12338 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
12339 Constraint[5] == ')' &&
12340 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000012341
Chris Lattner56d77c72009-09-13 22:41:48 +000012342 Res.first = X86::ST0+Constraint[4]-'0';
12343 Res.second = X86::RFP80RegisterClass;
12344 return Res;
12345 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000012346
Chris Lattner56d77c72009-09-13 22:41:48 +000012347 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000012348 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000012349 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +000012350 Res.second = X86::RFP80RegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000012351 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000012352 }
Chris Lattner56d77c72009-09-13 22:41:48 +000012353
12354 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000012355 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000012356 Res.first = X86::EFLAGS;
12357 Res.second = X86::CCRRegisterClass;
12358 return Res;
12359 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000012360
Dale Johannesen330169f2008-11-13 21:52:36 +000012361 // 'A' means EAX + EDX.
12362 if (Constraint == "A") {
12363 Res.first = X86::EAX;
Dan Gohman68a31c22009-07-30 17:02:08 +000012364 Res.second = X86::GR32_ADRegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000012365 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000012366 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000012367 return Res;
12368 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012369
Chris Lattnerf76d1802006-07-31 23:26:50 +000012370 // Otherwise, check to see if this is a register class of the wrong value
12371 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
12372 // turn into {ax},{dx}.
12373 if (Res.second->hasType(VT))
12374 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012375
Chris Lattnerf76d1802006-07-31 23:26:50 +000012376 // All of the single-register GCC register classes map their values onto
12377 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
12378 // really want an 8-bit or 32-bit register, map to the appropriate register
12379 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +000012380 if (Res.second == X86::GR16RegisterClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000012381 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000012382 unsigned DestReg = 0;
12383 switch (Res.first) {
12384 default: break;
12385 case X86::AX: DestReg = X86::AL; break;
12386 case X86::DX: DestReg = X86::DL; break;
12387 case X86::CX: DestReg = X86::CL; break;
12388 case X86::BX: DestReg = X86::BL; break;
12389 }
12390 if (DestReg) {
12391 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000012392 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000012393 }
Owen Anderson825b72b2009-08-11 20:47:22 +000012394 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000012395 unsigned DestReg = 0;
12396 switch (Res.first) {
12397 default: break;
12398 case X86::AX: DestReg = X86::EAX; break;
12399 case X86::DX: DestReg = X86::EDX; break;
12400 case X86::CX: DestReg = X86::ECX; break;
12401 case X86::BX: DestReg = X86::EBX; break;
12402 case X86::SI: DestReg = X86::ESI; break;
12403 case X86::DI: DestReg = X86::EDI; break;
12404 case X86::BP: DestReg = X86::EBP; break;
12405 case X86::SP: DestReg = X86::ESP; break;
12406 }
12407 if (DestReg) {
12408 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000012409 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000012410 }
Owen Anderson825b72b2009-08-11 20:47:22 +000012411 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000012412 unsigned DestReg = 0;
12413 switch (Res.first) {
12414 default: break;
12415 case X86::AX: DestReg = X86::RAX; break;
12416 case X86::DX: DestReg = X86::RDX; break;
12417 case X86::CX: DestReg = X86::RCX; break;
12418 case X86::BX: DestReg = X86::RBX; break;
12419 case X86::SI: DestReg = X86::RSI; break;
12420 case X86::DI: DestReg = X86::RDI; break;
12421 case X86::BP: DestReg = X86::RBP; break;
12422 case X86::SP: DestReg = X86::RSP; break;
12423 }
12424 if (DestReg) {
12425 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000012426 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000012427 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000012428 }
Chris Lattner6ba50a92008-08-26 06:19:02 +000012429 } else if (Res.second == X86::FR32RegisterClass ||
12430 Res.second == X86::FR64RegisterClass ||
12431 Res.second == X86::VR128RegisterClass) {
12432 // Handle references to XMM physical registers that got mapped into the
12433 // wrong class. This can happen with constraints like {xmm0} where the
12434 // target independent register mapper will just pick the first match it can
12435 // find, ignoring the required type.
Owen Anderson825b72b2009-08-11 20:47:22 +000012436 if (VT == MVT::f32)
Chris Lattner6ba50a92008-08-26 06:19:02 +000012437 Res.second = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +000012438 else if (VT == MVT::f64)
Chris Lattner6ba50a92008-08-26 06:19:02 +000012439 Res.second = X86::FR64RegisterClass;
12440 else if (X86::VR128RegisterClass->hasType(VT))
12441 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000012442 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012443
Chris Lattnerf76d1802006-07-31 23:26:50 +000012444 return Res;
12445}