blob: 9c9922cdf84a602fa3e6bc8d6e9cafdc9409c85b [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) {
Evan Cheng2bffee22011-02-01 01:14:13 +000064 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
65 bool is64Bit = Subtarget->is64Bit();
NAKAMURA Takumi27635382011-02-05 15:10:54 +000066
Evan Cheng2bffee22011-02-01 01:14:13 +000067 if (Subtarget->isTargetEnvMacho()) {
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
Evan Cheng2bffee22011-02-01 01:14:13 +000073 if (Subtarget->isTargetELF()) {
Chris Lattnere019ec12010-12-19 20:07:10 +000074 if (is64Bit)
75 return new X8664_ELFTargetObjectFile(TM);
76 return new X8632_ELFTargetObjectFile(TM);
77 }
Evan Cheng2bffee22011-02-01 01:14:13 +000078 if (Subtarget->isTargetCOFF() && !Subtarget->isTargetEnvMacho())
Chris Lattnere019ec12010-12-19 20:07:10 +000079 return new TargetLoweringObjectFileCOFF();
Eric Christopher62f35a22010-07-05 19:26:33 +000080 llvm_unreachable("unknown subtarget type");
Chris Lattnerf0144122009-07-28 03:13:23 +000081}
82
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +000083X86TargetLowering::X86TargetLowering(X86TargetMachine &TM)
Chris Lattnerf0144122009-07-28 03:13:23 +000084 : TargetLowering(TM, createTLOF(TM)) {
Evan Cheng559806f2006-01-27 08:10:46 +000085 Subtarget = &TM.getSubtarget<X86Subtarget>();
Nate Begeman2ea8ee72010-12-10 00:26:57 +000086 X86ScalarSSEf64 = Subtarget->hasXMMInt();
87 X86ScalarSSEf32 = Subtarget->hasXMM();
Evan Cheng25ab6902006-09-08 06:48:29 +000088 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000089
Anton Korobeynikov2365f512007-07-14 14:06:15 +000090 RegInfo = TM.getRegisterInfo();
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000091 TD = getTargetData();
Anton Korobeynikov2365f512007-07-14 14:06:15 +000092
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000093 // Set up the TargetLowering object.
Chris Lattnera34b3cf2010-12-19 20:03:11 +000094 static MVT IntVTs[] = { MVT::i8, MVT::i16, MVT::i32, MVT::i64 };
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000095
96 // X86 is weird, it always uses i8 for shift amounts and setcc results.
Owen Anderson825b72b2009-08-11 20:47:22 +000097 setShiftAmountType(MVT::i8);
Duncan Sands03228082008-11-23 15:47:28 +000098 setBooleanContents(ZeroOrOneBooleanContent);
Evan Cheng211ffa12010-05-19 20:19:50 +000099 setSchedulingPreference(Sched::RegPressure);
Evan Cheng25ab6902006-09-08 06:48:29 +0000100 setStackPointerRegisterToSaveRestore(X86StackPtr);
Evan Cheng714554d2006-03-16 21:47:42 +0000101
Michael J. Spencer92bf38c2010-10-10 23:11:06 +0000102 if (Subtarget->isTargetWindows() && !Subtarget->isTargetCygMing()) {
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000103 // Setup Windows compiler runtime calls.
104 setLibcallName(RTLIB::SDIV_I64, "_alldiv");
Michael J. Spencer335b8062010-10-11 05:29:15 +0000105 setLibcallName(RTLIB::UDIV_I64, "_aulldiv");
106 setLibcallName(RTLIB::FPTOUINT_F64_I64, "_ftol2");
Michael J. Spencer94f7eeb2010-10-19 07:32:52 +0000107 setLibcallName(RTLIB::FPTOUINT_F32_I64, "_ftol2");
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000108 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::X86_StdCall);
Michael J. Spencer335b8062010-10-11 05:29:15 +0000109 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::X86_StdCall);
Michael J. Spencer6dad10e2010-10-27 18:52:38 +0000110 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I64, CallingConv::C);
111 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I64, CallingConv::C);
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000112 }
113
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000114 if (Subtarget->isTargetDarwin()) {
Evan Chengdf57fa02006-03-17 20:31:41 +0000115 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000116 setUseUnderscoreSetJmp(false);
117 setUseUnderscoreLongJmp(false);
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000118 } else if (Subtarget->isTargetMingw()) {
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000119 // MS runtime is weird: it exports _setjmp, but longjmp!
120 setUseUnderscoreSetJmp(true);
121 setUseUnderscoreLongJmp(false);
122 } else {
123 setUseUnderscoreSetJmp(true);
124 setUseUnderscoreLongJmp(true);
125 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000126
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000127 // Set up the register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000128 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
Dan Gohman71edb242010-04-30 18:30:26 +0000129 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000130 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
Evan Cheng25ab6902006-09-08 06:48:29 +0000131 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000132 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000133
Owen Anderson825b72b2009-08-11 20:47:22 +0000134 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Evan Chengc5484282006-10-04 00:56:09 +0000135
Scott Michelfdc40a02009-02-17 22:15:04 +0000136 // We don't accept any truncstore of integer registers.
Owen Anderson825b72b2009-08-11 20:47:22 +0000137 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000138 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000139 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000140 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000141 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
142 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
Evan Cheng7f042682008-10-15 02:05:31 +0000143
144 // SETOEQ and SETUNE require checking two conditions.
Owen Anderson825b72b2009-08-11 20:47:22 +0000145 setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand);
146 setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand);
147 setCondCodeAction(ISD::SETOEQ, MVT::f80, Expand);
148 setCondCodeAction(ISD::SETUNE, MVT::f32, Expand);
149 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
150 setCondCodeAction(ISD::SETUNE, MVT::f80, Expand);
Chris Lattnerddf89562008-01-17 19:59:44 +0000151
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000152 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
153 // operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000154 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
155 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
156 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +0000157
Evan Cheng25ab6902006-09-08 06:48:29 +0000158 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000159 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
160 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
Eli Friedman948e95a2009-05-23 09:59:16 +0000161 } else if (!UseSoftFloat) {
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000162 // We have an algorithm for SSE2->double, and we turn this into a
163 // 64-bit FILD followed by conditional FADD for other targets.
164 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Eli Friedman948e95a2009-05-23 09:59:16 +0000165 // We have an algorithm for SSE2, and we turn this into a 64-bit
166 // FILD for other targets.
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000167 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000168 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000169
170 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
171 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000172 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
173 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000174
Devang Patel6a784892009-06-05 18:48:29 +0000175 if (!UseSoftFloat) {
Bill Wendling105be5a2009-03-13 08:41:47 +0000176 // SSE has no i16 to fp conversion, only i32
177 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000178 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000179 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000180 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000181 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000182 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
183 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000184 }
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000185 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000186 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
187 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Promote);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000188 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000189
Dale Johannesen73328d12007-09-19 23:55:34 +0000190 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
191 // are Legal, f80 is custom lowered.
Owen Anderson825b72b2009-08-11 20:47:22 +0000192 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
193 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Evan Cheng6dab0532006-01-30 08:02:57 +0000194
Evan Cheng02568ff2006-01-30 22:13:22 +0000195 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
196 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000197 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
198 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
Evan Cheng02568ff2006-01-30 22:13:22 +0000199
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000200 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000201 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000202 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000203 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000204 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000205 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
206 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000207 }
208
209 // Handle FP_TO_UINT by promoting the destination to a larger signed
210 // conversion.
Owen Anderson825b72b2009-08-11 20:47:22 +0000211 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
212 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
213 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000214
Evan Cheng25ab6902006-09-08 06:48:29 +0000215 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000216 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
217 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Eli Friedman948e95a2009-05-23 09:59:16 +0000218 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000219 if (X86ScalarSSEf32 && !Subtarget->hasSSE3())
Evan Cheng25ab6902006-09-08 06:48:29 +0000220 // Expand FP_TO_UINT into a select.
221 // FIXME: We would like to use a Custom expander here eventually to do
222 // the optimal thing for SSE vs. the default expansion in the legalizer.
Owen Anderson825b72b2009-08-11 20:47:22 +0000223 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000224 else
Eli Friedman948e95a2009-05-23 09:59:16 +0000225 // With SSE3 we can use fisttpll to convert to a signed i64; without
226 // SSE, we're stuck with a fistpll.
Owen Anderson825b72b2009-08-11 20:47:22 +0000227 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000228 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000229
Chris Lattner399610a2006-12-05 18:22:22 +0000230 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Michael J. Spencerec38de22010-10-10 22:04:20 +0000231 if (!X86ScalarSSEf64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000232 setOperationAction(ISD::BITCAST , MVT::f32 , Expand);
233 setOperationAction(ISD::BITCAST , MVT::i32 , Expand);
Dale Johannesene39859a2010-05-21 18:40:15 +0000234 if (Subtarget->is64Bit()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000235 setOperationAction(ISD::BITCAST , MVT::f64 , Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000236 // Without SSE, i64->f64 goes through memory.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000237 setOperationAction(ISD::BITCAST , MVT::i64 , Expand);
Dale Johannesen7d07b482010-05-21 00:52:33 +0000238 }
Chris Lattnerf3597a12006-12-05 18:45:06 +0000239 }
Chris Lattner21f66852005-12-23 05:15:23 +0000240
Dan Gohmanb00ee212008-02-18 19:34:53 +0000241 // Scalar integer divide and remainder are lowered to use operations that
242 // produce two results, to match the available instructions. This exposes
243 // the two-result form to trivial CSE, which is able to combine x/y and x%y
244 // into a single instruction.
245 //
246 // Scalar integer multiply-high is also lowered to use two-result
247 // operations, to match the available instructions. However, plain multiply
248 // (low) operations are left as Legal, as there are single-result
249 // instructions for this in x86. Using the two-result multiply instructions
250 // when both high and low results are needed must be arranged by dagcombine.
Chris Lattnere019ec12010-12-19 20:07:10 +0000251 for (unsigned i = 0, e = 4; i != e; ++i) {
252 MVT VT = IntVTs[i];
253 setOperationAction(ISD::MULHS, VT, Expand);
254 setOperationAction(ISD::MULHU, VT, Expand);
255 setOperationAction(ISD::SDIV, VT, Expand);
256 setOperationAction(ISD::UDIV, VT, Expand);
257 setOperationAction(ISD::SREM, VT, Expand);
258 setOperationAction(ISD::UREM, VT, Expand);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000259
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000260 // Add/Sub overflow ops with MVT::Glues are lowered to EFLAGS dependences.
Chris Lattnerd8ff7ec2010-12-20 01:03:27 +0000261 setOperationAction(ISD::ADDC, VT, Custom);
262 setOperationAction(ISD::ADDE, VT, Custom);
263 setOperationAction(ISD::SUBC, VT, Custom);
264 setOperationAction(ISD::SUBE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000265 }
Dan Gohmana37c9f72007-09-25 18:23:27 +0000266
Owen Anderson825b72b2009-08-11 20:47:22 +0000267 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
268 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
269 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
270 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000271 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000272 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
273 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
274 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
275 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
276 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
277 setOperationAction(ISD::FREM , MVT::f32 , Expand);
278 setOperationAction(ISD::FREM , MVT::f64 , Expand);
279 setOperationAction(ISD::FREM , MVT::f80 , Expand);
280 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000281
Owen Anderson825b72b2009-08-11 20:47:22 +0000282 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
283 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000284 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
285 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000286 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
287 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000288 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000289 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
290 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000291 }
292
Benjamin Kramer1292c222010-12-04 20:32:23 +0000293 if (Subtarget->hasPOPCNT()) {
294 setOperationAction(ISD::CTPOP , MVT::i8 , Promote);
295 } else {
296 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
297 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
298 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
299 if (Subtarget->is64Bit())
300 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
301 }
302
Owen Anderson825b72b2009-08-11 20:47:22 +0000303 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
304 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000305
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000306 // These should be promoted to a larger select which is supported.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000307 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000308 // X86 wants to expand cmov itself.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000309 setOperationAction(ISD::SELECT , MVT::i8 , Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000310 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000311 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
312 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
313 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
314 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
315 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000316 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000317 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
318 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
319 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
320 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000321 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000322 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
323 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000324 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000325 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000326
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000327 // Darwin ABI issue.
Owen Anderson825b72b2009-08-11 20:47:22 +0000328 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
329 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
330 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
331 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000332 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000333 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
334 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000335 setOperationAction(ISD::BlockAddress , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000336 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000337 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
338 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
339 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
340 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000341 setOperationAction(ISD::BlockAddress , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000342 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000343 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Owen Anderson825b72b2009-08-11 20:47:22 +0000344 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
345 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
346 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000347 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000348 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
349 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
350 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000351 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000352
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000353 if (Subtarget->hasXMM())
Owen Anderson825b72b2009-08-11 20:47:22 +0000354 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Cheng27b7db52008-03-08 00:58:38 +0000355
Eric Christopher9a9d2752010-07-22 02:48:34 +0000356 // We may not have a libcall for MEMBARRIER so we should lower this.
357 setOperationAction(ISD::MEMBARRIER , MVT::Other, Custom);
Michael J. Spencerec38de22010-10-10 22:04:20 +0000358
Jim Grosbachf1ab49e2010-06-23 16:25:07 +0000359 // On X86 and X86-64, atomic operations are lowered to locked instructions.
360 // Locked instructions, in turn, have implicit fence semantics (all memory
361 // operations are flushed before issuing the locked instruction, and they
362 // are not buffered), so we can fold away the common pattern of
363 // fence-atomic-fence.
364 setShouldFoldAtomicFences(true);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000365
Mon P Wang63307c32008-05-05 19:05:59 +0000366 // Expand certain atomics
Chris Lattnere019ec12010-12-19 20:07:10 +0000367 for (unsigned i = 0, e = 4; i != e; ++i) {
368 MVT VT = IntVTs[i];
369 setOperationAction(ISD::ATOMIC_CMP_SWAP, VT, Custom);
370 setOperationAction(ISD::ATOMIC_LOAD_SUB, VT, Custom);
371 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000372
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000373 if (!Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000374 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
375 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
376 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
377 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
378 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
379 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
380 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000381 }
382
Evan Cheng3c992d22006-03-07 02:02:57 +0000383 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000384 if (!Subtarget->isTargetDarwin() &&
385 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000386 !Subtarget->isTargetCygMing()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000387 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Dan Gohman44066042008-07-01 00:05:16 +0000388 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000389
Owen Anderson825b72b2009-08-11 20:47:22 +0000390 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
391 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
392 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
393 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000394 if (Subtarget->is64Bit()) {
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000395 setExceptionPointerRegister(X86::RAX);
396 setExceptionSelectorRegister(X86::RDX);
397 } else {
398 setExceptionPointerRegister(X86::EAX);
399 setExceptionSelectorRegister(X86::EDX);
400 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000401 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
402 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000403
Owen Anderson825b72b2009-08-11 20:47:22 +0000404 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000405
Owen Anderson825b72b2009-08-11 20:47:22 +0000406 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000407
Nate Begemanacc398c2006-01-25 18:21:52 +0000408 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson825b72b2009-08-11 20:47:22 +0000409 setOperationAction(ISD::VASTART , MVT::Other, Custom);
410 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000411 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000412 setOperationAction(ISD::VAARG , MVT::Other, Custom);
413 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000414 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000415 setOperationAction(ISD::VAARG , MVT::Other, Expand);
416 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000417 }
Evan Chengae642192007-03-02 23:16:35 +0000418
Owen Anderson825b72b2009-08-11 20:47:22 +0000419 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
420 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000421 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000422 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Michael J. Spencere9c253e2010-10-21 01:41:01 +0000423 if (Subtarget->isTargetCygMing() || Subtarget->isTargetWindows())
Owen Anderson825b72b2009-08-11 20:47:22 +0000424 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +0000425 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000426 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000427
Evan Chengc7ce29b2009-02-13 22:36:38 +0000428 if (!UseSoftFloat && X86ScalarSSEf64) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000429 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000430 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000431 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
432 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000433
Evan Cheng223547a2006-01-31 22:28:30 +0000434 // Use ANDPD to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000435 setOperationAction(ISD::FABS , MVT::f64, Custom);
436 setOperationAction(ISD::FABS , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000437
438 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000439 setOperationAction(ISD::FNEG , MVT::f64, Custom);
440 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000441
Evan Cheng68c47cb2007-01-05 07:55:56 +0000442 // Use ANDPD and ORPD to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000443 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
444 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000445
Evan Chengd25e9e82006-02-02 00:28:23 +0000446 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000447 setOperationAction(ISD::FSIN , MVT::f64, Expand);
448 setOperationAction(ISD::FCOS , MVT::f64, Expand);
449 setOperationAction(ISD::FSIN , MVT::f32, Expand);
450 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000451
Chris Lattnera54aa942006-01-29 06:26:08 +0000452 // Expand FP immediates into loads from the stack, except for the special
453 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000454 addLegalFPImmediate(APFloat(+0.0)); // xorpd
455 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Evan Chengc7ce29b2009-02-13 22:36:38 +0000456 } else if (!UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000457 // Use SSE for f32, x87 for f64.
458 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000459 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
460 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000461
462 // Use ANDPS to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000463 setOperationAction(ISD::FABS , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000464
465 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000466 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000467
Owen Anderson825b72b2009-08-11 20:47:22 +0000468 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000469
470 // Use ANDPS and ORPS to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000471 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
472 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000473
474 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000475 setOperationAction(ISD::FSIN , MVT::f32, Expand);
476 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000477
Nate Begemane1795842008-02-14 08:57:00 +0000478 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000479 addLegalFPImmediate(APFloat(+0.0f)); // xorps
480 addLegalFPImmediate(APFloat(+0.0)); // FLD0
481 addLegalFPImmediate(APFloat(+1.0)); // FLD1
482 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
483 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
484
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000485 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000486 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
487 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000488 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000489 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000490 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000491 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000492 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
493 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000494
Owen Anderson825b72b2009-08-11 20:47:22 +0000495 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
496 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
497 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
498 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000499
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000500 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000501 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
502 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000503 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000504 addLegalFPImmediate(APFloat(+0.0)); // FLD0
505 addLegalFPImmediate(APFloat(+1.0)); // FLD1
506 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
507 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000508 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
509 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
510 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
511 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000512 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000513
Dale Johannesen59a58732007-08-05 18:49:15 +0000514 // Long double always uses X87.
Evan Cheng92722532009-03-26 23:06:32 +0000515 if (!UseSoftFloat) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000516 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
517 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
518 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000519 {
Benjamin Kramer98383962010-12-04 14:22:24 +0000520 APFloat TmpFlt = APFloat::getZero(APFloat::x87DoubleExtended);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000521 addLegalFPImmediate(TmpFlt); // FLD0
522 TmpFlt.changeSign();
523 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
Benjamin Kramer98383962010-12-04 14:22:24 +0000524
525 bool ignored;
Evan Chengc7ce29b2009-02-13 22:36:38 +0000526 APFloat TmpFlt2(+1.0);
527 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
528 &ignored);
529 addLegalFPImmediate(TmpFlt2); // FLD1
530 TmpFlt2.changeSign();
531 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
532 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000533
Evan Chengc7ce29b2009-02-13 22:36:38 +0000534 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000535 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
536 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000537 }
Dale Johannesen2f429012007-09-26 21:10:55 +0000538 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000539
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000540 // Always use a library call for pow.
Owen Anderson825b72b2009-08-11 20:47:22 +0000541 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
542 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
543 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000544
Owen Anderson825b72b2009-08-11 20:47:22 +0000545 setOperationAction(ISD::FLOG, MVT::f80, Expand);
546 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
547 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
548 setOperationAction(ISD::FEXP, MVT::f80, Expand);
549 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000550
Mon P Wangf007a8b2008-11-06 05:31:54 +0000551 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000552 // (for widening) or expand (for scalarization). Then we will selectively
553 // turn on ones that can be effectively codegen'd.
Owen Anderson825b72b2009-08-11 20:47:22 +0000554 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
555 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
556 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
557 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
558 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
559 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
560 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
561 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
562 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
563 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
564 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
565 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
566 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
567 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
568 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
569 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
570 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000571 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
David Greenecfe33c42011-01-26 19:13:22 +0000572 setOperationAction(ISD::EXTRACT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
573 setOperationAction(ISD::INSERT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000574 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
575 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
576 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
577 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
578 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
579 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
580 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
581 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
582 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
583 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
584 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
585 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
586 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
587 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
588 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
589 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
590 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
591 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
592 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
593 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
594 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
595 setOperationAction(ISD::VSETCC, (MVT::SimpleValueType)VT, Expand);
596 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
597 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
598 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
599 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
600 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
601 setOperationAction(ISD::FP_TO_UINT, (MVT::SimpleValueType)VT, Expand);
602 setOperationAction(ISD::FP_TO_SINT, (MVT::SimpleValueType)VT, Expand);
603 setOperationAction(ISD::UINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
604 setOperationAction(ISD::SINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
Dan Gohman87862e72009-12-11 21:31:27 +0000605 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,Expand);
Dan Gohman2e141d72009-12-14 23:40:38 +0000606 setOperationAction(ISD::TRUNCATE, (MVT::SimpleValueType)VT, Expand);
607 setOperationAction(ISD::SIGN_EXTEND, (MVT::SimpleValueType)VT, Expand);
608 setOperationAction(ISD::ZERO_EXTEND, (MVT::SimpleValueType)VT, Expand);
609 setOperationAction(ISD::ANY_EXTEND, (MVT::SimpleValueType)VT, Expand);
610 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
611 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
612 setTruncStoreAction((MVT::SimpleValueType)VT,
613 (MVT::SimpleValueType)InnerVT, Expand);
614 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
615 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
616 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000617 }
618
Evan Chengc7ce29b2009-02-13 22:36:38 +0000619 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
620 // with -msoft-float, disable use of MMX as well.
Chris Lattner2a786eb2010-12-19 20:19:20 +0000621 if (!UseSoftFloat && Subtarget->hasMMX()) {
Dale Johannesene93d99c2010-10-20 21:32:10 +0000622 addRegisterClass(MVT::x86mmx, X86::VR64RegisterClass);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000623 // No operations on x86mmx supported, everything uses intrinsics.
Evan Cheng470a6ad2006-02-22 02:26:30 +0000624 }
625
Dale Johannesen0488fb62010-09-30 23:57:10 +0000626 // MMX-sized vectors (other than x86mmx) are expected to be expanded
627 // into smaller operations.
628 setOperationAction(ISD::MULHS, MVT::v8i8, Expand);
629 setOperationAction(ISD::MULHS, MVT::v4i16, Expand);
630 setOperationAction(ISD::MULHS, MVT::v2i32, Expand);
631 setOperationAction(ISD::MULHS, MVT::v1i64, Expand);
632 setOperationAction(ISD::AND, MVT::v8i8, Expand);
633 setOperationAction(ISD::AND, MVT::v4i16, Expand);
634 setOperationAction(ISD::AND, MVT::v2i32, Expand);
635 setOperationAction(ISD::AND, MVT::v1i64, Expand);
636 setOperationAction(ISD::OR, MVT::v8i8, Expand);
637 setOperationAction(ISD::OR, MVT::v4i16, Expand);
638 setOperationAction(ISD::OR, MVT::v2i32, Expand);
639 setOperationAction(ISD::OR, MVT::v1i64, Expand);
640 setOperationAction(ISD::XOR, MVT::v8i8, Expand);
641 setOperationAction(ISD::XOR, MVT::v4i16, Expand);
642 setOperationAction(ISD::XOR, MVT::v2i32, Expand);
643 setOperationAction(ISD::XOR, MVT::v1i64, Expand);
644 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Expand);
645 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Expand);
646 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Expand);
647 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Expand);
648 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v1i64, Expand);
649 setOperationAction(ISD::SELECT, MVT::v8i8, Expand);
650 setOperationAction(ISD::SELECT, MVT::v4i16, Expand);
651 setOperationAction(ISD::SELECT, MVT::v2i32, Expand);
652 setOperationAction(ISD::SELECT, MVT::v1i64, Expand);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000653 setOperationAction(ISD::BITCAST, MVT::v8i8, Expand);
654 setOperationAction(ISD::BITCAST, MVT::v4i16, Expand);
655 setOperationAction(ISD::BITCAST, MVT::v2i32, Expand);
656 setOperationAction(ISD::BITCAST, MVT::v1i64, Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000657
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000658 if (!UseSoftFloat && Subtarget->hasXMM()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000659 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000660
Owen Anderson825b72b2009-08-11 20:47:22 +0000661 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
662 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
663 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
664 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
665 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
666 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
667 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
668 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
669 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
670 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
671 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
672 setOperationAction(ISD::VSETCC, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000673 }
674
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000675 if (!UseSoftFloat && Subtarget->hasXMMInt()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000676 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000677
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000678 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
679 // registers cannot be used even for integer operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000680 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
681 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
682 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
683 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000684
Owen Anderson825b72b2009-08-11 20:47:22 +0000685 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
686 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
687 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
688 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
689 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
690 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
691 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
692 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
693 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
694 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
695 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
696 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
697 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
698 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
699 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
700 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000701
Owen Anderson825b72b2009-08-11 20:47:22 +0000702 setOperationAction(ISD::VSETCC, MVT::v2f64, Custom);
703 setOperationAction(ISD::VSETCC, MVT::v16i8, Custom);
704 setOperationAction(ISD::VSETCC, MVT::v8i16, Custom);
705 setOperationAction(ISD::VSETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000706
Owen Anderson825b72b2009-08-11 20:47:22 +0000707 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
708 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
709 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
710 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
711 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000712
Mon P Wangeb38ebf2010-01-24 00:05:03 +0000713 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2f64, Custom);
714 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2i64, Custom);
715 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i8, Custom);
716 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i16, Custom);
717 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i32, Custom);
718
Evan Cheng2c3ae372006-04-12 21:21:57 +0000719 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Owen Anderson825b72b2009-08-11 20:47:22 +0000720 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
721 EVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000722 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000723 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000724 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000725 // Do not attempt to custom lower non-128-bit vectors
726 if (!VT.is128BitVector())
727 continue;
Owen Anderson825b72b2009-08-11 20:47:22 +0000728 setOperationAction(ISD::BUILD_VECTOR,
729 VT.getSimpleVT().SimpleTy, Custom);
730 setOperationAction(ISD::VECTOR_SHUFFLE,
731 VT.getSimpleVT().SimpleTy, Custom);
732 setOperationAction(ISD::EXTRACT_VECTOR_ELT,
733 VT.getSimpleVT().SimpleTy, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000734 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000735
Owen Anderson825b72b2009-08-11 20:47:22 +0000736 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
737 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
738 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
739 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
740 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
741 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000742
Nate Begemancdd1eec2008-02-12 22:51:28 +0000743 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000744 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
745 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000746 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000747
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000748 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Owen Anderson825b72b2009-08-11 20:47:22 +0000749 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; i++) {
750 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
Owen Andersone50ed302009-08-10 22:56:29 +0000751 EVT VT = SVT;
David Greene9b9838d2009-06-29 16:47:10 +0000752
753 // Do not attempt to promote non-128-bit vectors
Chris Lattner32b4b5a2010-07-05 05:53:14 +0000754 if (!VT.is128BitVector())
David Greene9b9838d2009-06-29 16:47:10 +0000755 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +0000756
Owen Andersond6662ad2009-08-10 20:46:15 +0000757 setOperationAction(ISD::AND, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000758 AddPromotedToType (ISD::AND, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000759 setOperationAction(ISD::OR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000760 AddPromotedToType (ISD::OR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000761 setOperationAction(ISD::XOR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000762 AddPromotedToType (ISD::XOR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000763 setOperationAction(ISD::LOAD, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000764 AddPromotedToType (ISD::LOAD, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000765 setOperationAction(ISD::SELECT, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000766 AddPromotedToType (ISD::SELECT, SVT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000767 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000768
Owen Anderson825b72b2009-08-11 20:47:22 +0000769 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000770
Evan Cheng2c3ae372006-04-12 21:21:57 +0000771 // Custom lower v2i64 and v2f64 selects.
Owen Anderson825b72b2009-08-11 20:47:22 +0000772 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
773 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
774 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
775 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000776
Owen Anderson825b72b2009-08-11 20:47:22 +0000777 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
778 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000779 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000780
Nate Begeman14d12ca2008-02-11 04:19:36 +0000781 if (Subtarget->hasSSE41()) {
Dale Johannesen54feef22010-05-27 20:12:41 +0000782 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
783 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
784 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
785 setOperationAction(ISD::FRINT, MVT::f32, Legal);
786 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
787 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
788 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
789 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
790 setOperationAction(ISD::FRINT, MVT::f64, Legal);
791 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
792
Nate Begeman14d12ca2008-02-11 04:19:36 +0000793 // FIXME: Do we need to handle scalar-to-vector here?
Owen Anderson825b72b2009-08-11 20:47:22 +0000794 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000795
Nate Begemanbdcb5af2010-07-27 22:37:06 +0000796 // Can turn SHL into an integer multiply.
797 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
Nate Begeman51409212010-07-28 00:21:48 +0000798 setOperationAction(ISD::SHL, MVT::v16i8, Custom);
Nate Begemanbdcb5af2010-07-27 22:37:06 +0000799
Nate Begeman14d12ca2008-02-11 04:19:36 +0000800 // i8 and i16 vectors are custom , because the source register and source
801 // source memory operand types are not the same width. f32 vectors are
802 // custom since the immediate controlling the insert encodes additional
803 // information.
Owen Anderson825b72b2009-08-11 20:47:22 +0000804 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
805 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
806 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
807 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000808
Owen Anderson825b72b2009-08-11 20:47:22 +0000809 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
810 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
811 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
812 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000813
814 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000815 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
816 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000817 }
818 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000819
Chris Lattnera34b3cf2010-12-19 20:03:11 +0000820 if (Subtarget->hasSSE42())
Owen Anderson825b72b2009-08-11 20:47:22 +0000821 setOperationAction(ISD::VSETCC, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000822
David Greene9b9838d2009-06-29 16:47:10 +0000823 if (!UseSoftFloat && Subtarget->hasAVX()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000824 addRegisterClass(MVT::v8f32, X86::VR256RegisterClass);
825 addRegisterClass(MVT::v4f64, X86::VR256RegisterClass);
826 addRegisterClass(MVT::v8i32, X86::VR256RegisterClass);
827 addRegisterClass(MVT::v4i64, X86::VR256RegisterClass);
Bruno Cardoso Lopes405f11b2010-08-10 01:43:16 +0000828 addRegisterClass(MVT::v32i8, X86::VR256RegisterClass);
David Greened94c1012009-06-29 22:50:51 +0000829
Owen Anderson825b72b2009-08-11 20:47:22 +0000830 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
831 setOperationAction(ISD::LOAD, MVT::v8i32, Legal);
832 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
833 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
David Greene54d8eba2011-01-27 22:38:56 +0000834
Owen Anderson825b72b2009-08-11 20:47:22 +0000835 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
836 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
837 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
838 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
839 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
840 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000841
Owen Anderson825b72b2009-08-11 20:47:22 +0000842 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
843 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
844 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
845 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
846 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
847 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000848
David Greene54d8eba2011-01-27 22:38:56 +0000849 // Custom lower build_vector, vector_shuffle, scalar_to_vector,
850 // insert_vector_elt extract_subvector and extract_vector_elt for
851 // 256-bit types.
852 for (unsigned i = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
853 i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE;
854 ++i) {
855 MVT::SimpleValueType VT = (MVT::SimpleValueType)i;
856 // Do not attempt to custom lower non-256-bit vectors
857 if (!isPowerOf2_32(MVT(VT).getVectorNumElements())
858 || (MVT(VT).getSizeInBits() < 256))
David Greene9b9838d2009-06-29 16:47:10 +0000859 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000860 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
861 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
David Greene54d8eba2011-01-27 22:38:56 +0000862 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000863 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
David Greene54d8eba2011-01-27 22:38:56 +0000864 setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000865 }
David Greene54d8eba2011-01-27 22:38:56 +0000866 // Custom-lower insert_subvector and extract_subvector based on
867 // the result type.
868 for (unsigned i = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
869 i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE;
870 ++i) {
871 MVT::SimpleValueType VT = (MVT::SimpleValueType)i;
872 // Do not attempt to custom lower non-256-bit vectors
873 if (!isPowerOf2_32(MVT(VT).getVectorNumElements()))
David Greene9b9838d2009-06-29 16:47:10 +0000874 continue;
David Greene54d8eba2011-01-27 22:38:56 +0000875
876 if (MVT(VT).getSizeInBits() == 128) {
877 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000878 }
David Greene54d8eba2011-01-27 22:38:56 +0000879 else if (MVT(VT).getSizeInBits() == 256) {
880 setOperationAction(ISD::INSERT_SUBVECTOR, VT, Custom);
881 }
David Greene9b9838d2009-06-29 16:47:10 +0000882 }
883
David Greene54d8eba2011-01-27 22:38:56 +0000884 // Promote v32i8, v16i16, v8i32 select, and, or, xor to v4i64.
885 // Don't promote loads because we need them for VPERM vector index versions.
886
887 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
888 VT != (unsigned)MVT::LAST_VECTOR_VALUETYPE;
889 VT++) {
890 if (!isPowerOf2_32(MVT((MVT::SimpleValueType)VT).getVectorNumElements())
891 || (MVT((MVT::SimpleValueType)VT).getSizeInBits() < 256))
892 continue;
893 setOperationAction(ISD::AND, (MVT::SimpleValueType)VT, Promote);
894 AddPromotedToType (ISD::AND, (MVT::SimpleValueType)VT, MVT::v4i64);
895 setOperationAction(ISD::OR, (MVT::SimpleValueType)VT, Promote);
896 AddPromotedToType (ISD::OR, (MVT::SimpleValueType)VT, MVT::v4i64);
897 setOperationAction(ISD::XOR, (MVT::SimpleValueType)VT, Promote);
898 AddPromotedToType (ISD::XOR, (MVT::SimpleValueType)VT, MVT::v4i64);
899 //setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Promote);
900 //AddPromotedToType (ISD::LOAD, (MVT::SimpleValueType)VT, MVT::v4i64);
901 setOperationAction(ISD::SELECT, (MVT::SimpleValueType)VT, Promote);
902 AddPromotedToType (ISD::SELECT, (MVT::SimpleValueType)VT, MVT::v4i64);
903 }
David Greene9b9838d2009-06-29 16:47:10 +0000904 }
905
Evan Cheng6be2c582006-04-05 23:38:46 +0000906 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000907 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +0000908
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000909
Eli Friedman962f5492010-06-02 19:35:46 +0000910 // Only custom-lower 64-bit SADDO and friends on 64-bit because we don't
911 // handle type legalization for these operations here.
Dan Gohman71c62a22010-06-02 19:13:40 +0000912 //
Eli Friedman962f5492010-06-02 19:35:46 +0000913 // FIXME: We really should do custom legalization for addition and
914 // subtraction on x86-32 once PR3203 is fixed. We really can't do much better
915 // than generic legalization for 64-bit multiplication-with-overflow, though.
Chris Lattnera34b3cf2010-12-19 20:03:11 +0000916 for (unsigned i = 0, e = 3+Subtarget->is64Bit(); i != e; ++i) {
917 // Add/Sub/Mul with overflow operations are custom lowered.
918 MVT VT = IntVTs[i];
919 setOperationAction(ISD::SADDO, VT, Custom);
920 setOperationAction(ISD::UADDO, VT, Custom);
921 setOperationAction(ISD::SSUBO, VT, Custom);
922 setOperationAction(ISD::USUBO, VT, Custom);
923 setOperationAction(ISD::SMULO, VT, Custom);
924 setOperationAction(ISD::UMULO, VT, Custom);
Eli Friedmana993f0a2010-06-02 00:27:18 +0000925 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000926
Chris Lattnera34b3cf2010-12-19 20:03:11 +0000927 // There are no 8-bit 3-address imul/mul instructions
928 setOperationAction(ISD::SMULO, MVT::i8, Expand);
929 setOperationAction(ISD::UMULO, MVT::i8, Expand);
Bill Wendling41ea7e72008-11-24 19:21:46 +0000930
Evan Chengd54f2d52009-03-31 19:38:51 +0000931 if (!Subtarget->is64Bit()) {
932 // These libcalls are not available in 32-bit.
933 setLibcallName(RTLIB::SHL_I128, 0);
934 setLibcallName(RTLIB::SRL_I128, 0);
935 setLibcallName(RTLIB::SRA_I128, 0);
936 }
937
Evan Cheng206ee9d2006-07-07 08:33:52 +0000938 // We have target-specific dag combine patterns for the following nodes:
939 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Dan Gohman1bbf72b2010-03-15 23:23:03 +0000940 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Evan Chengd880b972008-05-09 21:53:03 +0000941 setTargetDAGCombine(ISD::BUILD_VECTOR);
Chris Lattner83e6c992006-10-04 06:57:07 +0000942 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +0000943 setTargetDAGCombine(ISD::SHL);
944 setTargetDAGCombine(ISD::SRA);
945 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +0000946 setTargetDAGCombine(ISD::OR);
Nate Begemanb65c1752010-12-17 22:55:37 +0000947 setTargetDAGCombine(ISD::AND);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +0000948 setTargetDAGCombine(ISD::ADD);
949 setTargetDAGCombine(ISD::SUB);
Chris Lattner149a4e52008-02-22 02:09:43 +0000950 setTargetDAGCombine(ISD::STORE);
Evan Cheng2e489c42009-12-16 00:53:11 +0000951 setTargetDAGCombine(ISD::ZERO_EXTEND);
Evan Cheng0b0cd912009-03-28 05:57:29 +0000952 if (Subtarget->is64Bit())
953 setTargetDAGCombine(ISD::MUL);
Evan Cheng206ee9d2006-07-07 08:33:52 +0000954
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000955 computeRegisterProperties();
956
Evan Cheng05219282011-01-06 06:52:41 +0000957 // On Darwin, -Os means optimize for size without hurting performance,
958 // do not reduce the limit.
Dan Gohman87060f52008-06-30 21:00:56 +0000959 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +0000960 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 16 : 8;
Evan Cheng255f20f2010-04-01 06:04:33 +0000961 maxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +0000962 maxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
963 maxStoresPerMemmove = 8; // For @llvm.memmove -> sequence of stores
964 maxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Evan Chengfb8075d2008-02-28 00:43:03 +0000965 setPrefLoopAlignment(16);
Evan Cheng6ebf7bc2009-05-13 21:42:09 +0000966 benefitFromCodePlacementOpt = true;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000967}
968
Scott Michel5b8f82e2008-03-10 15:42:14 +0000969
Owen Anderson825b72b2009-08-11 20:47:22 +0000970MVT::SimpleValueType X86TargetLowering::getSetCCResultType(EVT VT) const {
971 return MVT::i8;
Scott Michel5b8f82e2008-03-10 15:42:14 +0000972}
973
974
Evan Cheng29286502008-01-23 23:17:41 +0000975/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
976/// the desired ByVal argument alignment.
977static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
978 if (MaxAlign == 16)
979 return;
980 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
981 if (VTy->getBitWidth() == 128)
982 MaxAlign = 16;
Evan Cheng29286502008-01-23 23:17:41 +0000983 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
984 unsigned EltAlign = 0;
985 getMaxByValAlign(ATy->getElementType(), EltAlign);
986 if (EltAlign > MaxAlign)
987 MaxAlign = EltAlign;
988 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
989 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
990 unsigned EltAlign = 0;
991 getMaxByValAlign(STy->getElementType(i), EltAlign);
992 if (EltAlign > MaxAlign)
993 MaxAlign = EltAlign;
994 if (MaxAlign == 16)
995 break;
996 }
997 }
998 return;
999}
1000
1001/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1002/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001003/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1004/// are at 4-byte boundaries.
Evan Cheng29286502008-01-23 23:17:41 +00001005unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001006 if (Subtarget->is64Bit()) {
1007 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001008 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001009 if (TyAlign > 8)
1010 return TyAlign;
1011 return 8;
1012 }
1013
Evan Cheng29286502008-01-23 23:17:41 +00001014 unsigned Align = 4;
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001015 if (Subtarget->hasXMM())
Dale Johannesen0c191872008-02-08 19:48:20 +00001016 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001017 return Align;
1018}
Chris Lattner2b02a442007-02-25 08:29:00 +00001019
Evan Chengf0df0312008-05-15 08:39:06 +00001020/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengc3b0c342010-04-08 07:37:57 +00001021/// and store operations as a result of memset, memcpy, and memmove
1022/// lowering. If DstAlign is zero that means it's safe to destination
1023/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1024/// means there isn't a need to check it against alignment requirement,
1025/// probably because the source does not need to be loaded. If
1026/// 'NonScalarIntSafe' is true, that means it's safe to return a
1027/// non-scalar-integer type, e.g. empty string source, constant, or loaded
1028/// from memory. 'MemcpyStrSrc' indicates whether the memcpy source is
1029/// constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001030/// It returns EVT::Other if the type should be determined using generic
1031/// target-independent logic.
Owen Andersone50ed302009-08-10 22:56:29 +00001032EVT
Evan Cheng255f20f2010-04-01 06:04:33 +00001033X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1034 unsigned DstAlign, unsigned SrcAlign,
Evan Chengf28f8bc2010-04-02 19:36:14 +00001035 bool NonScalarIntSafe,
Evan Chengc3b0c342010-04-08 07:37:57 +00001036 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00001037 MachineFunction &MF) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001038 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
1039 // linux. This is because the stack realignment code can't handle certain
1040 // cases like PR2962. This should be removed when PR2962 is fixed.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001041 const Function *F = MF.getFunction();
Evan Chenga5e13622011-01-07 19:35:30 +00001042 if (NonScalarIntSafe &&
1043 !F->hasFnAttr(Attribute::NoImplicitFloat)) {
Evan Cheng255f20f2010-04-01 06:04:33 +00001044 if (Size >= 16 &&
Evan Chenga5e13622011-01-07 19:35:30 +00001045 (Subtarget->isUnalignedMemAccessFast() ||
1046 ((DstAlign == 0 || DstAlign >= 16) &&
1047 (SrcAlign == 0 || SrcAlign >= 16))) &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001048 Subtarget->getStackAlignment() >= 16) {
1049 if (Subtarget->hasSSE2())
1050 return MVT::v4i32;
Evan Chengf28f8bc2010-04-02 19:36:14 +00001051 if (Subtarget->hasSSE1())
Evan Cheng255f20f2010-04-01 06:04:33 +00001052 return MVT::v4f32;
Evan Chengc3b0c342010-04-08 07:37:57 +00001053 } else if (!MemcpyStrSrc && Size >= 8 &&
Evan Cheng3ea97552010-04-01 20:27:45 +00001054 !Subtarget->is64Bit() &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001055 Subtarget->getStackAlignment() >= 8 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001056 Subtarget->hasXMMInt()) {
Evan Chengc3b0c342010-04-08 07:37:57 +00001057 // Do not use f64 to lower memcpy if source is string constant. It's
1058 // better to use i32 to avoid the loads.
Evan Cheng255f20f2010-04-01 06:04:33 +00001059 return MVT::f64;
Evan Chengc3b0c342010-04-08 07:37:57 +00001060 }
Chris Lattner4002a1b2008-10-28 05:49:35 +00001061 }
Evan Chengf0df0312008-05-15 08:39:06 +00001062 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001063 return MVT::i64;
1064 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001065}
1066
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001067/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1068/// current function. The returned value is a member of the
1069/// MachineJumpTableInfo::JTEntryKind enum.
1070unsigned X86TargetLowering::getJumpTableEncoding() const {
1071 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1072 // symbol.
1073 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1074 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001075 return MachineJumpTableInfo::EK_Custom32;
Michael J. Spencerec38de22010-10-10 22:04:20 +00001076
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001077 // Otherwise, use the normal jump table encoding heuristics.
1078 return TargetLowering::getJumpTableEncoding();
1079}
1080
Chris Lattnerc64daab2010-01-26 05:02:42 +00001081const MCExpr *
1082X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1083 const MachineBasicBlock *MBB,
1084 unsigned uid,MCContext &Ctx) const{
1085 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1086 Subtarget->isPICStyleGOT());
1087 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1088 // entries.
Daniel Dunbar4e815f82010-03-15 23:51:06 +00001089 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1090 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001091}
1092
Evan Chengcc415862007-11-09 01:32:10 +00001093/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1094/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001095SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001096 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001097 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001098 // This doesn't have DebugLoc associated with it, but is not really the
1099 // same as a Register.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001100 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001101 return Table;
1102}
1103
Chris Lattner589c6f62010-01-26 06:28:43 +00001104/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1105/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1106/// MCExpr.
1107const MCExpr *X86TargetLowering::
1108getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1109 MCContext &Ctx) const {
1110 // X86-64 uses RIP relative addressing based on the jump table label.
1111 if (Subtarget->isPICStyleRIPRel())
1112 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1113
1114 // Otherwise, the reference is relative to the PIC base.
Chris Lattner142b5312010-11-14 22:48:15 +00001115 return MCSymbolRefExpr::Create(MF->getPICBaseSymbol(), Ctx);
Chris Lattner589c6f62010-01-26 06:28:43 +00001116}
1117
Bill Wendlingb4202b82009-07-01 18:50:55 +00001118/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +00001119unsigned X86TargetLowering::getFunctionAlignment(const Function *F) const {
Dan Gohman25103a22009-08-18 00:20:06 +00001120 return F->hasFnAttr(Attribute::OptimizeForSize) ? 0 : 4;
Bill Wendling20c568f2009-06-30 22:38:32 +00001121}
1122
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001123// FIXME: Why this routine is here? Move to RegInfo!
Evan Chengdee81012010-07-26 21:50:05 +00001124std::pair<const TargetRegisterClass*, uint8_t>
1125X86TargetLowering::findRepresentativeClass(EVT VT) const{
1126 const TargetRegisterClass *RRC = 0;
1127 uint8_t Cost = 1;
1128 switch (VT.getSimpleVT().SimpleTy) {
1129 default:
1130 return TargetLowering::findRepresentativeClass(VT);
1131 case MVT::i8: case MVT::i16: case MVT::i32: case MVT::i64:
1132 RRC = (Subtarget->is64Bit()
1133 ? X86::GR64RegisterClass : X86::GR32RegisterClass);
1134 break;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001135 case MVT::x86mmx:
Evan Chengdee81012010-07-26 21:50:05 +00001136 RRC = X86::VR64RegisterClass;
1137 break;
1138 case MVT::f32: case MVT::f64:
1139 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1140 case MVT::v4f32: case MVT::v2f64:
1141 case MVT::v32i8: case MVT::v8i32: case MVT::v4i64: case MVT::v8f32:
1142 case MVT::v4f64:
1143 RRC = X86::VR128RegisterClass;
1144 break;
1145 }
1146 return std::make_pair(RRC, Cost);
1147}
1148
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001149// FIXME: Why this routine is here? Move to RegInfo!
Evan Cheng70017e42010-07-24 00:39:05 +00001150unsigned
1151X86TargetLowering::getRegPressureLimit(const TargetRegisterClass *RC,
1152 MachineFunction &MF) const {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001153 const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
Anton Korobeynikovd0c38172010-11-18 21:19:35 +00001154
1155 unsigned FPDiff = TFI->hasFP(MF) ? 1 : 0;
Evan Cheng70017e42010-07-24 00:39:05 +00001156 switch (RC->getID()) {
1157 default:
1158 return 0;
1159 case X86::GR32RegClassID:
1160 return 4 - FPDiff;
1161 case X86::GR64RegClassID:
1162 return 8 - FPDiff;
1163 case X86::VR128RegClassID:
1164 return Subtarget->is64Bit() ? 10 : 4;
1165 case X86::VR64RegClassID:
1166 return 4;
1167 }
1168}
1169
Eric Christopherf7a0c7b2010-07-06 05:18:56 +00001170bool X86TargetLowering::getStackCookieLocation(unsigned &AddressSpace,
1171 unsigned &Offset) const {
1172 if (!Subtarget->isTargetLinux())
1173 return false;
1174
1175 if (Subtarget->is64Bit()) {
1176 // %fs:0x28, unless we're using a Kernel code model, in which case it's %gs:
1177 Offset = 0x28;
1178 if (getTargetMachine().getCodeModel() == CodeModel::Kernel)
1179 AddressSpace = 256;
1180 else
1181 AddressSpace = 257;
1182 } else {
1183 // %gs:0x14 on i386
1184 Offset = 0x14;
1185 AddressSpace = 256;
1186 }
1187 return true;
1188}
1189
1190
Chris Lattner2b02a442007-02-25 08:29:00 +00001191//===----------------------------------------------------------------------===//
1192// Return Value Calling Convention Implementation
1193//===----------------------------------------------------------------------===//
1194
Chris Lattner59ed56b2007-02-28 04:55:35 +00001195#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001196
Michael J. Spencerec38de22010-10-10 22:04:20 +00001197bool
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001198X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv, bool isVarArg,
Dan Gohman84023e02010-07-10 09:00:22 +00001199 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001200 LLVMContext &Context) const {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001201 SmallVector<CCValAssign, 16> RVLocs;
1202 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001203 RVLocs, Context);
Dan Gohman84023e02010-07-10 09:00:22 +00001204 return CCInfo.CheckReturn(Outs, RetCC_X86);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001205}
1206
Dan Gohman98ca4f22009-08-05 01:29:28 +00001207SDValue
1208X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001209 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001210 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001211 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001212 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001213 MachineFunction &MF = DAG.getMachineFunction();
1214 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001215
Chris Lattner9774c912007-02-27 05:28:59 +00001216 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001217 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1218 RVLocs, *DAG.getContext());
1219 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001220
Evan Chengdcea1632010-02-04 02:40:39 +00001221 // Add the regs to the liveout set for the function.
1222 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1223 for (unsigned i = 0; i != RVLocs.size(); ++i)
1224 if (RVLocs[i].isRegLoc() && !MRI.isLiveOut(RVLocs[i].getLocReg()))
1225 MRI.addLiveOut(RVLocs[i].getLocReg());
Scott Michelfdc40a02009-02-17 22:15:04 +00001226
Dan Gohman475871a2008-07-27 21:46:04 +00001227 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001228
Dan Gohman475871a2008-07-27 21:46:04 +00001229 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001230 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1231 // Operand #1 = Bytes To Pop
Dan Gohman1e93df62010-04-17 14:41:14 +00001232 RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1233 MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001234
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001235 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001236 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1237 CCValAssign &VA = RVLocs[i];
1238 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohmanc9403652010-07-07 15:54:55 +00001239 SDValue ValToCopy = OutVals[i];
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001240 EVT ValVT = ValToCopy.getValueType();
1241
Dale Johannesenc4510512010-09-24 19:05:48 +00001242 // If this is x86-64, and we disabled SSE, we can't return FP values,
1243 // or SSE or MMX vectors.
1244 if ((ValVT == MVT::f32 || ValVT == MVT::f64 ||
1245 VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001246 (Subtarget->is64Bit() && !Subtarget->hasXMM())) {
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001247 report_fatal_error("SSE register return with SSE disabled");
1248 }
1249 // Likewise we can't return F64 values with SSE1 only. gcc does so, but
1250 // llvm-gcc has never done it right and no one has noticed, so this
1251 // should be OK for now.
1252 if (ValVT == MVT::f64 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001253 (Subtarget->is64Bit() && !Subtarget->hasXMMInt()))
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001254 report_fatal_error("SSE2 register return with SSE2 disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001255
Chris Lattner447ff682008-03-11 03:23:40 +00001256 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1257 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001258 if (VA.getLocReg() == X86::ST0 ||
1259 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001260 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1261 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001262 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001263 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001264 RetOps.push_back(ValToCopy);
1265 // Don't emit a copytoreg.
1266 continue;
1267 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001268
Evan Cheng242b38b2009-02-23 09:03:22 +00001269 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1270 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001271 if (Subtarget->is64Bit()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001272 if (ValVT == MVT::x86mmx) {
Chris Lattner97a2a562010-08-26 05:24:29 +00001273 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001274 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ValToCopy);
Eric Christopher90eb4022010-07-22 00:26:08 +00001275 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
1276 ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001277 // If we don't have SSE2 available, convert to v4f32 so the generated
1278 // register is legal.
1279 if (!Subtarget->hasSSE2())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001280 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32,ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001281 }
Evan Cheng242b38b2009-02-23 09:03:22 +00001282 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001283 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00001284
Dale Johannesendd64c412009-02-04 00:33:20 +00001285 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001286 Flag = Chain.getValue(1);
1287 }
Dan Gohman61a92132008-04-21 23:59:07 +00001288
1289 // The x86-64 ABI for returning structs by value requires that we copy
1290 // the sret argument into %rax for the return. We saved the argument into
1291 // a virtual register in the entry block, so now we copy the value out
1292 // and into %rax.
1293 if (Subtarget->is64Bit() &&
1294 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1295 MachineFunction &MF = DAG.getMachineFunction();
1296 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1297 unsigned Reg = FuncInfo->getSRetReturnReg();
Michael J. Spencerec38de22010-10-10 22:04:20 +00001298 assert(Reg &&
Zhongxing Xuc2798a12010-05-26 08:10:02 +00001299 "SRetReturnReg should have been set in LowerFormalArguments().");
Dale Johannesendd64c412009-02-04 00:33:20 +00001300 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001301
Dale Johannesendd64c412009-02-04 00:33:20 +00001302 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001303 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001304
1305 // RAX now acts like a return value.
Evan Chengdcea1632010-02-04 02:40:39 +00001306 MRI.addLiveOut(X86::RAX);
Dan Gohman61a92132008-04-21 23:59:07 +00001307 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001308
Chris Lattner447ff682008-03-11 03:23:40 +00001309 RetOps[0] = Chain; // Update chain.
1310
1311 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001312 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001313 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001314
1315 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001316 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001317}
1318
Evan Cheng3d2125c2010-11-30 23:55:39 +00001319bool X86TargetLowering::isUsedByReturnOnly(SDNode *N) const {
1320 if (N->getNumValues() != 1)
1321 return false;
1322 if (!N->hasNUsesOfValue(1, 0))
1323 return false;
1324
1325 SDNode *Copy = *N->use_begin();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001326 if (Copy->getOpcode() != ISD::CopyToReg &&
1327 Copy->getOpcode() != ISD::FP_EXTEND)
Evan Cheng3d2125c2010-11-30 23:55:39 +00001328 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001329
1330 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001331 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001332 UI != UE; ++UI) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001333 if (UI->getOpcode() != X86ISD::RET_FLAG)
1334 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001335 HasRet = true;
1336 }
Evan Cheng3d2125c2010-11-30 23:55:39 +00001337
Evan Cheng1bf891a2010-12-01 22:59:46 +00001338 return HasRet;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001339}
1340
Dan Gohman98ca4f22009-08-05 01:29:28 +00001341/// LowerCallResult - Lower the result values of a call into the
1342/// appropriate copies out of appropriate physical registers.
1343///
1344SDValue
1345X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001346 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001347 const SmallVectorImpl<ISD::InputArg> &Ins,
1348 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001349 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001350
Chris Lattnere32bbf62007-02-28 07:09:55 +00001351 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001352 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001353 bool Is64Bit = Subtarget->is64Bit();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001354 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +00001355 RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001356 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001357
Chris Lattner3085e152007-02-25 08:59:22 +00001358 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001359 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001360 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001361 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001362
Torok Edwin3f142c32009-02-01 18:15:56 +00001363 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001364 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001365 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasXMM())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001366 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001367 }
1368
Evan Cheng79fb3b42009-02-20 20:43:02 +00001369 SDValue Val;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001370
1371 // If this is a call to a function that returns an fp value on the floating
1372 // point stack, we must guarantee the the value is popped from the stack, so
1373 // a CopyFromReg is not good enough - the copy instruction may be eliminated
1374 // if the return value is not used. We use the FpGET_ST0 instructions
1375 // instead.
1376 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1) {
1377 // If we prefer to use the value in xmm registers, copy it out as f80 and
1378 // use a truncate to move it from fp stack reg to xmm reg.
1379 if (isScalarFPTypeInSSEReg(VA.getValVT())) CopyVT = MVT::f80;
1380 bool isST0 = VA.getLocReg() == X86::ST0;
1381 unsigned Opc = 0;
1382 if (CopyVT == MVT::f32) Opc = isST0 ? X86::FpGET_ST0_32:X86::FpGET_ST1_32;
1383 if (CopyVT == MVT::f64) Opc = isST0 ? X86::FpGET_ST0_64:X86::FpGET_ST1_64;
1384 if (CopyVT == MVT::f80) Opc = isST0 ? X86::FpGET_ST0_80:X86::FpGET_ST1_80;
1385 SDValue Ops[] = { Chain, InFlag };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001386 Chain = SDValue(DAG.getMachineNode(Opc, dl, CopyVT, MVT::Other, MVT::Glue,
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001387 Ops, 2), 1);
1388 Val = Chain.getValue(0);
1389
1390 // Round the f80 to the right size, which also moves it to the appropriate
1391 // xmm register.
1392 if (CopyVT != VA.getValVT())
1393 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
1394 // This truncation won't change the value.
1395 DAG.getIntPtrConstant(1));
1396 } else if (Is64Bit && CopyVT.isVector() && CopyVT.getSizeInBits() == 64) {
Evan Cheng242b38b2009-02-23 09:03:22 +00001397 // For x86-64, MMX values are returned in XMM0 / XMM1 except for v1i64.
1398 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
1399 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001400 MVT::v2i64, InFlag).getValue(1);
Evan Cheng242b38b2009-02-23 09:03:22 +00001401 Val = Chain.getValue(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00001402 Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64,
1403 Val, DAG.getConstant(0, MVT::i64));
Evan Cheng242b38b2009-02-23 09:03:22 +00001404 } else {
1405 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001406 MVT::i64, InFlag).getValue(1);
Evan Cheng242b38b2009-02-23 09:03:22 +00001407 Val = Chain.getValue(0);
1408 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001409 Val = DAG.getNode(ISD::BITCAST, dl, CopyVT, Val);
Evan Cheng79fb3b42009-02-20 20:43:02 +00001410 } else {
1411 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1412 CopyVT, InFlag).getValue(1);
1413 Val = Chain.getValue(0);
1414 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001415 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001416 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001417 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001418
Dan Gohman98ca4f22009-08-05 01:29:28 +00001419 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001420}
1421
1422
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001423//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001424// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001425//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001426// StdCall calling convention seems to be standard for many Windows' API
1427// routines and around. It differs from C calling convention just a little:
1428// callee should clean up the stack, not caller. Symbols should be also
1429// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001430// For info on fast calling convention see Fast Calling Convention (tail call)
1431// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001432
Dan Gohman98ca4f22009-08-05 01:29:28 +00001433/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001434/// semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001435static bool CallIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
1436 if (Outs.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001437 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001438
Dan Gohman98ca4f22009-08-05 01:29:28 +00001439 return Outs[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001440}
1441
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001442/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001443/// return semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001444static bool
1445ArgsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
1446 if (Ins.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001447 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001448
Dan Gohman98ca4f22009-08-05 01:29:28 +00001449 return Ins[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001450}
1451
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001452/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1453/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001454/// the specific parameter attribute. The copy will be passed as a byval
1455/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001456static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001457CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001458 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1459 DebugLoc dl) {
Chris Lattnere72f2022010-09-21 05:40:29 +00001460 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Michael J. Spencerec38de22010-10-10 22:04:20 +00001461
Dale Johannesendd64c412009-02-04 00:33:20 +00001462 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Mon P Wang20adc9d2010-04-04 03:10:48 +00001463 /*isVolatile*/false, /*AlwaysInline=*/true,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001464 MachinePointerInfo(), MachinePointerInfo());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001465}
1466
Chris Lattner29689432010-03-11 00:22:57 +00001467/// IsTailCallConvention - Return true if the calling convention is one that
1468/// supports tail call optimization.
1469static bool IsTailCallConvention(CallingConv::ID CC) {
1470 return (CC == CallingConv::Fast || CC == CallingConv::GHC);
1471}
1472
Evan Cheng0c439eb2010-01-27 00:07:07 +00001473/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1474/// a tailcall target by changing its ABI.
1475static bool FuncIsMadeTailCallSafe(CallingConv::ID CC) {
Chris Lattner29689432010-03-11 00:22:57 +00001476 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001477}
1478
Dan Gohman98ca4f22009-08-05 01:29:28 +00001479SDValue
1480X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001481 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001482 const SmallVectorImpl<ISD::InputArg> &Ins,
1483 DebugLoc dl, SelectionDAG &DAG,
1484 const CCValAssign &VA,
1485 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00001486 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00001487 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001488 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Evan Cheng0c439eb2010-01-27 00:07:07 +00001489 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001490 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001491 EVT ValVT;
1492
1493 // If value is passed by pointer we have address passed instead of the value
1494 // itself.
1495 if (VA.getLocInfo() == CCValAssign::Indirect)
1496 ValVT = VA.getLocVT();
1497 else
1498 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001499
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001500 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001501 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001502 // In case of tail call optimization mark all arguments mutable. Since they
1503 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001504 if (Flags.isByVal()) {
1505 int FI = MFI->CreateFixedObject(Flags.getByValSize(),
Evan Chenged2ae132010-07-03 00:40:23 +00001506 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001507 return DAG.getFrameIndex(FI, getPointerTy());
1508 } else {
1509 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00001510 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001511 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1512 return DAG.getLoad(ValVT, dl, Chain, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001513 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00001514 false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001515 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001516}
1517
Dan Gohman475871a2008-07-27 21:46:04 +00001518SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001519X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001520 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001521 bool isVarArg,
1522 const SmallVectorImpl<ISD::InputArg> &Ins,
1523 DebugLoc dl,
1524 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001525 SmallVectorImpl<SDValue> &InVals)
1526 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00001527 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001528 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001529
Gordon Henriksen86737662008-01-05 16:56:59 +00001530 const Function* Fn = MF.getFunction();
1531 if (Fn->hasExternalLinkage() &&
1532 Subtarget->isTargetCygMing() &&
1533 Fn->getName() == "main")
1534 FuncInfo->setForceFramePointer(true);
1535
Evan Cheng1bc78042006-04-26 01:20:17 +00001536 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001537 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001538 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001539
Chris Lattner29689432010-03-11 00:22:57 +00001540 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1541 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001542
Chris Lattner638402b2007-02-28 07:00:42 +00001543 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001544 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001545 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1546 ArgLocs, *DAG.getContext());
Duncan Sands45907662010-10-31 13:21:44 +00001547 CCInfo.AnalyzeFormalArguments(Ins, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001548
Chris Lattnerf39f7712007-02-28 05:46:49 +00001549 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001550 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001551 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1552 CCValAssign &VA = ArgLocs[i];
1553 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1554 // places.
1555 assert(VA.getValNo() != LastVal &&
1556 "Don't support value assigned to multiple locs yet");
1557 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001558
Chris Lattnerf39f7712007-02-28 05:46:49 +00001559 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001560 EVT RegVT = VA.getLocVT();
Devang Patel8a84e442009-01-05 17:31:22 +00001561 TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001562 if (RegVT == MVT::i32)
Chris Lattnerf39f7712007-02-28 05:46:49 +00001563 RC = X86::GR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001564 else if (Is64Bit && RegVT == MVT::i64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001565 RC = X86::GR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001566 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001567 RC = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001568 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001569 RC = X86::FR64RegisterClass;
Bruno Cardoso Lopesac098352010-08-05 23:35:51 +00001570 else if (RegVT.isVector() && RegVT.getSizeInBits() == 256)
1571 RC = X86::VR256RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001572 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001573 RC = X86::VR128RegisterClass;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001574 else if (RegVT == MVT::x86mmx)
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001575 RC = X86::VR64RegisterClass;
1576 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001577 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001578
Devang Patele9a7ea62011-01-31 21:38:14 +00001579 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC, dl);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001580 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001581
Chris Lattnerf39f7712007-02-28 05:46:49 +00001582 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1583 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1584 // right size.
1585 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001586 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001587 DAG.getValueType(VA.getValVT()));
1588 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001589 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001590 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001591 else if (VA.getLocInfo() == CCValAssign::BCvt)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001592 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001593
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001594 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001595 // Handle MMX values passed in XMM regs.
1596 if (RegVT.isVector()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001597 ArgValue = DAG.getNode(X86ISD::MOVDQ2Q, dl, VA.getValVT(),
1598 ArgValue);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001599 } else
1600 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001601 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001602 } else {
1603 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001604 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001605 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001606
1607 // If value is passed via pointer - do a load.
1608 if (VA.getLocInfo() == CCValAssign::Indirect)
Chris Lattner51abfe42010-09-21 06:02:19 +00001609 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue,
1610 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001611
Dan Gohman98ca4f22009-08-05 01:29:28 +00001612 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001613 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001614
Dan Gohman61a92132008-04-21 23:59:07 +00001615 // The x86-64 ABI for returning structs by value requires that we copy
1616 // the sret argument into %rax for the return. Save the argument into
1617 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001618 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001619 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1620 unsigned Reg = FuncInfo->getSRetReturnReg();
1621 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001622 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001623 FuncInfo->setSRetReturnReg(Reg);
1624 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001625 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001626 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001627 }
1628
Chris Lattnerf39f7712007-02-28 05:46:49 +00001629 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00001630 // Align stack specially for tail calls.
1631 if (FuncIsMadeTailCallSafe(CallConv))
Gordon Henriksenae636f82008-01-03 16:47:34 +00001632 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001633
Evan Cheng1bc78042006-04-26 01:20:17 +00001634 // If the function takes variable number of arguments, make a frame index for
1635 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001636 if (isVarArg) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001637 if (!IsWin64 && (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
1638 CallConv != CallingConv::X86_ThisCall))) {
Jakob Stoklund Olesenb2eeed72010-07-29 17:42:27 +00001639 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,true));
Gordon Henriksen86737662008-01-05 16:56:59 +00001640 }
1641 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001642 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1643
1644 // FIXME: We should really autogenerate these arrays
1645 static const unsigned GPR64ArgRegsWin64[] = {
1646 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001647 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001648 static const unsigned GPR64ArgRegs64Bit[] = {
1649 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1650 };
1651 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001652 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1653 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1654 };
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001655 const unsigned *GPR64ArgRegs;
1656 unsigned NumXMMRegs = 0;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001657
1658 if (IsWin64) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001659 // The XMM registers which might contain var arg parameters are shadowed
1660 // in their paired GPR. So we only need to save the GPR to their home
1661 // slots.
1662 TotalNumIntRegs = 4;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001663 GPR64ArgRegs = GPR64ArgRegsWin64;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001664 } else {
1665 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1666 GPR64ArgRegs = GPR64ArgRegs64Bit;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001667
1668 NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs64Bit, TotalNumXMMRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001669 }
1670 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1671 TotalNumIntRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001672
Devang Patel578efa92009-06-05 21:57:13 +00001673 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001674 assert(!(NumXMMRegs && !Subtarget->hasXMM()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001675 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001676 assert(!(NumXMMRegs && UseSoftFloat && NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00001677 "SSE register cannot be used when SSE is disabled!");
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001678 if (UseSoftFloat || NoImplicitFloatOps || !Subtarget->hasXMM())
Torok Edwin3f142c32009-02-01 18:15:56 +00001679 // Kernel mode asks for SSE to be disabled, so don't push them
1680 // on the stack.
1681 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001682
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001683 if (IsWin64) {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001684 const TargetFrameLowering &TFI = *getTargetMachine().getFrameLowering();
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001685 // Get to the caller-allocated home save location. Add 8 to account
1686 // for the return address.
1687 int HomeOffset = TFI.getOffsetOfLocalArea() + 8;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001688 FuncInfo->setRegSaveFrameIndex(
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001689 MFI->CreateFixedObject(1, NumIntRegs * 8 + HomeOffset, false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001690 FuncInfo->setVarArgsFrameIndex(FuncInfo->getRegSaveFrameIndex());
1691 } else {
1692 // For X86-64, if there are vararg parameters that are passed via
1693 // registers, then we must store them to their spots on the stack so they
1694 // may be loaded by deferencing the result of va_next.
1695 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
1696 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
1697 FuncInfo->setRegSaveFrameIndex(
1698 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
Dan Gohman1e93df62010-04-17 14:41:14 +00001699 false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001700 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001701
Gordon Henriksen86737662008-01-05 16:56:59 +00001702 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001703 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00001704 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
1705 getPointerTy());
1706 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001707 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00001708 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
1709 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00001710 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
Devang Patele9a7ea62011-01-31 21:38:14 +00001711 X86::GR64RegisterClass, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00001712 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00001713 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001714 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001715 MachinePointerInfo::getFixedStack(
1716 FuncInfo->getRegSaveFrameIndex(), Offset),
1717 false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001718 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001719 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001720 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001721
Dan Gohmanface41a2009-08-16 21:24:25 +00001722 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
1723 // Now store the XMM (fp + vector) parameter registers.
1724 SmallVector<SDValue, 11> SaveXMMOps;
1725 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001726
Devang Patele9a7ea62011-01-31 21:38:14 +00001727 unsigned AL = MF.addLiveIn(X86::AL, X86::GR8RegisterClass, dl);
Dan Gohmanface41a2009-08-16 21:24:25 +00001728 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
1729 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001730
Dan Gohman1e93df62010-04-17 14:41:14 +00001731 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1732 FuncInfo->getRegSaveFrameIndex()));
1733 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1734 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00001735
Dan Gohmanface41a2009-08-16 21:24:25 +00001736 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001737 unsigned VReg = MF.addLiveIn(XMMArgRegs64Bit[NumXMMRegs],
Devang Patele9a7ea62011-01-31 21:38:14 +00001738 X86::VR128RegisterClass, dl);
Dan Gohmanface41a2009-08-16 21:24:25 +00001739 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
1740 SaveXMMOps.push_back(Val);
1741 }
1742 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
1743 MVT::Other,
1744 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00001745 }
Dan Gohmanface41a2009-08-16 21:24:25 +00001746
1747 if (!MemOps.empty())
1748 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1749 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001750 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001751 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001752
Gordon Henriksen86737662008-01-05 16:56:59 +00001753 // Some CCs need callee pop.
Dan Gohman4d3d6e12010-05-27 18:43:40 +00001754 if (Subtarget->IsCalleePop(isVarArg, CallConv)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001755 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001756 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00001757 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001758 // If this is an sret function, the return should pop the hidden pointer.
Chris Lattner29689432010-03-11 00:22:57 +00001759 if (!Is64Bit && !IsTailCallConvention(CallConv) && ArgsAreStructReturn(Ins))
Dan Gohman1e93df62010-04-17 14:41:14 +00001760 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001761 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001762
Gordon Henriksen86737662008-01-05 16:56:59 +00001763 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001764 // RegSaveFrameIndex is X86-64 only.
1765 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Anton Korobeynikovded05e32010-05-16 09:08:45 +00001766 if (CallConv == CallingConv::X86_FastCall ||
1767 CallConv == CallingConv::X86_ThisCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00001768 // fastcc functions can't have varargs.
1769 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00001770 }
Evan Cheng25caf632006-05-23 21:06:34 +00001771
Dan Gohman98ca4f22009-08-05 01:29:28 +00001772 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001773}
1774
Dan Gohman475871a2008-07-27 21:46:04 +00001775SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001776X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
1777 SDValue StackPtr, SDValue Arg,
1778 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00001779 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001780 ISD::ArgFlagsTy Flags) const {
Anton Korobeynikovc7c62bb2010-09-02 22:31:32 +00001781 const unsigned FirstStackArgOffset = (Subtarget->isTargetWin64() ? 32 : 0);
1782 unsigned LocMemOffset = FirstStackArgOffset + VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00001783 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00001784 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001785 if (Flags.isByVal())
Dale Johannesendd64c412009-02-04 00:33:20 +00001786 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001787
1788 return DAG.getStore(Chain, dl, Arg, PtrOff,
1789 MachinePointerInfo::getStack(LocMemOffset),
David Greene67c9d422010-02-15 16:53:33 +00001790 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00001791}
1792
Bill Wendling64e87322009-01-16 19:25:27 +00001793/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001794/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00001795SDValue
1796X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00001797 SDValue &OutRetAddr, SDValue Chain,
1798 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00001799 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001800 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00001801 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001802 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00001803
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001804 // Load the "old" Return address.
Chris Lattner51abfe42010-09-21 06:02:19 +00001805 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, MachinePointerInfo(),
1806 false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001807 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001808}
1809
1810/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1811/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00001812static SDValue
1813EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00001814 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00001815 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001816 // Store the return address to the appropriate stack slot.
1817 if (!FPDiff) return Chain;
1818 // Calculate the new stack slot for the return address.
1819 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00001820 int NewReturnAddrFI =
Evan Chenged2ae132010-07-03 00:40:23 +00001821 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false);
Owen Anderson825b72b2009-08-11 20:47:22 +00001822 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00001823 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001824 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00001825 MachinePointerInfo::getFixedStack(NewReturnAddrFI),
David Greene67c9d422010-02-15 16:53:33 +00001826 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001827 return Chain;
1828}
1829
Dan Gohman98ca4f22009-08-05 01:29:28 +00001830SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001831X86TargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001832 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001833 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001834 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001835 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001836 const SmallVectorImpl<ISD::InputArg> &Ins,
1837 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001838 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001839 MachineFunction &MF = DAG.getMachineFunction();
1840 bool Is64Bit = Subtarget->is64Bit();
1841 bool IsStructRet = CallIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00001842 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001843
Evan Cheng5f941932010-02-05 02:21:12 +00001844 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00001845 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00001846 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1847 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001848 Outs, OutVals, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00001849
1850 // Sibcalls are automatically detected tailcalls which do not require
1851 // ABI changes.
Dan Gohman1797ed52010-02-08 20:27:50 +00001852 if (!GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00001853 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00001854
1855 if (isTailCall)
1856 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00001857 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00001858
Chris Lattner29689432010-03-11 00:22:57 +00001859 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1860 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001861
Chris Lattner638402b2007-02-28 07:00:42 +00001862 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00001863 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001864 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1865 ArgLocs, *DAG.getContext());
Duncan Sands45907662010-10-31 13:21:44 +00001866 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001867
Chris Lattner423c5f42007-02-28 05:31:48 +00001868 // Get a count of how many bytes are to be pushed on the stack.
1869 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00001870 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00001871 // This is a sibcall. The memory operands are available in caller's
1872 // own caller's stack.
1873 NumBytes = 0;
Chris Lattner29689432010-03-11 00:22:57 +00001874 else if (GuaranteedTailCallOpt && IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00001875 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001876
Gordon Henriksen86737662008-01-05 16:56:59 +00001877 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00001878 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001879 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00001880 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00001881 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1882 FPDiff = NumBytesCallerPushed - NumBytes;
1883
1884 // Set the delta of movement of the returnaddr stackslot.
1885 // But only set if delta is greater than previous delta.
1886 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1887 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1888 }
1889
Evan Chengf22f9b32010-02-06 03:28:46 +00001890 if (!IsSibcall)
1891 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001892
Dan Gohman475871a2008-07-27 21:46:04 +00001893 SDValue RetAddrFrIdx;
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001894 // Load return adress for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00001895 if (isTailCall && FPDiff)
1896 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
1897 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00001898
Dan Gohman475871a2008-07-27 21:46:04 +00001899 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
1900 SmallVector<SDValue, 8> MemOpChains;
1901 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00001902
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001903 // Walk the register/memloc assignments, inserting copies/loads. In the case
1904 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00001905 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1906 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001907 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00001908 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00001909 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00001910 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00001911
Chris Lattner423c5f42007-02-28 05:31:48 +00001912 // Promote the value if needed.
1913 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001914 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00001915 case CCValAssign::Full: break;
1916 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001917 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001918 break;
1919 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001920 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001921 break;
1922 case CCValAssign::AExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001923 if (RegVT.isVector() && RegVT.getSizeInBits() == 128) {
1924 // Special case: passing MMX values in XMM registers.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001925 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00001926 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
1927 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001928 } else
1929 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
1930 break;
1931 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001932 Arg = DAG.getNode(ISD::BITCAST, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001933 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001934 case CCValAssign::Indirect: {
1935 // Store the argument.
1936 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00001937 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001938 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00001939 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00001940 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001941 Arg = SpillSlot;
1942 break;
1943 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00001944 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001945
Chris Lattner423c5f42007-02-28 05:31:48 +00001946 if (VA.isRegLoc()) {
1947 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00001948 if (isVarArg && Subtarget->isTargetWin64()) {
1949 // Win64 ABI requires argument XMM reg to be copied to the corresponding
1950 // shadow reg if callee is a varargs function.
1951 unsigned ShadowReg = 0;
1952 switch (VA.getLocReg()) {
1953 case X86::XMM0: ShadowReg = X86::RCX; break;
1954 case X86::XMM1: ShadowReg = X86::RDX; break;
1955 case X86::XMM2: ShadowReg = X86::R8; break;
1956 case X86::XMM3: ShadowReg = X86::R9; break;
1957 }
1958 if (ShadowReg)
1959 RegsToPass.push_back(std::make_pair(ShadowReg, Arg));
1960 }
Evan Chengf22f9b32010-02-06 03:28:46 +00001961 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00001962 assert(VA.isMemLoc());
1963 if (StackPtr.getNode() == 0)
1964 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
1965 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1966 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001967 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001968 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001969
Evan Cheng32fe1032006-05-25 00:59:30 +00001970 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001971 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001972 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001973
Evan Cheng347d5f72006-04-28 21:29:37 +00001974 // Build a sequence of copy-to-reg nodes chained together with token chain
1975 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001976 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001977 // Tail call byval lowering might overwrite argument registers so in case of
1978 // tail call optimization the copies to registers are lowered later.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001979 if (!isTailCall)
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001980 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001981 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00001982 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001983 InFlag = Chain.getValue(1);
1984 }
Gordon Henriksen86737662008-01-05 16:56:59 +00001985
Chris Lattner88e1fd52009-07-09 04:24:46 +00001986 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00001987 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1988 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001989 if (!isTailCall) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00001990 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
1991 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001992 DebugLoc(), getPointerTy()),
Chris Lattnerb133a0a2009-07-09 02:55:47 +00001993 InFlag);
1994 InFlag = Chain.getValue(1);
1995 } else {
1996 // If we are tail calling and generating PIC/GOT style code load the
1997 // address of the callee into ECX. The value in ecx is used as target of
1998 // the tail jump. This is done to circumvent the ebx/callee-saved problem
1999 // for tail calls on PIC/GOT architectures. Normally we would just put the
2000 // address of GOT into ebx and then call target@PLT. But for tail calls
2001 // ebx would be restored (since ebx is callee saved) before jumping to the
2002 // target@PLT.
2003
2004 // Note: The actual moving to ECX is done further down.
2005 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
2006 if (G && !G->getGlobal()->hasHiddenVisibility() &&
2007 !G->getGlobal()->hasProtectedVisibility())
2008 Callee = LowerGlobalAddress(Callee, DAG);
2009 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00002010 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002011 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002012 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002013
Nate Begemanc8ea6732010-07-21 20:49:52 +00002014 if (Is64Bit && isVarArg && !Subtarget->isTargetWin64()) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002015 // From AMD64 ABI document:
2016 // For calls that may call functions that use varargs or stdargs
2017 // (prototype-less calls or calls to functions containing ellipsis (...) in
2018 // the declaration) %al is used as hidden argument to specify the number
2019 // of SSE registers used. The contents of %al do not need to match exactly
2020 // the number of registers, but must be an ubound on the number of SSE
2021 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002022
Gordon Henriksen86737662008-01-05 16:56:59 +00002023 // Count the number of XMM registers allocated.
2024 static const unsigned XMMArgRegs[] = {
2025 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2026 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2027 };
2028 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00002029 assert((Subtarget->hasXMM() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00002030 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00002031
Dale Johannesendd64c412009-02-04 00:33:20 +00002032 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Owen Anderson825b72b2009-08-11 20:47:22 +00002033 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002034 InFlag = Chain.getValue(1);
2035 }
2036
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002037
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002038 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002039 if (isTailCall) {
2040 // Force all the incoming stack arguments to be loaded from the stack
2041 // before any new outgoing arguments are stored to the stack, because the
2042 // outgoing stack slots may alias the incoming argument stack slots, and
2043 // the alias isn't otherwise explicit. This is slightly more conservative
2044 // than necessary, because it means that each store effectively depends
2045 // on every argument instead of just those arguments it would clobber.
2046 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2047
Dan Gohman475871a2008-07-27 21:46:04 +00002048 SmallVector<SDValue, 8> MemOpChains2;
2049 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002050 int FI = 0;
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002051 // Do not flag preceeding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00002052 InFlag = SDValue();
Dan Gohman1797ed52010-02-08 20:27:50 +00002053 if (GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002054 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2055 CCValAssign &VA = ArgLocs[i];
2056 if (VA.isRegLoc())
2057 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002058 assert(VA.isMemLoc());
Dan Gohmanc9403652010-07-07 15:54:55 +00002059 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002060 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002061 // Create frame index.
2062 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002063 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002064 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002065 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002066
Duncan Sands276dcbd2008-03-21 09:14:45 +00002067 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002068 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002069 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002070 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00002071 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00002072 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002073 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002074
Dan Gohman98ca4f22009-08-05 01:29:28 +00002075 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2076 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002077 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002078 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002079 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002080 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002081 DAG.getStore(ArgChain, dl, Arg, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002082 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002083 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002084 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002085 }
2086 }
2087
2088 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002089 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002090 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002091
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002092 // Copy arguments to their registers.
2093 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002094 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002095 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002096 InFlag = Chain.getValue(1);
2097 }
Dan Gohman475871a2008-07-27 21:46:04 +00002098 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002099
Gordon Henriksen86737662008-01-05 16:56:59 +00002100 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002101 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00002102 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002103 }
2104
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002105 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2106 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2107 // In the 64-bit large code model, we have to make all calls
2108 // through a register, since the call instruction's 32-bit
2109 // pc-relative offset may not be large enough to hold the whole
2110 // address.
2111 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002112 // If the callee is a GlobalAddress node (quite common, every direct call
2113 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2114 // it.
2115
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002116 // We should use extra load for direct calls to dllimported functions in
2117 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002118 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002119 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002120 unsigned char OpFlags = 0;
Eric Christopherfd179292009-08-27 18:07:15 +00002121
Chris Lattner48a7d022009-07-09 05:02:21 +00002122 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2123 // external symbols most go through the PLT in PIC mode. If the symbol
2124 // has hidden or protected visibility, or if it is static or local, then
2125 // we don't need to use the PLT - we can directly call it.
2126 if (Subtarget->isTargetELF() &&
2127 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002128 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002129 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002130 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner80945782010-09-27 06:34:01 +00002131 (GV->isDeclaration() || GV->isWeakForLinker()) &&
2132 Subtarget->getDarwinVers() < 9) {
Chris Lattner74e726e2009-07-09 05:27:35 +00002133 // PC-relative references to external symbols should go through $stub,
2134 // unless we're building with the leopard linker or later, which
2135 // automatically synthesizes these stubs.
2136 OpFlags = X86II::MO_DARWIN_STUB;
2137 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002138
Devang Patel0d881da2010-07-06 22:08:15 +00002139 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002140 G->getOffset(), OpFlags);
2141 }
Bill Wendling056292f2008-09-16 21:48:12 +00002142 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002143 unsigned char OpFlags = 0;
2144
Evan Cheng1bf891a2010-12-01 22:59:46 +00002145 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to
2146 // external symbols should go through the PLT.
2147 if (Subtarget->isTargetELF() &&
2148 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
2149 OpFlags = X86II::MO_PLT;
2150 } else if (Subtarget->isPICStyleStubAny() &&
2151 Subtarget->getDarwinVers() < 9) {
2152 // PC-relative references to external symbols should go through $stub,
2153 // unless we're building with the leopard linker or later, which
2154 // automatically synthesizes these stubs.
2155 OpFlags = X86II::MO_DARWIN_STUB;
Chris Lattner74e726e2009-07-09 05:27:35 +00002156 }
Eric Christopherfd179292009-08-27 18:07:15 +00002157
Chris Lattner48a7d022009-07-09 05:02:21 +00002158 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2159 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002160 }
2161
Chris Lattnerd96d0722007-02-25 06:40:16 +00002162 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002163 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002164 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002165
Evan Chengf22f9b32010-02-06 03:28:46 +00002166 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002167 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2168 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002169 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002170 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002171
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002172 Ops.push_back(Chain);
2173 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002174
Dan Gohman98ca4f22009-08-05 01:29:28 +00002175 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002176 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002177
Gordon Henriksen86737662008-01-05 16:56:59 +00002178 // Add argument registers to the end of the list so that they are known live
2179 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002180 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2181 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2182 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002183
Evan Cheng586ccac2008-03-18 23:36:35 +00002184 // Add an implicit use GOT pointer in EBX.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002185 if (!isTailCall && Subtarget->isPICStyleGOT())
Evan Cheng586ccac2008-03-18 23:36:35 +00002186 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
2187
Anton Korobeynikov3a1e54a2010-08-17 21:06:07 +00002188 // Add an implicit use of AL for non-Windows x86 64-bit vararg functions.
2189 if (Is64Bit && isVarArg && !Subtarget->isTargetWin64())
Owen Anderson825b72b2009-08-11 20:47:22 +00002190 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
Evan Cheng586ccac2008-03-18 23:36:35 +00002191
Gabor Greifba36cb52008-08-28 21:40:38 +00002192 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002193 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002194
Dan Gohman98ca4f22009-08-05 01:29:28 +00002195 if (isTailCall) {
Dale Johannesen88004c22010-06-05 00:30:45 +00002196 // We used to do:
2197 //// If this is the first return lowered for this function, add the regs
2198 //// to the liveout set for the function.
2199 // This isn't right, although it's probably harmless on x86; liveouts
2200 // should be computed from returns not tail calls. Consider a void
2201 // function making a tail call to a function returning int.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002202 return DAG.getNode(X86ISD::TC_RETURN, dl,
2203 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002204 }
2205
Dale Johannesenace16102009-02-03 19:33:06 +00002206 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002207 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002208
Chris Lattner2d297092006-05-23 18:50:38 +00002209 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002210 unsigned NumBytesForCalleeToPush;
Dan Gohman4d3d6e12010-05-27 18:43:40 +00002211 if (Subtarget->IsCalleePop(isVarArg, CallConv))
Gordon Henriksen86737662008-01-05 16:56:59 +00002212 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Chris Lattner29689432010-03-11 00:22:57 +00002213 else if (!Is64Bit && !IsTailCallConvention(CallConv) && IsStructRet)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002214 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002215 // pops the hidden struct pointer, so we have to push it back.
2216 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002217 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002218 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002219 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002220
Gordon Henriksenae636f82008-01-03 16:47:34 +00002221 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002222 if (!IsSibcall) {
2223 Chain = DAG.getCALLSEQ_END(Chain,
2224 DAG.getIntPtrConstant(NumBytes, true),
2225 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2226 true),
2227 InFlag);
2228 InFlag = Chain.getValue(1);
2229 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002230
Chris Lattner3085e152007-02-25 08:59:22 +00002231 // Handle result values, copying them out of physregs into vregs that we
2232 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002233 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2234 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002235}
2236
Evan Cheng25ab6902006-09-08 06:48:29 +00002237
2238//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002239// Fast Calling Convention (tail call) implementation
2240//===----------------------------------------------------------------------===//
2241
2242// Like std call, callee cleans arguments, convention except that ECX is
2243// reserved for storing the tail called function address. Only 2 registers are
2244// free for argument passing (inreg). Tail call optimization is performed
2245// provided:
2246// * tailcallopt is enabled
2247// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002248// On X86_64 architecture with GOT-style position independent code only local
2249// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002250// To keep the stack aligned according to platform abi the function
2251// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2252// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002253// If a tail called function callee has more arguments than the caller the
2254// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002255// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002256// original REtADDR, but before the saved framepointer or the spilled registers
2257// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2258// stack layout:
2259// arg1
2260// arg2
2261// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002262// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002263// move area ]
2264// (possible EBP)
2265// ESI
2266// EDI
2267// local1 ..
2268
2269/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2270/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002271unsigned
2272X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2273 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002274 MachineFunction &MF = DAG.getMachineFunction();
2275 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002276 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002277 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002278 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002279 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002280 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002281 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2282 // Number smaller than 12 so just add the difference.
2283 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2284 } else {
2285 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002286 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002287 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002288 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002289 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002290}
2291
Evan Cheng5f941932010-02-05 02:21:12 +00002292/// MatchingStackOffset - Return true if the given stack call argument is
2293/// already available in the same position (relatively) of the caller's
2294/// incoming argument stack.
2295static
2296bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2297 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2298 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002299 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2300 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002301 if (Arg.getOpcode() == ISD::CopyFromReg) {
2302 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00002303 if (!TargetRegisterInfo::isVirtualRegister(VR))
Evan Cheng5f941932010-02-05 02:21:12 +00002304 return false;
2305 MachineInstr *Def = MRI->getVRegDef(VR);
2306 if (!Def)
2307 return false;
2308 if (!Flags.isByVal()) {
2309 if (!TII->isLoadFromStackSlot(Def, FI))
2310 return false;
2311 } else {
2312 unsigned Opcode = Def->getOpcode();
2313 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2314 Def->getOperand(1).isFI()) {
2315 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002316 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002317 } else
2318 return false;
2319 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002320 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2321 if (Flags.isByVal())
2322 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002323 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002324 // define @foo(%struct.X* %A) {
2325 // tail call @bar(%struct.X* byval %A)
2326 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002327 return false;
2328 SDValue Ptr = Ld->getBasePtr();
2329 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2330 if (!FINode)
2331 return false;
2332 FI = FINode->getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002333 } else
2334 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002335
Evan Cheng4cae1332010-03-05 08:38:04 +00002336 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002337 if (!MFI->isFixedObjectIndex(FI))
2338 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002339 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002340}
2341
Dan Gohman98ca4f22009-08-05 01:29:28 +00002342/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2343/// for tail call optimization. Targets which want to do tail call
2344/// optimization should implement this function.
2345bool
2346X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002347 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002348 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002349 bool isCalleeStructRet,
2350 bool isCallerStructRet,
Evan Chengb1712452010-01-27 06:25:16 +00002351 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002352 const SmallVectorImpl<SDValue> &OutVals,
Evan Chengb1712452010-01-27 06:25:16 +00002353 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002354 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002355 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002356 CalleeCC != CallingConv::C)
2357 return false;
2358
Evan Cheng7096ae42010-01-29 06:45:59 +00002359 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002360 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002361 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Cheng13617962010-04-30 01:12:32 +00002362 CallingConv::ID CallerCC = CallerF->getCallingConv();
2363 bool CCMatch = CallerCC == CalleeCC;
2364
Dan Gohman1797ed52010-02-08 20:27:50 +00002365 if (GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002366 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002367 return true;
2368 return false;
2369 }
2370
Dale Johannesen2f05cc02010-05-28 23:24:28 +00002371 // Look for obvious safe cases to perform tail call optimization that do not
2372 // require ABI changes. This is what gcc calls sibcall.
Evan Chengb2c92902010-02-02 02:22:50 +00002373
Evan Cheng2c12cb42010-03-26 16:26:03 +00002374 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2375 // emit a special epilogue.
2376 if (RegInfo->needsStackRealignment(MF))
2377 return false;
2378
Eric Christopher90eb4022010-07-22 00:26:08 +00002379 // Do not sibcall optimize vararg calls unless the call site is not passing
2380 // any arguments.
Evan Cheng3c262ee2010-03-26 02:13:13 +00002381 if (isVarArg && !Outs.empty())
Evan Cheng843bd692010-01-31 06:44:49 +00002382 return false;
2383
Evan Chenga375d472010-03-15 18:54:48 +00002384 // Also avoid sibcall optimization if either caller or callee uses struct
2385 // return semantics.
2386 if (isCalleeStructRet || isCallerStructRet)
2387 return false;
2388
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002389 // If the call result is in ST0 / ST1, it needs to be popped off the x87 stack.
2390 // Therefore if it's not used by the call it is not safe to optimize this into
2391 // a sibcall.
2392 bool Unused = false;
2393 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2394 if (!Ins[i].Used) {
2395 Unused = true;
2396 break;
2397 }
2398 }
2399 if (Unused) {
2400 SmallVector<CCValAssign, 16> RVLocs;
2401 CCState CCInfo(CalleeCC, false, getTargetMachine(),
2402 RVLocs, *DAG.getContext());
2403 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002404 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002405 CCValAssign &VA = RVLocs[i];
2406 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2407 return false;
2408 }
2409 }
2410
Evan Cheng13617962010-04-30 01:12:32 +00002411 // If the calling conventions do not match, then we'd better make sure the
2412 // results are returned in the same way as what the caller expects.
2413 if (!CCMatch) {
2414 SmallVector<CCValAssign, 16> RVLocs1;
2415 CCState CCInfo1(CalleeCC, false, getTargetMachine(),
2416 RVLocs1, *DAG.getContext());
2417 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2418
2419 SmallVector<CCValAssign, 16> RVLocs2;
2420 CCState CCInfo2(CallerCC, false, getTargetMachine(),
2421 RVLocs2, *DAG.getContext());
2422 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2423
2424 if (RVLocs1.size() != RVLocs2.size())
2425 return false;
2426 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2427 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2428 return false;
2429 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2430 return false;
2431 if (RVLocs1[i].isRegLoc()) {
2432 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2433 return false;
2434 } else {
2435 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2436 return false;
2437 }
2438 }
2439 }
2440
Evan Chenga6bff982010-01-30 01:22:00 +00002441 // If the callee takes no arguments then go on to check the results of the
2442 // call.
2443 if (!Outs.empty()) {
2444 // Check if stack adjustment is needed. For now, do not do this if any
2445 // argument is passed on the stack.
2446 SmallVector<CCValAssign, 16> ArgLocs;
2447 CCState CCInfo(CalleeCC, isVarArg, getTargetMachine(),
2448 ArgLocs, *DAG.getContext());
Duncan Sands45907662010-10-31 13:21:44 +00002449 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Evan Chengb2c92902010-02-02 02:22:50 +00002450 if (CCInfo.getNextStackOffset()) {
2451 MachineFunction &MF = DAG.getMachineFunction();
2452 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2453 return false;
Evan Chengb2c92902010-02-02 02:22:50 +00002454
2455 // Check if the arguments are already laid out in the right way as
2456 // the caller's fixed stack objects.
2457 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002458 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2459 const X86InstrInfo *TII =
2460 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002461 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2462 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00002463 SDValue Arg = OutVals[i];
Evan Chengb2c92902010-02-02 02:22:50 +00002464 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002465 if (VA.getLocInfo() == CCValAssign::Indirect)
2466 return false;
2467 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002468 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2469 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002470 return false;
2471 }
2472 }
2473 }
Evan Cheng9c044672010-05-29 01:35:22 +00002474
2475 // If the tailcall address may be in a register, then make sure it's
2476 // possible to register allocate for it. In 32-bit, the call address can
2477 // only target EAX, EDX, or ECX since the tail call must be scheduled after
Evan Chengdedd9742010-07-14 06:44:01 +00002478 // callee-saved registers are restored. These happen to be the same
2479 // registers used to pass 'inreg' arguments so watch out for those.
2480 if (!Subtarget->is64Bit() &&
2481 !isa<GlobalAddressSDNode>(Callee) &&
Evan Cheng9c044672010-05-29 01:35:22 +00002482 !isa<ExternalSymbolSDNode>(Callee)) {
Evan Cheng9c044672010-05-29 01:35:22 +00002483 unsigned NumInRegs = 0;
2484 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2485 CCValAssign &VA = ArgLocs[i];
Evan Chengdedd9742010-07-14 06:44:01 +00002486 if (!VA.isRegLoc())
2487 continue;
2488 unsigned Reg = VA.getLocReg();
2489 switch (Reg) {
2490 default: break;
2491 case X86::EAX: case X86::EDX: case X86::ECX:
2492 if (++NumInRegs == 3)
Evan Cheng9c044672010-05-29 01:35:22 +00002493 return false;
Evan Chengdedd9742010-07-14 06:44:01 +00002494 break;
Evan Cheng9c044672010-05-29 01:35:22 +00002495 }
2496 }
2497 }
Evan Chenga6bff982010-01-30 01:22:00 +00002498 }
Evan Chengb1712452010-01-27 06:25:16 +00002499
Dale Johannesend155d7e2010-10-25 22:17:05 +00002500 // An stdcall caller is expected to clean up its arguments; the callee
Dale Johannesen0e034562010-11-12 00:43:18 +00002501 // isn't going to do that.
Dale Johannesend155d7e2010-10-25 22:17:05 +00002502 if (!CCMatch && CallerCC==CallingConv::X86_StdCall)
2503 return false;
2504
Evan Cheng86809cc2010-02-03 03:28:02 +00002505 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002506}
2507
Dan Gohman3df24e62008-09-03 23:12:08 +00002508FastISel *
Dan Gohmana4160c32010-07-07 16:29:44 +00002509X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
2510 return X86::createFastISel(funcInfo);
Dan Gohmand9f3c482008-08-19 21:32:53 +00002511}
2512
2513
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002514//===----------------------------------------------------------------------===//
2515// Other Lowering Hooks
2516//===----------------------------------------------------------------------===//
2517
Bruno Cardoso Lopese654b562010-09-01 00:51:36 +00002518static bool MayFoldLoad(SDValue Op) {
2519 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
2520}
2521
2522static bool MayFoldIntoStore(SDValue Op) {
2523 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
2524}
2525
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002526static bool isTargetShuffle(unsigned Opcode) {
2527 switch(Opcode) {
2528 default: return false;
2529 case X86ISD::PSHUFD:
2530 case X86ISD::PSHUFHW:
2531 case X86ISD::PSHUFLW:
2532 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002533 case X86ISD::PALIGN:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002534 case X86ISD::SHUFPS:
2535 case X86ISD::MOVLHPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002536 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002537 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002538 case X86ISD::MOVLPS:
2539 case X86ISD::MOVLPD:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002540 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002541 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002542 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002543 case X86ISD::MOVSS:
2544 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002545 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002546 case X86ISD::UNPCKLPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002547 case X86ISD::PUNPCKLWD:
2548 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002549 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002550 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002551 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002552 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002553 case X86ISD::PUNPCKHWD:
2554 case X86ISD::PUNPCKHBW:
2555 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002556 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002557 return true;
2558 }
2559 return false;
2560}
2561
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002562static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002563 SDValue V1, SelectionDAG &DAG) {
2564 switch(Opc) {
2565 default: llvm_unreachable("Unknown x86 shuffle node");
2566 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002567 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002568 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002569 return DAG.getNode(Opc, dl, VT, V1);
2570 }
2571
2572 return SDValue();
2573}
2574
2575static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00002576 SDValue V1, unsigned TargetMask, SelectionDAG &DAG) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002577 switch(Opc) {
2578 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002579 case X86ISD::PSHUFD:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002580 case X86ISD::PSHUFHW:
2581 case X86ISD::PSHUFLW:
2582 return DAG.getNode(Opc, dl, VT, V1, DAG.getConstant(TargetMask, MVT::i8));
2583 }
2584
2585 return SDValue();
2586}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002587
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002588static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2589 SDValue V1, SDValue V2, unsigned TargetMask, SelectionDAG &DAG) {
2590 switch(Opc) {
2591 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002592 case X86ISD::PALIGN:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002593 case X86ISD::SHUFPD:
2594 case X86ISD::SHUFPS:
2595 return DAG.getNode(Opc, dl, VT, V1, V2,
2596 DAG.getConstant(TargetMask, MVT::i8));
2597 }
2598 return SDValue();
2599}
2600
2601static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2602 SDValue V1, SDValue V2, SelectionDAG &DAG) {
2603 switch(Opc) {
2604 default: llvm_unreachable("Unknown x86 shuffle node");
2605 case X86ISD::MOVLHPS:
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00002606 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002607 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002608 case X86ISD::MOVLPS:
2609 case X86ISD::MOVLPD:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002610 case X86ISD::MOVSS:
2611 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002612 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002613 case X86ISD::UNPCKLPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002614 case X86ISD::PUNPCKLWD:
2615 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002616 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002617 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002618 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002619 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002620 case X86ISD::PUNPCKHWD:
2621 case X86ISD::PUNPCKHBW:
2622 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002623 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002624 return DAG.getNode(Opc, dl, VT, V1, V2);
2625 }
2626 return SDValue();
2627}
2628
Dan Gohmand858e902010-04-17 15:26:15 +00002629SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002630 MachineFunction &MF = DAG.getMachineFunction();
2631 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2632 int ReturnAddrIndex = FuncInfo->getRAIndex();
2633
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002634 if (ReturnAddrIndex == 0) {
2635 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002636 uint64_t SlotSize = TD->getPointerSize();
David Greene3f2bf852009-11-12 20:49:22 +00002637 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00002638 false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002639 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002640 }
2641
Evan Cheng25ab6902006-09-08 06:48:29 +00002642 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002643}
2644
2645
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002646bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
2647 bool hasSymbolicDisplacement) {
2648 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00002649 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002650 return false;
2651
2652 // If we don't have a symbolic displacement - we don't have any extra
2653 // restrictions.
2654 if (!hasSymbolicDisplacement)
2655 return true;
2656
2657 // FIXME: Some tweaks might be needed for medium code model.
2658 if (M != CodeModel::Small && M != CodeModel::Kernel)
2659 return false;
2660
2661 // For small code model we assume that latest object is 16MB before end of 31
2662 // bits boundary. We may also accept pretty large negative constants knowing
2663 // that all objects are in the positive half of address space.
2664 if (M == CodeModel::Small && Offset < 16*1024*1024)
2665 return true;
2666
2667 // For kernel code model we know that all object resist in the negative half
2668 // of 32bits address space. We may not accept negative offsets, since they may
2669 // be just off and we may accept pretty large positive ones.
2670 if (M == CodeModel::Kernel && Offset > 0)
2671 return true;
2672
2673 return false;
2674}
2675
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002676/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
2677/// specific condition code, returning the condition code and the LHS/RHS of the
2678/// comparison to make.
2679static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2680 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00002681 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002682 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2683 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2684 // X > -1 -> X == 0, jump !sign.
2685 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002686 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002687 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2688 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002689 return X86::COND_S;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002690 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00002691 // X < 1 -> X <= 0
2692 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002693 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002694 }
Chris Lattnerf9570512006-09-13 03:22:10 +00002695 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002696
Evan Chengd9558e02006-01-06 00:43:03 +00002697 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002698 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002699 case ISD::SETEQ: return X86::COND_E;
2700 case ISD::SETGT: return X86::COND_G;
2701 case ISD::SETGE: return X86::COND_GE;
2702 case ISD::SETLT: return X86::COND_L;
2703 case ISD::SETLE: return X86::COND_LE;
2704 case ISD::SETNE: return X86::COND_NE;
2705 case ISD::SETULT: return X86::COND_B;
2706 case ISD::SETUGT: return X86::COND_A;
2707 case ISD::SETULE: return X86::COND_BE;
2708 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00002709 }
Chris Lattner4c78e022008-12-23 23:42:27 +00002710 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002711
Chris Lattner4c78e022008-12-23 23:42:27 +00002712 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00002713
Chris Lattner4c78e022008-12-23 23:42:27 +00002714 // If LHS is a foldable load, but RHS is not, flip the condition.
Rafael Espindolaf297c932011-02-03 03:58:05 +00002715 if (ISD::isNON_EXTLoad(LHS.getNode()) &&
2716 !ISD::isNON_EXTLoad(RHS.getNode())) {
Chris Lattner4c78e022008-12-23 23:42:27 +00002717 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
2718 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00002719 }
2720
Chris Lattner4c78e022008-12-23 23:42:27 +00002721 switch (SetCCOpcode) {
2722 default: break;
2723 case ISD::SETOLT:
2724 case ISD::SETOLE:
2725 case ISD::SETUGT:
2726 case ISD::SETUGE:
2727 std::swap(LHS, RHS);
2728 break;
2729 }
2730
2731 // On a floating point condition, the flags are set as follows:
2732 // ZF PF CF op
2733 // 0 | 0 | 0 | X > Y
2734 // 0 | 0 | 1 | X < Y
2735 // 1 | 0 | 0 | X == Y
2736 // 1 | 1 | 1 | unordered
2737 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002738 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00002739 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002740 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00002741 case ISD::SETOLT: // flipped
2742 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002743 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00002744 case ISD::SETOLE: // flipped
2745 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002746 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002747 case ISD::SETUGT: // flipped
2748 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002749 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00002750 case ISD::SETUGE: // flipped
2751 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002752 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002753 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002754 case ISD::SETNE: return X86::COND_NE;
2755 case ISD::SETUO: return X86::COND_P;
2756 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00002757 case ISD::SETOEQ:
2758 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00002759 }
Evan Chengd9558e02006-01-06 00:43:03 +00002760}
2761
Evan Cheng4a460802006-01-11 00:33:36 +00002762/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2763/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00002764/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00002765static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00002766 switch (X86CC) {
2767 default:
2768 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00002769 case X86::COND_B:
2770 case X86::COND_BE:
2771 case X86::COND_E:
2772 case X86::COND_P:
2773 case X86::COND_A:
2774 case X86::COND_AE:
2775 case X86::COND_NE:
2776 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00002777 return true;
2778 }
2779}
2780
Evan Chengeb2f9692009-10-27 19:56:55 +00002781/// isFPImmLegal - Returns true if the target can instruction select the
2782/// specified FP immediate natively. If false, the legalizer will
2783/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00002784bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00002785 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
2786 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
2787 return true;
2788 }
2789 return false;
2790}
2791
Nate Begeman9008ca62009-04-27 18:41:29 +00002792/// isUndefOrInRange - Return true if Val is undef or if its value falls within
2793/// the specified range (L, H].
2794static bool isUndefOrInRange(int Val, int Low, int Hi) {
2795 return (Val < 0) || (Val >= Low && Val < Hi);
2796}
2797
2798/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
2799/// specified value.
2800static bool isUndefOrEqual(int Val, int CmpVal) {
2801 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00002802 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00002803 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00002804}
2805
Nate Begeman9008ca62009-04-27 18:41:29 +00002806/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
2807/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
2808/// the second operand.
Owen Andersone50ed302009-08-10 22:56:29 +00002809static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00002810 if (VT == MVT::v4f32 || VT == MVT::v4i32 )
Nate Begeman9008ca62009-04-27 18:41:29 +00002811 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002812 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00002813 return (Mask[0] < 2 && Mask[1] < 2);
2814 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002815}
2816
Nate Begeman9008ca62009-04-27 18:41:29 +00002817bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002818 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002819 N->getMask(M);
2820 return ::isPSHUFDMask(M, N->getValueType(0));
2821}
Evan Cheng0188ecb2006-03-22 18:59:22 +00002822
Nate Begeman9008ca62009-04-27 18:41:29 +00002823/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
2824/// is suitable for input to PSHUFHW.
Owen Andersone50ed302009-08-10 22:56:29 +00002825static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002826 if (VT != MVT::v8i16)
Evan Cheng0188ecb2006-03-22 18:59:22 +00002827 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002828
Nate Begeman9008ca62009-04-27 18:41:29 +00002829 // Lower quadword copied in order or undef.
2830 for (int i = 0; i != 4; ++i)
2831 if (Mask[i] >= 0 && Mask[i] != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00002832 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002833
Evan Cheng506d3df2006-03-29 23:07:14 +00002834 // Upper quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002835 for (int i = 4; i != 8; ++i)
2836 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Evan Cheng506d3df2006-03-29 23:07:14 +00002837 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002838
Evan Cheng506d3df2006-03-29 23:07:14 +00002839 return true;
2840}
2841
Nate Begeman9008ca62009-04-27 18:41:29 +00002842bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002843 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002844 N->getMask(M);
2845 return ::isPSHUFHWMask(M, N->getValueType(0));
2846}
Evan Cheng506d3df2006-03-29 23:07:14 +00002847
Nate Begeman9008ca62009-04-27 18:41:29 +00002848/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
2849/// is suitable for input to PSHUFLW.
Owen Andersone50ed302009-08-10 22:56:29 +00002850static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002851 if (VT != MVT::v8i16)
Evan Cheng506d3df2006-03-29 23:07:14 +00002852 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002853
Rafael Espindola15684b22009-04-24 12:40:33 +00002854 // Upper quadword copied in order.
Nate Begeman9008ca62009-04-27 18:41:29 +00002855 for (int i = 4; i != 8; ++i)
2856 if (Mask[i] >= 0 && Mask[i] != i)
Rafael Espindola15684b22009-04-24 12:40:33 +00002857 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002858
Rafael Espindola15684b22009-04-24 12:40:33 +00002859 // Lower quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002860 for (int i = 0; i != 4; ++i)
2861 if (Mask[i] >= 4)
Rafael Espindola15684b22009-04-24 12:40:33 +00002862 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002863
Rafael Espindola15684b22009-04-24 12:40:33 +00002864 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002865}
2866
Nate Begeman9008ca62009-04-27 18:41:29 +00002867bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002868 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002869 N->getMask(M);
2870 return ::isPSHUFLWMask(M, N->getValueType(0));
2871}
2872
Nate Begemana09008b2009-10-19 02:17:23 +00002873/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
2874/// is suitable for input to PALIGNR.
2875static bool isPALIGNRMask(const SmallVectorImpl<int> &Mask, EVT VT,
2876 bool hasSSSE3) {
2877 int i, e = VT.getVectorNumElements();
Michael J. Spencerec38de22010-10-10 22:04:20 +00002878
Nate Begemana09008b2009-10-19 02:17:23 +00002879 // Do not handle v2i64 / v2f64 shuffles with palignr.
2880 if (e < 4 || !hasSSSE3)
2881 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00002882
Nate Begemana09008b2009-10-19 02:17:23 +00002883 for (i = 0; i != e; ++i)
2884 if (Mask[i] >= 0)
2885 break;
Michael J. Spencerec38de22010-10-10 22:04:20 +00002886
Nate Begemana09008b2009-10-19 02:17:23 +00002887 // All undef, not a palignr.
2888 if (i == e)
2889 return false;
2890
2891 // Determine if it's ok to perform a palignr with only the LHS, since we
2892 // don't have access to the actual shuffle elements to see if RHS is undef.
2893 bool Unary = Mask[i] < (int)e;
2894 bool NeedsUnary = false;
2895
2896 int s = Mask[i] - i;
Michael J. Spencerec38de22010-10-10 22:04:20 +00002897
Nate Begemana09008b2009-10-19 02:17:23 +00002898 // Check the rest of the elements to see if they are consecutive.
2899 for (++i; i != e; ++i) {
2900 int m = Mask[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00002901 if (m < 0)
Nate Begemana09008b2009-10-19 02:17:23 +00002902 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00002903
Nate Begemana09008b2009-10-19 02:17:23 +00002904 Unary = Unary && (m < (int)e);
2905 NeedsUnary = NeedsUnary || (m < s);
2906
2907 if (NeedsUnary && !Unary)
2908 return false;
2909 if (Unary && m != ((s+i) & (e-1)))
2910 return false;
2911 if (!Unary && m != (s+i))
2912 return false;
2913 }
2914 return true;
2915}
2916
2917bool X86::isPALIGNRMask(ShuffleVectorSDNode *N) {
2918 SmallVector<int, 8> M;
2919 N->getMask(M);
2920 return ::isPALIGNRMask(M, N->getValueType(0), true);
2921}
2922
Evan Cheng14aed5e2006-03-24 01:18:28 +00002923/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2924/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Owen Andersone50ed302009-08-10 22:56:29 +00002925static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002926 int NumElems = VT.getVectorNumElements();
2927 if (NumElems != 2 && NumElems != 4)
2928 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002929
Nate Begeman9008ca62009-04-27 18:41:29 +00002930 int Half = NumElems / 2;
2931 for (int i = 0; i < Half; ++i)
2932 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002933 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002934 for (int i = Half; i < NumElems; ++i)
2935 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002936 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002937
Evan Cheng14aed5e2006-03-24 01:18:28 +00002938 return true;
2939}
2940
Nate Begeman9008ca62009-04-27 18:41:29 +00002941bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
2942 SmallVector<int, 8> M;
2943 N->getMask(M);
2944 return ::isSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002945}
2946
Evan Cheng213d2cf2007-05-17 18:45:50 +00002947/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00002948/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2949/// half elements to come from vector 1 (which would equal the dest.) and
2950/// the upper half to come from vector 2.
Owen Andersone50ed302009-08-10 22:56:29 +00002951static bool isCommutedSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002952 int NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00002953
2954 if (NumElems != 2 && NumElems != 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00002955 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002956
Nate Begeman9008ca62009-04-27 18:41:29 +00002957 int Half = NumElems / 2;
2958 for (int i = 0; i < Half; ++i)
2959 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002960 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002961 for (int i = Half; i < NumElems; ++i)
2962 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002963 return false;
2964 return true;
2965}
2966
Nate Begeman9008ca62009-04-27 18:41:29 +00002967static bool isCommutedSHUFP(ShuffleVectorSDNode *N) {
2968 SmallVector<int, 8> M;
2969 N->getMask(M);
2970 return isCommutedSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002971}
2972
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002973/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2974/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00002975bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
2976 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002977 return false;
2978
Evan Cheng2064a2b2006-03-28 06:50:32 +00002979 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman9008ca62009-04-27 18:41:29 +00002980 return isUndefOrEqual(N->getMaskElt(0), 6) &&
2981 isUndefOrEqual(N->getMaskElt(1), 7) &&
2982 isUndefOrEqual(N->getMaskElt(2), 2) &&
2983 isUndefOrEqual(N->getMaskElt(3), 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00002984}
2985
Nate Begeman0b10b912009-11-07 23:17:15 +00002986/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2987/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2988/// <2, 3, 2, 3>
2989bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
2990 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Michael J. Spencerec38de22010-10-10 22:04:20 +00002991
Nate Begeman0b10b912009-11-07 23:17:15 +00002992 if (NumElems != 4)
2993 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00002994
Nate Begeman0b10b912009-11-07 23:17:15 +00002995 return isUndefOrEqual(N->getMaskElt(0), 2) &&
2996 isUndefOrEqual(N->getMaskElt(1), 3) &&
2997 isUndefOrEqual(N->getMaskElt(2), 2) &&
2998 isUndefOrEqual(N->getMaskElt(3), 3);
2999}
3000
Evan Cheng5ced1d82006-04-06 23:23:56 +00003001/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
3002/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman9008ca62009-04-27 18:41:29 +00003003bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
3004 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003005
Evan Cheng5ced1d82006-04-06 23:23:56 +00003006 if (NumElems != 2 && NumElems != 4)
3007 return false;
3008
Evan Chengc5cdff22006-04-07 21:53:05 +00003009 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003010 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003011 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003012
Evan Chengc5cdff22006-04-07 21:53:05 +00003013 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003014 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003015 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003016
3017 return true;
3018}
3019
Nate Begeman0b10b912009-11-07 23:17:15 +00003020/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
3021/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
3022bool X86::isMOVLHPSMask(ShuffleVectorSDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003023 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003024
Evan Cheng5ced1d82006-04-06 23:23:56 +00003025 if (NumElems != 2 && NumElems != 4)
3026 return false;
3027
Evan Chengc5cdff22006-04-07 21:53:05 +00003028 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003029 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003030 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003031
Nate Begeman9008ca62009-04-27 18:41:29 +00003032 for (unsigned i = 0; i < NumElems/2; ++i)
3033 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003034 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003035
3036 return true;
3037}
3038
Evan Cheng0038e592006-03-28 00:39:58 +00003039/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
3040/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Owen Andersone50ed302009-08-10 22:56:29 +00003041static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003042 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003043 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003044 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng0038e592006-03-28 00:39:58 +00003045 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003046
Nate Begeman9008ca62009-04-27 18:41:29 +00003047 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
3048 int BitI = Mask[i];
3049 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00003050 if (!isUndefOrEqual(BitI, j))
3051 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00003052 if (V2IsSplat) {
Mon P Wang7bcaefa2009-02-04 01:16:59 +00003053 if (!isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003054 return false;
3055 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00003056 if (!isUndefOrEqual(BitI1, j + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003057 return false;
3058 }
Evan Cheng0038e592006-03-28 00:39:58 +00003059 }
Evan Cheng0038e592006-03-28 00:39:58 +00003060 return true;
3061}
3062
Nate Begeman9008ca62009-04-27 18:41:29 +00003063bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3064 SmallVector<int, 8> M;
3065 N->getMask(M);
3066 return ::isUNPCKLMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003067}
3068
Evan Cheng4fcb9222006-03-28 02:43:26 +00003069/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3070/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Eric Christopherfd179292009-08-27 18:07:15 +00003071static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003072 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003073 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003074 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng4fcb9222006-03-28 02:43:26 +00003075 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003076
Nate Begeman9008ca62009-04-27 18:41:29 +00003077 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
3078 int BitI = Mask[i];
3079 int BitI1 = Mask[i+1];
Chris Lattner5a88b832007-02-25 07:10:00 +00003080 if (!isUndefOrEqual(BitI, j + NumElts/2))
Evan Chengc5cdff22006-04-07 21:53:05 +00003081 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00003082 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00003083 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003084 return false;
3085 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00003086 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003087 return false;
3088 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003089 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003090 return true;
3091}
3092
Nate Begeman9008ca62009-04-27 18:41:29 +00003093bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3094 SmallVector<int, 8> M;
3095 N->getMask(M);
3096 return ::isUNPCKHMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003097}
3098
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003099/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3100/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3101/// <0, 0, 1, 1>
Owen Andersone50ed302009-08-10 22:56:29 +00003102static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003103 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003104 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003105 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003106
Nate Begeman9008ca62009-04-27 18:41:29 +00003107 for (int i = 0, j = 0; i != NumElems; i += 2, ++j) {
3108 int BitI = Mask[i];
3109 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00003110 if (!isUndefOrEqual(BitI, j))
3111 return false;
3112 if (!isUndefOrEqual(BitI1, j))
3113 return false;
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003114 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003115 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003116}
3117
Nate Begeman9008ca62009-04-27 18:41:29 +00003118bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
3119 SmallVector<int, 8> M;
3120 N->getMask(M);
3121 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
3122}
3123
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003124/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3125/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3126/// <2, 2, 3, 3>
Owen Andersone50ed302009-08-10 22:56:29 +00003127static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003128 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003129 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
3130 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003131
Nate Begeman9008ca62009-04-27 18:41:29 +00003132 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
3133 int BitI = Mask[i];
3134 int BitI1 = Mask[i+1];
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003135 if (!isUndefOrEqual(BitI, j))
3136 return false;
3137 if (!isUndefOrEqual(BitI1, j))
3138 return false;
3139 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003140 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003141}
3142
Nate Begeman9008ca62009-04-27 18:41:29 +00003143bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
3144 SmallVector<int, 8> M;
3145 N->getMask(M);
3146 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
3147}
3148
Evan Cheng017dcc62006-04-21 01:05:10 +00003149/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3150/// specifies a shuffle of elements that is suitable for input to MOVSS,
3151/// MOVSD, and MOVD, i.e. setting the lowest element.
Owen Andersone50ed302009-08-10 22:56:29 +00003152static bool isMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003153 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003154 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003155
3156 int NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003157
Nate Begeman9008ca62009-04-27 18:41:29 +00003158 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003159 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003160
Nate Begeman9008ca62009-04-27 18:41:29 +00003161 for (int i = 1; i < NumElts; ++i)
3162 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003163 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003164
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003165 return true;
3166}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003167
Nate Begeman9008ca62009-04-27 18:41:29 +00003168bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
3169 SmallVector<int, 8> M;
3170 N->getMask(M);
3171 return ::isMOVLMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003172}
3173
Evan Cheng017dcc62006-04-21 01:05:10 +00003174/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
3175/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003176/// element of vector 2 and the other elements to come from vector 1 in order.
Owen Andersone50ed302009-08-10 22:56:29 +00003177static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003178 bool V2IsSplat = false, bool V2IsUndef = false) {
3179 int NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003180 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003181 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003182
Nate Begeman9008ca62009-04-27 18:41:29 +00003183 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003184 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003185
Nate Begeman9008ca62009-04-27 18:41:29 +00003186 for (int i = 1; i < NumOps; ++i)
3187 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3188 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3189 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003190 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003191
Evan Cheng39623da2006-04-20 08:58:49 +00003192 return true;
3193}
3194
Nate Begeman9008ca62009-04-27 18:41:29 +00003195static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00003196 bool V2IsUndef = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003197 SmallVector<int, 8> M;
3198 N->getMask(M);
3199 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00003200}
3201
Evan Chengd9539472006-04-14 21:59:03 +00003202/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3203/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003204bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N) {
3205 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003206 return false;
3207
3208 // Expect 1, 1, 3, 3
Rafael Espindola15684b22009-04-24 12:40:33 +00003209 for (unsigned i = 0; i < 2; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003210 int Elt = N->getMaskElt(i);
3211 if (Elt >= 0 && Elt != 1)
3212 return false;
Rafael Espindola15684b22009-04-24 12:40:33 +00003213 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003214
3215 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003216 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003217 int Elt = N->getMaskElt(i);
3218 if (Elt >= 0 && Elt != 3)
3219 return false;
3220 if (Elt == 3)
3221 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003222 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003223 // Don't use movshdup if it can be done with a shufps.
Nate Begeman9008ca62009-04-27 18:41:29 +00003224 // FIXME: verify that matching u, u, 3, 3 is what we want.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003225 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003226}
3227
3228/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3229/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003230bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N) {
3231 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003232 return false;
3233
3234 // Expect 0, 0, 2, 2
Nate Begeman9008ca62009-04-27 18:41:29 +00003235 for (unsigned i = 0; i < 2; ++i)
3236 if (N->getMaskElt(i) > 0)
3237 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003238
3239 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003240 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003241 int Elt = N->getMaskElt(i);
3242 if (Elt >= 0 && Elt != 2)
3243 return false;
3244 if (Elt == 2)
3245 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003246 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003247 // Don't use movsldup if it can be done with a shufps.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003248 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003249}
3250
Evan Cheng0b457f02008-09-25 20:50:48 +00003251/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3252/// specifies a shuffle of elements that is suitable for input to MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003253bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
3254 int e = N->getValueType(0).getVectorNumElements() / 2;
Eric Christopherfd179292009-08-27 18:07:15 +00003255
Nate Begeman9008ca62009-04-27 18:41:29 +00003256 for (int i = 0; i < e; ++i)
3257 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003258 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003259 for (int i = 0; i < e; ++i)
3260 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003261 return false;
3262 return true;
3263}
3264
David Greenec38a03e2011-02-03 15:50:00 +00003265/// isVEXTRACTF128Index - Return true if the specified
3266/// EXTRACT_SUBVECTOR operand specifies a vector extract that is
3267/// suitable for input to VEXTRACTF128.
3268bool X86::isVEXTRACTF128Index(SDNode *N) {
3269 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
3270 return false;
3271
3272 // The index should be aligned on a 128-bit boundary.
3273 uint64_t Index =
3274 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
3275
3276 unsigned VL = N->getValueType(0).getVectorNumElements();
3277 unsigned VBits = N->getValueType(0).getSizeInBits();
3278 unsigned ElSize = VBits / VL;
3279 bool Result = (Index * ElSize) % 128 == 0;
3280
3281 return Result;
3282}
3283
David Greeneccacdc12011-02-04 16:08:29 +00003284/// isVINSERTF128Index - Return true if the specified INSERT_SUBVECTOR
3285/// operand specifies a subvector insert that is suitable for input to
3286/// VINSERTF128.
3287bool X86::isVINSERTF128Index(SDNode *N) {
3288 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
3289 return false;
3290
3291 // The index should be aligned on a 128-bit boundary.
3292 uint64_t Index =
3293 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
3294
3295 unsigned VL = N->getValueType(0).getVectorNumElements();
3296 unsigned VBits = N->getValueType(0).getSizeInBits();
3297 unsigned ElSize = VBits / VL;
3298 bool Result = (Index * ElSize) % 128 == 0;
3299
3300 return Result;
3301}
3302
Evan Cheng63d33002006-03-22 08:01:21 +00003303/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003304/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Evan Cheng63d33002006-03-22 08:01:21 +00003305unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003306 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3307 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
3308
Evan Chengb9df0ca2006-03-22 02:53:00 +00003309 unsigned Shift = (NumOperands == 4) ? 2 : 1;
3310 unsigned Mask = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00003311 for (int i = 0; i < NumOperands; ++i) {
3312 int Val = SVOp->getMaskElt(NumOperands-i-1);
3313 if (Val < 0) Val = 0;
Evan Cheng14aed5e2006-03-24 01:18:28 +00003314 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00003315 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00003316 if (i != NumOperands - 1)
3317 Mask <<= Shift;
3318 }
Evan Cheng63d33002006-03-22 08:01:21 +00003319 return Mask;
3320}
3321
Evan Cheng506d3df2006-03-29 23:07:14 +00003322/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003323/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003324unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003325 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003326 unsigned Mask = 0;
3327 // 8 nodes, but we only care about the last 4.
3328 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003329 int Val = SVOp->getMaskElt(i);
3330 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00003331 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00003332 if (i != 4)
3333 Mask <<= 2;
3334 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003335 return Mask;
3336}
3337
3338/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003339/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003340unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003341 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003342 unsigned Mask = 0;
3343 // 8 nodes, but we only care about the first 4.
3344 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003345 int Val = SVOp->getMaskElt(i);
3346 if (Val >= 0)
3347 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00003348 if (i != 0)
3349 Mask <<= 2;
3350 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003351 return Mask;
3352}
3353
Nate Begemana09008b2009-10-19 02:17:23 +00003354/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
3355/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
3356unsigned X86::getShufflePALIGNRImmediate(SDNode *N) {
3357 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3358 EVT VVT = N->getValueType(0);
3359 unsigned EltSize = VVT.getVectorElementType().getSizeInBits() >> 3;
3360 int Val = 0;
3361
3362 unsigned i, e;
3363 for (i = 0, e = VVT.getVectorNumElements(); i != e; ++i) {
3364 Val = SVOp->getMaskElt(i);
3365 if (Val >= 0)
3366 break;
3367 }
3368 return (Val - i) * EltSize;
3369}
3370
David Greenec38a03e2011-02-03 15:50:00 +00003371/// getExtractVEXTRACTF128Immediate - Return the appropriate immediate
3372/// to extract the specified EXTRACT_SUBVECTOR index with VEXTRACTF128
3373/// instructions.
3374unsigned X86::getExtractVEXTRACTF128Immediate(SDNode *N) {
3375 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
3376 llvm_unreachable("Illegal extract subvector for VEXTRACTF128");
3377
3378 uint64_t Index =
3379 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
3380
3381 EVT VecVT = N->getOperand(0).getValueType();
3382 EVT ElVT = VecVT.getVectorElementType();
3383
3384 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
3385
3386 return Index / NumElemsPerChunk;
3387}
3388
David Greeneccacdc12011-02-04 16:08:29 +00003389/// getInsertVINSERTF128Immediate - Return the appropriate immediate
3390/// to insert at the specified INSERT_SUBVECTOR index with VINSERTF128
3391/// instructions.
3392unsigned X86::getInsertVINSERTF128Immediate(SDNode *N) {
3393 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
3394 llvm_unreachable("Illegal insert subvector for VINSERTF128");
3395
3396 uint64_t Index =
NAKAMURA Takumi27635382011-02-05 15:10:54 +00003397 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
David Greeneccacdc12011-02-04 16:08:29 +00003398
3399 EVT VecVT = N->getValueType(0);
3400 EVT ElVT = VecVT.getVectorElementType();
3401
3402 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
3403
3404 return Index / NumElemsPerChunk;
3405}
3406
Evan Cheng37b73872009-07-30 08:33:02 +00003407/// isZeroNode - Returns true if Elt is a constant zero or a floating point
3408/// constant +0.0.
3409bool X86::isZeroNode(SDValue Elt) {
3410 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmane368b462010-06-18 14:22:04 +00003411 cast<ConstantSDNode>(Elt)->isNullValue()) ||
Evan Cheng37b73872009-07-30 08:33:02 +00003412 (isa<ConstantFPSDNode>(Elt) &&
3413 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
3414}
3415
Nate Begeman9008ca62009-04-27 18:41:29 +00003416/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
3417/// their permute mask.
3418static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
3419 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003420 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003421 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00003422 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00003423
Nate Begeman5a5ca152009-04-29 05:20:52 +00003424 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003425 int idx = SVOp->getMaskElt(i);
3426 if (idx < 0)
3427 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003428 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003429 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003430 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003431 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003432 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003433 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
3434 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003435}
3436
Evan Cheng779ccea2007-12-07 21:30:01 +00003437/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3438/// the two vector operands have swapped position.
Owen Andersone50ed302009-08-10 22:56:29 +00003439static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00003440 unsigned NumElems = VT.getVectorNumElements();
3441 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003442 int idx = Mask[i];
3443 if (idx < 0)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003444 continue;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003445 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003446 Mask[i] = idx + NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003447 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003448 Mask[i] = idx - NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003449 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003450}
3451
Evan Cheng533a0aa2006-04-19 20:35:22 +00003452/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
3453/// match movhlps. The lower half elements should come from upper half of
3454/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003455/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00003456static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
3457 if (Op->getValueType(0).getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00003458 return false;
3459 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003460 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003461 return false;
3462 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003463 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003464 return false;
3465 return true;
3466}
3467
Evan Cheng5ced1d82006-04-06 23:23:56 +00003468/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00003469/// is promoted to a vector. It also returns the LoadSDNode by reference if
3470/// required.
3471static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00003472 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
3473 return false;
3474 N = N->getOperand(0).getNode();
3475 if (!ISD::isNON_EXTLoad(N))
3476 return false;
3477 if (LD)
3478 *LD = cast<LoadSDNode>(N);
3479 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003480}
3481
Evan Cheng533a0aa2006-04-19 20:35:22 +00003482/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
3483/// match movlp{s|d}. The lower half elements should come from lower half of
3484/// V1 (and in order), and the upper half elements should come from the upper
3485/// half of V2 (and in order). And since V1 will become the source of the
3486/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003487static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
3488 ShuffleVectorSDNode *Op) {
Evan Cheng466685d2006-10-09 20:57:25 +00003489 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003490 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00003491 // Is V2 is a vector load, don't do this transformation. We will try to use
3492 // load folding shufps op.
3493 if (ISD::isNON_EXTLoad(V2))
3494 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003495
Nate Begeman5a5ca152009-04-29 05:20:52 +00003496 unsigned NumElems = Op->getValueType(0).getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003497
Evan Cheng533a0aa2006-04-19 20:35:22 +00003498 if (NumElems != 2 && NumElems != 4)
3499 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003500 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003501 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003502 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003503 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003504 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003505 return false;
3506 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003507}
3508
Evan Cheng39623da2006-04-20 08:58:49 +00003509/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
3510/// all the same.
3511static bool isSplatVector(SDNode *N) {
3512 if (N->getOpcode() != ISD::BUILD_VECTOR)
3513 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003514
Dan Gohman475871a2008-07-27 21:46:04 +00003515 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00003516 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
3517 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003518 return false;
3519 return true;
3520}
3521
Evan Cheng213d2cf2007-05-17 18:45:50 +00003522/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00003523/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00003524/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00003525static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00003526 SDValue V1 = N->getOperand(0);
3527 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003528 unsigned NumElems = N->getValueType(0).getVectorNumElements();
3529 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003530 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003531 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003532 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00003533 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
3534 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003535 if (Opc != ISD::BUILD_VECTOR ||
3536 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00003537 return false;
3538 } else if (Idx >= 0) {
3539 unsigned Opc = V1.getOpcode();
3540 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
3541 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003542 if (Opc != ISD::BUILD_VECTOR ||
3543 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00003544 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00003545 }
3546 }
3547 return true;
3548}
3549
3550/// getZeroVector - Returns a vector of specified type with all zero elements.
3551///
Owen Andersone50ed302009-08-10 22:56:29 +00003552static SDValue getZeroVector(EVT VT, bool HasSSE2, SelectionDAG &DAG,
Dale Johannesenace16102009-02-03 19:33:06 +00003553 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003554 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003555
Dale Johannesen0488fb62010-09-30 23:57:10 +00003556 // Always build SSE zero vectors as <4 x i32> bitcasted
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003557 // to their dest type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00003558 SDValue Vec;
Dale Johannesen0488fb62010-09-30 23:57:10 +00003559 if (VT.getSizeInBits() == 128) { // SSE
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003560 if (HasSSE2) { // SSE2
3561 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3562 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
3563 } else { // SSE1
3564 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
3565 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
3566 }
3567 } else if (VT.getSizeInBits() == 256) { // AVX
3568 // 256-bit logic and arithmetic instructions in AVX are
3569 // all floating-point, no support for integer ops. Default
3570 // to emitting fp zeroed vectors then.
Owen Anderson825b72b2009-08-11 20:47:22 +00003571 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003572 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
3573 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops, 8);
Evan Chengf0df0312008-05-15 08:39:06 +00003574 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003575 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00003576}
3577
Chris Lattner8a594482007-11-25 00:24:49 +00003578/// getOnesVector - Returns a vector of specified type with all bits set.
3579///
Owen Andersone50ed302009-08-10 22:56:29 +00003580static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003581 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003582
Chris Lattner8a594482007-11-25 00:24:49 +00003583 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
3584 // type. This ensures they get CSE'd.
Owen Anderson825b72b2009-08-11 20:47:22 +00003585 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00003586 SDValue Vec;
Dale Johannesen0488fb62010-09-30 23:57:10 +00003587 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003588 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00003589}
3590
3591
Evan Cheng39623da2006-04-20 08:58:49 +00003592/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
3593/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00003594static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003595 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003596 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003597
Evan Cheng39623da2006-04-20 08:58:49 +00003598 bool Changed = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003599 SmallVector<int, 8> MaskVec;
3600 SVOp->getMask(MaskVec);
Eric Christopherfd179292009-08-27 18:07:15 +00003601
Nate Begeman5a5ca152009-04-29 05:20:52 +00003602 for (unsigned i = 0; i != NumElems; ++i) {
3603 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003604 MaskVec[i] = NumElems;
3605 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00003606 }
Evan Cheng39623da2006-04-20 08:58:49 +00003607 }
Evan Cheng39623da2006-04-20 08:58:49 +00003608 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00003609 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
3610 SVOp->getOperand(1), &MaskVec[0]);
3611 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00003612}
3613
Evan Cheng017dcc62006-04-21 01:05:10 +00003614/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
3615/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00003616static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003617 SDValue V2) {
3618 unsigned NumElems = VT.getVectorNumElements();
3619 SmallVector<int, 8> Mask;
3620 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00003621 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003622 Mask.push_back(i);
3623 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00003624}
3625
Nate Begeman9008ca62009-04-27 18:41:29 +00003626/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003627static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003628 SDValue V2) {
3629 unsigned NumElems = VT.getVectorNumElements();
3630 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00003631 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003632 Mask.push_back(i);
3633 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00003634 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003635 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00003636}
3637
Nate Begeman9008ca62009-04-27 18:41:29 +00003638/// getUnpackhMask - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003639static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003640 SDValue V2) {
3641 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00003642 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00003643 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00003644 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003645 Mask.push_back(i + Half);
3646 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00003647 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003648 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00003649}
3650
Bruno Cardoso Lopesbb0a9482010-08-13 17:50:47 +00003651/// PromoteSplat - Promote a splat of v4i32, v8i16 or v16i8 to v4f32.
3652static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003653 EVT PVT = MVT::v4f32;
Owen Andersone50ed302009-08-10 22:56:29 +00003654 EVT VT = SV->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00003655 DebugLoc dl = SV->getDebugLoc();
3656 SDValue V1 = SV->getOperand(0);
3657 int NumElems = VT.getVectorNumElements();
3658 int EltNo = SV->getSplatIndex();
Rafael Espindola15684b22009-04-24 12:40:33 +00003659
Nate Begeman9008ca62009-04-27 18:41:29 +00003660 // unpack elements to the correct location
3661 while (NumElems > 4) {
3662 if (EltNo < NumElems/2) {
3663 V1 = getUnpackl(DAG, dl, VT, V1, V1);
3664 } else {
3665 V1 = getUnpackh(DAG, dl, VT, V1, V1);
3666 EltNo -= NumElems/2;
3667 }
3668 NumElems >>= 1;
3669 }
Eric Christopherfd179292009-08-27 18:07:15 +00003670
Nate Begeman9008ca62009-04-27 18:41:29 +00003671 // Perform the splat.
3672 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003673 V1 = DAG.getNode(ISD::BITCAST, dl, PVT, V1);
Nate Begeman9008ca62009-04-27 18:41:29 +00003674 V1 = DAG.getVectorShuffle(PVT, dl, V1, DAG.getUNDEF(PVT), &SplatMask[0]);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003675 return DAG.getNode(ISD::BITCAST, dl, VT, V1);
Evan Chengc575ca22006-04-17 20:43:08 +00003676}
3677
Evan Chengba05f722006-04-21 23:03:30 +00003678/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00003679/// vector of zero or undef vector. This produces a shuffle where the low
3680/// element of V2 is swizzled into the zero/undef vector, landing at element
3681/// 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 +00003682static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Chengf0df0312008-05-15 08:39:06 +00003683 bool isZero, bool HasSSE2,
3684 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003685 EVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00003686 SDValue V1 = isZero
Nate Begeman9008ca62009-04-27 18:41:29 +00003687 ? getZeroVector(VT, HasSSE2, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
3688 unsigned NumElems = VT.getVectorNumElements();
3689 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00003690 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003691 // If this is the insertion idx, put the low elt of V2 here.
3692 MaskVec.push_back(i == Idx ? NumElems : i);
3693 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00003694}
3695
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003696/// getShuffleScalarElt - Returns the scalar element that will make up the ith
3697/// element of the result of the vector shuffle.
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003698SDValue getShuffleScalarElt(SDNode *N, int Index, SelectionDAG &DAG,
3699 unsigned Depth) {
3700 if (Depth == 6)
3701 return SDValue(); // Limit search depth.
3702
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003703 SDValue V = SDValue(N, 0);
3704 EVT VT = V.getValueType();
3705 unsigned Opcode = V.getOpcode();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003706
3707 // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
3708 if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
3709 Index = SV->getMaskElt(Index);
3710
3711 if (Index < 0)
3712 return DAG.getUNDEF(VT.getVectorElementType());
3713
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003714 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003715 SDValue NewV = (Index < NumElems) ? SV->getOperand(0) : SV->getOperand(1);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003716 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG, Depth+1);
Evan Chengf26ffe92008-05-29 08:22:04 +00003717 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003718
3719 // Recurse into target specific vector shuffles to find scalars.
3720 if (isTargetShuffle(Opcode)) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003721 int NumElems = VT.getVectorNumElements();
3722 SmallVector<unsigned, 16> ShuffleMask;
3723 SDValue ImmN;
3724
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003725 switch(Opcode) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003726 case X86ISD::SHUFPS:
3727 case X86ISD::SHUFPD:
3728 ImmN = N->getOperand(N->getNumOperands()-1);
3729 DecodeSHUFPSMask(NumElems,
3730 cast<ConstantSDNode>(ImmN)->getZExtValue(),
3731 ShuffleMask);
3732 break;
3733 case X86ISD::PUNPCKHBW:
3734 case X86ISD::PUNPCKHWD:
3735 case X86ISD::PUNPCKHDQ:
3736 case X86ISD::PUNPCKHQDQ:
3737 DecodePUNPCKHMask(NumElems, ShuffleMask);
3738 break;
3739 case X86ISD::UNPCKHPS:
3740 case X86ISD::UNPCKHPD:
3741 DecodeUNPCKHPMask(NumElems, ShuffleMask);
3742 break;
3743 case X86ISD::PUNPCKLBW:
3744 case X86ISD::PUNPCKLWD:
3745 case X86ISD::PUNPCKLDQ:
3746 case X86ISD::PUNPCKLQDQ:
3747 DecodePUNPCKLMask(NumElems, ShuffleMask);
3748 break;
3749 case X86ISD::UNPCKLPS:
3750 case X86ISD::UNPCKLPD:
3751 DecodeUNPCKLPMask(NumElems, ShuffleMask);
3752 break;
3753 case X86ISD::MOVHLPS:
3754 DecodeMOVHLPSMask(NumElems, ShuffleMask);
3755 break;
3756 case X86ISD::MOVLHPS:
3757 DecodeMOVLHPSMask(NumElems, ShuffleMask);
3758 break;
3759 case X86ISD::PSHUFD:
3760 ImmN = N->getOperand(N->getNumOperands()-1);
3761 DecodePSHUFMask(NumElems,
3762 cast<ConstantSDNode>(ImmN)->getZExtValue(),
3763 ShuffleMask);
3764 break;
3765 case X86ISD::PSHUFHW:
3766 ImmN = N->getOperand(N->getNumOperands()-1);
3767 DecodePSHUFHWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
3768 ShuffleMask);
3769 break;
3770 case X86ISD::PSHUFLW:
3771 ImmN = N->getOperand(N->getNumOperands()-1);
3772 DecodePSHUFLWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
3773 ShuffleMask);
3774 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003775 case X86ISD::MOVSS:
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00003776 case X86ISD::MOVSD: {
3777 // The index 0 always comes from the first element of the second source,
3778 // this is why MOVSS and MOVSD are used in the first place. The other
3779 // elements come from the other positions of the first source vector.
3780 unsigned OpNum = (Index == 0) ? 1 : 0;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003781 return getShuffleScalarElt(V.getOperand(OpNum).getNode(), Index, DAG,
3782 Depth+1);
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00003783 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003784 default:
3785 assert("not implemented for target shuffle node");
3786 return SDValue();
3787 }
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003788
3789 Index = ShuffleMask[Index];
3790 if (Index < 0)
3791 return DAG.getUNDEF(VT.getVectorElementType());
3792
3793 SDValue NewV = (Index < NumElems) ? N->getOperand(0) : N->getOperand(1);
3794 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG,
3795 Depth+1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003796 }
3797
3798 // Actual nodes that may contain scalar elements
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003799 if (Opcode == ISD::BITCAST) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003800 V = V.getOperand(0);
3801 EVT SrcVT = V.getValueType();
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003802 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003803
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003804 if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003805 return SDValue();
3806 }
3807
3808 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
3809 return (Index == 0) ? V.getOperand(0)
3810 : DAG.getUNDEF(VT.getVectorElementType());
3811
3812 if (V.getOpcode() == ISD::BUILD_VECTOR)
3813 return V.getOperand(Index);
3814
3815 return SDValue();
3816}
3817
3818/// getNumOfConsecutiveZeros - Return the number of elements of a vector
3819/// shuffle operation which come from a consecutively from a zero. The
3820/// search can start in two diferent directions, from left or right.
3821static
3822unsigned getNumOfConsecutiveZeros(SDNode *N, int NumElems,
3823 bool ZerosFromLeft, SelectionDAG &DAG) {
3824 int i = 0;
3825
3826 while (i < NumElems) {
3827 unsigned Index = ZerosFromLeft ? i : NumElems-i-1;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003828 SDValue Elt = getShuffleScalarElt(N, Index, DAG, 0);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003829 if (!(Elt.getNode() &&
3830 (Elt.getOpcode() == ISD::UNDEF || X86::isZeroNode(Elt))))
3831 break;
3832 ++i;
3833 }
3834
3835 return i;
3836}
3837
3838/// isShuffleMaskConsecutive - Check if the shuffle mask indicies from MaskI to
3839/// MaskE correspond consecutively to elements from one of the vector operands,
3840/// starting from its index OpIdx. Also tell OpNum which source vector operand.
3841static
3842bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp, int MaskI, int MaskE,
3843 int OpIdx, int NumElems, unsigned &OpNum) {
3844 bool SeenV1 = false;
3845 bool SeenV2 = false;
3846
3847 for (int i = MaskI; i <= MaskE; ++i, ++OpIdx) {
3848 int Idx = SVOp->getMaskElt(i);
3849 // Ignore undef indicies
3850 if (Idx < 0)
3851 continue;
3852
3853 if (Idx < NumElems)
3854 SeenV1 = true;
3855 else
3856 SeenV2 = true;
3857
3858 // Only accept consecutive elements from the same vector
3859 if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
3860 return false;
3861 }
3862
3863 OpNum = SeenV1 ? 0 : 1;
3864 return true;
3865}
3866
3867/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
3868/// logical left shift of a vector.
3869static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
3870 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
3871 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
3872 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
3873 false /* check zeros from right */, DAG);
3874 unsigned OpSrc;
3875
3876 if (!NumZeros)
3877 return false;
3878
3879 // Considering the elements in the mask that are not consecutive zeros,
3880 // check if they consecutively come from only one of the source vectors.
3881 //
3882 // V1 = {X, A, B, C} 0
3883 // \ \ \ /
3884 // vector_shuffle V1, V2 <1, 2, 3, X>
3885 //
3886 if (!isShuffleMaskConsecutive(SVOp,
3887 0, // Mask Start Index
3888 NumElems-NumZeros-1, // Mask End Index
3889 NumZeros, // Where to start looking in the src vector
3890 NumElems, // Number of elements in vector
3891 OpSrc)) // Which source operand ?
3892 return false;
3893
3894 isLeft = false;
3895 ShAmt = NumZeros;
3896 ShVal = SVOp->getOperand(OpSrc);
3897 return true;
3898}
3899
3900/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
3901/// logical left shift of a vector.
3902static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
3903 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
3904 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
3905 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
3906 true /* check zeros from left */, DAG);
3907 unsigned OpSrc;
3908
3909 if (!NumZeros)
3910 return false;
3911
3912 // Considering the elements in the mask that are not consecutive zeros,
3913 // check if they consecutively come from only one of the source vectors.
3914 //
3915 // 0 { A, B, X, X } = V2
3916 // / \ / /
3917 // vector_shuffle V1, V2 <X, X, 4, 5>
3918 //
3919 if (!isShuffleMaskConsecutive(SVOp,
3920 NumZeros, // Mask Start Index
3921 NumElems-1, // Mask End Index
3922 0, // Where to start looking in the src vector
3923 NumElems, // Number of elements in vector
3924 OpSrc)) // Which source operand ?
3925 return false;
3926
3927 isLeft = true;
3928 ShAmt = NumZeros;
3929 ShVal = SVOp->getOperand(OpSrc);
3930 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00003931}
3932
3933/// isVectorShift - Returns true if the shuffle can be implemented as a
3934/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003935static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00003936 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003937 if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
3938 isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
3939 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00003940
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003941 return false;
Evan Chengf26ffe92008-05-29 08:22:04 +00003942}
3943
Evan Chengc78d3b42006-04-24 18:01:45 +00003944/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
3945///
Dan Gohman475871a2008-07-27 21:46:04 +00003946static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00003947 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00003948 SelectionDAG &DAG,
3949 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003950 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00003951 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00003952
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003953 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003954 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003955 bool First = true;
3956 for (unsigned i = 0; i < 16; ++i) {
3957 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
3958 if (ThisIsNonZero && 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 }
3965
3966 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00003967 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003968 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
3969 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003970 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003971 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00003972 }
3973 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003974 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
3975 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
3976 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00003977 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003978 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00003979 } else
3980 ThisElt = LastElt;
3981
Gabor Greifba36cb52008-08-28 21:40:38 +00003982 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00003983 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00003984 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00003985 }
3986 }
3987
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003988 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00003989}
3990
Bill Wendlinga348c562007-03-22 18:42:45 +00003991/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00003992///
Dan Gohman475871a2008-07-27 21:46:04 +00003993static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00003994 unsigned NumNonZero, unsigned NumZero,
3995 SelectionDAG &DAG,
3996 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003997 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00003998 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00003999
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004000 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004001 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004002 bool First = true;
4003 for (unsigned i = 0; i < 8; ++i) {
4004 bool isNonZero = (NonZeros & (1 << i)) != 0;
4005 if (isNonZero) {
4006 if (First) {
4007 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004008 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004009 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004010 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004011 First = false;
4012 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004013 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004014 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00004015 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00004016 }
4017 }
4018
4019 return V;
4020}
4021
Evan Chengf26ffe92008-05-29 08:22:04 +00004022/// getVShift - Return a vector logical shift node.
4023///
Owen Andersone50ed302009-08-10 22:56:29 +00004024static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00004025 unsigned NumBits, SelectionDAG &DAG,
4026 const TargetLowering &TLI, DebugLoc dl) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004027 EVT ShVT = MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00004028 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004029 SrcOp = DAG.getNode(ISD::BITCAST, dl, ShVT, SrcOp);
4030 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004031 DAG.getNode(Opc, dl, ShVT, SrcOp,
Gabor Greif327ef032008-08-28 23:19:51 +00004032 DAG.getConstant(NumBits, TLI.getShiftAmountTy())));
Evan Chengf26ffe92008-05-29 08:22:04 +00004033}
4034
Dan Gohman475871a2008-07-27 21:46:04 +00004035SDValue
Evan Chengc3630942009-12-09 21:00:30 +00004036X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00004037 SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00004038
Evan Chengc3630942009-12-09 21:00:30 +00004039 // Check if the scalar load can be widened into a vector load. And if
4040 // the address is "base + cst" see if the cst can be "absorbed" into
4041 // the shuffle mask.
4042 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
4043 SDValue Ptr = LD->getBasePtr();
4044 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
4045 return SDValue();
4046 EVT PVT = LD->getValueType(0);
4047 if (PVT != MVT::i32 && PVT != MVT::f32)
4048 return SDValue();
4049
4050 int FI = -1;
4051 int64_t Offset = 0;
4052 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
4053 FI = FINode->getIndex();
4054 Offset = 0;
4055 } else if (Ptr.getOpcode() == ISD::ADD &&
4056 isa<ConstantSDNode>(Ptr.getOperand(1)) &&
4057 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4058 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4059 Offset = Ptr.getConstantOperandVal(1);
4060 Ptr = Ptr.getOperand(0);
4061 } else {
4062 return SDValue();
4063 }
4064
4065 SDValue Chain = LD->getChain();
4066 // Make sure the stack object alignment is at least 16.
4067 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
4068 if (DAG.InferPtrAlignment(Ptr) < 16) {
4069 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00004070 // Can't change the alignment. FIXME: It's possible to compute
4071 // the exact stack offset and reference FI + adjust offset instead.
4072 // If someone *really* cares about this. That's the way to implement it.
4073 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004074 } else {
4075 MFI->setObjectAlignment(FI, 16);
4076 }
4077 }
4078
4079 // (Offset % 16) must be multiple of 4. Then address is then
4080 // Ptr + (Offset & ~15).
4081 if (Offset < 0)
4082 return SDValue();
4083 if ((Offset % 16) & 3)
4084 return SDValue();
4085 int64_t StartOffset = Offset & ~15;
4086 if (StartOffset)
4087 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
4088 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
4089
4090 int EltNo = (Offset - StartOffset) >> 2;
4091 int Mask[4] = { EltNo, EltNo, EltNo, EltNo };
4092 EVT VT = (PVT == MVT::i32) ? MVT::v4i32 : MVT::v4f32;
Chris Lattner51abfe42010-09-21 06:02:19 +00004093 SDValue V1 = DAG.getLoad(VT, dl, Chain, Ptr,
4094 LD->getPointerInfo().getWithOffset(StartOffset),
David Greene67c9d422010-02-15 16:53:33 +00004095 false, false, 0);
Evan Chengc3630942009-12-09 21:00:30 +00004096 // Canonicalize it to a v4i32 shuffle.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004097 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, V1);
4098 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Chengc3630942009-12-09 21:00:30 +00004099 DAG.getVectorShuffle(MVT::v4i32, dl, V1,
Chris Lattner51abfe42010-09-21 06:02:19 +00004100 DAG.getUNDEF(MVT::v4i32),&Mask[0]));
Evan Chengc3630942009-12-09 21:00:30 +00004101 }
4102
4103 return SDValue();
4104}
4105
Michael J. Spencerec38de22010-10-10 22:04:20 +00004106/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
4107/// vector of type 'VT', see if the elements can be replaced by a single large
Nate Begeman1449f292010-03-24 22:19:06 +00004108/// load which has the same value as a build_vector whose operands are 'elts'.
4109///
4110/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
Michael J. Spencerec38de22010-10-10 22:04:20 +00004111///
Nate Begeman1449f292010-03-24 22:19:06 +00004112/// FIXME: we'd also like to handle the case where the last elements are zero
4113/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
4114/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004115static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
Chris Lattner88641552010-09-22 00:34:38 +00004116 DebugLoc &DL, SelectionDAG &DAG) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00004117 EVT EltVT = VT.getVectorElementType();
4118 unsigned NumElems = Elts.size();
Michael J. Spencerec38de22010-10-10 22:04:20 +00004119
Nate Begemanfdea31a2010-03-24 20:49:50 +00004120 LoadSDNode *LDBase = NULL;
4121 unsigned LastLoadedElt = -1U;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004122
Nate Begeman1449f292010-03-24 22:19:06 +00004123 // For each element in the initializer, see if we've found a load or an undef.
Michael J. Spencerec38de22010-10-10 22:04:20 +00004124 // If we don't find an initial load element, or later load elements are
Nate Begeman1449f292010-03-24 22:19:06 +00004125 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004126 for (unsigned i = 0; i < NumElems; ++i) {
4127 SDValue Elt = Elts[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00004128
Nate Begemanfdea31a2010-03-24 20:49:50 +00004129 if (!Elt.getNode() ||
4130 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
4131 return SDValue();
4132 if (!LDBase) {
4133 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
4134 return SDValue();
4135 LDBase = cast<LoadSDNode>(Elt.getNode());
4136 LastLoadedElt = i;
4137 continue;
4138 }
4139 if (Elt.getOpcode() == ISD::UNDEF)
4140 continue;
4141
4142 LoadSDNode *LD = cast<LoadSDNode>(Elt);
4143 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
4144 return SDValue();
4145 LastLoadedElt = i;
4146 }
Nate Begeman1449f292010-03-24 22:19:06 +00004147
4148 // If we have found an entire vector of loads and undefs, then return a large
4149 // load of the entire vector width starting at the base pointer. If we found
4150 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004151 if (LastLoadedElt == NumElems - 1) {
4152 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
Chris Lattner88641552010-09-22 00:34:38 +00004153 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004154 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004155 LDBase->isVolatile(), LDBase->isNonTemporal(), 0);
Chris Lattner88641552010-09-22 00:34:38 +00004156 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004157 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004158 LDBase->isVolatile(), LDBase->isNonTemporal(),
4159 LDBase->getAlignment());
4160 } else if (NumElems == 4 && LastLoadedElt == 1) {
4161 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
4162 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
Chris Lattner88641552010-09-22 00:34:38 +00004163 SDValue ResNode = DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys,
4164 Ops, 2, MVT::i32,
4165 LDBase->getMemOperand());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004166 return DAG.getNode(ISD::BITCAST, DL, VT, ResNode);
Nate Begemanfdea31a2010-03-24 20:49:50 +00004167 }
4168 return SDValue();
4169}
4170
Evan Chengc3630942009-12-09 21:00:30 +00004171SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004172X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004173 DebugLoc dl = Op.getDebugLoc();
Chris Lattner6e80e442010-08-28 17:15:43 +00004174 // All zero's are handled with pxor in SSE2 and above, xorps in SSE1.
4175 // All one's are handled with pcmpeqd. In AVX, zero's are handled with
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004176 // vpxor in 128-bit and xor{pd,ps} in 256-bit, but no 256 version of pcmpeqd
4177 // is present, so AllOnes is ignored.
4178 if (ISD::isBuildVectorAllZeros(Op.getNode()) ||
4179 (Op.getValueType().getSizeInBits() != 256 &&
4180 ISD::isBuildVectorAllOnes(Op.getNode()))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004181 // Canonicalize this to <4 x i32> (SSE) to
Chris Lattner8a594482007-11-25 00:24:49 +00004182 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
4183 // eliminated on x86-32 hosts.
Dale Johannesen0488fb62010-09-30 23:57:10 +00004184 if (Op.getValueType() == MVT::v4i32)
Chris Lattner8a594482007-11-25 00:24:49 +00004185 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004186
Gabor Greifba36cb52008-08-28 21:40:38 +00004187 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00004188 return getOnesVector(Op.getValueType(), DAG, dl);
4189 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00004190 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004191
Owen Andersone50ed302009-08-10 22:56:29 +00004192 EVT VT = Op.getValueType();
4193 EVT ExtVT = VT.getVectorElementType();
4194 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004195
4196 unsigned NumElems = Op.getNumOperands();
4197 unsigned NumZero = 0;
4198 unsigned NumNonZero = 0;
4199 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004200 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00004201 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004202 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00004203 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00004204 if (Elt.getOpcode() == ISD::UNDEF)
4205 continue;
4206 Values.insert(Elt);
4207 if (Elt.getOpcode() != ISD::Constant &&
4208 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004209 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00004210 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00004211 NumZero++;
4212 else {
4213 NonZeros |= (1 << i);
4214 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004215 }
4216 }
4217
Chris Lattner97a2a562010-08-26 05:24:29 +00004218 // All undef vector. Return an UNDEF. All zero vectors were handled above.
4219 if (NumNonZero == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00004220 return DAG.getUNDEF(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004221
Chris Lattner67f453a2008-03-09 05:42:06 +00004222 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00004223 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004224 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00004225 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00004226
Chris Lattner62098042008-03-09 01:05:04 +00004227 // If this is an insertion of an i64 value on x86-32, and if the top bits of
4228 // the value are obviously zero, truncate the value to i32 and do the
4229 // insertion that way. Only do this if the value is non-constant or if the
4230 // value is a constant being inserted into element 0. It is cheaper to do
4231 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00004232 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00004233 (!IsAllConstants || Idx == 0)) {
4234 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004235 // Handle SSE only.
4236 assert(VT == MVT::v2i64 && "Expected an SSE value type!");
4237 EVT VecVT = MVT::v4i32;
4238 unsigned VecElts = 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00004239
Chris Lattner62098042008-03-09 01:05:04 +00004240 // Truncate the value (which may itself be a constant) to i32, and
4241 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00004242 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00004243 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00004244 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
4245 Subtarget->hasSSE2(), DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00004246
Chris Lattner62098042008-03-09 01:05:04 +00004247 // Now we have our 32-bit value zero extended in the low element of
4248 // a vector. If Idx != 0, swizzle it into place.
4249 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004250 SmallVector<int, 4> Mask;
4251 Mask.push_back(Idx);
4252 for (unsigned i = 1; i != VecElts; ++i)
4253 Mask.push_back(i);
4254 Item = DAG.getVectorShuffle(VecVT, dl, Item,
Eric Christopherfd179292009-08-27 18:07:15 +00004255 DAG.getUNDEF(Item.getValueType()),
Nate Begeman9008ca62009-04-27 18:41:29 +00004256 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00004257 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004258 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00004259 }
4260 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004261
Chris Lattner19f79692008-03-08 22:59:52 +00004262 // If we have a constant or non-constant insertion into the low element of
4263 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
4264 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00004265 // depending on what the source datatype is.
4266 if (Idx == 0) {
4267 if (NumZero == 0) {
4268 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Owen Anderson825b72b2009-08-11 20:47:22 +00004269 } else if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
4270 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Eli Friedman10415532009-06-06 06:05:10 +00004271 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
4272 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
4273 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget->hasSSE2(),
4274 DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00004275 } else if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
4276 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
Dale Johannesen0488fb62010-09-30 23:57:10 +00004277 assert(VT.getSizeInBits() == 128 && "Expected an SSE value type!");
4278 EVT MiddleVT = MVT::v4i32;
Eli Friedman10415532009-06-06 06:05:10 +00004279 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MiddleVT, Item);
4280 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
4281 Subtarget->hasSSE2(), DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004282 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Eli Friedman10415532009-06-06 06:05:10 +00004283 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004284 }
Evan Chengf26ffe92008-05-29 08:22:04 +00004285
4286 // Is it a vector logical left shift?
4287 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00004288 X86::isZeroNode(Op.getOperand(0)) &&
4289 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004290 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00004291 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00004292 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004293 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00004294 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004295 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004296
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004297 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00004298 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004299
Chris Lattner19f79692008-03-08 22:59:52 +00004300 // Otherwise, if this is a vector with i32 or f32 elements, and the element
4301 // is a non-constant being inserted into an element other than the low one,
4302 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
4303 // movd/movss) to move this into the low element, then shuffle it into
4304 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004305 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00004306 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00004307
Evan Cheng0db9fe62006-04-25 20:13:52 +00004308 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00004309 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
4310 Subtarget->hasSSE2(), DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00004311 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004312 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00004313 MaskVec.push_back(i == Idx ? 0 : 1);
4314 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004315 }
4316 }
4317
Chris Lattner67f453a2008-03-09 05:42:06 +00004318 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00004319 if (Values.size() == 1) {
4320 if (EVTBits == 32) {
4321 // Instead of a shuffle like this:
4322 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
4323 // Check if it's possible to issue this instead.
4324 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
4325 unsigned Idx = CountTrailingZeros_32(NonZeros);
4326 SDValue Item = Op.getOperand(Idx);
4327 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
4328 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
4329 }
Dan Gohman475871a2008-07-27 21:46:04 +00004330 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004331 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004332
Dan Gohmana3941172007-07-24 22:55:08 +00004333 // A vector full of immediates; various special cases are already
4334 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004335 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00004336 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00004337
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00004338 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00004339 if (EVTBits == 64) {
4340 if (NumNonZero == 1) {
4341 // One half is zero or undef.
4342 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00004343 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00004344 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00004345 return getShuffleVectorZeroOrUndef(V2, Idx, true,
4346 Subtarget->hasSSE2(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00004347 }
Dan Gohman475871a2008-07-27 21:46:04 +00004348 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00004349 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004350
4351 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00004352 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00004353 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00004354 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004355 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004356 }
4357
Bill Wendling826f36f2007-03-28 00:57:11 +00004358 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00004359 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Chris Lattner97a2a562010-08-26 05:24:29 +00004360 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004361 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004362 }
4363
4364 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00004365 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00004366 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004367 if (NumElems == 4 && NumZero > 0) {
4368 for (unsigned i = 0; i < 4; ++i) {
4369 bool isZero = !(NonZeros & (1 << i));
4370 if (isZero)
Dale Johannesenace16102009-02-03 19:33:06 +00004371 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004372 else
Dale Johannesenace16102009-02-03 19:33:06 +00004373 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004374 }
4375
4376 for (unsigned i = 0; i < 2; ++i) {
4377 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
4378 default: break;
4379 case 0:
4380 V[i] = V[i*2]; // Must be a zero vector.
4381 break;
4382 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00004383 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004384 break;
4385 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00004386 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004387 break;
4388 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00004389 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004390 break;
4391 }
4392 }
4393
Nate Begeman9008ca62009-04-27 18:41:29 +00004394 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004395 bool Reverse = (NonZeros & 0x3) == 2;
4396 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004397 MaskVec.push_back(Reverse ? 1-i : i);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004398 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
4399 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004400 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
4401 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004402 }
4403
Nate Begemanfdea31a2010-03-24 20:49:50 +00004404 if (Values.size() > 1 && VT.getSizeInBits() == 128) {
4405 // Check for a build vector of consecutive loads.
4406 for (unsigned i = 0; i < NumElems; ++i)
4407 V[i] = Op.getOperand(i);
Michael J. Spencerec38de22010-10-10 22:04:20 +00004408
Nate Begemanfdea31a2010-03-24 20:49:50 +00004409 // Check for elements which are consecutive loads.
4410 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
4411 if (LD.getNode())
4412 return LD;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004413
4414 // For SSE 4.1, use insertps to put the high elements into the low element.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004415 if (getSubtarget()->hasSSE41()) {
Chris Lattner24faf612010-08-28 17:59:08 +00004416 SDValue Result;
4417 if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
4418 Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
4419 else
4420 Result = DAG.getUNDEF(VT);
Michael J. Spencerec38de22010-10-10 22:04:20 +00004421
Chris Lattner24faf612010-08-28 17:59:08 +00004422 for (unsigned i = 1; i < NumElems; ++i) {
4423 if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
4424 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
Nate Begeman9008ca62009-04-27 18:41:29 +00004425 Op.getOperand(i), DAG.getIntPtrConstant(i));
Chris Lattner24faf612010-08-28 17:59:08 +00004426 }
4427 return Result;
Nate Begeman9008ca62009-04-27 18:41:29 +00004428 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00004429
Chris Lattner6e80e442010-08-28 17:15:43 +00004430 // Otherwise, expand into a number of unpckl*, start by extending each of
4431 // our (non-undef) elements to the full vector width with the element in the
4432 // bottom slot of the vector (which generates no code for SSE).
4433 for (unsigned i = 0; i < NumElems; ++i) {
4434 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
4435 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
4436 else
4437 V[i] = DAG.getUNDEF(VT);
4438 }
4439
4440 // Next, we iteratively mix elements, e.g. for v4f32:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004441 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
4442 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
4443 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Chris Lattner6e80e442010-08-28 17:15:43 +00004444 unsigned EltStride = NumElems >> 1;
4445 while (EltStride != 0) {
Chris Lattner3ddcc432010-08-28 17:28:30 +00004446 for (unsigned i = 0; i < EltStride; ++i) {
4447 // If V[i+EltStride] is undef and this is the first round of mixing,
4448 // then it is safe to just drop this shuffle: V[i] is already in the
4449 // right place, the one element (since it's the first round) being
4450 // inserted as undef can be dropped. This isn't safe for successive
4451 // rounds because they will permute elements within both vectors.
4452 if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
4453 EltStride == NumElems/2)
4454 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004455
Chris Lattner6e80e442010-08-28 17:15:43 +00004456 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
Chris Lattner3ddcc432010-08-28 17:28:30 +00004457 }
Chris Lattner6e80e442010-08-28 17:15:43 +00004458 EltStride >>= 1;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004459 }
4460 return V[0];
4461 }
Dan Gohman475871a2008-07-27 21:46:04 +00004462 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004463}
4464
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004465SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004466X86TargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const {
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004467 // We support concatenate two MMX registers and place them in a MMX
4468 // register. This is better than doing a stack convert.
4469 DebugLoc dl = Op.getDebugLoc();
4470 EVT ResVT = Op.getValueType();
4471 assert(Op.getNumOperands() == 2);
4472 assert(ResVT == MVT::v2i64 || ResVT == MVT::v4i32 ||
4473 ResVT == MVT::v8i16 || ResVT == MVT::v16i8);
4474 int Mask[2];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004475 SDValue InVec = DAG.getNode(ISD::BITCAST,dl, MVT::v1i64, Op.getOperand(0));
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004476 SDValue VecOp = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
4477 InVec = Op.getOperand(1);
4478 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4479 unsigned NumElts = ResVT.getVectorNumElements();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004480 VecOp = DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004481 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, ResVT, VecOp,
4482 InVec.getOperand(0), DAG.getIntPtrConstant(NumElts/2+1));
4483 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004484 InVec = DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, InVec);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004485 SDValue VecOp2 = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
4486 Mask[0] = 0; Mask[1] = 2;
4487 VecOp = DAG.getVectorShuffle(MVT::v2i64, dl, VecOp, VecOp2, Mask);
4488 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004489 return DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004490}
4491
Nate Begemanb9a47b82009-02-23 08:49:38 +00004492// v8i16 shuffles - Prefer shuffles in the following order:
4493// 1. [all] pshuflw, pshufhw, optional move
4494// 2. [ssse3] 1 x pshufb
4495// 3. [ssse3] 2 x pshufb + 1 x por
4496// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004497SDValue
4498X86TargetLowering::LowerVECTOR_SHUFFLEv8i16(SDValue Op,
4499 SelectionDAG &DAG) const {
4500 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00004501 SDValue V1 = SVOp->getOperand(0);
4502 SDValue V2 = SVOp->getOperand(1);
4503 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00004504 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00004505
Nate Begemanb9a47b82009-02-23 08:49:38 +00004506 // Determine if more than 1 of the words in each of the low and high quadwords
4507 // of the result come from the same quadword of one of the two inputs. Undef
4508 // mask values count as coming from any quadword, for better codegen.
4509 SmallVector<unsigned, 4> LoQuad(4);
4510 SmallVector<unsigned, 4> HiQuad(4);
4511 BitVector InputQuads(4);
4512 for (unsigned i = 0; i < 8; ++i) {
4513 SmallVectorImpl<unsigned> &Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00004514 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004515 MaskVals.push_back(EltIdx);
4516 if (EltIdx < 0) {
4517 ++Quad[0];
4518 ++Quad[1];
4519 ++Quad[2];
4520 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00004521 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004522 }
4523 ++Quad[EltIdx / 4];
4524 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00004525 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004526
Nate Begemanb9a47b82009-02-23 08:49:38 +00004527 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00004528 unsigned MaxQuad = 1;
4529 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004530 if (LoQuad[i] > MaxQuad) {
4531 BestLoQuad = i;
4532 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00004533 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004534 }
4535
Nate Begemanb9a47b82009-02-23 08:49:38 +00004536 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00004537 MaxQuad = 1;
4538 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004539 if (HiQuad[i] > MaxQuad) {
4540 BestHiQuad = i;
4541 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00004542 }
4543 }
4544
Nate Begemanb9a47b82009-02-23 08:49:38 +00004545 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00004546 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00004547 // single pshufb instruction is necessary. If There are more than 2 input
4548 // quads, disable the next transformation since it does not help SSSE3.
4549 bool V1Used = InputQuads[0] || InputQuads[1];
4550 bool V2Used = InputQuads[2] || InputQuads[3];
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004551 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004552 if (InputQuads.count() == 2 && V1Used && V2Used) {
4553 BestLoQuad = InputQuads.find_first();
4554 BestHiQuad = InputQuads.find_next(BestLoQuad);
4555 }
4556 if (InputQuads.count() > 2) {
4557 BestLoQuad = -1;
4558 BestHiQuad = -1;
4559 }
4560 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004561
Nate Begemanb9a47b82009-02-23 08:49:38 +00004562 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
4563 // the shuffle mask. If a quad is scored as -1, that means that it contains
4564 // words from all 4 input quadwords.
4565 SDValue NewV;
4566 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004567 SmallVector<int, 8> MaskV;
4568 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
4569 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
Eric Christopherfd179292009-08-27 18:07:15 +00004570 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004571 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V1),
4572 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V2), &MaskV[0]);
4573 NewV = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00004574
Nate Begemanb9a47b82009-02-23 08:49:38 +00004575 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
4576 // source words for the shuffle, to aid later transformations.
4577 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00004578 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00004579 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004580 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00004581 if (idx != (int)i)
4582 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004583 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00004584 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004585 AllWordsInNewV = false;
4586 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00004587 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004588
Nate Begemanb9a47b82009-02-23 08:49:38 +00004589 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
4590 if (AllWordsInNewV) {
4591 for (int i = 0; i != 8; ++i) {
4592 int idx = MaskVals[i];
4593 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00004594 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004595 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004596 if ((idx != i) && idx < 4)
4597 pshufhw = false;
4598 if ((idx != i) && idx > 3)
4599 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00004600 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00004601 V1 = NewV;
4602 V2Used = false;
4603 BestLoQuad = 0;
4604 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004605 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004606
Nate Begemanb9a47b82009-02-23 08:49:38 +00004607 // If we've eliminated the use of V2, and the new mask is a pshuflw or
4608 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00004609 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00004610 unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
4611 unsigned TargetMask = 0;
4612 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00004613 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00004614 TargetMask = pshufhw ? X86::getShufflePSHUFHWImmediate(NewV.getNode()):
4615 X86::getShufflePSHUFLWImmediate(NewV.getNode());
4616 V1 = NewV.getOperand(0);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00004617 return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
Evan Cheng14b32e12007-12-11 01:46:18 +00004618 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004619 }
Eric Christopherfd179292009-08-27 18:07:15 +00004620
Nate Begemanb9a47b82009-02-23 08:49:38 +00004621 // If we have SSSE3, and all words of the result are from 1 input vector,
4622 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
4623 // is present, fall back to case 4.
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004624 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004625 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00004626
Nate Begemanb9a47b82009-02-23 08:49:38 +00004627 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00004628 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00004629 // mask, and elements that come from V1 in the V2 mask, so that the two
4630 // results can be OR'd together.
4631 bool TwoInputs = V1Used && V2Used;
4632 for (unsigned i = 0; i != 8; ++i) {
4633 int EltIdx = MaskVals[i] * 2;
4634 if (TwoInputs && (EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004635 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4636 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004637 continue;
4638 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004639 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
4640 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004641 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004642 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004643 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00004644 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004645 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004646 if (!TwoInputs)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004647 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004648
Nate Begemanb9a47b82009-02-23 08:49:38 +00004649 // Calculate the shuffle mask for the second input, shuffle it, and
4650 // OR it with the first shuffled input.
4651 pshufbMask.clear();
4652 for (unsigned i = 0; i != 8; ++i) {
4653 int EltIdx = MaskVals[i] * 2;
4654 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004655 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4656 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004657 continue;
4658 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004659 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
4660 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004661 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004662 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00004663 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00004664 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004665 MVT::v16i8, &pshufbMask[0], 16));
4666 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004667 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004668 }
4669
4670 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
4671 // and update MaskVals with new element order.
4672 BitVector InOrder(8);
4673 if (BestLoQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004674 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004675 for (int i = 0; i != 4; ++i) {
4676 int idx = MaskVals[i];
4677 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004678 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004679 InOrder.set(i);
4680 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004681 MaskV.push_back(idx & 3);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004682 InOrder.set(i);
4683 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004684 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004685 }
4686 }
4687 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004688 MaskV.push_back(i);
Owen Anderson825b72b2009-08-11 20:47:22 +00004689 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00004690 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00004691
4692 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3())
4693 NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
4694 NewV.getOperand(0),
4695 X86::getShufflePSHUFLWImmediate(NewV.getNode()),
4696 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004697 }
Eric Christopherfd179292009-08-27 18:07:15 +00004698
Nate Begemanb9a47b82009-02-23 08:49:38 +00004699 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
4700 // and update MaskVals with the new element order.
4701 if (BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004702 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004703 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004704 MaskV.push_back(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004705 for (unsigned i = 4; i != 8; ++i) {
4706 int idx = MaskVals[i];
4707 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004708 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004709 InOrder.set(i);
4710 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004711 MaskV.push_back((idx & 3) + 4);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004712 InOrder.set(i);
4713 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004714 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004715 }
4716 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004717 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00004718 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00004719
4720 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3())
4721 NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
4722 NewV.getOperand(0),
4723 X86::getShufflePSHUFHWImmediate(NewV.getNode()),
4724 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004725 }
Eric Christopherfd179292009-08-27 18:07:15 +00004726
Nate Begemanb9a47b82009-02-23 08:49:38 +00004727 // In case BestHi & BestLo were both -1, which means each quadword has a word
4728 // from each of the four input quadwords, calculate the InOrder bitvector now
4729 // before falling through to the insert/extract cleanup.
4730 if (BestLoQuad == -1 && BestHiQuad == -1) {
4731 NewV = V1;
4732 for (int i = 0; i != 8; ++i)
4733 if (MaskVals[i] < 0 || MaskVals[i] == i)
4734 InOrder.set(i);
4735 }
Eric Christopherfd179292009-08-27 18:07:15 +00004736
Nate Begemanb9a47b82009-02-23 08:49:38 +00004737 // The other elements are put in the right place using pextrw and pinsrw.
4738 for (unsigned i = 0; i != 8; ++i) {
4739 if (InOrder[i])
4740 continue;
4741 int EltIdx = MaskVals[i];
4742 if (EltIdx < 0)
4743 continue;
4744 SDValue ExtOp = (EltIdx < 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00004745 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004746 DAG.getIntPtrConstant(EltIdx))
Owen Anderson825b72b2009-08-11 20:47:22 +00004747 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004748 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00004749 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004750 DAG.getIntPtrConstant(i));
4751 }
4752 return NewV;
4753}
4754
4755// v16i8 shuffles - Prefer shuffles in the following order:
4756// 1. [ssse3] 1 x pshufb
4757// 2. [ssse3] 2 x pshufb + 1 x por
4758// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
4759static
Nate Begeman9008ca62009-04-27 18:41:29 +00004760SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00004761 SelectionDAG &DAG,
4762 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004763 SDValue V1 = SVOp->getOperand(0);
4764 SDValue V2 = SVOp->getOperand(1);
4765 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00004766 SmallVector<int, 16> MaskVals;
Nate Begeman9008ca62009-04-27 18:41:29 +00004767 SVOp->getMask(MaskVals);
Eric Christopherfd179292009-08-27 18:07:15 +00004768
Nate Begemanb9a47b82009-02-23 08:49:38 +00004769 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00004770 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00004771 // present, fall back to case 3.
4772 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
4773 bool V1Only = true;
4774 bool V2Only = true;
4775 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004776 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00004777 if (EltIdx < 0)
4778 continue;
4779 if (EltIdx < 16)
4780 V2Only = false;
4781 else
4782 V1Only = false;
4783 }
Eric Christopherfd179292009-08-27 18:07:15 +00004784
Nate Begemanb9a47b82009-02-23 08:49:38 +00004785 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
4786 if (TLI.getSubtarget()->hasSSSE3()) {
4787 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00004788
Nate Begemanb9a47b82009-02-23 08:49:38 +00004789 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00004790 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00004791 //
4792 // Otherwise, we have elements from both input vectors, and must zero out
4793 // elements that come from V2 in the first mask, and V1 in the second mask
4794 // so that we can OR them together.
4795 bool TwoInputs = !(V1Only || V2Only);
4796 for (unsigned i = 0; i != 16; ++i) {
4797 int EltIdx = MaskVals[i];
4798 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004799 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004800 continue;
4801 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004802 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004803 }
4804 // If all the elements are from V2, assign it to V1 and return after
4805 // building the first pshufb.
4806 if (V2Only)
4807 V1 = V2;
Owen Anderson825b72b2009-08-11 20:47:22 +00004808 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00004809 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004810 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004811 if (!TwoInputs)
4812 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00004813
Nate Begemanb9a47b82009-02-23 08:49:38 +00004814 // Calculate the shuffle mask for the second input, shuffle it, and
4815 // OR it with the first shuffled input.
4816 pshufbMask.clear();
4817 for (unsigned i = 0; i != 16; ++i) {
4818 int EltIdx = MaskVals[i];
4819 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004820 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004821 continue;
4822 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004823 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004824 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004825 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00004826 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004827 MVT::v16i8, &pshufbMask[0], 16));
4828 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004829 }
Eric Christopherfd179292009-08-27 18:07:15 +00004830
Nate Begemanb9a47b82009-02-23 08:49:38 +00004831 // No SSSE3 - Calculate in place words and then fix all out of place words
4832 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
4833 // the 16 different words that comprise the two doublequadword input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004834 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
4835 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004836 SDValue NewV = V2Only ? V2 : V1;
4837 for (int i = 0; i != 8; ++i) {
4838 int Elt0 = MaskVals[i*2];
4839 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00004840
Nate Begemanb9a47b82009-02-23 08:49:38 +00004841 // This word of the result is all undef, skip it.
4842 if (Elt0 < 0 && Elt1 < 0)
4843 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004844
Nate Begemanb9a47b82009-02-23 08:49:38 +00004845 // This word of the result is already in the correct place, skip it.
4846 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
4847 continue;
4848 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
4849 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004850
Nate Begemanb9a47b82009-02-23 08:49:38 +00004851 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
4852 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
4853 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00004854
4855 // If Elt0 and Elt1 are defined, are consecutive, and can be load
4856 // using a single extract together, load it and store it.
4857 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004858 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00004859 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00004860 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00004861 DAG.getIntPtrConstant(i));
4862 continue;
4863 }
4864
Nate Begemanb9a47b82009-02-23 08:49:38 +00004865 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00004866 // source byte is not also odd, shift the extracted word left 8 bits
4867 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00004868 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004869 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004870 DAG.getIntPtrConstant(Elt1 / 2));
4871 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004872 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004873 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00004874 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004875 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
4876 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004877 }
4878 // If Elt0 is defined, extract it from the appropriate source. If the
4879 // source byte is not also even, shift the extracted word right 8 bits. If
4880 // Elt1 was also defined, OR the extracted values together before
4881 // inserting them in the result.
4882 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004883 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004884 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
4885 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004886 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004887 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00004888 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004889 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
4890 DAG.getConstant(0x00FF, MVT::i16));
4891 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00004892 : InsElt0;
4893 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004894 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004895 DAG.getIntPtrConstant(i));
4896 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004897 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00004898}
4899
Evan Cheng7a831ce2007-12-15 03:00:47 +00004900/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00004901/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00004902/// done when every pair / quad of shuffle mask elements point to elements in
4903/// the right sequence. e.g.
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00004904/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
Evan Cheng14b32e12007-12-11 01:46:18 +00004905static
Nate Begeman9008ca62009-04-27 18:41:29 +00004906SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00004907 SelectionDAG &DAG, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00004908 EVT VT = SVOp->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00004909 SDValue V1 = SVOp->getOperand(0);
4910 SDValue V2 = SVOp->getOperand(1);
4911 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00004912 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00004913 EVT NewVT;
Owen Anderson825b72b2009-08-11 20:47:22 +00004914 switch (VT.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004915 default: assert(false && "Unexpected!");
Owen Anderson825b72b2009-08-11 20:47:22 +00004916 case MVT::v4f32: NewVT = MVT::v2f64; break;
4917 case MVT::v4i32: NewVT = MVT::v2i64; break;
4918 case MVT::v8i16: NewVT = MVT::v4i32; break;
4919 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00004920 }
4921
Nate Begeman9008ca62009-04-27 18:41:29 +00004922 int Scale = NumElems / NewWidth;
4923 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00004924 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004925 int StartIdx = -1;
4926 for (int j = 0; j < Scale; ++j) {
4927 int EltIdx = SVOp->getMaskElt(i+j);
4928 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00004929 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00004930 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00004931 StartIdx = EltIdx - (EltIdx % Scale);
4932 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00004933 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00004934 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004935 if (StartIdx == -1)
4936 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00004937 else
Nate Begeman9008ca62009-04-27 18:41:29 +00004938 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004939 }
4940
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004941 V1 = DAG.getNode(ISD::BITCAST, dl, NewVT, V1);
4942 V2 = DAG.getNode(ISD::BITCAST, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00004943 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004944}
4945
Evan Chengd880b972008-05-09 21:53:03 +00004946/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00004947///
Owen Andersone50ed302009-08-10 22:56:29 +00004948static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00004949 SDValue SrcOp, SelectionDAG &DAG,
4950 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004951 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004952 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00004953 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00004954 LD = dyn_cast<LoadSDNode>(SrcOp);
4955 if (!LD) {
4956 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
4957 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00004958 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Duncan Sandscdfad362010-11-03 12:17:33 +00004959 if ((ExtVT != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00004960 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004961 SrcOp.getOperand(0).getOpcode() == ISD::BITCAST &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00004962 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004963 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00004964 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004965 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004966 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
4967 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
4968 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00004969 SrcOp.getOperand(0)
4970 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00004971 }
4972 }
4973 }
4974
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004975 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004976 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004977 DAG.getNode(ISD::BITCAST, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00004978 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00004979}
4980
Evan Chengace3c172008-07-22 21:13:36 +00004981/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
4982/// shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00004983static SDValue
Nate Begeman9008ca62009-04-27 18:41:29 +00004984LowerVECTOR_SHUFFLE_4wide(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
4985 SDValue V1 = SVOp->getOperand(0);
4986 SDValue V2 = SVOp->getOperand(1);
4987 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00004988 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00004989
Evan Chengace3c172008-07-22 21:13:36 +00004990 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00004991 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00004992 SmallVector<int, 8> Mask1(4U, -1);
4993 SmallVector<int, 8> PermMask;
4994 SVOp->getMask(PermMask);
4995
Evan Chengace3c172008-07-22 21:13:36 +00004996 unsigned NumHi = 0;
4997 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00004998 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004999 int Idx = PermMask[i];
5000 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00005001 Locs[i] = std::make_pair(-1, -1);
5002 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005003 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
5004 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00005005 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00005006 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005007 NumLo++;
5008 } else {
5009 Locs[i] = std::make_pair(1, NumHi);
5010 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00005011 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005012 NumHi++;
5013 }
5014 }
5015 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005016
Evan Chengace3c172008-07-22 21:13:36 +00005017 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005018 // If no more than two elements come from either vector. This can be
5019 // implemented with two shuffles. First shuffle gather the elements.
5020 // The second shuffle, which takes the first shuffle as both of its
5021 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00005022 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005023
Nate Begeman9008ca62009-04-27 18:41:29 +00005024 SmallVector<int, 8> Mask2(4U, -1);
Eric Christopherfd179292009-08-27 18:07:15 +00005025
Evan Chengace3c172008-07-22 21:13:36 +00005026 for (unsigned i = 0; i != 4; ++i) {
5027 if (Locs[i].first == -1)
5028 continue;
5029 else {
5030 unsigned Idx = (i < 2) ? 0 : 4;
5031 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00005032 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005033 }
5034 }
5035
Nate Begeman9008ca62009-04-27 18:41:29 +00005036 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005037 } else if (NumLo == 3 || NumHi == 3) {
5038 // Otherwise, we must have three elements from one vector, call it X, and
5039 // one element from the other, call it Y. First, use a shufps to build an
5040 // intermediate vector with the one element from Y and the element from X
5041 // that will be in the same half in the final destination (the indexes don't
5042 // matter). Then, use a shufps to build the final vector, taking the half
5043 // containing the element from Y from the intermediate, and the other half
5044 // from X.
5045 if (NumHi == 3) {
5046 // Normalize it so the 3 elements come from V1.
Nate Begeman9008ca62009-04-27 18:41:29 +00005047 CommuteVectorShuffleMask(PermMask, VT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005048 std::swap(V1, V2);
5049 }
5050
5051 // Find the element from V2.
5052 unsigned HiIndex;
5053 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005054 int Val = PermMask[HiIndex];
5055 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005056 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005057 if (Val >= 4)
5058 break;
5059 }
5060
Nate Begeman9008ca62009-04-27 18:41:29 +00005061 Mask1[0] = PermMask[HiIndex];
5062 Mask1[1] = -1;
5063 Mask1[2] = PermMask[HiIndex^1];
5064 Mask1[3] = -1;
5065 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005066
5067 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005068 Mask1[0] = PermMask[0];
5069 Mask1[1] = PermMask[1];
5070 Mask1[2] = HiIndex & 1 ? 6 : 4;
5071 Mask1[3] = HiIndex & 1 ? 4 : 6;
5072 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005073 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005074 Mask1[0] = HiIndex & 1 ? 2 : 0;
5075 Mask1[1] = HiIndex & 1 ? 0 : 2;
5076 Mask1[2] = PermMask[2];
5077 Mask1[3] = PermMask[3];
5078 if (Mask1[2] >= 0)
5079 Mask1[2] += 4;
5080 if (Mask1[3] >= 0)
5081 Mask1[3] += 4;
5082 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005083 }
Evan Chengace3c172008-07-22 21:13:36 +00005084 }
5085
5086 // Break it into (shuffle shuffle_hi, shuffle_lo).
5087 Locs.clear();
Nate Begeman9008ca62009-04-27 18:41:29 +00005088 SmallVector<int,8> LoMask(4U, -1);
5089 SmallVector<int,8> HiMask(4U, -1);
5090
5091 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00005092 unsigned MaskIdx = 0;
5093 unsigned LoIdx = 0;
5094 unsigned HiIdx = 2;
5095 for (unsigned i = 0; i != 4; ++i) {
5096 if (i == 2) {
5097 MaskPtr = &HiMask;
5098 MaskIdx = 1;
5099 LoIdx = 0;
5100 HiIdx = 2;
5101 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005102 int Idx = PermMask[i];
5103 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00005104 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00005105 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00005106 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00005107 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005108 LoIdx++;
5109 } else {
5110 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00005111 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005112 HiIdx++;
5113 }
5114 }
5115
Nate Begeman9008ca62009-04-27 18:41:29 +00005116 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
5117 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
5118 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00005119 for (unsigned i = 0; i != 4; ++i) {
5120 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005121 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00005122 } else {
5123 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00005124 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00005125 }
5126 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005127 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00005128}
5129
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005130static bool MayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005131 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005132 V = V.getOperand(0);
5133 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
5134 V = V.getOperand(0);
5135 if (MayFoldLoad(V))
5136 return true;
5137 return false;
5138}
5139
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005140// FIXME: the version above should always be used. Since there's
5141// a bug where several vector shuffles can't be folded because the
5142// DAG is not updated during lowering and a node claims to have two
5143// uses while it only has one, use this version, and let isel match
5144// another instruction if the load really happens to have more than
5145// one use. Remove this version after this bug get fixed.
Evan Cheng835580f2010-10-07 20:50:20 +00005146// rdar://8434668, PR8156
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005147static bool RelaxedMayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005148 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005149 V = V.getOperand(0);
5150 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
5151 V = V.getOperand(0);
5152 if (ISD::isNormalLoad(V.getNode()))
5153 return true;
5154 return false;
5155}
5156
5157/// CanFoldShuffleIntoVExtract - Check if the current shuffle is used by
5158/// a vector extract, and if both can be later optimized into a single load.
5159/// This is done in visitEXTRACT_VECTOR_ELT and the conditions are checked
5160/// here because otherwise a target specific shuffle node is going to be
5161/// emitted for this shuffle, and the optimization not done.
5162/// FIXME: This is probably not the best approach, but fix the problem
5163/// until the right path is decided.
5164static
5165bool CanXFormVExtractWithShuffleIntoLoad(SDValue V, SelectionDAG &DAG,
5166 const TargetLowering &TLI) {
5167 EVT VT = V.getValueType();
5168 ShuffleVectorSDNode *SVOp = dyn_cast<ShuffleVectorSDNode>(V);
5169
5170 // Be sure that the vector shuffle is present in a pattern like this:
5171 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), c) -> (f32 load $addr)
5172 if (!V.hasOneUse())
5173 return false;
5174
5175 SDNode *N = *V.getNode()->use_begin();
5176 if (N->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
5177 return false;
5178
5179 SDValue EltNo = N->getOperand(1);
5180 if (!isa<ConstantSDNode>(EltNo))
5181 return false;
5182
5183 // If the bit convert changed the number of elements, it is unsafe
5184 // to examine the mask.
5185 bool HasShuffleIntoBitcast = false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005186 if (V.getOpcode() == ISD::BITCAST) {
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005187 EVT SrcVT = V.getOperand(0).getValueType();
5188 if (SrcVT.getVectorNumElements() != VT.getVectorNumElements())
5189 return false;
5190 V = V.getOperand(0);
5191 HasShuffleIntoBitcast = true;
5192 }
5193
5194 // Select the input vector, guarding against out of range extract vector.
5195 unsigned NumElems = VT.getVectorNumElements();
5196 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
5197 int Idx = (Elt > NumElems) ? -1 : SVOp->getMaskElt(Elt);
5198 V = (Idx < (int)NumElems) ? V.getOperand(0) : V.getOperand(1);
5199
5200 // Skip one more bit_convert if necessary
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005201 if (V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005202 V = V.getOperand(0);
5203
5204 if (ISD::isNormalLoad(V.getNode())) {
5205 // Is the original load suitable?
5206 LoadSDNode *LN0 = cast<LoadSDNode>(V);
5207
5208 // FIXME: avoid the multi-use bug that is preventing lots of
5209 // of foldings to be detected, this is still wrong of course, but
5210 // give the temporary desired behavior, and if it happens that
5211 // the load has real more uses, during isel it will not fold, and
5212 // will generate poor code.
5213 if (!LN0 || LN0->isVolatile()) // || !LN0->hasOneUse()
5214 return false;
5215
5216 if (!HasShuffleIntoBitcast)
5217 return true;
5218
5219 // If there's a bitcast before the shuffle, check if the load type and
5220 // alignment is valid.
5221 unsigned Align = LN0->getAlignment();
5222 unsigned NewAlign =
5223 TLI.getTargetData()->getABITypeAlignment(
5224 VT.getTypeForEVT(*DAG.getContext()));
5225
5226 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
5227 return false;
5228 }
5229
5230 return true;
5231}
5232
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005233static
Evan Cheng835580f2010-10-07 20:50:20 +00005234SDValue getMOVDDup(SDValue &Op, DebugLoc &dl, SDValue V1, SelectionDAG &DAG) {
5235 EVT VT = Op.getValueType();
5236
5237 // Canonizalize to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005238 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1);
5239 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng835580f2010-10-07 20:50:20 +00005240 getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64,
5241 V1, DAG));
5242}
5243
5244static
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005245SDValue getMOVLowToHigh(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG,
5246 bool HasSSE2) {
5247 SDValue V1 = Op.getOperand(0);
5248 SDValue V2 = Op.getOperand(1);
5249 EVT VT = Op.getValueType();
5250
5251 assert(VT != MVT::v2i64 && "unsupported shuffle type");
5252
5253 if (HasSSE2 && VT == MVT::v2f64)
5254 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
5255
5256 // v4f32 or v4i32
5257 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V2, DAG);
5258}
5259
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00005260static
5261SDValue getMOVHighToLow(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG) {
5262 SDValue V1 = Op.getOperand(0);
5263 SDValue V2 = Op.getOperand(1);
5264 EVT VT = Op.getValueType();
5265
5266 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
5267 "unsupported shuffle type");
5268
5269 if (V2.getOpcode() == ISD::UNDEF)
5270 V2 = V1;
5271
5272 // v4i32 or v4f32
5273 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
5274}
5275
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005276static
5277SDValue getMOVLP(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG, bool HasSSE2) {
5278 SDValue V1 = Op.getOperand(0);
5279 SDValue V2 = Op.getOperand(1);
5280 EVT VT = Op.getValueType();
5281 unsigned NumElems = VT.getVectorNumElements();
5282
5283 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
5284 // operand of these instructions is only memory, so check if there's a
5285 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
5286 // same masks.
5287 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005288
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00005289 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005290 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005291 CanFoldLoad = true;
5292
5293 // When V1 is a load, it can be folded later into a store in isel, example:
5294 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
5295 // turns into:
5296 // (MOVLPSmr addr:$src1, VR128:$src2)
5297 // So, recognize this potential and also use MOVLPS or MOVLPD
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005298 if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005299 CanFoldLoad = true;
5300
5301 if (CanFoldLoad) {
5302 if (HasSSE2 && NumElems == 2)
5303 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
5304
5305 if (NumElems == 4)
5306 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
5307 }
5308
5309 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5310 // movl and movlp will both match v2i64, but v2i64 is never matched by
5311 // movl earlier because we make it strict to avoid messing with the movlp load
5312 // folding logic (see the code above getMOVLP call). Match it here then,
5313 // this is horrible, but will stay like this until we move all shuffle
5314 // matching to x86 specific nodes. Note that for the 1st condition all
5315 // types are matched with movsd.
5316 if ((HasSSE2 && NumElems == 2) || !X86::isMOVLMask(SVOp))
5317 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
5318 else if (HasSSE2)
5319 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
5320
5321
5322 assert(VT != MVT::v4i32 && "unsupported shuffle type");
5323
5324 // Invert the operand order and use SHUFPS to match it.
5325 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V2, V1,
5326 X86::getShuffleSHUFImmediate(SVOp), DAG);
5327}
5328
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005329static inline unsigned getUNPCKLOpcode(EVT VT) {
5330 switch(VT.getSimpleVT().SimpleTy) {
5331 case MVT::v4i32: return X86ISD::PUNPCKLDQ;
5332 case MVT::v2i64: return X86ISD::PUNPCKLQDQ;
5333 case MVT::v4f32: return X86ISD::UNPCKLPS;
5334 case MVT::v2f64: return X86ISD::UNPCKLPD;
5335 case MVT::v16i8: return X86ISD::PUNPCKLBW;
5336 case MVT::v8i16: return X86ISD::PUNPCKLWD;
5337 default:
5338 llvm_unreachable("Unknow type for unpckl");
5339 }
5340 return 0;
5341}
5342
5343static inline unsigned getUNPCKHOpcode(EVT VT) {
5344 switch(VT.getSimpleVT().SimpleTy) {
5345 case MVT::v4i32: return X86ISD::PUNPCKHDQ;
5346 case MVT::v2i64: return X86ISD::PUNPCKHQDQ;
5347 case MVT::v4f32: return X86ISD::UNPCKHPS;
5348 case MVT::v2f64: return X86ISD::UNPCKHPD;
5349 case MVT::v16i8: return X86ISD::PUNPCKHBW;
5350 case MVT::v8i16: return X86ISD::PUNPCKHWD;
5351 default:
5352 llvm_unreachable("Unknow type for unpckh");
5353 }
5354 return 0;
5355}
5356
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005357static
5358SDValue NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG,
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005359 const TargetLowering &TLI,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005360 const X86Subtarget *Subtarget) {
5361 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5362 EVT VT = Op.getValueType();
5363 DebugLoc dl = Op.getDebugLoc();
5364 SDValue V1 = Op.getOperand(0);
5365 SDValue V2 = Op.getOperand(1);
5366
5367 if (isZeroShuffle(SVOp))
5368 return getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
5369
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005370 // Handle splat operations
5371 if (SVOp->isSplat()) {
5372 // Special case, this is the only place now where it's
5373 // allowed to return a vector_shuffle operation without
5374 // using a target specific node, because *hopefully* it
5375 // will be optimized away by the dag combiner.
5376 if (VT.getVectorNumElements() <= 4 &&
5377 CanXFormVExtractWithShuffleIntoLoad(Op, DAG, TLI))
5378 return Op;
5379
5380 // Handle splats by matching through known masks
5381 if (VT.getVectorNumElements() <= 4)
5382 return SDValue();
5383
Evan Cheng835580f2010-10-07 20:50:20 +00005384 // Canonicalize all of the remaining to v4f32.
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005385 return PromoteSplat(SVOp, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005386 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005387
5388 // If the shuffle can be profitably rewritten as a narrower shuffle, then
5389 // do it!
5390 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
5391 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5392 if (NewOp.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005393 return DAG.getNode(ISD::BITCAST, dl, VT, NewOp);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005394 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
5395 // FIXME: Figure out a cleaner way to do this.
5396 // Try to make use of movq to zero out the top part.
5397 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
5398 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5399 if (NewOp.getNode()) {
5400 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
5401 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
5402 DAG, Subtarget, dl);
5403 }
5404 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
5405 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5406 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
5407 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
5408 DAG, Subtarget, dl);
5409 }
5410 }
5411 return SDValue();
5412}
5413
Dan Gohman475871a2008-07-27 21:46:04 +00005414SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005415X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00005416 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00005417 SDValue V1 = Op.getOperand(0);
5418 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00005419 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005420 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00005421 unsigned NumElems = VT.getVectorNumElements();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005422 bool isMMX = VT.getSizeInBits() == 64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005423 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
5424 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00005425 bool V1IsSplat = false;
5426 bool V2IsSplat = false;
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005427 bool HasSSE2 = Subtarget->hasSSE2() || Subtarget->hasAVX();
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00005428 bool HasSSE3 = Subtarget->hasSSE3() || Subtarget->hasAVX();
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00005429 bool HasSSSE3 = Subtarget->hasSSSE3() || Subtarget->hasAVX();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005430 MachineFunction &MF = DAG.getMachineFunction();
5431 bool OptForSize = MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005432
Dale Johannesen0488fb62010-09-30 23:57:10 +00005433 // Shuffle operations on MMX not supported.
5434 if (isMMX)
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00005435 return Op;
5436
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005437 // Vector shuffle lowering takes 3 steps:
5438 //
5439 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
5440 // narrowing and commutation of operands should be handled.
5441 // 2) Matching of shuffles with known shuffle masks to x86 target specific
5442 // shuffle nodes.
5443 // 3) Rewriting of unmatched masks into new generic shuffle operations,
5444 // so the shuffle can be broken into other shuffles and the legalizer can
5445 // try the lowering again.
5446 //
5447 // The general ideia is that no vector_shuffle operation should be left to
5448 // be matched during isel, all of them must be converted to a target specific
5449 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00005450
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005451 // Normalize the input vectors. Here splats, zeroed vectors, profitable
5452 // narrowing and commutation of operands should be handled. The actual code
5453 // doesn't include all of those, work in progress...
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005454 SDValue NewOp = NormalizeVectorShuffle(Op, DAG, *this, Subtarget);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005455 if (NewOp.getNode())
5456 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00005457
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005458 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
5459 // unpckh_undef). Only use pshufd if speed is more important than size.
5460 if (OptForSize && X86::isUNPCKL_v_undef_Mask(SVOp))
5461 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5462 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V1, DAG);
5463 if (OptForSize && X86::isUNPCKH_v_undef_Mask(SVOp))
5464 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5465 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00005466
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005467 if (X86::isMOVDDUPMask(SVOp) && HasSSE3 && V2IsUndef &&
Dale Johannesen0488fb62010-09-30 23:57:10 +00005468 RelaxedMayFoldVectorLoad(V1))
Evan Cheng835580f2010-10-07 20:50:20 +00005469 return getMOVDDup(Op, dl, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005470
Dale Johannesen0488fb62010-09-30 23:57:10 +00005471 if (X86::isMOVHLPS_v_undef_Mask(SVOp))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005472 return getMOVHighToLow(Op, dl, DAG);
5473
5474 // Use to match splats
5475 if (HasSSE2 && X86::isUNPCKHMask(SVOp) && V2IsUndef &&
5476 (VT == MVT::v2f64 || VT == MVT::v2i64))
5477 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
5478
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005479 if (X86::isPSHUFDMask(SVOp)) {
5480 // The actual implementation will match the mask in the if above and then
5481 // during isel it can match several different instructions, not only pshufd
5482 // as its name says, sad but true, emulate the behavior for now...
5483 if (X86::isMOVDDUPMask(SVOp) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
5484 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
5485
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005486 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
5487
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005488 if (HasSSE2 && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005489 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
5490
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005491 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005492 return getTargetShuffleNode(X86ISD::SHUFPD, dl, VT, V1, V1,
5493 TargetMask, DAG);
5494
5495 if (VT == MVT::v4f32)
5496 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V1, V1,
5497 TargetMask, DAG);
5498 }
Eric Christopherfd179292009-08-27 18:07:15 +00005499
Evan Chengf26ffe92008-05-29 08:22:04 +00005500 // Check if this can be converted into a logical shift.
5501 bool isLeft = false;
5502 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00005503 SDValue ShVal;
Nate Begeman9008ca62009-04-27 18:41:29 +00005504 bool isShift = getSubtarget()->hasSSE2() &&
Evan Chengc3630942009-12-09 21:00:30 +00005505 isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00005506 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00005507 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00005508 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00005509 EVT EltVT = VT.getVectorElementType();
5510 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00005511 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005512 }
Eric Christopherfd179292009-08-27 18:07:15 +00005513
Nate Begeman9008ca62009-04-27 18:41:29 +00005514 if (X86::isMOVLMask(SVOp)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005515 if (V1IsUndef)
5516 return V2;
Gabor Greifba36cb52008-08-28 21:40:38 +00005517 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00005518 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Dale Johannesen0488fb62010-09-30 23:57:10 +00005519 if (!X86::isMOVLPMask(SVOp)) {
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005520 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00005521 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
5522
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005523 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00005524 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
5525 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00005526 }
Eric Christopherfd179292009-08-27 18:07:15 +00005527
Nate Begeman9008ca62009-04-27 18:41:29 +00005528 // FIXME: fold these into legal mask.
Dale Johannesen0488fb62010-09-30 23:57:10 +00005529 if (X86::isMOVLHPSMask(SVOp) && !X86::isUNPCKLMask(SVOp))
5530 return getMOVLowToHigh(Op, dl, DAG, HasSSE2);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005531
Dale Johannesen0488fb62010-09-30 23:57:10 +00005532 if (X86::isMOVHLPSMask(SVOp))
5533 return getMOVHighToLow(Op, dl, DAG);
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00005534
Dale Johannesen0488fb62010-09-30 23:57:10 +00005535 if (X86::isMOVSHDUPMask(SVOp) && HasSSE3 && V2IsUndef && NumElems == 4)
5536 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00005537
Dale Johannesen0488fb62010-09-30 23:57:10 +00005538 if (X86::isMOVSLDUPMask(SVOp) && HasSSE3 && V2IsUndef && NumElems == 4)
5539 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00005540
Dale Johannesen0488fb62010-09-30 23:57:10 +00005541 if (X86::isMOVLPMask(SVOp))
5542 return getMOVLP(Op, dl, DAG, HasSSE2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005543
Nate Begeman9008ca62009-04-27 18:41:29 +00005544 if (ShouldXformToMOVHLPS(SVOp) ||
5545 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
5546 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005547
Evan Chengf26ffe92008-05-29 08:22:04 +00005548 if (isShift) {
5549 // No better options. Use a vshl / vsrl.
Dan Gohman8a55ce42009-09-23 21:02:20 +00005550 EVT EltVT = VT.getVectorElementType();
5551 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00005552 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005553 }
Eric Christopherfd179292009-08-27 18:07:15 +00005554
Evan Cheng9eca5e82006-10-25 21:49:50 +00005555 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00005556 // FIXME: This should also accept a bitcast of a splat? Be careful, not
5557 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00005558 V1IsSplat = isSplatVector(V1.getNode());
5559 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00005560
Chris Lattner8a594482007-11-25 00:24:49 +00005561 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00005562 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005563 Op = CommuteVectorShuffle(SVOp, DAG);
5564 SVOp = cast<ShuffleVectorSDNode>(Op);
5565 V1 = SVOp->getOperand(0);
5566 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00005567 std::swap(V1IsSplat, V2IsSplat);
5568 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00005569 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00005570 }
5571
Nate Begeman9008ca62009-04-27 18:41:29 +00005572 if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) {
5573 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00005574 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00005575 return V1;
5576 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
5577 // the instruction selector will not match, so get a canonical MOVL with
5578 // swapped operands to undo the commute.
5579 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00005580 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005581
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005582 if (X86::isUNPCKLMask(SVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005583 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005584
5585 if (X86::isUNPCKHMask(SVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005586 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V2, DAG);
Evan Chenge1113032006-10-04 18:33:38 +00005587
Evan Cheng9bbbb982006-10-25 20:48:19 +00005588 if (V2IsSplat) {
5589 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005590 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00005591 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00005592 SDValue NewMask = NormalizeMask(SVOp, DAG);
5593 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
5594 if (NSVOp != SVOp) {
5595 if (X86::isUNPCKLMask(NSVOp, true)) {
5596 return NewMask;
5597 } else if (X86::isUNPCKHMask(NSVOp, true)) {
5598 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005599 }
5600 }
5601 }
5602
Evan Cheng9eca5e82006-10-25 21:49:50 +00005603 if (Commuted) {
5604 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00005605 // FIXME: this seems wrong.
5606 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
5607 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005608
5609 if (X86::isUNPCKLMask(NewSVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005610 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V2, V1, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005611
5612 if (X86::isUNPCKHMask(NewSVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005613 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V2, V1, DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00005614 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005615
Nate Begeman9008ca62009-04-27 18:41:29 +00005616 // Normalize the node to match x86 shuffle ops if needed
Dale Johannesen0488fb62010-09-30 23:57:10 +00005617 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp))
Nate Begeman9008ca62009-04-27 18:41:29 +00005618 return CommuteVectorShuffle(SVOp, DAG);
5619
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00005620 // The checks below are all present in isShuffleMaskLegal, but they are
5621 // inlined here right now to enable us to directly emit target specific
5622 // nodes, and remove one by one until they don't return Op anymore.
5623 SmallVector<int, 16> M;
5624 SVOp->getMask(M);
5625
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00005626 if (isPALIGNRMask(M, VT, HasSSSE3))
5627 return getTargetShuffleNode(X86ISD::PALIGN, dl, VT, V1, V2,
5628 X86::getShufflePALIGNRImmediate(SVOp),
5629 DAG);
5630
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00005631 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
5632 SVOp->getSplatIndex() == 0 && V2IsUndef) {
5633 if (VT == MVT::v2f64)
5634 return getTargetShuffleNode(X86ISD::UNPCKLPD, dl, VT, V1, V1, DAG);
5635 if (VT == MVT::v2i64)
5636 return getTargetShuffleNode(X86ISD::PUNPCKLQDQ, dl, VT, V1, V1, DAG);
5637 }
5638
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00005639 if (isPSHUFHWMask(M, VT))
5640 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
5641 X86::getShufflePSHUFHWImmediate(SVOp),
5642 DAG);
5643
5644 if (isPSHUFLWMask(M, VT))
5645 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
5646 X86::getShufflePSHUFLWImmediate(SVOp),
5647 DAG);
5648
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00005649 if (isSHUFPMask(M, VT)) {
5650 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
5651 if (VT == MVT::v4f32 || VT == MVT::v4i32)
5652 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V1, V2,
5653 TargetMask, DAG);
5654 if (VT == MVT::v2f64 || VT == MVT::v2i64)
5655 return getTargetShuffleNode(X86ISD::SHUFPD, dl, VT, V1, V2,
5656 TargetMask, DAG);
5657 }
5658
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005659 if (X86::isUNPCKL_v_undef_Mask(SVOp))
5660 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5661 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V1, DAG);
5662 if (X86::isUNPCKH_v_undef_Mask(SVOp))
5663 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5664 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
5665
Evan Cheng14b32e12007-12-11 01:46:18 +00005666 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
Owen Anderson825b72b2009-08-11 20:47:22 +00005667 if (VT == MVT::v8i16) {
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005668 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00005669 if (NewOp.getNode())
Evan Cheng14b32e12007-12-11 01:46:18 +00005670 return NewOp;
5671 }
5672
Owen Anderson825b72b2009-08-11 20:47:22 +00005673 if (VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005674 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005675 if (NewOp.getNode())
5676 return NewOp;
5677 }
Eric Christopherfd179292009-08-27 18:07:15 +00005678
Dale Johannesen0488fb62010-09-30 23:57:10 +00005679 // Handle all 4 wide cases with a number of shuffles.
5680 if (NumElems == 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00005681 return LowerVECTOR_SHUFFLE_4wide(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005682
Dan Gohman475871a2008-07-27 21:46:04 +00005683 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005684}
5685
Dan Gohman475871a2008-07-27 21:46:04 +00005686SDValue
5687X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00005688 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005689 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005690 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005691 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005692 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005693 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00005694 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005695 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00005696 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005697 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00005698 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
5699 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
5700 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005701 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
5702 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005703 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005704 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00005705 Op.getOperand(0)),
5706 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00005707 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005708 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00005709 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005710 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00005711 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Owen Anderson825b72b2009-08-11 20:47:22 +00005712 } else if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00005713 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
5714 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00005715 // result has a single use which is a store or a bitcast to i32. And in
5716 // the case of a store, it's not worth it if the index is a constant 0,
5717 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00005718 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00005719 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00005720 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00005721 if ((User->getOpcode() != ISD::STORE ||
5722 (isa<ConstantSDNode>(Op.getOperand(1)) &&
5723 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005724 (User->getOpcode() != ISD::BITCAST ||
Owen Anderson825b72b2009-08-11 20:47:22 +00005725 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00005726 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00005727 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005728 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00005729 Op.getOperand(0)),
5730 Op.getOperand(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005731 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Extract);
Owen Anderson825b72b2009-08-11 20:47:22 +00005732 } else if (VT == MVT::i32) {
Mon P Wangf0fcdd82009-01-15 21:10:20 +00005733 // ExtractPS works with constant index.
5734 if (isa<ConstantSDNode>(Op.getOperand(1)))
5735 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00005736 }
Dan Gohman475871a2008-07-27 21:46:04 +00005737 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00005738}
5739
5740
Dan Gohman475871a2008-07-27 21:46:04 +00005741SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005742X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
5743 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005744 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00005745 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005746
Evan Cheng62a3f152008-03-24 21:52:23 +00005747 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005748 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00005749 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00005750 return Res;
5751 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00005752
Owen Andersone50ed302009-08-10 22:56:29 +00005753 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005754 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005755 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005756 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00005757 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005758 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00005759 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005760 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
5761 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005762 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005763 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00005764 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005765 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00005766 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00005767 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00005768 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00005769 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00005770 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00005771 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005772 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005773 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005774 if (Idx == 0)
5775 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00005776
Evan Cheng0db9fe62006-04-25 20:13:52 +00005777 // SHUFPS the element to the lowest double word, then movss.
Nate Begeman9008ca62009-04-27 18:41:29 +00005778 int Mask[4] = { Idx, -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00005779 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00005780 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00005781 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00005782 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00005783 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00005784 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00005785 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
5786 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
5787 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005788 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005789 if (Idx == 0)
5790 return Op;
5791
5792 // UNPCKHPD the element to the lowest double word, then movsd.
5793 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
5794 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00005795 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00005796 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00005797 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00005798 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00005799 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00005800 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005801 }
5802
Dan Gohman475871a2008-07-27 21:46:04 +00005803 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005804}
5805
Dan Gohman475871a2008-07-27 21:46:04 +00005806SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005807X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op,
5808 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005809 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00005810 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005811 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00005812
Dan Gohman475871a2008-07-27 21:46:04 +00005813 SDValue N0 = Op.getOperand(0);
5814 SDValue N1 = Op.getOperand(1);
5815 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00005816
Dan Gohman8a55ce42009-09-23 21:02:20 +00005817 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00005818 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00005819 unsigned Opc;
5820 if (VT == MVT::v8i16)
5821 Opc = X86ISD::PINSRW;
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00005822 else if (VT == MVT::v16i8)
5823 Opc = X86ISD::PINSRB;
5824 else
5825 Opc = X86ISD::PINSRB;
5826
Nate Begeman14d12ca2008-02-11 04:19:36 +00005827 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
5828 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00005829 if (N1.getValueType() != MVT::i32)
5830 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
5831 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005832 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00005833 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00005834 } else if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00005835 // Bits [7:6] of the constant are the source select. This will always be
5836 // zero here. The DAG Combiner may combine an extract_elt index into these
5837 // bits. For example (insert (extract, 3), 2) could be matched by putting
5838 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00005839 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00005840 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00005841 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00005842 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005843 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00005844 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00005845 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00005846 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00005847 } else if (EltVT == MVT::i32 && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00005848 // PINSR* works with constant index.
5849 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00005850 }
Dan Gohman475871a2008-07-27 21:46:04 +00005851 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00005852}
5853
Dan Gohman475871a2008-07-27 21:46:04 +00005854SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005855X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005856 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00005857 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00005858
5859 if (Subtarget->hasSSE41())
5860 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
5861
Dan Gohman8a55ce42009-09-23 21:02:20 +00005862 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00005863 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00005864
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005865 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00005866 SDValue N0 = Op.getOperand(0);
5867 SDValue N1 = Op.getOperand(1);
5868 SDValue N2 = Op.getOperand(2);
Evan Cheng794405e2007-12-12 07:55:34 +00005869
Dan Gohman8a55ce42009-09-23 21:02:20 +00005870 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00005871 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
5872 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00005873 if (N1.getValueType() != MVT::i32)
5874 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
5875 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005876 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesen0488fb62010-09-30 23:57:10 +00005877 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005878 }
Dan Gohman475871a2008-07-27 21:46:04 +00005879 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005880}
5881
Dan Gohman475871a2008-07-27 21:46:04 +00005882SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005883X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005884 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00005885
Chris Lattnerf172ecd2010-07-04 23:07:25 +00005886 if (Op.getValueType() == MVT::v1i64 &&
5887 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00005888 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00005889
Owen Anderson825b72b2009-08-11 20:47:22 +00005890 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Dale Johannesen0488fb62010-09-30 23:57:10 +00005891 assert(Op.getValueType().getSimpleVT().getSizeInBits() == 128 &&
5892 "Expected an SSE type!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005893 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(),
Dale Johannesen0488fb62010-09-30 23:57:10 +00005894 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005895}
5896
David Greene91585092011-01-26 15:38:49 +00005897// Lower a node with an EXTRACT_SUBVECTOR opcode. This may result in
5898// a simple subregister reference or explicit instructions to grab
5899// upper bits of a vector.
5900SDValue
5901X86TargetLowering::LowerEXTRACT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
5902 if (Subtarget->hasAVX()) {
5903 // TODO
5904 }
5905 return SDValue();
5906}
5907
David Greenecfe33c42011-01-26 19:13:22 +00005908// Lower a node with an INSERT_SUBVECTOR opcode. This may result in a
5909// simple superregister reference or explicit instructions to insert
5910// the upper bits of a vector.
5911SDValue
5912X86TargetLowering::LowerINSERT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
5913 if (Subtarget->hasAVX()) {
5914 DebugLoc dl = Op.getNode()->getDebugLoc();
5915 SDValue Vec = Op.getNode()->getOperand(0);
5916 SDValue SubVec = Op.getNode()->getOperand(1);
5917 SDValue Idx = Op.getNode()->getOperand(2);
5918
5919 if (Op.getNode()->getValueType(0).getSizeInBits() == 256
5920 && SubVec.getNode()->getValueType(0).getSizeInBits() == 128) {
5921 // TODO
5922 }
5923 }
5924 return SDValue();
5925}
5926
Bill Wendling056292f2008-09-16 21:48:12 +00005927// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
5928// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
5929// one of the above mentioned nodes. It has to be wrapped because otherwise
5930// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
5931// be used to form addressing mode. These wrapped nodes will be selected
5932// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00005933SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005934X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005935 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00005936
Chris Lattner41621a22009-06-26 19:22:52 +00005937 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
5938 // global base reg.
5939 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00005940 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005941 CodeModel::Model M = getTargetMachine().getCodeModel();
5942
Chris Lattner4f066492009-07-11 20:29:19 +00005943 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005944 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00005945 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00005946 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005947 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00005948 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005949 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00005950
Evan Cheng1606e8e2009-03-13 07:51:59 +00005951 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00005952 CP->getAlignment(),
5953 CP->getOffset(), OpFlag);
5954 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00005955 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00005956 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00005957 if (OpFlag) {
5958 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00005959 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00005960 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00005961 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005962 }
5963
5964 return Result;
5965}
5966
Dan Gohmand858e902010-04-17 15:26:15 +00005967SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00005968 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00005969
Chris Lattner18c59872009-06-27 04:16:01 +00005970 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
5971 // global base reg.
5972 unsigned char OpFlag = 0;
5973 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005974 CodeModel::Model M = getTargetMachine().getCodeModel();
5975
Chris Lattner4f066492009-07-11 20:29:19 +00005976 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005977 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00005978 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00005979 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005980 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00005981 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005982 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00005983
Chris Lattner18c59872009-06-27 04:16:01 +00005984 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
5985 OpFlag);
5986 DebugLoc DL = JT->getDebugLoc();
5987 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00005988
Chris Lattner18c59872009-06-27 04:16:01 +00005989 // With PIC, the address is actually $g + Offset.
Chris Lattner1e61e692010-11-15 02:46:57 +00005990 if (OpFlag)
Chris Lattner18c59872009-06-27 04:16:01 +00005991 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
5992 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00005993 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00005994 Result);
Eric Christopherfd179292009-08-27 18:07:15 +00005995
Chris Lattner18c59872009-06-27 04:16:01 +00005996 return Result;
5997}
5998
5999SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006000X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00006001 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00006002
Chris Lattner18c59872009-06-27 04:16:01 +00006003 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6004 // global base reg.
6005 unsigned char OpFlag = 0;
6006 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006007 CodeModel::Model M = getTargetMachine().getCodeModel();
6008
Chris Lattner4f066492009-07-11 20:29:19 +00006009 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006010 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00006011 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00006012 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006013 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00006014 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006015 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00006016
Chris Lattner18c59872009-06-27 04:16:01 +00006017 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00006018
Chris Lattner18c59872009-06-27 04:16:01 +00006019 DebugLoc DL = Op.getDebugLoc();
6020 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00006021
6022
Chris Lattner18c59872009-06-27 04:16:01 +00006023 // With PIC, the address is actually $g + Offset.
6024 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00006025 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00006026 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
6027 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006028 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00006029 Result);
6030 }
Eric Christopherfd179292009-08-27 18:07:15 +00006031
Chris Lattner18c59872009-06-27 04:16:01 +00006032 return Result;
6033}
6034
Dan Gohman475871a2008-07-27 21:46:04 +00006035SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006036X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00006037 // Create the TargetBlockAddressAddress node.
6038 unsigned char OpFlags =
6039 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00006040 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00006041 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Dan Gohman29cbade2009-11-20 23:18:13 +00006042 DebugLoc dl = Op.getDebugLoc();
6043 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(),
6044 /*isTarget=*/true, OpFlags);
6045
Dan Gohmanf705adb2009-10-30 01:28:02 +00006046 if (Subtarget->isPICStyleRIPRel() &&
6047 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00006048 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
6049 else
6050 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00006051
Dan Gohman29cbade2009-11-20 23:18:13 +00006052 // With PIC, the address is actually $g + Offset.
6053 if (isGlobalRelativeToPICBase(OpFlags)) {
6054 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
6055 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
6056 Result);
6057 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00006058
6059 return Result;
6060}
6061
6062SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00006063X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00006064 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00006065 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00006066 // Create the TargetGlobalAddress node, folding in the constant
6067 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00006068 unsigned char OpFlags =
6069 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006070 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00006071 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006072 if (OpFlags == X86II::MO_NO_FLAG &&
6073 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00006074 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00006075 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00006076 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00006077 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00006078 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00006079 }
Eric Christopherfd179292009-08-27 18:07:15 +00006080
Chris Lattner4f066492009-07-11 20:29:19 +00006081 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006082 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00006083 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
6084 else
6085 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00006086
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006087 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00006088 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00006089 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
6090 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006091 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006092 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006093
Chris Lattner36c25012009-07-10 07:34:39 +00006094 // For globals that require a load from a stub to get the address, emit the
6095 // load.
6096 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00006097 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00006098 MachinePointerInfo::getGOT(), false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006099
Dan Gohman6520e202008-10-18 02:06:02 +00006100 // If there was a non-zero offset that we didn't fold, create an explicit
6101 // addition for it.
6102 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00006103 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00006104 DAG.getConstant(Offset, getPointerTy()));
6105
Evan Cheng0db9fe62006-04-25 20:13:52 +00006106 return Result;
6107}
6108
Evan Chengda43bcf2008-09-24 00:05:32 +00006109SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006110X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00006111 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00006112 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006113 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00006114}
6115
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006116static SDValue
6117GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00006118 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Chris Lattnerb903bed2009-06-26 21:20:29 +00006119 unsigned char OperandFlags) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006120 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006121 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006122 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00006123 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006124 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00006125 GA->getOffset(),
6126 OperandFlags);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006127 if (InFlag) {
6128 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00006129 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006130 } else {
6131 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00006132 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006133 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006134
6135 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00006136 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006137
Rafael Espindola15f1b662009-04-24 12:59:40 +00006138 SDValue Flag = Chain.getValue(1);
6139 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006140}
6141
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006142// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00006143static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006144LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006145 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00006146 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00006147 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
6148 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006149 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006150 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006151 InFlag = Chain.getValue(1);
6152
Chris Lattnerb903bed2009-06-26 21:20:29 +00006153 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006154}
6155
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006156// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00006157static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006158LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006159 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00006160 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
6161 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006162}
6163
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006164// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
6165// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00006166static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006167 const EVT PtrVT, TLSModel::Model model,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00006168 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00006169 DebugLoc dl = GA->getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00006170
Chris Lattnerf93b90c2010-09-22 04:39:11 +00006171 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
6172 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
6173 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00006174
Michael J. Spencerec38de22010-10-10 22:04:20 +00006175 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Chris Lattnerf93b90c2010-09-22 04:39:11 +00006176 DAG.getIntPtrConstant(0),
6177 MachinePointerInfo(Ptr), false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00006178
Chris Lattnerb903bed2009-06-26 21:20:29 +00006179 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00006180 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
6181 // initialexec.
6182 unsigned WrapperKind = X86ISD::Wrapper;
6183 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00006184 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Chris Lattner18c59872009-06-27 04:16:01 +00006185 } else if (is64Bit) {
6186 assert(model == TLSModel::InitialExec);
6187 OperandFlags = X86II::MO_GOTTPOFF;
6188 WrapperKind = X86ISD::WrapperRIP;
6189 } else {
6190 assert(model == TLSModel::InitialExec);
6191 OperandFlags = X86II::MO_INDNTPOFF;
Chris Lattnerb903bed2009-06-26 21:20:29 +00006192 }
Eric Christopherfd179292009-08-27 18:07:15 +00006193
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006194 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
6195 // exec)
Michael J. Spencerec38de22010-10-10 22:04:20 +00006196 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Devang Patel0d881da2010-07-06 22:08:15 +00006197 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00006198 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00006199 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00006200
Rafael Espindola9a580232009-02-27 13:37:18 +00006201 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00006202 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00006203 MachinePointerInfo::getGOT(), false, false, 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00006204
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006205 // The address of the thread local variable is the add of the thread
6206 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00006207 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006208}
6209
Dan Gohman475871a2008-07-27 21:46:04 +00006210SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006211X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00006212
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006213 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00006214 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00006215
Eric Christopher30ef0e52010-06-03 04:07:48 +00006216 if (Subtarget->isTargetELF()) {
6217 // TODO: implement the "local dynamic" model
6218 // TODO: implement the "initial exec"model for pic executables
Michael J. Spencerec38de22010-10-10 22:04:20 +00006219
Eric Christopher30ef0e52010-06-03 04:07:48 +00006220 // If GV is an alias then use the aliasee for determining
6221 // thread-localness.
6222 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
6223 GV = GA->resolveAliasedGlobal(false);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006224
6225 TLSModel::Model model
Eric Christopher30ef0e52010-06-03 04:07:48 +00006226 = getTLSModel(GV, getTargetMachine().getRelocationModel());
Michael J. Spencerec38de22010-10-10 22:04:20 +00006227
Eric Christopher30ef0e52010-06-03 04:07:48 +00006228 switch (model) {
6229 case TLSModel::GeneralDynamic:
6230 case TLSModel::LocalDynamic: // not implemented
6231 if (Subtarget->is64Bit())
6232 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
6233 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Michael J. Spencerec38de22010-10-10 22:04:20 +00006234
Eric Christopher30ef0e52010-06-03 04:07:48 +00006235 case TLSModel::InitialExec:
6236 case TLSModel::LocalExec:
6237 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
6238 Subtarget->is64Bit());
6239 }
6240 } else if (Subtarget->isTargetDarwin()) {
6241 // Darwin only has one model of TLS. Lower to that.
6242 unsigned char OpFlag = 0;
6243 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
6244 X86ISD::WrapperRIP : X86ISD::Wrapper;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006245
Eric Christopher30ef0e52010-06-03 04:07:48 +00006246 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6247 // global base reg.
6248 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
6249 !Subtarget->is64Bit();
6250 if (PIC32)
6251 OpFlag = X86II::MO_TLVP_PIC_BASE;
6252 else
6253 OpFlag = X86II::MO_TLVP;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006254 DebugLoc DL = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00006255 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopherd8c05362010-12-09 06:25:53 +00006256 GA->getValueType(0),
Eric Christopher30ef0e52010-06-03 04:07:48 +00006257 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00006258 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006259
Eric Christopher30ef0e52010-06-03 04:07:48 +00006260 // With PIC32, the address is actually $g + Offset.
6261 if (PIC32)
6262 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
6263 DAG.getNode(X86ISD::GlobalBaseReg,
6264 DebugLoc(), getPointerTy()),
6265 Offset);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006266
Eric Christopher30ef0e52010-06-03 04:07:48 +00006267 // Lowering the machine isd will make sure everything is in the right
6268 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00006269 SDValue Chain = DAG.getEntryNode();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006270 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Eric Christopherd8c05362010-12-09 06:25:53 +00006271 SDValue Args[] = { Chain, Offset };
6272 Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006273
Eric Christopher30ef0e52010-06-03 04:07:48 +00006274 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
6275 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
6276 MFI->setAdjustsStack(true);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00006277
Eric Christopher30ef0e52010-06-03 04:07:48 +00006278 // And our return value (tls address) is in the standard call return value
6279 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00006280 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
6281 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy());
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006282 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00006283
Eric Christopher30ef0e52010-06-03 04:07:48 +00006284 assert(false &&
6285 "TLS not implemented for this target.");
Eric Christopherfd179292009-08-27 18:07:15 +00006286
Torok Edwinc23197a2009-07-14 16:55:14 +00006287 llvm_unreachable("Unreachable");
Chris Lattner5867de12009-04-01 22:14:45 +00006288 return SDValue();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006289}
6290
Evan Cheng0db9fe62006-04-25 20:13:52 +00006291
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006292/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michelfdc40a02009-02-17 22:15:04 +00006293/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00006294SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman4c1fa612008-03-03 22:22:09 +00006295 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00006296 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006297 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006298 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006299 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00006300 SDValue ShOpLo = Op.getOperand(0);
6301 SDValue ShOpHi = Op.getOperand(1);
6302 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00006303 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00006304 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00006305 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00006306
Dan Gohman475871a2008-07-27 21:46:04 +00006307 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006308 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00006309 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
6310 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006311 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00006312 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
6313 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006314 }
Evan Chenge3413162006-01-09 18:33:28 +00006315
Owen Anderson825b72b2009-08-11 20:47:22 +00006316 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
6317 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00006318 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00006319 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00006320
Dan Gohman475871a2008-07-27 21:46:04 +00006321 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00006322 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00006323 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
6324 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00006325
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006326 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00006327 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
6328 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006329 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00006330 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
6331 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006332 }
6333
Dan Gohman475871a2008-07-27 21:46:04 +00006334 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00006335 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006336}
Evan Chenga3195e82006-01-12 22:54:21 +00006337
Dan Gohmand858e902010-04-17 15:26:15 +00006338SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
6339 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006340 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00006341
Dale Johannesen0488fb62010-09-30 23:57:10 +00006342 if (SrcVT.isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00006343 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00006344
Owen Anderson825b72b2009-08-11 20:47:22 +00006345 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00006346 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00006347
Eli Friedman36df4992009-05-27 00:47:34 +00006348 // These are really Legal; return the operand so the caller accepts it as
6349 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00006350 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00006351 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00006352 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00006353 Subtarget->is64Bit()) {
6354 return Op;
6355 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006356
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006357 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006358 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006359 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00006360 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006361 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00006362 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00006363 StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006364 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006365 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00006366 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
6367}
Evan Cheng0db9fe62006-04-25 20:13:52 +00006368
Owen Andersone50ed302009-08-10 22:56:29 +00006369SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Michael J. Spencerec38de22010-10-10 22:04:20 +00006370 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00006371 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006372 // Build the FILD
Chris Lattner492a43e2010-09-22 01:28:21 +00006373 DebugLoc DL = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00006374 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00006375 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006376 if (useSSE)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006377 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Glue);
Chris Lattner5a88b832007-02-25 07:10:00 +00006378 else
Owen Anderson825b72b2009-08-11 20:47:22 +00006379 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006380
Chris Lattner492a43e2010-09-22 01:28:21 +00006381 unsigned ByteSize = SrcVT.getSizeInBits()/8;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006382
Chris Lattner492a43e2010-09-22 01:28:21 +00006383 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
6384 MachineMemOperand *MMO =
6385 DAG.getMachineFunction()
6386 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6387 MachineMemOperand::MOLoad, ByteSize, ByteSize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006388
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006389 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Chris Lattner492a43e2010-09-22 01:28:21 +00006390 SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
6391 X86ISD::FILD, DL,
6392 Tys, Ops, array_lengthof(Ops),
6393 SrcVT, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006394
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006395 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006396 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00006397 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006398
6399 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
6400 // shouldn't be necessary except that RFP cannot be live across
6401 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006402 MachineFunction &MF = DAG.getMachineFunction();
Bob Wilsoneafca4e2010-09-22 17:35:14 +00006403 unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
6404 int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006405 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00006406 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006407 SDValue Ops[] = {
6408 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
6409 };
Chris Lattner492a43e2010-09-22 01:28:21 +00006410 MachineMemOperand *MMO =
6411 DAG.getMachineFunction()
6412 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
Bob Wilsoneafca4e2010-09-22 17:35:14 +00006413 MachineMemOperand::MOStore, SSFISize, SSFISize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006414
Chris Lattner492a43e2010-09-22 01:28:21 +00006415 Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
6416 Ops, array_lengthof(Ops),
6417 Op.getValueType(), MMO);
6418 Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006419 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006420 false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006421 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006422
Evan Cheng0db9fe62006-04-25 20:13:52 +00006423 return Result;
6424}
6425
Bill Wendling8b8a6362009-01-17 03:56:04 +00006426// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00006427SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
6428 SelectionDAG &DAG) const {
Bill Wendling8b8a6362009-01-17 03:56:04 +00006429 // This algorithm is not obvious. Here it is in C code, more or less:
6430 /*
6431 double uint64_to_double( uint32_t hi, uint32_t lo ) {
6432 static const __m128i exp = { 0x4330000045300000ULL, 0 };
6433 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00006434
Bill Wendling8b8a6362009-01-17 03:56:04 +00006435 // Copy ints to xmm registers.
6436 __m128i xh = _mm_cvtsi32_si128( hi );
6437 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00006438
Bill Wendling8b8a6362009-01-17 03:56:04 +00006439 // Combine into low half of a single xmm register.
6440 __m128i x = _mm_unpacklo_epi32( xh, xl );
6441 __m128d d;
6442 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00006443
Bill Wendling8b8a6362009-01-17 03:56:04 +00006444 // Merge in appropriate exponents to give the integer bits the right
6445 // magnitude.
6446 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00006447
Bill Wendling8b8a6362009-01-17 03:56:04 +00006448 // Subtract away the biases to deal with the IEEE-754 double precision
6449 // implicit 1.
6450 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00006451
Bill Wendling8b8a6362009-01-17 03:56:04 +00006452 // All conversions up to here are exact. The correctly rounded result is
6453 // calculated using the current rounding mode using the following
6454 // horizontal add.
6455 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
6456 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
6457 // store doesn't really need to be here (except
6458 // maybe to zero the other double)
6459 return sd;
6460 }
6461 */
Dale Johannesen040225f2008-10-21 23:07:49 +00006462
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006463 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00006464 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00006465
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006466 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00006467 std::vector<Constant*> CV0;
Owen Andersoneed707b2009-07-24 23:12:02 +00006468 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x45300000)));
6469 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x43300000)));
6470 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
6471 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
Owen Andersonaf7ec972009-07-28 21:19:26 +00006472 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006473 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006474
Bill Wendling8b8a6362009-01-17 03:56:04 +00006475 std::vector<Constant*> CV1;
Owen Andersona90b3dc2009-07-15 21:51:10 +00006476 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006477 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
Owen Andersona90b3dc2009-07-15 21:51:10 +00006478 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006479 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Owen Andersonaf7ec972009-07-28 21:19:26 +00006480 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006481 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006482
Owen Anderson825b72b2009-08-11 20:47:22 +00006483 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6484 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00006485 Op.getOperand(0),
6486 DAG.getIntPtrConstant(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006487 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6488 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00006489 Op.getOperand(0),
6490 DAG.getIntPtrConstant(0)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006491 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
6492 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Chris Lattnere8639032010-09-21 06:22:23 +00006493 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006494 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00006495 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006496 SDValue XR2F = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Unpck2);
Owen Anderson825b72b2009-08-11 20:47:22 +00006497 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00006498 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006499 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00006500 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006501
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006502 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman9008ca62009-04-27 18:41:29 +00006503 int ShufMask[2] = { 1, -1 };
Owen Anderson825b72b2009-08-11 20:47:22 +00006504 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
6505 DAG.getUNDEF(MVT::v2f64), ShufMask);
6506 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
6507 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006508 DAG.getIntPtrConstant(0));
6509}
6510
Bill Wendling8b8a6362009-01-17 03:56:04 +00006511// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00006512SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
6513 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006514 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00006515 // FP constant to bias correct the final result.
6516 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00006517 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006518
6519 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00006520 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6521 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Bill Wendling8b8a6362009-01-17 03:56:04 +00006522 Op.getOperand(0),
6523 DAG.getIntPtrConstant(0)));
6524
Owen Anderson825b72b2009-08-11 20:47:22 +00006525 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006526 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00006527 DAG.getIntPtrConstant(0));
6528
6529 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00006530 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006531 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00006532 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006533 MVT::v2f64, Load)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006534 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00006535 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006536 MVT::v2f64, Bias)));
6537 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006538 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00006539 DAG.getIntPtrConstant(0));
6540
6541 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00006542 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006543
6544 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00006545 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00006546
Owen Anderson825b72b2009-08-11 20:47:22 +00006547 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006548 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00006549 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00006550 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006551 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00006552 }
6553
6554 // Handle final rounding.
6555 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00006556}
6557
Dan Gohmand858e902010-04-17 15:26:15 +00006558SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
6559 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00006560 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006561 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00006562
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006563 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00006564 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
6565 // the optimization here.
6566 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00006567 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00006568
Owen Andersone50ed302009-08-10 22:56:29 +00006569 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006570 EVT DstVT = Op.getValueType();
6571 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00006572 return LowerUINT_TO_FP_i64(Op, DAG);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006573 else if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00006574 return LowerUINT_TO_FP_i32(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00006575
6576 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00006577 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006578 if (SrcVT == MVT::i32) {
6579 SDValue WordOff = DAG.getConstant(4, getPointerTy());
6580 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
6581 getPointerTy(), StackSlot, WordOff);
6582 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006583 StackSlot, MachinePointerInfo(),
6584 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006585 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006586 OffsetSlot, MachinePointerInfo(),
6587 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006588 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
6589 return Fild;
6590 }
6591
6592 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
6593 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006594 StackSlot, MachinePointerInfo(),
6595 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006596 // For i64 source, we need to add the appropriate power of 2 if the input
6597 // was negative. This is the same as the optimization in
6598 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
6599 // we must be careful to do the computation in x87 extended precision, not
6600 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00006601 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
6602 MachineMemOperand *MMO =
6603 DAG.getMachineFunction()
6604 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6605 MachineMemOperand::MOLoad, 8, 8);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006606
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006607 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
6608 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Chris Lattner492a43e2010-09-22 01:28:21 +00006609 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, 3,
6610 MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006611
6612 APInt FF(32, 0x5F800000ULL);
6613
6614 // Check whether the sign bit is set.
6615 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
6616 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
6617 ISD::SETLT);
6618
6619 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
6620 SDValue FudgePtr = DAG.getConstantPool(
6621 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
6622 getPointerTy());
6623
6624 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
6625 SDValue Zero = DAG.getIntPtrConstant(0);
6626 SDValue Four = DAG.getIntPtrConstant(4);
6627 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
6628 Zero, Four);
6629 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
6630
6631 // Load the value out, extending it from f32 to f80.
6632 // FIXME: Avoid the extend by constructing the right constant pool?
Evan Chengbcc80172010-07-07 22:15:37 +00006633 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, MVT::f80, dl, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00006634 FudgePtr, MachinePointerInfo::getConstantPool(),
6635 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006636 // Extend everything to 80 bits to force it to be done on x87.
6637 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
6638 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00006639}
6640
Dan Gohman475871a2008-07-27 21:46:04 +00006641std::pair<SDValue,SDValue> X86TargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00006642FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) const {
Chris Lattner07290932010-09-22 01:05:16 +00006643 DebugLoc DL = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00006644
Owen Andersone50ed302009-08-10 22:56:29 +00006645 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00006646
6647 if (!IsSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006648 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
6649 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00006650 }
6651
Owen Anderson825b72b2009-08-11 20:47:22 +00006652 assert(DstTy.getSimpleVT() <= MVT::i64 &&
6653 DstTy.getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00006654 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00006655
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006656 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00006657 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00006658 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00006659 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00006660 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00006661 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00006662 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00006663 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006664
Evan Cheng87c89352007-10-15 20:11:21 +00006665 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
6666 // stack slot.
6667 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00006668 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00006669 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006670 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00006671
Michael J. Spencerec38de22010-10-10 22:04:20 +00006672
6673
Evan Cheng0db9fe62006-04-25 20:13:52 +00006674 unsigned Opc;
Owen Anderson825b72b2009-08-11 20:47:22 +00006675 switch (DstTy.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00006676 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
Owen Anderson825b72b2009-08-11 20:47:22 +00006677 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
6678 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
6679 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006680 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006681
Dan Gohman475871a2008-07-27 21:46:04 +00006682 SDValue Chain = DAG.getEntryNode();
6683 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00006684 EVT TheVT = Op.getOperand(0).getValueType();
6685 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006686 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00006687 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006688 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006689 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00006690 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00006691 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00006692 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00006693 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00006694
Chris Lattner492a43e2010-09-22 01:28:21 +00006695 MachineMemOperand *MMO =
6696 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6697 MachineMemOperand::MOLoad, MemSize, MemSize);
6698 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, 3,
6699 DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006700 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00006701 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006702 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
6703 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00006704
Chris Lattner07290932010-09-22 01:05:16 +00006705 MachineMemOperand *MMO =
6706 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6707 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006708
Evan Cheng0db9fe62006-04-25 20:13:52 +00006709 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00006710 SDValue Ops[] = { Chain, Value, StackSlot };
Chris Lattner07290932010-09-22 01:05:16 +00006711 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
6712 Ops, 3, DstTy, MMO);
Evan Chengd9558e02006-01-06 00:43:03 +00006713
Chris Lattner27a6c732007-11-24 07:07:01 +00006714 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006715}
6716
Dan Gohmand858e902010-04-17 15:26:15 +00006717SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
6718 SelectionDAG &DAG) const {
Dale Johannesen0488fb62010-09-30 23:57:10 +00006719 if (Op.getValueType().isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00006720 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00006721
Eli Friedman948e95a2009-05-23 09:59:16 +00006722 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00006723 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00006724 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
6725 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00006726
Chris Lattner27a6c732007-11-24 07:07:01 +00006727 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006728 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00006729 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00006730}
6731
Dan Gohmand858e902010-04-17 15:26:15 +00006732SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
6733 SelectionDAG &DAG) const {
Eli Friedman948e95a2009-05-23 09:59:16 +00006734 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
6735 SDValue FIST = Vals.first, StackSlot = Vals.second;
6736 assert(FIST.getNode() && "Unexpected failure");
6737
6738 // Load the result.
6739 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00006740 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00006741}
6742
Dan Gohmand858e902010-04-17 15:26:15 +00006743SDValue X86TargetLowering::LowerFABS(SDValue Op,
6744 SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00006745 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006746 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00006747 EVT VT = Op.getValueType();
6748 EVT EltVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00006749 if (VT.isVector())
6750 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006751 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00006752 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006753 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00006754 CV.push_back(C);
6755 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006756 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006757 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00006758 CV.push_back(C);
6759 CV.push_back(C);
6760 CV.push_back(C);
6761 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006762 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00006763 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006764 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006765 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00006766 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006767 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006768 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006769}
6770
Dan Gohmand858e902010-04-17 15:26:15 +00006771SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00006772 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006773 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00006774 EVT VT = Op.getValueType();
6775 EVT EltVT = VT;
Duncan Sandsda9ad382009-09-06 19:29:07 +00006776 if (VT.isVector())
Duncan Sands83ec4b62008-06-06 12:08:01 +00006777 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006778 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00006779 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006780 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00006781 CV.push_back(C);
6782 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006783 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006784 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00006785 CV.push_back(C);
6786 CV.push_back(C);
6787 CV.push_back(C);
6788 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006789 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00006790 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006791 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006792 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00006793 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006794 false, false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006795 if (VT.isVector()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006796 return DAG.getNode(ISD::BITCAST, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00006797 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006798 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00006799 Op.getOperand(0)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006800 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00006801 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00006802 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00006803 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006804}
6805
Dan Gohmand858e902010-04-17 15:26:15 +00006806SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00006807 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00006808 SDValue Op0 = Op.getOperand(0);
6809 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006810 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00006811 EVT VT = Op.getValueType();
6812 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00006813
6814 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00006815 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006816 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00006817 SrcVT = VT;
6818 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00006819 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00006820 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006821 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00006822 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00006823 }
6824
6825 // At this point the operands and the result should have the same
6826 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00006827
Evan Cheng68c47cb2007-01-05 07:55:56 +00006828 // First get the sign bit of second operand.
6829 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00006830 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006831 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
6832 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00006833 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006834 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
6835 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
6836 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
6837 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00006838 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00006839 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006840 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006841 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00006842 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006843 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006844 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00006845
6846 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00006847 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006848 // Op0 is MVT::f32, Op1 is MVT::f64.
6849 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
6850 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
6851 DAG.getConstant(32, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006852 SignBit = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, SignBit);
Owen Anderson825b72b2009-08-11 20:47:22 +00006853 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00006854 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00006855 }
6856
Evan Cheng73d6cf12007-01-05 21:37:56 +00006857 // Clear first operand sign bit.
6858 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00006859 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006860 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
6861 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00006862 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006863 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
6864 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
6865 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
6866 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00006867 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00006868 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006869 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006870 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00006871 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006872 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006873 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00006874
6875 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00006876 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00006877}
6878
Dan Gohman076aee32009-03-04 19:44:21 +00006879/// Emit nodes that will be selected as "test Op0,Op0", or something
6880/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00006881SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00006882 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00006883 DebugLoc dl = Op.getDebugLoc();
6884
Dan Gohman31125812009-03-07 01:58:32 +00006885 // CF and OF aren't always set the way we want. Determine which
6886 // of these we need.
6887 bool NeedCF = false;
6888 bool NeedOF = false;
6889 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006890 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00006891 case X86::COND_A: case X86::COND_AE:
6892 case X86::COND_B: case X86::COND_BE:
6893 NeedCF = true;
6894 break;
6895 case X86::COND_G: case X86::COND_GE:
6896 case X86::COND_L: case X86::COND_LE:
6897 case X86::COND_O: case X86::COND_NO:
6898 NeedOF = true;
6899 break;
Dan Gohman31125812009-03-07 01:58:32 +00006900 }
6901
Dan Gohman076aee32009-03-04 19:44:21 +00006902 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00006903 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
6904 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006905 if (Op.getResNo() != 0 || NeedOF || NeedCF)
6906 // Emit a CMP with 0, which is the TEST pattern.
6907 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
6908 DAG.getConstant(0, Op.getValueType()));
6909
6910 unsigned Opcode = 0;
6911 unsigned NumOperands = 0;
6912 switch (Op.getNode()->getOpcode()) {
6913 case ISD::ADD:
6914 // Due to an isel shortcoming, be conservative if this add is likely to be
6915 // selected as part of a load-modify-store instruction. When the root node
6916 // in a match is a store, isel doesn't know how to remap non-chain non-flag
6917 // uses of other nodes in the match, such as the ADD in this case. This
6918 // leads to the ADD being left around and reselected, with the result being
6919 // two adds in the output. Alas, even if none our users are stores, that
6920 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
6921 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
6922 // climbing the DAG back to the root, and it doesn't seem to be worth the
6923 // effort.
6924 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Dan Gohman076aee32009-03-04 19:44:21 +00006925 UE = Op.getNode()->use_end(); UI != UE; ++UI)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006926 if (UI->getOpcode() != ISD::CopyToReg && UI->getOpcode() != ISD::SETCC)
6927 goto default_case;
6928
6929 if (ConstantSDNode *C =
6930 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
6931 // An add of one will be selected as an INC.
6932 if (C->getAPIntValue() == 1) {
6933 Opcode = X86ISD::INC;
6934 NumOperands = 1;
6935 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00006936 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006937
6938 // An add of negative one (subtract of one) will be selected as a DEC.
6939 if (C->getAPIntValue().isAllOnesValue()) {
6940 Opcode = X86ISD::DEC;
6941 NumOperands = 1;
6942 break;
6943 }
Dan Gohman076aee32009-03-04 19:44:21 +00006944 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006945
6946 // Otherwise use a regular EFLAGS-setting add.
6947 Opcode = X86ISD::ADD;
6948 NumOperands = 2;
6949 break;
6950 case ISD::AND: {
6951 // If the primary and result isn't used, don't bother using X86ISD::AND,
6952 // because a TEST instruction will be better.
6953 bool NonFlagUse = false;
6954 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
6955 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
6956 SDNode *User = *UI;
6957 unsigned UOpNo = UI.getOperandNo();
6958 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
6959 // Look pass truncate.
6960 UOpNo = User->use_begin().getOperandNo();
6961 User = *User->use_begin();
6962 }
6963
6964 if (User->getOpcode() != ISD::BRCOND &&
6965 User->getOpcode() != ISD::SETCC &&
6966 (User->getOpcode() != ISD::SELECT || UOpNo != 0)) {
6967 NonFlagUse = true;
6968 break;
6969 }
Dan Gohman076aee32009-03-04 19:44:21 +00006970 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006971
6972 if (!NonFlagUse)
6973 break;
6974 }
6975 // FALL THROUGH
6976 case ISD::SUB:
6977 case ISD::OR:
6978 case ISD::XOR:
6979 // Due to the ISEL shortcoming noted above, be conservative if this op is
6980 // likely to be selected as part of a load-modify-store instruction.
6981 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
6982 UE = Op.getNode()->use_end(); UI != UE; ++UI)
6983 if (UI->getOpcode() == ISD::STORE)
6984 goto default_case;
6985
6986 // Otherwise use a regular EFLAGS-setting instruction.
6987 switch (Op.getNode()->getOpcode()) {
6988 default: llvm_unreachable("unexpected operator!");
6989 case ISD::SUB: Opcode = X86ISD::SUB; break;
6990 case ISD::OR: Opcode = X86ISD::OR; break;
6991 case ISD::XOR: Opcode = X86ISD::XOR; break;
6992 case ISD::AND: Opcode = X86ISD::AND; break;
6993 }
6994
6995 NumOperands = 2;
6996 break;
6997 case X86ISD::ADD:
6998 case X86ISD::SUB:
6999 case X86ISD::INC:
7000 case X86ISD::DEC:
7001 case X86ISD::OR:
7002 case X86ISD::XOR:
7003 case X86ISD::AND:
7004 return SDValue(Op.getNode(), 1);
7005 default:
7006 default_case:
7007 break;
Dan Gohman076aee32009-03-04 19:44:21 +00007008 }
7009
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007010 if (Opcode == 0)
7011 // Emit a CMP with 0, which is the TEST pattern.
7012 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
7013 DAG.getConstant(0, Op.getValueType()));
7014
7015 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
7016 SmallVector<SDValue, 4> Ops;
7017 for (unsigned i = 0; i != NumOperands; ++i)
7018 Ops.push_back(Op.getOperand(i));
7019
7020 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
7021 DAG.ReplaceAllUsesWith(Op, New);
7022 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00007023}
7024
7025/// Emit nodes that will be selected as "cmp Op0,Op1", or something
7026/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00007027SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00007028 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00007029 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
7030 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00007031 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00007032
7033 DebugLoc dl = Op0.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00007034 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00007035}
7036
Evan Chengd40d03e2010-01-06 19:38:29 +00007037/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
7038/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00007039SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
7040 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00007041 SDValue Op0 = And.getOperand(0);
7042 SDValue Op1 = And.getOperand(1);
7043 if (Op0.getOpcode() == ISD::TRUNCATE)
7044 Op0 = Op0.getOperand(0);
7045 if (Op1.getOpcode() == ISD::TRUNCATE)
7046 Op1 = Op1.getOperand(0);
7047
Evan Chengd40d03e2010-01-06 19:38:29 +00007048 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00007049 if (Op1.getOpcode() == ISD::SHL)
7050 std::swap(Op0, Op1);
7051 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00007052 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
7053 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00007054 // If we looked past a truncate, check that it's only truncating away
7055 // known zeros.
7056 unsigned BitWidth = Op0.getValueSizeInBits();
7057 unsigned AndBitWidth = And.getValueSizeInBits();
7058 if (BitWidth > AndBitWidth) {
7059 APInt Mask = APInt::getAllOnesValue(BitWidth), Zeros, Ones;
7060 DAG.ComputeMaskedBits(Op0, Mask, Zeros, Ones);
7061 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
7062 return SDValue();
7063 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00007064 LHS = Op1;
7065 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00007066 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00007067 } else if (Op1.getOpcode() == ISD::Constant) {
7068 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
7069 SDValue AndLHS = Op0;
Evan Chengd40d03e2010-01-06 19:38:29 +00007070 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
7071 LHS = AndLHS.getOperand(0);
7072 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00007073 }
Evan Chengd40d03e2010-01-06 19:38:29 +00007074 }
Evan Cheng0488db92007-09-25 01:57:46 +00007075
Evan Chengd40d03e2010-01-06 19:38:29 +00007076 if (LHS.getNode()) {
Evan Chenge5b51ac2010-04-17 06:13:15 +00007077 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00007078 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00007079 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00007080 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00007081 // Also promote i16 to i32 for performance / code size reason.
7082 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00007083 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00007084 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00007085
Evan Chengd40d03e2010-01-06 19:38:29 +00007086 // If the operand types disagree, extend the shift amount to match. Since
7087 // BT ignores high bits (like shifts) we can use anyextend.
7088 if (LHS.getValueType() != RHS.getValueType())
7089 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00007090
Evan Chengd40d03e2010-01-06 19:38:29 +00007091 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
7092 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
7093 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
7094 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00007095 }
7096
Evan Cheng54de3ea2010-01-05 06:52:31 +00007097 return SDValue();
7098}
7099
Dan Gohmand858e902010-04-17 15:26:15 +00007100SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng54de3ea2010-01-05 06:52:31 +00007101 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
7102 SDValue Op0 = Op.getOperand(0);
7103 SDValue Op1 = Op.getOperand(1);
7104 DebugLoc dl = Op.getDebugLoc();
7105 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
7106
7107 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00007108 // Lower (X & (1 << N)) == 0 to BT(X, N).
7109 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
7110 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Chris Lattner481eebc2010-12-19 21:23:48 +00007111 if (Op0.getOpcode() == ISD::AND && Op0.hasOneUse() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00007112 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane368b462010-06-18 14:22:04 +00007113 cast<ConstantSDNode>(Op1)->isNullValue() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00007114 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
7115 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
7116 if (NewSetCC.getNode())
7117 return NewSetCC;
7118 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00007119
Chris Lattner481eebc2010-12-19 21:23:48 +00007120 // Look for X == 0, X == 1, X != 0, or X != 1. We can simplify some forms of
7121 // these.
7122 if (Op1.getOpcode() == ISD::Constant &&
Evan Cheng2c755ba2010-02-27 07:36:59 +00007123 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
7124 cast<ConstantSDNode>(Op1)->isNullValue()) &&
7125 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007126
Chris Lattner481eebc2010-12-19 21:23:48 +00007127 // If the input is a setcc, then reuse the input setcc or use a new one with
7128 // the inverted condition.
7129 if (Op0.getOpcode() == X86ISD::SETCC) {
7130 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
7131 bool Invert = (CC == ISD::SETNE) ^
7132 cast<ConstantSDNode>(Op1)->isNullValue();
7133 if (!Invert) return Op0;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007134
Evan Cheng2c755ba2010-02-27 07:36:59 +00007135 CCode = X86::GetOppositeBranchCondition(CCode);
Chris Lattner481eebc2010-12-19 21:23:48 +00007136 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
7137 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
7138 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00007139 }
7140
Evan Chenge5b51ac2010-04-17 06:13:15 +00007141 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00007142 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00007143 if (X86CC == X86::COND_INVALID)
7144 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00007145
Chris Lattnerc19d1c32010-12-19 22:08:31 +00007146 SDValue EFLAGS = EmitCmp(Op0, Op1, X86CC, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00007147 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattnerc19d1c32010-12-19 22:08:31 +00007148 DAG.getConstant(X86CC, MVT::i8), EFLAGS);
Evan Cheng0488db92007-09-25 01:57:46 +00007149}
7150
Dan Gohmand858e902010-04-17 15:26:15 +00007151SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00007152 SDValue Cond;
7153 SDValue Op0 = Op.getOperand(0);
7154 SDValue Op1 = Op.getOperand(1);
7155 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00007156 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00007157 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
7158 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007159 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00007160
7161 if (isFP) {
7162 unsigned SSECC = 8;
Owen Andersone50ed302009-08-10 22:56:29 +00007163 EVT VT0 = Op0.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00007164 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
7165 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00007166 bool Swap = false;
7167
7168 switch (SetCCOpcode) {
7169 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00007170 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00007171 case ISD::SETEQ: SSECC = 0; break;
Scott Michelfdc40a02009-02-17 22:15:04 +00007172 case ISD::SETOGT:
Nate Begeman30a0de92008-07-17 16:51:19 +00007173 case ISD::SETGT: Swap = true; // Fallthrough
7174 case ISD::SETLT:
7175 case ISD::SETOLT: SSECC = 1; break;
7176 case ISD::SETOGE:
7177 case ISD::SETGE: Swap = true; // Fallthrough
7178 case ISD::SETLE:
7179 case ISD::SETOLE: SSECC = 2; break;
7180 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00007181 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00007182 case ISD::SETNE: SSECC = 4; break;
7183 case ISD::SETULE: Swap = true;
7184 case ISD::SETUGE: SSECC = 5; break;
7185 case ISD::SETULT: Swap = true;
7186 case ISD::SETUGT: SSECC = 6; break;
7187 case ISD::SETO: SSECC = 7; break;
7188 }
7189 if (Swap)
7190 std::swap(Op0, Op1);
7191
Nate Begemanfb8ead02008-07-25 19:05:58 +00007192 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00007193 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00007194 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00007195 SDValue UNORD, EQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00007196 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
7197 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00007198 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00007199 }
7200 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00007201 SDValue ORD, NEQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00007202 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
7203 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00007204 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00007205 }
Torok Edwinc23197a2009-07-14 16:55:14 +00007206 llvm_unreachable("Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00007207 }
7208 // Handle all other FP comparisons here.
Owen Anderson825b72b2009-08-11 20:47:22 +00007209 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00007210 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007211
Nate Begeman30a0de92008-07-17 16:51:19 +00007212 // We are handling one of the integer comparisons here. Since SSE only has
7213 // GT and EQ comparisons for integer, swapping operands and multiple
7214 // operations may be required for some comparisons.
7215 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
7216 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00007217
Owen Anderson825b72b2009-08-11 20:47:22 +00007218 switch (VT.getSimpleVT().SimpleTy) {
Nate Begeman30a0de92008-07-17 16:51:19 +00007219 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007220 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007221 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007222 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
7223 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00007224 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007225
Nate Begeman30a0de92008-07-17 16:51:19 +00007226 switch (SetCCOpcode) {
7227 default: break;
7228 case ISD::SETNE: Invert = true;
7229 case ISD::SETEQ: Opc = EQOpc; break;
7230 case ISD::SETLT: Swap = true;
7231 case ISD::SETGT: Opc = GTOpc; break;
7232 case ISD::SETGE: Swap = true;
7233 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
7234 case ISD::SETULT: Swap = true;
7235 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
7236 case ISD::SETUGE: Swap = true;
7237 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
7238 }
7239 if (Swap)
7240 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007241
Nate Begeman30a0de92008-07-17 16:51:19 +00007242 // Since SSE has no unsigned integer comparisons, we need to flip the sign
7243 // bits of the inputs before performing those operations.
7244 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00007245 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00007246 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
7247 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00007248 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00007249 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
7250 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00007251 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
7252 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00007253 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007254
Dale Johannesenace16102009-02-03 19:33:06 +00007255 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00007256
7257 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00007258 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00007259 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00007260
Nate Begeman30a0de92008-07-17 16:51:19 +00007261 return Result;
7262}
Evan Cheng0488db92007-09-25 01:57:46 +00007263
Evan Cheng370e5342008-12-03 08:38:43 +00007264// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00007265static bool isX86LogicalCmp(SDValue Op) {
7266 unsigned Opc = Op.getNode()->getOpcode();
7267 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
7268 return true;
7269 if (Op.getResNo() == 1 &&
7270 (Opc == X86ISD::ADD ||
7271 Opc == X86ISD::SUB ||
Chris Lattner5b856542010-12-20 00:59:46 +00007272 Opc == X86ISD::ADC ||
7273 Opc == X86ISD::SBB ||
Dan Gohman076aee32009-03-04 19:44:21 +00007274 Opc == X86ISD::SMUL ||
7275 Opc == X86ISD::UMUL ||
7276 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00007277 Opc == X86ISD::DEC ||
7278 Opc == X86ISD::OR ||
7279 Opc == X86ISD::XOR ||
7280 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00007281 return true;
7282
Chris Lattner9637d5b2010-12-05 07:49:54 +00007283 if (Op.getResNo() == 2 && Opc == X86ISD::UMUL)
7284 return true;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007285
Dan Gohman076aee32009-03-04 19:44:21 +00007286 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00007287}
7288
Chris Lattnera2b56002010-12-05 01:23:24 +00007289static bool isZero(SDValue V) {
7290 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
7291 return C && C->isNullValue();
7292}
7293
Chris Lattner96908b12010-12-05 02:00:51 +00007294static bool isAllOnes(SDValue V) {
7295 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
7296 return C && C->isAllOnesValue();
7297}
7298
Dan Gohmand858e902010-04-17 15:26:15 +00007299SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00007300 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00007301 SDValue Cond = Op.getOperand(0);
Chris Lattnera2b56002010-12-05 01:23:24 +00007302 SDValue Op1 = Op.getOperand(1);
7303 SDValue Op2 = Op.getOperand(2);
7304 DebugLoc DL = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00007305 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00007306
Dan Gohman1a492952009-10-20 16:22:37 +00007307 if (Cond.getOpcode() == ISD::SETCC) {
7308 SDValue NewCond = LowerSETCC(Cond, DAG);
7309 if (NewCond.getNode())
7310 Cond = NewCond;
7311 }
Evan Cheng734503b2006-09-11 02:19:56 +00007312
Chris Lattnera2b56002010-12-05 01:23:24 +00007313 // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00007314 // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
Chris Lattnera2b56002010-12-05 01:23:24 +00007315 // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00007316 // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007317 if (Cond.getOpcode() == X86ISD::SETCC &&
Chris Lattner96908b12010-12-05 02:00:51 +00007318 Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
7319 isZero(Cond.getOperand(1).getOperand(1))) {
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007320 SDValue Cmp = Cond.getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007321
Chris Lattnera2b56002010-12-05 01:23:24 +00007322 unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007323
7324 if ((isAllOnes(Op1) || isAllOnes(Op2)) &&
Chris Lattner96908b12010-12-05 02:00:51 +00007325 (CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
7326 SDValue Y = isAllOnes(Op2) ? Op1 : Op2;
Chris Lattnera2b56002010-12-05 01:23:24 +00007327
7328 SDValue CmpOp0 = Cmp.getOperand(0);
7329 Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32,
7330 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007331
Chris Lattner96908b12010-12-05 02:00:51 +00007332 SDValue Res = // Res = 0 or -1.
Chris Lattnera2b56002010-12-05 01:23:24 +00007333 DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
7334 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007335
Chris Lattner96908b12010-12-05 02:00:51 +00007336 if (isAllOnes(Op1) != (CondCode == X86::COND_E))
7337 Res = DAG.getNOT(DL, Res, Res.getValueType());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007338
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007339 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
Chris Lattnera2b56002010-12-05 01:23:24 +00007340 if (N2C == 0 || !N2C->isNullValue())
7341 Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
7342 return Res;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007343 }
7344 }
7345
Chris Lattnera2b56002010-12-05 01:23:24 +00007346 // Look past (and (setcc_carry (cmp ...)), 1).
Evan Chengad9c0a32009-12-15 00:53:42 +00007347 if (Cond.getOpcode() == ISD::AND &&
7348 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
7349 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00007350 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00007351 Cond = Cond.getOperand(0);
7352 }
7353
Evan Cheng3f41d662007-10-08 22:16:29 +00007354 // If condition flag is set by a X86ISD::CMP, then use it as the condition
7355 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00007356 if (Cond.getOpcode() == X86ISD::SETCC ||
7357 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00007358 CC = Cond.getOperand(0);
7359
Dan Gohman475871a2008-07-27 21:46:04 +00007360 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00007361 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00007362 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00007363
Evan Cheng3f41d662007-10-08 22:16:29 +00007364 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007365 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00007366 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00007367 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00007368
Chris Lattnerd1980a52009-03-12 06:52:53 +00007369 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
7370 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00007371 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00007372 addTest = false;
7373 }
7374 }
7375
7376 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007377 // Look pass the truncate.
7378 if (Cond.getOpcode() == ISD::TRUNCATE)
7379 Cond = Cond.getOperand(0);
7380
7381 // We know the result of AND is compared against zero. Try to match
7382 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00007383 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Chris Lattnera2b56002010-12-05 01:23:24 +00007384 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG);
Evan Chengd40d03e2010-01-06 19:38:29 +00007385 if (NewSetCC.getNode()) {
7386 CC = NewSetCC.getOperand(0);
7387 Cond = NewSetCC.getOperand(1);
7388 addTest = false;
7389 }
7390 }
7391 }
7392
7393 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007394 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00007395 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00007396 }
7397
Benjamin Kramere915ff32010-12-22 23:09:28 +00007398 // a < b ? -1 : 0 -> RES = ~setcc_carry
7399 // a < b ? 0 : -1 -> RES = setcc_carry
7400 // a >= b ? -1 : 0 -> RES = setcc_carry
7401 // a >= b ? 0 : -1 -> RES = ~setcc_carry
7402 if (Cond.getOpcode() == X86ISD::CMP) {
7403 unsigned CondCode = cast<ConstantSDNode>(CC)->getZExtValue();
7404
7405 if ((CondCode == X86::COND_AE || CondCode == X86::COND_B) &&
7406 (isAllOnes(Op1) || isAllOnes(Op2)) && (isZero(Op1) || isZero(Op2))) {
7407 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
7408 DAG.getConstant(X86::COND_B, MVT::i8), Cond);
7409 if (isAllOnes(Op1) != (CondCode == X86::COND_B))
7410 return DAG.getNOT(DL, Res, Res.getValueType());
7411 return Res;
7412 }
7413 }
7414
Evan Cheng0488db92007-09-25 01:57:46 +00007415 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
7416 // condition is true.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007417 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007418 SDValue Ops[] = { Op2, Op1, CC, Cond };
Chris Lattnera2b56002010-12-05 01:23:24 +00007419 return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00007420}
7421
Evan Cheng370e5342008-12-03 08:38:43 +00007422// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
7423// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
7424// from the AND / OR.
7425static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
7426 Opc = Op.getOpcode();
7427 if (Opc != ISD::OR && Opc != ISD::AND)
7428 return false;
7429 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
7430 Op.getOperand(0).hasOneUse() &&
7431 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
7432 Op.getOperand(1).hasOneUse());
7433}
7434
Evan Cheng961d6d42009-02-02 08:19:07 +00007435// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
7436// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00007437static bool isXor1OfSetCC(SDValue Op) {
7438 if (Op.getOpcode() != ISD::XOR)
7439 return false;
7440 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
7441 if (N1C && N1C->getAPIntValue() == 1) {
7442 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
7443 Op.getOperand(0).hasOneUse();
7444 }
7445 return false;
7446}
7447
Dan Gohmand858e902010-04-17 15:26:15 +00007448SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00007449 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00007450 SDValue Chain = Op.getOperand(0);
7451 SDValue Cond = Op.getOperand(1);
7452 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007453 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00007454 SDValue CC;
Evan Cheng734503b2006-09-11 02:19:56 +00007455
Dan Gohman1a492952009-10-20 16:22:37 +00007456 if (Cond.getOpcode() == ISD::SETCC) {
7457 SDValue NewCond = LowerSETCC(Cond, DAG);
7458 if (NewCond.getNode())
7459 Cond = NewCond;
7460 }
Chris Lattnere55484e2008-12-25 05:34:37 +00007461#if 0
7462 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00007463 else if (Cond.getOpcode() == X86ISD::ADD ||
7464 Cond.getOpcode() == X86ISD::SUB ||
7465 Cond.getOpcode() == X86ISD::SMUL ||
7466 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00007467 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00007468#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00007469
Evan Chengad9c0a32009-12-15 00:53:42 +00007470 // Look pass (and (setcc_carry (cmp ...)), 1).
7471 if (Cond.getOpcode() == ISD::AND &&
7472 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
7473 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00007474 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00007475 Cond = Cond.getOperand(0);
7476 }
7477
Evan Cheng3f41d662007-10-08 22:16:29 +00007478 // If condition flag is set by a X86ISD::CMP, then use it as the condition
7479 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00007480 if (Cond.getOpcode() == X86ISD::SETCC ||
7481 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00007482 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007483
Dan Gohman475871a2008-07-27 21:46:04 +00007484 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00007485 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00007486 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00007487 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00007488 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00007489 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00007490 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00007491 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00007492 default: break;
7493 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00007494 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00007495 // These can only come from an arithmetic instruction with overflow,
7496 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00007497 Cond = Cond.getNode()->getOperand(1);
7498 addTest = false;
7499 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00007500 }
Evan Cheng0488db92007-09-25 01:57:46 +00007501 }
Evan Cheng370e5342008-12-03 08:38:43 +00007502 } else {
7503 unsigned CondOpc;
7504 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
7505 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00007506 if (CondOpc == ISD::OR) {
7507 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
7508 // two branches instead of an explicit OR instruction with a
7509 // separate test.
7510 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00007511 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00007512 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007513 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00007514 Chain, Dest, CC, Cmp);
7515 CC = Cond.getOperand(1).getOperand(0);
7516 Cond = Cmp;
7517 addTest = false;
7518 }
7519 } else { // ISD::AND
7520 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
7521 // two branches instead of an explicit AND instruction with a
7522 // separate test. However, we only do this if this block doesn't
7523 // have a fall-through edge, because this requires an explicit
7524 // jmp when the condition is false.
7525 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00007526 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00007527 Op.getNode()->hasOneUse()) {
7528 X86::CondCode CCode =
7529 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
7530 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007531 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00007532 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00007533 // Look for an unconditional branch following this conditional branch.
7534 // We need this because we need to reverse the successors in order
7535 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00007536 if (User->getOpcode() == ISD::BR) {
7537 SDValue FalseBB = User->getOperand(1);
7538 SDNode *NewBR =
7539 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00007540 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00007541 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00007542 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00007543
Dale Johannesene4d209d2009-02-03 20:21:25 +00007544 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00007545 Chain, Dest, CC, Cmp);
7546 X86::CondCode CCode =
7547 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
7548 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007549 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00007550 Cond = Cmp;
7551 addTest = false;
7552 }
7553 }
Dan Gohman279c22e2008-10-21 03:29:32 +00007554 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00007555 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
7556 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
7557 // It should be transformed during dag combiner except when the condition
7558 // is set by a arithmetics with overflow node.
7559 X86::CondCode CCode =
7560 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
7561 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007562 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00007563 Cond = Cond.getOperand(0).getOperand(1);
7564 addTest = false;
Dan Gohman279c22e2008-10-21 03:29:32 +00007565 }
Evan Cheng0488db92007-09-25 01:57:46 +00007566 }
7567
7568 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007569 // Look pass the truncate.
7570 if (Cond.getOpcode() == ISD::TRUNCATE)
7571 Cond = Cond.getOperand(0);
7572
7573 // We know the result of AND is compared against zero. Try to match
7574 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00007575 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007576 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
7577 if (NewSetCC.getNode()) {
7578 CC = NewSetCC.getOperand(0);
7579 Cond = NewSetCC.getOperand(1);
7580 addTest = false;
7581 }
7582 }
7583 }
7584
7585 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007586 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00007587 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00007588 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00007589 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00007590 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00007591}
7592
Anton Korobeynikove060b532007-04-17 19:34:00 +00007593
7594// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
7595// Calls to _alloca is needed to probe the stack when allocating more than 4k
7596// bytes in one go. Touching the stack at 4K increments is necessary to ensure
7597// that the guard pages used by the OS virtual memory manager are allocated in
7598// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00007599SDValue
7600X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00007601 SelectionDAG &DAG) const {
Duncan Sands1e1ca0b2010-10-21 16:02:12 +00007602 assert((Subtarget->isTargetCygMing() || Subtarget->isTargetWindows()) &&
Michael J. Spencere9c253e2010-10-21 01:41:01 +00007603 "This should be used only on Windows targets");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007604 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007605
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007606 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00007607 SDValue Chain = Op.getOperand(0);
7608 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007609 // FIXME: Ensure alignment here
7610
Dan Gohman475871a2008-07-27 21:46:04 +00007611 SDValue Flag;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007612
Owen Anderson825b72b2009-08-11 20:47:22 +00007613 EVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007614
Dale Johannesendd64c412009-02-04 00:33:20 +00007615 Chain = DAG.getCopyToReg(Chain, dl, X86::EAX, Size, Flag);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00007616 Flag = Chain.getValue(1);
7617
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007618 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00007619
Michael J. Spencere9c253e2010-10-21 01:41:01 +00007620 Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag);
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00007621 Flag = Chain.getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007622
Dale Johannesendd64c412009-02-04 00:33:20 +00007623 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007624
Dan Gohman475871a2008-07-27 21:46:04 +00007625 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Dale Johannesene4d209d2009-02-03 20:21:25 +00007626 return DAG.getMergeValues(Ops1, 2, dl);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007627}
7628
Dan Gohmand858e902010-04-17 15:26:15 +00007629SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00007630 MachineFunction &MF = DAG.getMachineFunction();
7631 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
7632
Dan Gohman69de1932008-02-06 22:27:42 +00007633 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00007634 DebugLoc DL = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00007635
Anton Korobeynikove7beda12010-10-03 22:52:07 +00007636 if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00007637 // vastart just stores the address of the VarArgsFrameIndex slot into the
7638 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +00007639 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
7640 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00007641 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
7642 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007643 }
7644
7645 // __va_list_tag:
7646 // gp_offset (0 - 6 * 8)
7647 // fp_offset (48 - 48 + 8 * 16)
7648 // overflow_arg_area (point to parameters coming in memory).
7649 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00007650 SmallVector<SDValue, 8> MemOps;
7651 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00007652 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00007653 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00007654 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
7655 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007656 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007657 MemOps.push_back(Store);
7658
7659 // Store fp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00007660 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007661 FIN, DAG.getIntPtrConstant(4));
Chris Lattner8026a9d2010-09-21 17:50:43 +00007662 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00007663 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
7664 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007665 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007666 MemOps.push_back(Store);
7667
7668 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +00007669 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007670 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +00007671 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
7672 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00007673 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
7674 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +00007675 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007676 MemOps.push_back(Store);
7677
7678 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +00007679 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007680 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +00007681 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
7682 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00007683 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
7684 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007685 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +00007686 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00007687 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00007688}
7689
Dan Gohmand858e902010-04-17 15:26:15 +00007690SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman320afb82010-10-12 18:00:49 +00007691 assert(Subtarget->is64Bit() &&
7692 "LowerVAARG only handles 64-bit va_arg!");
7693 assert((Subtarget->isTargetLinux() ||
7694 Subtarget->isTargetDarwin()) &&
7695 "Unhandled target in LowerVAARG");
7696 assert(Op.getNode()->getNumOperands() == 4);
7697 SDValue Chain = Op.getOperand(0);
7698 SDValue SrcPtr = Op.getOperand(1);
7699 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
7700 unsigned Align = Op.getConstantOperandVal(3);
7701 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9018e832008-05-10 01:26:14 +00007702
Dan Gohman320afb82010-10-12 18:00:49 +00007703 EVT ArgVT = Op.getNode()->getValueType(0);
7704 const Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
7705 uint32_t ArgSize = getTargetData()->getTypeAllocSize(ArgTy);
7706 uint8_t ArgMode;
7707
7708 // Decide which area this value should be read from.
7709 // TODO: Implement the AMD64 ABI in its entirety. This simple
7710 // selection mechanism works only for the basic types.
7711 if (ArgVT == MVT::f80) {
7712 llvm_unreachable("va_arg for f80 not yet implemented");
7713 } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) {
7714 ArgMode = 2; // Argument passed in XMM register. Use fp_offset.
7715 } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) {
7716 ArgMode = 1; // Argument passed in GPR64 register(s). Use gp_offset.
7717 } else {
7718 llvm_unreachable("Unhandled argument type in LowerVAARG");
7719 }
7720
7721 if (ArgMode == 2) {
7722 // Sanity Check: Make sure using fp_offset makes sense.
Michael J. Spencer87b86652010-10-19 07:32:42 +00007723 assert(!UseSoftFloat &&
Eric Christopher52b45052010-10-12 19:44:17 +00007724 !(DAG.getMachineFunction()
7725 .getFunction()->hasFnAttr(Attribute::NoImplicitFloat)) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00007726 Subtarget->hasXMM());
Dan Gohman320afb82010-10-12 18:00:49 +00007727 }
7728
7729 // Insert VAARG_64 node into the DAG
7730 // VAARG_64 returns two values: Variable Argument Address, Chain
7731 SmallVector<SDValue, 11> InstOps;
7732 InstOps.push_back(Chain);
7733 InstOps.push_back(SrcPtr);
7734 InstOps.push_back(DAG.getConstant(ArgSize, MVT::i32));
7735 InstOps.push_back(DAG.getConstant(ArgMode, MVT::i8));
7736 InstOps.push_back(DAG.getConstant(Align, MVT::i32));
7737 SDVTList VTs = DAG.getVTList(getPointerTy(), MVT::Other);
7738 SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl,
7739 VTs, &InstOps[0], InstOps.size(),
7740 MVT::i64,
7741 MachinePointerInfo(SV),
7742 /*Align=*/0,
7743 /*Volatile=*/false,
7744 /*ReadMem=*/true,
7745 /*WriteMem=*/true);
7746 Chain = VAARG.getValue(1);
7747
7748 // Load the next argument and return it
7749 return DAG.getLoad(ArgVT, dl,
7750 Chain,
7751 VAARG,
7752 MachinePointerInfo(),
7753 false, false, 0);
Dan Gohman9018e832008-05-10 01:26:14 +00007754}
7755
Dan Gohmand858e902010-04-17 15:26:15 +00007756SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) const {
Evan Chengae642192007-03-02 23:16:35 +00007757 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00007758 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00007759 SDValue Chain = Op.getOperand(0);
7760 SDValue DstPtr = Op.getOperand(1);
7761 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00007762 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
7763 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Chris Lattnere72f2022010-09-21 05:40:29 +00007764 DebugLoc DL = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00007765
Chris Lattnere72f2022010-09-21 05:40:29 +00007766 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +00007767 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Michael J. Spencerec38de22010-10-10 22:04:20 +00007768 false,
Chris Lattnere72f2022010-09-21 05:40:29 +00007769 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +00007770}
7771
Dan Gohman475871a2008-07-27 21:46:04 +00007772SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007773X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007774 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007775 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007776 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00007777 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00007778 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00007779 case Intrinsic::x86_sse_comieq_ss:
7780 case Intrinsic::x86_sse_comilt_ss:
7781 case Intrinsic::x86_sse_comile_ss:
7782 case Intrinsic::x86_sse_comigt_ss:
7783 case Intrinsic::x86_sse_comige_ss:
7784 case Intrinsic::x86_sse_comineq_ss:
7785 case Intrinsic::x86_sse_ucomieq_ss:
7786 case Intrinsic::x86_sse_ucomilt_ss:
7787 case Intrinsic::x86_sse_ucomile_ss:
7788 case Intrinsic::x86_sse_ucomigt_ss:
7789 case Intrinsic::x86_sse_ucomige_ss:
7790 case Intrinsic::x86_sse_ucomineq_ss:
7791 case Intrinsic::x86_sse2_comieq_sd:
7792 case Intrinsic::x86_sse2_comilt_sd:
7793 case Intrinsic::x86_sse2_comile_sd:
7794 case Intrinsic::x86_sse2_comigt_sd:
7795 case Intrinsic::x86_sse2_comige_sd:
7796 case Intrinsic::x86_sse2_comineq_sd:
7797 case Intrinsic::x86_sse2_ucomieq_sd:
7798 case Intrinsic::x86_sse2_ucomilt_sd:
7799 case Intrinsic::x86_sse2_ucomile_sd:
7800 case Intrinsic::x86_sse2_ucomigt_sd:
7801 case Intrinsic::x86_sse2_ucomige_sd:
7802 case Intrinsic::x86_sse2_ucomineq_sd: {
7803 unsigned Opc = 0;
7804 ISD::CondCode CC = ISD::SETCC_INVALID;
7805 switch (IntNo) {
7806 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007807 case Intrinsic::x86_sse_comieq_ss:
7808 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007809 Opc = X86ISD::COMI;
7810 CC = ISD::SETEQ;
7811 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00007812 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007813 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007814 Opc = X86ISD::COMI;
7815 CC = ISD::SETLT;
7816 break;
7817 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007818 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007819 Opc = X86ISD::COMI;
7820 CC = ISD::SETLE;
7821 break;
7822 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007823 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007824 Opc = X86ISD::COMI;
7825 CC = ISD::SETGT;
7826 break;
7827 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007828 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007829 Opc = X86ISD::COMI;
7830 CC = ISD::SETGE;
7831 break;
7832 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007833 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007834 Opc = X86ISD::COMI;
7835 CC = ISD::SETNE;
7836 break;
7837 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007838 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007839 Opc = X86ISD::UCOMI;
7840 CC = ISD::SETEQ;
7841 break;
7842 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007843 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007844 Opc = X86ISD::UCOMI;
7845 CC = ISD::SETLT;
7846 break;
7847 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007848 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007849 Opc = X86ISD::UCOMI;
7850 CC = ISD::SETLE;
7851 break;
7852 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007853 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007854 Opc = X86ISD::UCOMI;
7855 CC = ISD::SETGT;
7856 break;
7857 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007858 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007859 Opc = X86ISD::UCOMI;
7860 CC = ISD::SETGE;
7861 break;
7862 case Intrinsic::x86_sse_ucomineq_ss:
7863 case Intrinsic::x86_sse2_ucomineq_sd:
7864 Opc = X86ISD::UCOMI;
7865 CC = ISD::SETNE;
7866 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00007867 }
Evan Cheng734503b2006-09-11 02:19:56 +00007868
Dan Gohman475871a2008-07-27 21:46:04 +00007869 SDValue LHS = Op.getOperand(1);
7870 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00007871 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00007872 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00007873 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
7874 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
7875 DAG.getConstant(X86CC, MVT::i8), Cond);
7876 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00007877 }
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007878 // ptest and testp intrinsics. The intrinsic these come from are designed to
7879 // return an integer value, not just an instruction so lower it to the ptest
7880 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00007881 case Intrinsic::x86_sse41_ptestz:
7882 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007883 case Intrinsic::x86_sse41_ptestnzc:
7884 case Intrinsic::x86_avx_ptestz_256:
7885 case Intrinsic::x86_avx_ptestc_256:
7886 case Intrinsic::x86_avx_ptestnzc_256:
7887 case Intrinsic::x86_avx_vtestz_ps:
7888 case Intrinsic::x86_avx_vtestc_ps:
7889 case Intrinsic::x86_avx_vtestnzc_ps:
7890 case Intrinsic::x86_avx_vtestz_pd:
7891 case Intrinsic::x86_avx_vtestc_pd:
7892 case Intrinsic::x86_avx_vtestnzc_pd:
7893 case Intrinsic::x86_avx_vtestz_ps_256:
7894 case Intrinsic::x86_avx_vtestc_ps_256:
7895 case Intrinsic::x86_avx_vtestnzc_ps_256:
7896 case Intrinsic::x86_avx_vtestz_pd_256:
7897 case Intrinsic::x86_avx_vtestc_pd_256:
7898 case Intrinsic::x86_avx_vtestnzc_pd_256: {
7899 bool IsTestPacked = false;
Eric Christopher71c67532009-07-29 00:28:05 +00007900 unsigned X86CC = 0;
7901 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00007902 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007903 case Intrinsic::x86_avx_vtestz_ps:
7904 case Intrinsic::x86_avx_vtestz_pd:
7905 case Intrinsic::x86_avx_vtestz_ps_256:
7906 case Intrinsic::x86_avx_vtestz_pd_256:
7907 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00007908 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007909 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +00007910 // ZF = 1
7911 X86CC = X86::COND_E;
7912 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007913 case Intrinsic::x86_avx_vtestc_ps:
7914 case Intrinsic::x86_avx_vtestc_pd:
7915 case Intrinsic::x86_avx_vtestc_ps_256:
7916 case Intrinsic::x86_avx_vtestc_pd_256:
7917 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00007918 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007919 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00007920 // CF = 1
7921 X86CC = X86::COND_B;
7922 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007923 case Intrinsic::x86_avx_vtestnzc_ps:
7924 case Intrinsic::x86_avx_vtestnzc_pd:
7925 case Intrinsic::x86_avx_vtestnzc_ps_256:
7926 case Intrinsic::x86_avx_vtestnzc_pd_256:
7927 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +00007928 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007929 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00007930 // ZF and CF = 0
7931 X86CC = X86::COND_A;
7932 break;
7933 }
Eric Christopherfd179292009-08-27 18:07:15 +00007934
Eric Christopher71c67532009-07-29 00:28:05 +00007935 SDValue LHS = Op.getOperand(1);
7936 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007937 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
7938 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00007939 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
7940 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
7941 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +00007942 }
Evan Cheng5759f972008-05-04 09:15:50 +00007943
7944 // Fix vector shift instructions where the last operand is a non-immediate
7945 // i32 value.
7946 case Intrinsic::x86_sse2_pslli_w:
7947 case Intrinsic::x86_sse2_pslli_d:
7948 case Intrinsic::x86_sse2_pslli_q:
7949 case Intrinsic::x86_sse2_psrli_w:
7950 case Intrinsic::x86_sse2_psrli_d:
7951 case Intrinsic::x86_sse2_psrli_q:
7952 case Intrinsic::x86_sse2_psrai_w:
7953 case Intrinsic::x86_sse2_psrai_d:
7954 case Intrinsic::x86_mmx_pslli_w:
7955 case Intrinsic::x86_mmx_pslli_d:
7956 case Intrinsic::x86_mmx_pslli_q:
7957 case Intrinsic::x86_mmx_psrli_w:
7958 case Intrinsic::x86_mmx_psrli_d:
7959 case Intrinsic::x86_mmx_psrli_q:
7960 case Intrinsic::x86_mmx_psrai_w:
7961 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00007962 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00007963 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00007964 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00007965
7966 unsigned NewIntNo = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00007967 EVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00007968 switch (IntNo) {
7969 case Intrinsic::x86_sse2_pslli_w:
7970 NewIntNo = Intrinsic::x86_sse2_psll_w;
7971 break;
7972 case Intrinsic::x86_sse2_pslli_d:
7973 NewIntNo = Intrinsic::x86_sse2_psll_d;
7974 break;
7975 case Intrinsic::x86_sse2_pslli_q:
7976 NewIntNo = Intrinsic::x86_sse2_psll_q;
7977 break;
7978 case Intrinsic::x86_sse2_psrli_w:
7979 NewIntNo = Intrinsic::x86_sse2_psrl_w;
7980 break;
7981 case Intrinsic::x86_sse2_psrli_d:
7982 NewIntNo = Intrinsic::x86_sse2_psrl_d;
7983 break;
7984 case Intrinsic::x86_sse2_psrli_q:
7985 NewIntNo = Intrinsic::x86_sse2_psrl_q;
7986 break;
7987 case Intrinsic::x86_sse2_psrai_w:
7988 NewIntNo = Intrinsic::x86_sse2_psra_w;
7989 break;
7990 case Intrinsic::x86_sse2_psrai_d:
7991 NewIntNo = Intrinsic::x86_sse2_psra_d;
7992 break;
7993 default: {
Owen Anderson825b72b2009-08-11 20:47:22 +00007994 ShAmtVT = MVT::v2i32;
Evan Cheng5759f972008-05-04 09:15:50 +00007995 switch (IntNo) {
7996 case Intrinsic::x86_mmx_pslli_w:
7997 NewIntNo = Intrinsic::x86_mmx_psll_w;
7998 break;
7999 case Intrinsic::x86_mmx_pslli_d:
8000 NewIntNo = Intrinsic::x86_mmx_psll_d;
8001 break;
8002 case Intrinsic::x86_mmx_pslli_q:
8003 NewIntNo = Intrinsic::x86_mmx_psll_q;
8004 break;
8005 case Intrinsic::x86_mmx_psrli_w:
8006 NewIntNo = Intrinsic::x86_mmx_psrl_w;
8007 break;
8008 case Intrinsic::x86_mmx_psrli_d:
8009 NewIntNo = Intrinsic::x86_mmx_psrl_d;
8010 break;
8011 case Intrinsic::x86_mmx_psrli_q:
8012 NewIntNo = Intrinsic::x86_mmx_psrl_q;
8013 break;
8014 case Intrinsic::x86_mmx_psrai_w:
8015 NewIntNo = Intrinsic::x86_mmx_psra_w;
8016 break;
8017 case Intrinsic::x86_mmx_psrai_d:
8018 NewIntNo = Intrinsic::x86_mmx_psra_d;
8019 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00008020 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng5759f972008-05-04 09:15:50 +00008021 }
8022 break;
8023 }
8024 }
Mon P Wangefa42202009-09-03 19:56:25 +00008025
8026 // The vector shift intrinsics with scalars uses 32b shift amounts but
8027 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
8028 // to be zero.
8029 SDValue ShOps[4];
8030 ShOps[0] = ShAmt;
8031 ShOps[1] = DAG.getConstant(0, MVT::i32);
8032 if (ShAmtVT == MVT::v4i32) {
8033 ShOps[2] = DAG.getUNDEF(MVT::i32);
8034 ShOps[3] = DAG.getUNDEF(MVT::i32);
8035 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 4);
8036 } else {
8037 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 2);
Dale Johannesen0488fb62010-09-30 23:57:10 +00008038// FIXME this must be lowered to get rid of the invalid type.
Mon P Wangefa42202009-09-03 19:56:25 +00008039 }
8040
Owen Andersone50ed302009-08-10 22:56:29 +00008041 EVT VT = Op.getValueType();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008042 ShAmt = DAG.getNode(ISD::BITCAST, dl, VT, ShAmt);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008043 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008044 DAG.getConstant(NewIntNo, MVT::i32),
Evan Cheng5759f972008-05-04 09:15:50 +00008045 Op.getOperand(1), ShAmt);
8046 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00008047 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008048}
Evan Cheng72261582005-12-20 06:22:03 +00008049
Dan Gohmand858e902010-04-17 15:26:15 +00008050SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
8051 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +00008052 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
8053 MFI->setReturnAddressIsTaken(true);
8054
Bill Wendling64e87322009-01-16 19:25:27 +00008055 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008056 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00008057
8058 if (Depth > 0) {
8059 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
8060 SDValue Offset =
8061 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +00008062 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008063 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00008064 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008065 FrameAddr, Offset),
Chris Lattner51abfe42010-09-21 06:02:19 +00008066 MachinePointerInfo(), false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +00008067 }
8068
8069 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00008070 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00008071 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Chris Lattner51abfe42010-09-21 06:02:19 +00008072 RetAddrFI, MachinePointerInfo(), false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00008073}
8074
Dan Gohmand858e902010-04-17 15:26:15 +00008075SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +00008076 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
8077 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00008078
Owen Andersone50ed302009-08-10 22:56:29 +00008079 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008080 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00008081 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
8082 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00008083 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00008084 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +00008085 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
8086 MachinePointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +00008087 false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00008088 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00008089}
8090
Dan Gohman475871a2008-07-27 21:46:04 +00008091SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008092 SelectionDAG &DAG) const {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00008093 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008094}
8095
Dan Gohmand858e902010-04-17 15:26:15 +00008096SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008097 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00008098 SDValue Chain = Op.getOperand(0);
8099 SDValue Offset = Op.getOperand(1);
8100 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008101 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008102
Dan Gohmand8816272010-08-11 18:14:00 +00008103 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
8104 Subtarget->is64Bit() ? X86::RBP : X86::EBP,
8105 getPointerTy());
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008106 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008107
Dan Gohmand8816272010-08-11 18:14:00 +00008108 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Frame,
8109 DAG.getIntPtrConstant(TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008110 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +00008111 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
8112 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00008113 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008114 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008115
Dale Johannesene4d209d2009-02-03 20:21:25 +00008116 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008117 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008118 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008119}
8120
Dan Gohman475871a2008-07-27 21:46:04 +00008121SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008122 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00008123 SDValue Root = Op.getOperand(0);
8124 SDValue Trmp = Op.getOperand(1); // trampoline
8125 SDValue FPtr = Op.getOperand(2); // nested function
8126 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008127 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008128
Dan Gohman69de1932008-02-06 22:27:42 +00008129 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008130
8131 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00008132 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00008133
8134 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +00008135 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
8136 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +00008137
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00008138 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
8139 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00008140
8141 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
8142
8143 // Load the pointer to the nested function into R11.
8144 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00008145 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00008146 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008147 Addr, MachinePointerInfo(TrmpAddr),
8148 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008149
Owen Anderson825b72b2009-08-11 20:47:22 +00008150 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8151 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008152 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
8153 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +00008154 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00008155
8156 // Load the 'nest' parameter value into R10.
8157 // R10 is specified in X86CallingConv.td
8158 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +00008159 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8160 DAG.getConstant(10, MVT::i64));
8161 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008162 Addr, MachinePointerInfo(TrmpAddr, 10),
8163 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008164
Owen Anderson825b72b2009-08-11 20:47:22 +00008165 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8166 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008167 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
8168 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +00008169 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00008170
8171 // Jump to the nested function.
8172 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +00008173 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8174 DAG.getConstant(20, MVT::i64));
8175 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008176 Addr, MachinePointerInfo(TrmpAddr, 20),
8177 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008178
8179 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +00008180 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8181 DAG.getConstant(22, MVT::i64));
8182 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00008183 MachinePointerInfo(TrmpAddr, 22),
8184 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008185
Dan Gohman475871a2008-07-27 21:46:04 +00008186 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00008187 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008188 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008189 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00008190 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00008191 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00008192 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00008193 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008194
8195 switch (CC) {
8196 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00008197 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +00008198 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00008199 case CallingConv::X86_StdCall: {
8200 // Pass 'nest' parameter in ECX.
8201 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00008202 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008203
8204 // Check that ECX wasn't needed by an 'inreg' parameter.
8205 const FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00008206 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008207
Chris Lattner58d74912008-03-12 17:45:29 +00008208 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00008209 unsigned InRegCount = 0;
8210 unsigned Idx = 1;
8211
8212 for (FunctionType::param_iterator I = FTy->param_begin(),
8213 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00008214 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00008215 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00008216 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008217
8218 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +00008219 report_fatal_error("Nest register in use - reduce number of inreg"
8220 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +00008221 }
8222 }
8223 break;
8224 }
8225 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +00008226 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00008227 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00008228 // Pass 'nest' parameter in EAX.
8229 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00008230 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008231 break;
8232 }
8233
Dan Gohman475871a2008-07-27 21:46:04 +00008234 SDValue OutChains[4];
8235 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008236
Owen Anderson825b72b2009-08-11 20:47:22 +00008237 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8238 DAG.getConstant(10, MVT::i32));
8239 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008240
Chris Lattnera62fe662010-02-05 19:20:30 +00008241 // This is storing the opcode for MOV32ri.
8242 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00008243 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00008244 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008245 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008246 Trmp, MachinePointerInfo(TrmpAddr),
8247 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008248
Owen Anderson825b72b2009-08-11 20:47:22 +00008249 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8250 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008251 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
8252 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +00008253 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008254
Chris Lattnera62fe662010-02-05 19:20:30 +00008255 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +00008256 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8257 DAG.getConstant(5, MVT::i32));
8258 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00008259 MachinePointerInfo(TrmpAddr, 5),
8260 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008261
Owen Anderson825b72b2009-08-11 20:47:22 +00008262 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8263 DAG.getConstant(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008264 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
8265 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +00008266 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008267
Dan Gohman475871a2008-07-27 21:46:04 +00008268 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00008269 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008270 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008271 }
8272}
8273
Dan Gohmand858e902010-04-17 15:26:15 +00008274SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
8275 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008276 /*
8277 The rounding mode is in bits 11:10 of FPSR, and has the following
8278 settings:
8279 00 Round to nearest
8280 01 Round to -inf
8281 10 Round to +inf
8282 11 Round to 0
8283
8284 FLT_ROUNDS, on the other hand, expects the following:
8285 -1 Undefined
8286 0 Round to 0
8287 1 Round to nearest
8288 2 Round to +inf
8289 3 Round to -inf
8290
8291 To perform the conversion, we do:
8292 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
8293 */
8294
8295 MachineFunction &MF = DAG.getMachineFunction();
8296 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00008297 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008298 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00008299 EVT VT = Op.getValueType();
Chris Lattner2156b792010-09-22 01:11:26 +00008300 DebugLoc DL = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008301
8302 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +00008303 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +00008304 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008305
Michael J. Spencerec38de22010-10-10 22:04:20 +00008306
Chris Lattner2156b792010-09-22 01:11:26 +00008307 MachineMemOperand *MMO =
8308 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8309 MachineMemOperand::MOStore, 2, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008310
Chris Lattner2156b792010-09-22 01:11:26 +00008311 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
8312 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
8313 DAG.getVTList(MVT::Other),
8314 Ops, 2, MVT::i16, MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008315
8316 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +00008317 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Chris Lattner51abfe42010-09-21 06:02:19 +00008318 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008319
8320 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00008321 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +00008322 DAG.getNode(ISD::SRL, DL, MVT::i16,
8323 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00008324 CWD, DAG.getConstant(0x800, MVT::i16)),
8325 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00008326 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +00008327 DAG.getNode(ISD::SRL, DL, MVT::i16,
8328 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00008329 CWD, DAG.getConstant(0x400, MVT::i16)),
8330 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008331
Dan Gohman475871a2008-07-27 21:46:04 +00008332 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +00008333 DAG.getNode(ISD::AND, DL, MVT::i16,
8334 DAG.getNode(ISD::ADD, DL, MVT::i16,
8335 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +00008336 DAG.getConstant(1, MVT::i16)),
8337 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008338
8339
Duncan Sands83ec4b62008-06-06 12:08:01 +00008340 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +00008341 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008342}
8343
Dan Gohmand858e902010-04-17 15:26:15 +00008344SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008345 EVT VT = Op.getValueType();
8346 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008347 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008348 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00008349
8350 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008351 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00008352 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +00008353 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00008354 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008355 }
Evan Cheng18efe262007-12-14 02:13:44 +00008356
Evan Cheng152804e2007-12-14 08:30:15 +00008357 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008358 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008359 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00008360
8361 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00008362 SDValue Ops[] = {
8363 Op,
8364 DAG.getConstant(NumBits+NumBits-1, OpVT),
8365 DAG.getConstant(X86::COND_E, MVT::i8),
8366 Op.getValue(1)
8367 };
8368 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00008369
8370 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +00008371 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +00008372
Owen Anderson825b72b2009-08-11 20:47:22 +00008373 if (VT == MVT::i8)
8374 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008375 return Op;
8376}
8377
Dan Gohmand858e902010-04-17 15:26:15 +00008378SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008379 EVT VT = Op.getValueType();
8380 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008381 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008382 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00008383
8384 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008385 if (VT == MVT::i8) {
8386 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00008387 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008388 }
Evan Cheng152804e2007-12-14 08:30:15 +00008389
8390 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008391 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008392 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00008393
8394 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00008395 SDValue Ops[] = {
8396 Op,
8397 DAG.getConstant(NumBits, OpVT),
8398 DAG.getConstant(X86::COND_E, MVT::i8),
8399 Op.getValue(1)
8400 };
8401 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00008402
Owen Anderson825b72b2009-08-11 20:47:22 +00008403 if (VT == MVT::i8)
8404 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008405 return Op;
8406}
8407
Dan Gohmand858e902010-04-17 15:26:15 +00008408SDValue X86TargetLowering::LowerMUL_V2I64(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008409 EVT VT = Op.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00008410 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008411 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00008412
Mon P Wangaf9b9522008-12-18 21:42:19 +00008413 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
8414 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
8415 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
8416 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
8417 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
8418 //
8419 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
8420 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
8421 // return AloBlo + AloBhi + AhiBlo;
8422
8423 SDValue A = Op.getOperand(0);
8424 SDValue B = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008425
Dale Johannesene4d209d2009-02-03 20:21:25 +00008426 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008427 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
8428 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008429 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008430 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
8431 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008432 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008433 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008434 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008435 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008436 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008437 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008438 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008439 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008440 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008441 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008442 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
8443 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008444 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008445 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
8446 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008447 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
8448 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +00008449 return Res;
8450}
8451
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008452SDValue X86TargetLowering::LowerSHL(SDValue Op, SelectionDAG &DAG) const {
8453 EVT VT = Op.getValueType();
8454 DebugLoc dl = Op.getDebugLoc();
8455 SDValue R = Op.getOperand(0);
8456
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008457 LLVMContext *Context = DAG.getContext();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008458
Nate Begeman51409212010-07-28 00:21:48 +00008459 assert(Subtarget->hasSSE41() && "Cannot lower SHL without SSE4.1 or later");
8460
8461 if (VT == MVT::v4i32) {
8462 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8463 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
8464 Op.getOperand(1), DAG.getConstant(23, MVT::i32));
8465
8466 ConstantInt *CI = ConstantInt::get(*Context, APInt(32, 0x3f800000U));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008467
Nate Begeman51409212010-07-28 00:21:48 +00008468 std::vector<Constant*> CV(4, CI);
8469 Constant *C = ConstantVector::get(CV);
8470 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8471 SDValue Addend = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008472 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +00008473 false, false, 16);
8474
8475 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Addend);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008476 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op);
Nate Begeman51409212010-07-28 00:21:48 +00008477 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
8478 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
8479 }
8480 if (VT == MVT::v16i8) {
8481 // a = a << 5;
8482 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8483 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
8484 Op.getOperand(1), DAG.getConstant(5, MVT::i32));
8485
8486 ConstantInt *CM1 = ConstantInt::get(*Context, APInt(8, 15));
8487 ConstantInt *CM2 = ConstantInt::get(*Context, APInt(8, 63));
8488
8489 std::vector<Constant*> CVM1(16, CM1);
8490 std::vector<Constant*> CVM2(16, CM2);
8491 Constant *C = ConstantVector::get(CVM1);
8492 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8493 SDValue M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008494 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +00008495 false, false, 16);
8496
8497 // r = pblendv(r, psllw(r & (char16)15, 4), a);
8498 M = DAG.getNode(ISD::AND, dl, VT, R, M);
8499 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8500 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
8501 DAG.getConstant(4, MVT::i32));
Nate Begeman672fb622010-12-20 22:04:24 +00008502 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT, R, M, Op);
Nate Begeman51409212010-07-28 00:21:48 +00008503 // a += a
8504 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008505
Nate Begeman51409212010-07-28 00:21:48 +00008506 C = ConstantVector::get(CVM2);
8507 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8508 M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008509 MachinePointerInfo::getConstantPool(),
Chris Lattner51abfe42010-09-21 06:02:19 +00008510 false, false, 16);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008511
Nate Begeman51409212010-07-28 00:21:48 +00008512 // r = pblendv(r, psllw(r & (char16)63, 2), a);
8513 M = DAG.getNode(ISD::AND, dl, VT, R, M);
8514 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8515 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
8516 DAG.getConstant(2, MVT::i32));
Nate Begeman672fb622010-12-20 22:04:24 +00008517 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT, R, M, Op);
Nate Begeman51409212010-07-28 00:21:48 +00008518 // a += a
8519 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008520
Nate Begeman51409212010-07-28 00:21:48 +00008521 // return pblendv(r, r+r, a);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008522 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT,
Nate Begeman51409212010-07-28 00:21:48 +00008523 R, DAG.getNode(ISD::ADD, dl, VT, R, R), Op);
8524 return R;
8525 }
8526 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008527}
Mon P Wangaf9b9522008-12-18 21:42:19 +00008528
Dan Gohmand858e902010-04-17 15:26:15 +00008529SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
Bill Wendling74c37652008-12-09 22:08:41 +00008530 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
8531 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +00008532 // looks for this combo and may remove the "setcc" instruction if the "setcc"
8533 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +00008534 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +00008535 SDValue LHS = N->getOperand(0);
8536 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +00008537 unsigned BaseOp = 0;
8538 unsigned Cond = 0;
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008539 DebugLoc DL = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +00008540 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008541 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +00008542 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +00008543 // A subtract of one will be selected as a INC. Note that INC doesn't
8544 // set CF, so we can't do this for UADDO.
8545 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
8546 if (C->getAPIntValue() == 1) {
8547 BaseOp = X86ISD::INC;
8548 Cond = X86::COND_O;
8549 break;
8550 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008551 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +00008552 Cond = X86::COND_O;
8553 break;
8554 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008555 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +00008556 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00008557 break;
8558 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +00008559 // A subtract of one will be selected as a DEC. Note that DEC doesn't
8560 // set CF, so we can't do this for USUBO.
8561 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
8562 if (C->getAPIntValue() == 1) {
8563 BaseOp = X86ISD::DEC;
8564 Cond = X86::COND_O;
8565 break;
8566 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008567 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +00008568 Cond = X86::COND_O;
8569 break;
8570 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008571 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +00008572 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00008573 break;
8574 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00008575 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +00008576 Cond = X86::COND_O;
8577 break;
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008578 case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs
8579 SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0),
8580 MVT::i32);
8581 SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008582
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008583 SDValue SetCC =
8584 DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
8585 DAG.getConstant(X86::COND_O, MVT::i32),
8586 SDValue(Sum.getNode(), 2));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008587
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008588 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
8589 return Sum;
8590 }
Bill Wendling74c37652008-12-09 22:08:41 +00008591 }
Bill Wendling3fafd932008-11-26 22:37:40 +00008592
Bill Wendling61edeb52008-12-02 01:06:39 +00008593 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008594 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008595 SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +00008596
Bill Wendling61edeb52008-12-02 01:06:39 +00008597 SDValue SetCC =
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008598 DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1),
8599 DAG.getConstant(Cond, MVT::i32),
8600 SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +00008601
Bill Wendling61edeb52008-12-02 01:06:39 +00008602 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
8603 return Sum;
Bill Wendling41ea7e72008-11-24 19:21:46 +00008604}
8605
Eric Christopher9a9d2752010-07-22 02:48:34 +00008606SDValue X86TargetLowering::LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG) const{
8607 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00008608
Eric Christopherb6729dc2010-08-04 23:03:04 +00008609 if (!Subtarget->hasSSE2()) {
Eric Christopherc0b2a202010-08-14 21:51:50 +00008610 SDValue Chain = Op.getOperand(0);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008611 SDValue Zero = DAG.getConstant(0,
Eric Christopherb6729dc2010-08-04 23:03:04 +00008612 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Eric Christopherc0b2a202010-08-14 21:51:50 +00008613 SDValue Ops[] = {
8614 DAG.getRegister(X86::ESP, MVT::i32), // Base
8615 DAG.getTargetConstant(1, MVT::i8), // Scale
8616 DAG.getRegister(0, MVT::i32), // Index
8617 DAG.getTargetConstant(0, MVT::i32), // Disp
8618 DAG.getRegister(0, MVT::i32), // Segment.
8619 Zero,
8620 Chain
8621 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00008622 SDNode *Res =
Eric Christopherc0b2a202010-08-14 21:51:50 +00008623 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
8624 array_lengthof(Ops));
8625 return SDValue(Res, 0);
Eric Christopherb6729dc2010-08-04 23:03:04 +00008626 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00008627
Eric Christopher9a9d2752010-07-22 02:48:34 +00008628 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
Chris Lattner132929a2010-08-14 17:26:09 +00008629 if (!isDev)
Eric Christopher9a9d2752010-07-22 02:48:34 +00008630 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008631
Chris Lattner132929a2010-08-14 17:26:09 +00008632 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
8633 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
8634 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
8635 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
Michael J. Spencerec38de22010-10-10 22:04:20 +00008636
Chris Lattner132929a2010-08-14 17:26:09 +00008637 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
8638 if (!Op1 && !Op2 && !Op3 && Op4)
8639 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008640
Chris Lattner132929a2010-08-14 17:26:09 +00008641 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
8642 if (Op1 && !Op2 && !Op3 && !Op4)
8643 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008644
8645 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
Chris Lattner132929a2010-08-14 17:26:09 +00008646 // (MFENCE)>;
8647 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
Eric Christopher9a9d2752010-07-22 02:48:34 +00008648}
8649
Dan Gohmand858e902010-04-17 15:26:15 +00008650SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008651 EVT T = Op.getValueType();
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008652 DebugLoc DL = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +00008653 unsigned Reg = 0;
8654 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00008655 switch(T.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00008656 default:
8657 assert(false && "Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +00008658 case MVT::i8: Reg = X86::AL; size = 1; break;
8659 case MVT::i16: Reg = X86::AX; size = 2; break;
8660 case MVT::i32: Reg = X86::EAX; size = 4; break;
8661 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +00008662 assert(Subtarget->is64Bit() && "Node not type legal!");
8663 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +00008664 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00008665 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008666 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +00008667 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +00008668 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +00008669 Op.getOperand(1),
8670 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +00008671 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +00008672 cpIn.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00008673 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008674 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
8675 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
8676 Ops, 5, T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +00008677 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008678 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +00008679 return cpOut;
8680}
8681
Duncan Sands1607f052008-12-01 11:39:25 +00008682SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008683 SelectionDAG &DAG) const {
Duncan Sands1607f052008-12-01 11:39:25 +00008684 assert(Subtarget->is64Bit() && "Result not type legalized?");
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00008685 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +00008686 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008687 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +00008688 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00008689 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
8690 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +00008691 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +00008692 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
8693 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +00008694 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +00008695 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +00008696 rdx.getValue(1)
8697 };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008698 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008699}
8700
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008701SDValue X86TargetLowering::LowerBITCAST(SDValue Op,
Dale Johannesen7d07b482010-05-21 00:52:33 +00008702 SelectionDAG &DAG) const {
8703 EVT SrcVT = Op.getOperand(0).getValueType();
8704 EVT DstVT = Op.getValueType();
Chris Lattner2a786eb2010-12-19 20:19:20 +00008705 assert(Subtarget->is64Bit() && !Subtarget->hasSSE2() &&
8706 Subtarget->hasMMX() && "Unexpected custom BITCAST");
Michael J. Spencerec38de22010-10-10 22:04:20 +00008707 assert((DstVT == MVT::i64 ||
Dale Johannesen7d07b482010-05-21 00:52:33 +00008708 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008709 "Unexpected custom BITCAST");
Dale Johannesen7d07b482010-05-21 00:52:33 +00008710 // i64 <=> MMX conversions are Legal.
8711 if (SrcVT==MVT::i64 && DstVT.isVector())
8712 return Op;
8713 if (DstVT==MVT::i64 && SrcVT.isVector())
8714 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +00008715 // MMX <=> MMX conversions are Legal.
8716 if (SrcVT.isVector() && DstVT.isVector())
8717 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +00008718 // All other conversions need to be expanded.
8719 return SDValue();
8720}
Chris Lattner5b856542010-12-20 00:59:46 +00008721
Dan Gohmand858e902010-04-17 15:26:15 +00008722SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen71d1bf52008-09-29 22:25:26 +00008723 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +00008724 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008725 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008726 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +00008727 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008728 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008729 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +00008730 Node->getOperand(0),
8731 Node->getOperand(1), negOp,
8732 cast<AtomicSDNode>(Node)->getSrcValue(),
8733 cast<AtomicSDNode>(Node)->getAlignment());
Mon P Wang63307c32008-05-05 19:05:59 +00008734}
8735
Chris Lattner5b856542010-12-20 00:59:46 +00008736static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
8737 EVT VT = Op.getNode()->getValueType(0);
8738
8739 // Let legalize expand this if it isn't a legal type yet.
8740 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
8741 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008742
Chris Lattner5b856542010-12-20 00:59:46 +00008743 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008744
Chris Lattner5b856542010-12-20 00:59:46 +00008745 unsigned Opc;
8746 bool ExtraOp = false;
8747 switch (Op.getOpcode()) {
8748 default: assert(0 && "Invalid code");
8749 case ISD::ADDC: Opc = X86ISD::ADD; break;
8750 case ISD::ADDE: Opc = X86ISD::ADC; ExtraOp = true; break;
8751 case ISD::SUBC: Opc = X86ISD::SUB; break;
8752 case ISD::SUBE: Opc = X86ISD::SBB; ExtraOp = true; break;
8753 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008754
Chris Lattner5b856542010-12-20 00:59:46 +00008755 if (!ExtraOp)
8756 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
8757 Op.getOperand(1));
8758 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
8759 Op.getOperand(1), Op.getOperand(2));
8760}
8761
Evan Cheng0db9fe62006-04-25 20:13:52 +00008762/// LowerOperation - Provide custom lowering hooks for some operations.
8763///
Dan Gohmand858e902010-04-17 15:26:15 +00008764SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00008765 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008766 default: llvm_unreachable("Should not custom lower this!");
Eric Christopher9a9d2752010-07-22 02:48:34 +00008767 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op,DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008768 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
8769 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008770 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00008771 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008772 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
8773 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
8774 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
David Greene91585092011-01-26 15:38:49 +00008775 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op, DAG);
David Greenecfe33c42011-01-26 19:13:22 +00008776 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008777 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
8778 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
8779 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00008780 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +00008781 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +00008782 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008783 case ISD::SHL_PARTS:
8784 case ISD::SRA_PARTS:
8785 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
8786 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00008787 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008788 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00008789 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008790 case ISD::FABS: return LowerFABS(Op, DAG);
8791 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008792 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00008793 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman30a0de92008-07-17 16:51:19 +00008794 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00008795 case ISD::SELECT: return LowerSELECT(Op, DAG);
8796 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008797 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008798 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +00008799 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +00008800 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008801 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00008802 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
8803 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008804 case ISD::FRAME_TO_ARGS_OFFSET:
8805 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008806 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008807 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008808 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +00008809 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +00008810 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
8811 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Mon P Wangaf9b9522008-12-18 21:42:19 +00008812 case ISD::MUL: return LowerMUL_V2I64(Op, DAG);
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008813 case ISD::SHL: return LowerSHL(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +00008814 case ISD::SADDO:
8815 case ISD::UADDO:
8816 case ISD::SSUBO:
8817 case ISD::USUBO:
8818 case ISD::SMULO:
8819 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +00008820 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008821 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Chris Lattner5b856542010-12-20 00:59:46 +00008822 case ISD::ADDC:
8823 case ISD::ADDE:
8824 case ISD::SUBC:
8825 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008826 }
Chris Lattner27a6c732007-11-24 07:07:01 +00008827}
8828
Duncan Sands1607f052008-12-01 11:39:25 +00008829void X86TargetLowering::
8830ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00008831 SelectionDAG &DAG, unsigned NewOp) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008832 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008833 DebugLoc dl = Node->getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00008834 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +00008835
8836 SDValue Chain = Node->getOperand(0);
8837 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00008838 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00008839 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00008840 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00008841 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +00008842 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +00008843 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +00008844 SDValue Result =
8845 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
8846 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +00008847 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +00008848 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00008849 Results.push_back(Result.getValue(2));
8850}
8851
Duncan Sands126d9072008-07-04 11:47:58 +00008852/// ReplaceNodeResults - Replace a node with an illegal result type
8853/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00008854void X86TargetLowering::ReplaceNodeResults(SDNode *N,
8855 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00008856 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +00008857 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +00008858 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +00008859 default:
Duncan Sands1607f052008-12-01 11:39:25 +00008860 assert(false && "Do not know how to custom type legalize this operation!");
8861 return;
Chris Lattner5b856542010-12-20 00:59:46 +00008862 case ISD::ADDC:
8863 case ISD::ADDE:
8864 case ISD::SUBC:
8865 case ISD::SUBE:
8866 // We don't want to expand or promote these.
8867 return;
Duncan Sands1607f052008-12-01 11:39:25 +00008868 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +00008869 std::pair<SDValue,SDValue> Vals =
8870 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +00008871 SDValue FIST = Vals.first, StackSlot = Vals.second;
8872 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +00008873 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +00008874 // Return a load from the stack slot.
Chris Lattner51abfe42010-09-21 06:02:19 +00008875 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
8876 MachinePointerInfo(), false, false, 0));
Duncan Sands1607f052008-12-01 11:39:25 +00008877 }
8878 return;
8879 }
8880 case ISD::READCYCLECOUNTER: {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00008881 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +00008882 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008883 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00008884 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +00008885 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00008886 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00008887 eax.getValue(2));
8888 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
8889 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +00008890 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00008891 Results.push_back(edx.getValue(1));
8892 return;
8893 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008894 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +00008895 EVT T = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008896 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
Duncan Sands1607f052008-12-01 11:39:25 +00008897 SDValue cpInL, cpInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00008898 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
8899 DAG.getConstant(0, MVT::i32));
8900 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
8901 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00008902 cpInL = DAG.getCopyToReg(N->getOperand(0), dl, X86::EAX, cpInL, SDValue());
8903 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl, X86::EDX, cpInH,
Duncan Sands1607f052008-12-01 11:39:25 +00008904 cpInL.getValue(1));
8905 SDValue swapInL, swapInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00008906 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
8907 DAG.getConstant(0, MVT::i32));
8908 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
8909 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00008910 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl, X86::EBX, swapInL,
Duncan Sands1607f052008-12-01 11:39:25 +00008911 cpInH.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00008912 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl, X86::ECX, swapInH,
Duncan Sands1607f052008-12-01 11:39:25 +00008913 swapInL.getValue(1));
8914 SDValue Ops[] = { swapInH.getValue(0),
8915 N->getOperand(1),
8916 swapInH.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00008917 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Andrew Trick1a2cf3b2010-10-11 19:02:04 +00008918 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
8919 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG8_DAG, dl, Tys,
8920 Ops, 3, T, MMO);
Dale Johannesendd64c412009-02-04 00:33:20 +00008921 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl, X86::EAX,
Owen Anderson825b72b2009-08-11 20:47:22 +00008922 MVT::i32, Result.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00008923 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl, X86::EDX,
Owen Anderson825b72b2009-08-11 20:47:22 +00008924 MVT::i32, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +00008925 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Owen Anderson825b72b2009-08-11 20:47:22 +00008926 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00008927 Results.push_back(cpOutH.getValue(1));
8928 return;
8929 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008930 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +00008931 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
8932 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008933 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +00008934 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
8935 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008936 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +00008937 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
8938 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008939 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +00008940 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
8941 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008942 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +00008943 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
8944 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008945 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +00008946 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
8947 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008948 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +00008949 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
8950 return;
Chris Lattner27a6c732007-11-24 07:07:01 +00008951 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00008952}
8953
Evan Cheng72261582005-12-20 06:22:03 +00008954const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
8955 switch (Opcode) {
8956 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +00008957 case X86ISD::BSF: return "X86ISD::BSF";
8958 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +00008959 case X86ISD::SHLD: return "X86ISD::SHLD";
8960 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00008961 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +00008962 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +00008963 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +00008964 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +00008965 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00008966 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00008967 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
8968 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
8969 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00008970 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00008971 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +00008972 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +00008973 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +00008974 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +00008975 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +00008976 case X86ISD::COMI: return "X86ISD::COMI";
8977 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +00008978 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +00008979 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Evan Cheng72261582005-12-20 06:22:03 +00008980 case X86ISD::CMOV: return "X86ISD::CMOV";
8981 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00008982 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00008983 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
8984 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +00008985 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00008986 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +00008987 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +00008988 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +00008989 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +00008990 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
8991 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +00008992 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +00008993 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Nate Begemanb65c1752010-12-17 22:55:37 +00008994 case X86ISD::PANDN: return "X86ISD::PANDN";
8995 case X86ISD::PSIGNB: return "X86ISD::PSIGNB";
8996 case X86ISD::PSIGNW: return "X86ISD::PSIGNW";
8997 case X86ISD::PSIGND: return "X86ISD::PSIGND";
Nate Begeman672fb622010-12-20 22:04:24 +00008998 case X86ISD::PBLENDVB: return "X86ISD::PBLENDVB";
Evan Cheng8ca29322006-11-10 21:43:37 +00008999 case X86ISD::FMAX: return "X86ISD::FMAX";
9000 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +00009001 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
9002 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00009003 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +00009004 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009005 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00009006 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009007 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +00009008 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
9009 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009010 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
9011 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
9012 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
9013 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
9014 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
9015 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +00009016 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
9017 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +00009018 case X86ISD::VSHL: return "X86ISD::VSHL";
9019 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +00009020 case X86ISD::CMPPD: return "X86ISD::CMPPD";
9021 case X86ISD::CMPPS: return "X86ISD::CMPPS";
9022 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
9023 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
9024 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
9025 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
9026 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
9027 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
9028 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
9029 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +00009030 case X86ISD::ADD: return "X86ISD::ADD";
9031 case X86ISD::SUB: return "X86ISD::SUB";
Chris Lattner5b856542010-12-20 00:59:46 +00009032 case X86ISD::ADC: return "X86ISD::ADC";
9033 case X86ISD::SBB: return "X86ISD::SBB";
Bill Wendlingd350e022008-12-12 21:15:41 +00009034 case X86ISD::SMUL: return "X86ISD::SMUL";
9035 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +00009036 case X86ISD::INC: return "X86ISD::INC";
9037 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +00009038 case X86ISD::OR: return "X86ISD::OR";
9039 case X86ISD::XOR: return "X86ISD::XOR";
9040 case X86ISD::AND: return "X86ISD::AND";
Evan Cheng73f24c92009-03-30 21:36:47 +00009041 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +00009042 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009043 case X86ISD::TESTP: return "X86ISD::TESTP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009044 case X86ISD::PALIGN: return "X86ISD::PALIGN";
9045 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
9046 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
9047 case X86ISD::PSHUFHW_LD: return "X86ISD::PSHUFHW_LD";
9048 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
9049 case X86ISD::PSHUFLW_LD: return "X86ISD::PSHUFLW_LD";
9050 case X86ISD::SHUFPS: return "X86ISD::SHUFPS";
9051 case X86ISD::SHUFPD: return "X86ISD::SHUFPD";
9052 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009053 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00009054 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009055 case X86ISD::MOVHLPD: return "X86ISD::MOVHLPD";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00009056 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
9057 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009058 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
9059 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
9060 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
9061 case X86ISD::MOVSHDUP_LD: return "X86ISD::MOVSHDUP_LD";
9062 case X86ISD::MOVSLDUP_LD: return "X86ISD::MOVSLDUP_LD";
9063 case X86ISD::MOVSD: return "X86ISD::MOVSD";
9064 case X86ISD::MOVSS: return "X86ISD::MOVSS";
9065 case X86ISD::UNPCKLPS: return "X86ISD::UNPCKLPS";
9066 case X86ISD::UNPCKLPD: return "X86ISD::UNPCKLPD";
9067 case X86ISD::UNPCKHPS: return "X86ISD::UNPCKHPS";
9068 case X86ISD::UNPCKHPD: return "X86ISD::UNPCKHPD";
9069 case X86ISD::PUNPCKLBW: return "X86ISD::PUNPCKLBW";
9070 case X86ISD::PUNPCKLWD: return "X86ISD::PUNPCKLWD";
9071 case X86ISD::PUNPCKLDQ: return "X86ISD::PUNPCKLDQ";
9072 case X86ISD::PUNPCKLQDQ: return "X86ISD::PUNPCKLQDQ";
9073 case X86ISD::PUNPCKHBW: return "X86ISD::PUNPCKHBW";
9074 case X86ISD::PUNPCKHWD: return "X86ISD::PUNPCKHWD";
9075 case X86ISD::PUNPCKHDQ: return "X86ISD::PUNPCKHDQ";
9076 case X86ISD::PUNPCKHQDQ: return "X86ISD::PUNPCKHQDQ";
Dan Gohmand6708ea2009-08-15 01:38:56 +00009077 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Dan Gohman320afb82010-10-12 18:00:49 +00009078 case X86ISD::VAARG_64: return "X86ISD::VAARG_64";
Michael J. Spencere9c253e2010-10-21 01:41:01 +00009079 case X86ISD::WIN_ALLOCA: return "X86ISD::WIN_ALLOCA";
Evan Cheng72261582005-12-20 06:22:03 +00009080 }
9081}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00009082
Chris Lattnerc9addb72007-03-30 23:15:24 +00009083// isLegalAddressingMode - Return true if the addressing mode represented
9084// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +00009085bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerc9addb72007-03-30 23:15:24 +00009086 const Type *Ty) const {
9087 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009088 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +00009089 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +00009090
Chris Lattnerc9addb72007-03-30 23:15:24 +00009091 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009092 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +00009093 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00009094
Chris Lattnerc9addb72007-03-30 23:15:24 +00009095 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +00009096 unsigned GVFlags =
9097 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009098
Chris Lattnerdfed4132009-07-10 07:38:24 +00009099 // If a reference to this global requires an extra load, we can't fold it.
9100 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +00009101 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009102
Chris Lattnerdfed4132009-07-10 07:38:24 +00009103 // If BaseGV requires a register for the PIC base, we cannot also have a
9104 // BaseReg specified.
9105 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +00009106 return false;
Evan Cheng52787842007-08-01 23:46:47 +00009107
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009108 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +00009109 if ((M != CodeModel::Small || R != Reloc::Static) &&
9110 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009111 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +00009112 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009113
Chris Lattnerc9addb72007-03-30 23:15:24 +00009114 switch (AM.Scale) {
9115 case 0:
9116 case 1:
9117 case 2:
9118 case 4:
9119 case 8:
9120 // These scales always work.
9121 break;
9122 case 3:
9123 case 5:
9124 case 9:
9125 // These scales are formed with basereg+scalereg. Only accept if there is
9126 // no basereg yet.
9127 if (AM.HasBaseReg)
9128 return false;
9129 break;
9130 default: // Other stuff never works.
9131 return false;
9132 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009133
Chris Lattnerc9addb72007-03-30 23:15:24 +00009134 return true;
9135}
9136
9137
Evan Cheng2bd122c2007-10-26 01:56:11 +00009138bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00009139 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +00009140 return false;
Evan Chenge127a732007-10-29 07:57:50 +00009141 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
9142 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00009143 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +00009144 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00009145 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +00009146}
9147
Owen Andersone50ed302009-08-10 22:56:29 +00009148bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +00009149 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009150 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009151 unsigned NumBits1 = VT1.getSizeInBits();
9152 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00009153 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009154 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00009155 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009156}
Evan Cheng2bd122c2007-10-26 01:56:11 +00009157
Dan Gohman97121ba2009-04-08 00:15:30 +00009158bool X86TargetLowering::isZExtFree(const Type *Ty1, const Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00009159 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00009160 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00009161}
9162
Owen Andersone50ed302009-08-10 22:56:29 +00009163bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00009164 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00009165 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00009166}
9167
Owen Andersone50ed302009-08-10 22:56:29 +00009168bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +00009169 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +00009170 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +00009171}
9172
Evan Cheng60c07e12006-07-05 22:17:51 +00009173/// isShuffleMaskLegal - Targets can use this to indicate that they only
9174/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
9175/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
9176/// are assumed to be legal.
9177bool
Eric Christopherfd179292009-08-27 18:07:15 +00009178X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +00009179 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +00009180 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +00009181 if (VT.getSizeInBits() == 64)
Eric Christophercff6f852010-04-15 01:40:20 +00009182 return isPALIGNRMask(M, VT, Subtarget->hasSSSE3());
Nate Begeman9008ca62009-04-27 18:41:29 +00009183
Nate Begemana09008b2009-10-19 02:17:23 +00009184 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +00009185 return (VT.getVectorNumElements() == 2 ||
9186 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
9187 isMOVLMask(M, VT) ||
9188 isSHUFPMask(M, VT) ||
9189 isPSHUFDMask(M, VT) ||
9190 isPSHUFHWMask(M, VT) ||
9191 isPSHUFLWMask(M, VT) ||
Nate Begemana09008b2009-10-19 02:17:23 +00009192 isPALIGNRMask(M, VT, Subtarget->hasSSSE3()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +00009193 isUNPCKLMask(M, VT) ||
9194 isUNPCKHMask(M, VT) ||
9195 isUNPCKL_v_undef_Mask(M, VT) ||
9196 isUNPCKH_v_undef_Mask(M, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00009197}
9198
Dan Gohman7d8143f2008-04-09 20:09:42 +00009199bool
Nate Begeman5a5ca152009-04-29 05:20:52 +00009200X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +00009201 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00009202 unsigned NumElts = VT.getVectorNumElements();
9203 // FIXME: This collection of masks seems suspect.
9204 if (NumElts == 2)
9205 return true;
9206 if (NumElts == 4 && VT.getSizeInBits() == 128) {
9207 return (isMOVLMask(Mask, VT) ||
9208 isCommutedMOVLMask(Mask, VT, true) ||
9209 isSHUFPMask(Mask, VT) ||
9210 isCommutedSHUFPMask(Mask, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00009211 }
9212 return false;
9213}
9214
9215//===----------------------------------------------------------------------===//
9216// X86 Scheduler Hooks
9217//===----------------------------------------------------------------------===//
9218
Mon P Wang63307c32008-05-05 19:05:59 +00009219// private utility function
9220MachineBasicBlock *
9221X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
9222 MachineBasicBlock *MBB,
9223 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009224 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009225 unsigned LoadOpc,
9226 unsigned CXchgOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009227 unsigned notOpc,
9228 unsigned EAXreg,
9229 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009230 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00009231 // For the atomic bitwise operator, we generate
9232 // thisMBB:
9233 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00009234 // ld t1 = [bitinstr.addr]
9235 // op t2 = t1, [bitinstr.val]
9236 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00009237 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
9238 // bz newMBB
9239 // fallthrough -->nextMBB
9240 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9241 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009242 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00009243 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009244
Mon P Wang63307c32008-05-05 19:05:59 +00009245 /// First build the CFG
9246 MachineFunction *F = MBB->getParent();
9247 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009248 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9249 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9250 F->insert(MBBIter, newMBB);
9251 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009252
Dan Gohman14152b42010-07-06 20:24:04 +00009253 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9254 nextMBB->splice(nextMBB->begin(), thisMBB,
9255 llvm::next(MachineBasicBlock::iterator(bInstr)),
9256 thisMBB->end());
9257 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009258
Mon P Wang63307c32008-05-05 19:05:59 +00009259 // Update thisMBB to fall through to newMBB
9260 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009261
Mon P Wang63307c32008-05-05 19:05:59 +00009262 // newMBB jumps to itself and fall through to nextMBB
9263 newMBB->addSuccessor(nextMBB);
9264 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009265
Mon P Wang63307c32008-05-05 19:05:59 +00009266 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009267 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009268 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +00009269 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00009270 MachineOperand& destOper = bInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009271 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00009272 int numArgs = bInstr->getNumOperands() - 1;
9273 for (int i=0; i < numArgs; ++i)
9274 argOpers[i] = &bInstr->getOperand(i+1);
9275
9276 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009277 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009278 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00009279
Dale Johannesen140be2d2008-08-19 18:47:28 +00009280 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009281 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00009282 for (int i=0; i <= lastAddrIndx; ++i)
9283 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009284
Dale Johannesen140be2d2008-08-19 18:47:28 +00009285 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009286 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00009287 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009288 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009289 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009290 tt = t1;
9291
Dale Johannesen140be2d2008-08-19 18:47:28 +00009292 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +00009293 assert((argOpers[valArgIndx]->isReg() ||
9294 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00009295 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +00009296 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009297 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00009298 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009299 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009300 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +00009301 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009302
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009303 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +00009304 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009305
Dale Johannesene4d209d2009-02-03 20:21:25 +00009306 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +00009307 for (int i=0; i <= lastAddrIndx; ++i)
9308 (*MIB).addOperand(*argOpers[i]);
9309 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +00009310 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00009311 (*MIB).setMemRefs(bInstr->memoperands_begin(),
9312 bInstr->memoperands_end());
Mon P Wangf5952662008-07-17 04:54:06 +00009313
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009314 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +00009315 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +00009316
Mon P Wang63307c32008-05-05 19:05:59 +00009317 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009318 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00009319
Dan Gohman14152b42010-07-06 20:24:04 +00009320 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00009321 return nextMBB;
9322}
9323
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00009324// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +00009325MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009326X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
9327 MachineBasicBlock *MBB,
9328 unsigned regOpcL,
9329 unsigned regOpcH,
9330 unsigned immOpcL,
9331 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009332 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009333 // For the atomic bitwise operator, we generate
9334 // thisMBB (instructions are in pairs, except cmpxchg8b)
9335 // ld t1,t2 = [bitinstr.addr]
9336 // newMBB:
9337 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
9338 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +00009339 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009340 // mov ECX, EBX <- t5, t6
9341 // mov EAX, EDX <- t1, t2
9342 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
9343 // mov t3, t4 <- EAX, EDX
9344 // bz newMBB
9345 // result in out1, out2
9346 // fallthrough -->nextMBB
9347
9348 const TargetRegisterClass *RC = X86::GR32RegisterClass;
9349 const unsigned LoadOpc = X86::MOV32rm;
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009350 const unsigned NotOpc = X86::NOT32r;
9351 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9352 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
9353 MachineFunction::iterator MBBIter = MBB;
9354 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009355
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009356 /// First build the CFG
9357 MachineFunction *F = MBB->getParent();
9358 MachineBasicBlock *thisMBB = MBB;
9359 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9360 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9361 F->insert(MBBIter, newMBB);
9362 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009363
Dan Gohman14152b42010-07-06 20:24:04 +00009364 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9365 nextMBB->splice(nextMBB->begin(), thisMBB,
9366 llvm::next(MachineBasicBlock::iterator(bInstr)),
9367 thisMBB->end());
9368 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009369
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009370 // Update thisMBB to fall through to newMBB
9371 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009372
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009373 // newMBB jumps to itself and fall through to nextMBB
9374 newMBB->addSuccessor(nextMBB);
9375 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009376
Dale Johannesene4d209d2009-02-03 20:21:25 +00009377 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009378 // Insert instructions into newMBB based on incoming instruction
9379 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009380 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009381 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009382 MachineOperand& dest1Oper = bInstr->getOperand(0);
9383 MachineOperand& dest2Oper = bInstr->getOperand(1);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009384 MachineOperand* argOpers[2 + X86::AddrNumOperands];
9385 for (int i=0; i < 2 + X86::AddrNumOperands; ++i) {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009386 argOpers[i] = &bInstr->getOperand(i+2);
9387
Dan Gohman71ea4e52010-05-14 21:01:44 +00009388 // We use some of the operands multiple times, so conservatively just
9389 // clear any kill flags that might be present.
9390 if (argOpers[i]->isReg() && argOpers[i]->isUse())
9391 argOpers[i]->setIsKill(false);
9392 }
9393
Evan Chengad5b52f2010-01-08 19:14:57 +00009394 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009395 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +00009396
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009397 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009398 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009399 for (int i=0; i <= lastAddrIndx; ++i)
9400 (*MIB).addOperand(*argOpers[i]);
9401 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009402 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +00009403 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +00009404 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009405 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +00009406 MachineOperand newOp3 = *(argOpers[3]);
9407 if (newOp3.isImm())
9408 newOp3.setImm(newOp3.getImm()+4);
9409 else
9410 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009411 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +00009412 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009413
9414 // t3/4 are defined later, at the bottom of the loop
9415 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
9416 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009417 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009418 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009419 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009420 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
9421
Evan Cheng306b4ca2010-01-08 23:41:50 +00009422 // The subsequent operations should be using the destination registers of
9423 //the PHI instructions.
Scott Michelfdc40a02009-02-17 22:15:04 +00009424 if (invSrc) {
Evan Cheng306b4ca2010-01-08 23:41:50 +00009425 t1 = F->getRegInfo().createVirtualRegister(RC);
9426 t2 = F->getRegInfo().createVirtualRegister(RC);
9427 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t1).addReg(dest1Oper.getReg());
9428 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t2).addReg(dest2Oper.getReg());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009429 } else {
Evan Cheng306b4ca2010-01-08 23:41:50 +00009430 t1 = dest1Oper.getReg();
9431 t2 = dest2Oper.getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009432 }
9433
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009434 int valArgIndx = lastAddrIndx + 1;
9435 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +00009436 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009437 "invalid operand");
9438 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
9439 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009440 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009441 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009442 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009443 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +00009444 if (regOpcL != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00009445 MIB.addReg(t1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009446 (*MIB).addOperand(*argOpers[valArgIndx]);
9447 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00009448 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009449 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00009450 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009451 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009452 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009453 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009454 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +00009455 if (regOpcH != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00009456 MIB.addReg(t2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009457 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009458
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009459 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009460 MIB.addReg(t1);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009461 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009462 MIB.addReg(t2);
9463
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009464 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009465 MIB.addReg(t5);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009466 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009467 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +00009468
Dale Johannesene4d209d2009-02-03 20:21:25 +00009469 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009470 for (int i=0; i <= lastAddrIndx; ++i)
9471 (*MIB).addOperand(*argOpers[i]);
9472
9473 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00009474 (*MIB).setMemRefs(bInstr->memoperands_begin(),
9475 bInstr->memoperands_end());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009476
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009477 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009478 MIB.addReg(X86::EAX);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009479 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009480 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +00009481
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009482 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009483 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009484
Dan Gohman14152b42010-07-06 20:24:04 +00009485 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009486 return nextMBB;
9487}
9488
9489// private utility function
9490MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +00009491X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
9492 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009493 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00009494 // For the atomic min/max operator, we generate
9495 // thisMBB:
9496 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00009497 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +00009498 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +00009499 // cmp t1, t2
9500 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +00009501 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00009502 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
9503 // bz newMBB
9504 // fallthrough -->nextMBB
9505 //
9506 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9507 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009508 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00009509 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009510
Mon P Wang63307c32008-05-05 19:05:59 +00009511 /// First build the CFG
9512 MachineFunction *F = MBB->getParent();
9513 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009514 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9515 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9516 F->insert(MBBIter, newMBB);
9517 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009518
Dan Gohman14152b42010-07-06 20:24:04 +00009519 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9520 nextMBB->splice(nextMBB->begin(), thisMBB,
9521 llvm::next(MachineBasicBlock::iterator(mInstr)),
9522 thisMBB->end());
9523 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009524
Mon P Wang63307c32008-05-05 19:05:59 +00009525 // Update thisMBB to fall through to newMBB
9526 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009527
Mon P Wang63307c32008-05-05 19:05:59 +00009528 // newMBB jumps to newMBB and fall through to nextMBB
9529 newMBB->addSuccessor(nextMBB);
9530 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009531
Dale Johannesene4d209d2009-02-03 20:21:25 +00009532 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00009533 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009534 assert(mInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009535 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +00009536 MachineOperand& destOper = mInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009537 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00009538 int numArgs = mInstr->getNumOperands() - 1;
9539 for (int i=0; i < numArgs; ++i)
9540 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009541
Mon P Wang63307c32008-05-05 19:05:59 +00009542 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009543 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009544 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00009545
Mon P Wangab3e7472008-05-05 22:56:23 +00009546 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009547 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00009548 for (int i=0; i <= lastAddrIndx; ++i)
9549 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +00009550
Mon P Wang63307c32008-05-05 19:05:59 +00009551 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +00009552 assert((argOpers[valArgIndx]->isReg() ||
9553 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00009554 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +00009555
9556 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +00009557 if (argOpers[valArgIndx]->isReg())
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009558 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +00009559 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009560 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00009561 (*MIB).addOperand(*argOpers[valArgIndx]);
9562
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009563 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +00009564 MIB.addReg(t1);
9565
Dale Johannesene4d209d2009-02-03 20:21:25 +00009566 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +00009567 MIB.addReg(t1);
9568 MIB.addReg(t2);
9569
9570 // Generate movc
9571 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009572 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +00009573 MIB.addReg(t2);
9574 MIB.addReg(t1);
9575
9576 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +00009577 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +00009578 for (int i=0; i <= lastAddrIndx; ++i)
9579 (*MIB).addOperand(*argOpers[i]);
9580 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +00009581 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00009582 (*MIB).setMemRefs(mInstr->memoperands_begin(),
9583 mInstr->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +00009584
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009585 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +00009586 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +00009587
Mon P Wang63307c32008-05-05 19:05:59 +00009588 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009589 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00009590
Dan Gohman14152b42010-07-06 20:24:04 +00009591 mInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00009592 return nextMBB;
9593}
9594
Eric Christopherf83a5de2009-08-27 18:08:16 +00009595// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00009596// or XMM0_V32I8 in AVX all of this code can be replaced with that
9597// in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +00009598MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +00009599X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +00009600 unsigned numArgs, bool memArg) const {
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00009601 assert((Subtarget->hasSSE42() || Subtarget->hasAVX()) &&
9602 "Target must have SSE4.2 or AVX features enabled");
9603
Eric Christopherb120ab42009-08-18 22:50:32 +00009604 DebugLoc dl = MI->getDebugLoc();
9605 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Eric Christopherb120ab42009-08-18 22:50:32 +00009606 unsigned Opc;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00009607 if (!Subtarget->hasAVX()) {
9608 if (memArg)
9609 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
9610 else
9611 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
9612 } else {
9613 if (memArg)
9614 Opc = numArgs == 3 ? X86::VPCMPISTRM128rm : X86::VPCMPESTRM128rm;
9615 else
9616 Opc = numArgs == 3 ? X86::VPCMPISTRM128rr : X86::VPCMPESTRM128rr;
9617 }
Eric Christopherb120ab42009-08-18 22:50:32 +00009618
Eric Christopher41c902f2010-11-30 08:20:21 +00009619 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Eric Christopherb120ab42009-08-18 22:50:32 +00009620 for (unsigned i = 0; i < numArgs; ++i) {
9621 MachineOperand &Op = MI->getOperand(i+1);
Eric Christopherb120ab42009-08-18 22:50:32 +00009622 if (!(Op.isReg() && Op.isImplicit()))
9623 MIB.addOperand(Op);
9624 }
Eric Christopher41c902f2010-11-30 08:20:21 +00009625 BuildMI(*BB, MI, dl, TII->get(X86::MOVAPSrr), MI->getOperand(0).getReg())
Eric Christopherb120ab42009-08-18 22:50:32 +00009626 .addReg(X86::XMM0);
9627
Dan Gohman14152b42010-07-06 20:24:04 +00009628 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +00009629 return BB;
9630}
9631
9632MachineBasicBlock *
Eric Christopher228232b2010-11-30 07:20:12 +00009633X86TargetLowering::EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +00009634 DebugLoc dl = MI->getDebugLoc();
9635 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009636
Eric Christopher228232b2010-11-30 07:20:12 +00009637 // Address into RAX/EAX, other two args into ECX, EDX.
9638 unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
9639 unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
9640 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg);
9641 for (int i = 0; i < X86::AddrNumOperands; ++i)
Eric Christopher82be2202010-11-30 08:10:28 +00009642 MIB.addOperand(MI->getOperand(i));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009643
Eric Christopher228232b2010-11-30 07:20:12 +00009644 unsigned ValOps = X86::AddrNumOperands;
9645 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
9646 .addReg(MI->getOperand(ValOps).getReg());
9647 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX)
9648 .addReg(MI->getOperand(ValOps+1).getReg());
9649
9650 // The instruction doesn't actually take any operands though.
9651 BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009652
Eric Christopher228232b2010-11-30 07:20:12 +00009653 MI->eraseFromParent(); // The pseudo is gone now.
9654 return BB;
9655}
9656
9657MachineBasicBlock *
9658X86TargetLowering::EmitMwait(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +00009659 DebugLoc dl = MI->getDebugLoc();
9660 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009661
Eric Christopher228232b2010-11-30 07:20:12 +00009662 // First arg in ECX, the second in EAX.
9663 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
9664 .addReg(MI->getOperand(0).getReg());
9665 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EAX)
9666 .addReg(MI->getOperand(1).getReg());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009667
Eric Christopher228232b2010-11-30 07:20:12 +00009668 // The instruction doesn't actually take any operands though.
9669 BuildMI(*BB, MI, dl, TII->get(X86::MWAITrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009670
Eric Christopher228232b2010-11-30 07:20:12 +00009671 MI->eraseFromParent(); // The pseudo is gone now.
9672 return BB;
9673}
9674
9675MachineBasicBlock *
Dan Gohman320afb82010-10-12 18:00:49 +00009676X86TargetLowering::EmitVAARG64WithCustomInserter(
9677 MachineInstr *MI,
9678 MachineBasicBlock *MBB) const {
9679 // Emit va_arg instruction on X86-64.
9680
9681 // Operands to this pseudo-instruction:
9682 // 0 ) Output : destination address (reg)
9683 // 1-5) Input : va_list address (addr, i64mem)
9684 // 6 ) ArgSize : Size (in bytes) of vararg type
9685 // 7 ) ArgMode : 0=overflow only, 1=use gp_offset, 2=use fp_offset
9686 // 8 ) Align : Alignment of type
9687 // 9 ) EFLAGS (implicit-def)
9688
9689 assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
9690 assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands");
9691
9692 unsigned DestReg = MI->getOperand(0).getReg();
9693 MachineOperand &Base = MI->getOperand(1);
9694 MachineOperand &Scale = MI->getOperand(2);
9695 MachineOperand &Index = MI->getOperand(3);
9696 MachineOperand &Disp = MI->getOperand(4);
9697 MachineOperand &Segment = MI->getOperand(5);
9698 unsigned ArgSize = MI->getOperand(6).getImm();
9699 unsigned ArgMode = MI->getOperand(7).getImm();
9700 unsigned Align = MI->getOperand(8).getImm();
9701
9702 // Memory Reference
9703 assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand");
9704 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
9705 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
9706
9707 // Machine Information
9708 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9709 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
9710 const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64);
9711 const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32);
9712 DebugLoc DL = MI->getDebugLoc();
9713
9714 // struct va_list {
9715 // i32 gp_offset
9716 // i32 fp_offset
9717 // i64 overflow_area (address)
9718 // i64 reg_save_area (address)
9719 // }
9720 // sizeof(va_list) = 24
9721 // alignment(va_list) = 8
9722
9723 unsigned TotalNumIntRegs = 6;
9724 unsigned TotalNumXMMRegs = 8;
9725 bool UseGPOffset = (ArgMode == 1);
9726 bool UseFPOffset = (ArgMode == 2);
9727 unsigned MaxOffset = TotalNumIntRegs * 8 +
9728 (UseFPOffset ? TotalNumXMMRegs * 16 : 0);
9729
9730 /* Align ArgSize to a multiple of 8 */
9731 unsigned ArgSizeA8 = (ArgSize + 7) & ~7;
9732 bool NeedsAlign = (Align > 8);
9733
9734 MachineBasicBlock *thisMBB = MBB;
9735 MachineBasicBlock *overflowMBB;
9736 MachineBasicBlock *offsetMBB;
9737 MachineBasicBlock *endMBB;
9738
9739 unsigned OffsetDestReg = 0; // Argument address computed by offsetMBB
9740 unsigned OverflowDestReg = 0; // Argument address computed by overflowMBB
9741 unsigned OffsetReg = 0;
9742
9743 if (!UseGPOffset && !UseFPOffset) {
9744 // If we only pull from the overflow region, we don't create a branch.
9745 // We don't need to alter control flow.
9746 OffsetDestReg = 0; // unused
9747 OverflowDestReg = DestReg;
9748
9749 offsetMBB = NULL;
9750 overflowMBB = thisMBB;
9751 endMBB = thisMBB;
9752 } else {
9753 // First emit code to check if gp_offset (or fp_offset) is below the bound.
9754 // If so, pull the argument from reg_save_area. (branch to offsetMBB)
9755 // If not, pull from overflow_area. (branch to overflowMBB)
9756 //
9757 // thisMBB
9758 // | .
9759 // | .
9760 // offsetMBB overflowMBB
9761 // | .
9762 // | .
9763 // endMBB
9764
9765 // Registers for the PHI in endMBB
9766 OffsetDestReg = MRI.createVirtualRegister(AddrRegClass);
9767 OverflowDestReg = MRI.createVirtualRegister(AddrRegClass);
9768
9769 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
9770 MachineFunction *MF = MBB->getParent();
9771 overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB);
9772 offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB);
9773 endMBB = MF->CreateMachineBasicBlock(LLVM_BB);
9774
9775 MachineFunction::iterator MBBIter = MBB;
9776 ++MBBIter;
9777
9778 // Insert the new basic blocks
9779 MF->insert(MBBIter, offsetMBB);
9780 MF->insert(MBBIter, overflowMBB);
9781 MF->insert(MBBIter, endMBB);
9782
9783 // Transfer the remainder of MBB and its successor edges to endMBB.
9784 endMBB->splice(endMBB->begin(), thisMBB,
9785 llvm::next(MachineBasicBlock::iterator(MI)),
9786 thisMBB->end());
9787 endMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
9788
9789 // Make offsetMBB and overflowMBB successors of thisMBB
9790 thisMBB->addSuccessor(offsetMBB);
9791 thisMBB->addSuccessor(overflowMBB);
9792
9793 // endMBB is a successor of both offsetMBB and overflowMBB
9794 offsetMBB->addSuccessor(endMBB);
9795 overflowMBB->addSuccessor(endMBB);
9796
9797 // Load the offset value into a register
9798 OffsetReg = MRI.createVirtualRegister(OffsetRegClass);
9799 BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg)
9800 .addOperand(Base)
9801 .addOperand(Scale)
9802 .addOperand(Index)
9803 .addDisp(Disp, UseFPOffset ? 4 : 0)
9804 .addOperand(Segment)
9805 .setMemRefs(MMOBegin, MMOEnd);
9806
9807 // Check if there is enough room left to pull this argument.
9808 BuildMI(thisMBB, DL, TII->get(X86::CMP32ri))
9809 .addReg(OffsetReg)
9810 .addImm(MaxOffset + 8 - ArgSizeA8);
9811
9812 // Branch to "overflowMBB" if offset >= max
9813 // Fall through to "offsetMBB" otherwise
9814 BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE)))
9815 .addMBB(overflowMBB);
9816 }
9817
9818 // In offsetMBB, emit code to use the reg_save_area.
9819 if (offsetMBB) {
9820 assert(OffsetReg != 0);
9821
9822 // Read the reg_save_area address.
9823 unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass);
9824 BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg)
9825 .addOperand(Base)
9826 .addOperand(Scale)
9827 .addOperand(Index)
9828 .addDisp(Disp, 16)
9829 .addOperand(Segment)
9830 .setMemRefs(MMOBegin, MMOEnd);
9831
9832 // Zero-extend the offset
9833 unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass);
9834 BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64)
9835 .addImm(0)
9836 .addReg(OffsetReg)
9837 .addImm(X86::sub_32bit);
9838
9839 // Add the offset to the reg_save_area to get the final address.
9840 BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg)
9841 .addReg(OffsetReg64)
9842 .addReg(RegSaveReg);
9843
9844 // Compute the offset for the next argument
9845 unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass);
9846 BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg)
9847 .addReg(OffsetReg)
9848 .addImm(UseFPOffset ? 16 : 8);
9849
9850 // Store it back into the va_list.
9851 BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr))
9852 .addOperand(Base)
9853 .addOperand(Scale)
9854 .addOperand(Index)
9855 .addDisp(Disp, UseFPOffset ? 4 : 0)
9856 .addOperand(Segment)
9857 .addReg(NextOffsetReg)
9858 .setMemRefs(MMOBegin, MMOEnd);
9859
9860 // Jump to endMBB
9861 BuildMI(offsetMBB, DL, TII->get(X86::JMP_4))
9862 .addMBB(endMBB);
9863 }
9864
9865 //
9866 // Emit code to use overflow area
9867 //
9868
9869 // Load the overflow_area address into a register.
9870 unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass);
9871 BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg)
9872 .addOperand(Base)
9873 .addOperand(Scale)
9874 .addOperand(Index)
9875 .addDisp(Disp, 8)
9876 .addOperand(Segment)
9877 .setMemRefs(MMOBegin, MMOEnd);
9878
9879 // If we need to align it, do so. Otherwise, just copy the address
9880 // to OverflowDestReg.
9881 if (NeedsAlign) {
9882 // Align the overflow address
9883 assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2");
9884 unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass);
9885
9886 // aligned_addr = (addr + (align-1)) & ~(align-1)
9887 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg)
9888 .addReg(OverflowAddrReg)
9889 .addImm(Align-1);
9890
9891 BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg)
9892 .addReg(TmpReg)
9893 .addImm(~(uint64_t)(Align-1));
9894 } else {
9895 BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg)
9896 .addReg(OverflowAddrReg);
9897 }
9898
9899 // Compute the next overflow address after this argument.
9900 // (the overflow address should be kept 8-byte aligned)
9901 unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass);
9902 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg)
9903 .addReg(OverflowDestReg)
9904 .addImm(ArgSizeA8);
9905
9906 // Store the new overflow address.
9907 BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr))
9908 .addOperand(Base)
9909 .addOperand(Scale)
9910 .addOperand(Index)
9911 .addDisp(Disp, 8)
9912 .addOperand(Segment)
9913 .addReg(NextAddrReg)
9914 .setMemRefs(MMOBegin, MMOEnd);
9915
9916 // If we branched, emit the PHI to the front of endMBB.
9917 if (offsetMBB) {
9918 BuildMI(*endMBB, endMBB->begin(), DL,
9919 TII->get(X86::PHI), DestReg)
9920 .addReg(OffsetDestReg).addMBB(offsetMBB)
9921 .addReg(OverflowDestReg).addMBB(overflowMBB);
9922 }
9923
9924 // Erase the pseudo instruction
9925 MI->eraseFromParent();
9926
9927 return endMBB;
9928}
9929
9930MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +00009931X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
9932 MachineInstr *MI,
9933 MachineBasicBlock *MBB) const {
9934 // Emit code to save XMM registers to the stack. The ABI says that the
9935 // number of registers to save is given in %al, so it's theoretically
9936 // possible to do an indirect jump trick to avoid saving all of them,
9937 // however this code takes a simpler approach and just executes all
9938 // of the stores if %al is non-zero. It's less code, and it's probably
9939 // easier on the hardware branch predictor, and stores aren't all that
9940 // expensive anyway.
9941
9942 // Create the new basic blocks. One block contains all the XMM stores,
9943 // and one block is the final destination regardless of whether any
9944 // stores were performed.
9945 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
9946 MachineFunction *F = MBB->getParent();
9947 MachineFunction::iterator MBBIter = MBB;
9948 ++MBBIter;
9949 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
9950 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
9951 F->insert(MBBIter, XMMSaveMBB);
9952 F->insert(MBBIter, EndMBB);
9953
Dan Gohman14152b42010-07-06 20:24:04 +00009954 // Transfer the remainder of MBB and its successor edges to EndMBB.
9955 EndMBB->splice(EndMBB->begin(), MBB,
9956 llvm::next(MachineBasicBlock::iterator(MI)),
9957 MBB->end());
9958 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
9959
Dan Gohmand6708ea2009-08-15 01:38:56 +00009960 // The original block will now fall through to the XMM save block.
9961 MBB->addSuccessor(XMMSaveMBB);
9962 // The XMMSaveMBB will fall through to the end block.
9963 XMMSaveMBB->addSuccessor(EndMBB);
9964
9965 // Now add the instructions.
9966 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9967 DebugLoc DL = MI->getDebugLoc();
9968
9969 unsigned CountReg = MI->getOperand(0).getReg();
9970 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
9971 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
9972
9973 if (!Subtarget->isTargetWin64()) {
9974 // If %al is 0, branch around the XMM save block.
9975 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009976 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +00009977 MBB->addSuccessor(EndMBB);
9978 }
9979
9980 // In the XMM save block, save all the XMM argument registers.
9981 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
9982 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +00009983 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +00009984 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +00009985 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +00009986 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +00009987 /*Size=*/16, /*Align=*/16);
Dan Gohmand6708ea2009-08-15 01:38:56 +00009988 BuildMI(XMMSaveMBB, DL, TII->get(X86::MOVAPSmr))
9989 .addFrameIndex(RegSaveFrameIndex)
9990 .addImm(/*Scale=*/1)
9991 .addReg(/*IndexReg=*/0)
9992 .addImm(/*Disp=*/Offset)
9993 .addReg(/*Segment=*/0)
9994 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +00009995 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +00009996 }
9997
Dan Gohman14152b42010-07-06 20:24:04 +00009998 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +00009999
10000 return EndMBB;
10001}
Mon P Wang63307c32008-05-05 19:05:59 +000010002
Evan Cheng60c07e12006-07-05 22:17:51 +000010003MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +000010004X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010005 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +000010006 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10007 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +000010008
Chris Lattner52600972009-09-02 05:57:00 +000010009 // To "insert" a SELECT_CC instruction, we actually have to insert the
10010 // diamond control-flow pattern. The incoming instruction knows the
10011 // destination vreg to set, the condition code register to branch on, the
10012 // true/false values to select between, and a branch opcode to use.
10013 const BasicBlock *LLVM_BB = BB->getBasicBlock();
10014 MachineFunction::iterator It = BB;
10015 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +000010016
Chris Lattner52600972009-09-02 05:57:00 +000010017 // thisMBB:
10018 // ...
10019 // TrueVal = ...
10020 // cmpTY ccX, r1, r2
10021 // bCC copy1MBB
10022 // fallthrough --> copy0MBB
10023 MachineBasicBlock *thisMBB = BB;
10024 MachineFunction *F = BB->getParent();
10025 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
10026 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +000010027 F->insert(It, copy0MBB);
10028 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +000010029
Bill Wendling730c07e2010-06-25 20:48:10 +000010030 // If the EFLAGS register isn't dead in the terminator, then claim that it's
10031 // live into the sink and copy blocks.
10032 const MachineFunction *MF = BB->getParent();
10033 const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo();
10034 BitVector ReservedRegs = TRI->getReservedRegs(*MF);
Bill Wendling730c07e2010-06-25 20:48:10 +000010035
Dan Gohman14152b42010-07-06 20:24:04 +000010036 for (unsigned I = 0, E = MI->getNumOperands(); I != E; ++I) {
10037 const MachineOperand &MO = MI->getOperand(I);
10038 if (!MO.isReg() || !MO.isUse() || MO.isKill()) continue;
Bill Wendling730c07e2010-06-25 20:48:10 +000010039 unsigned Reg = MO.getReg();
10040 if (Reg != X86::EFLAGS) continue;
10041 copy0MBB->addLiveIn(Reg);
10042 sinkMBB->addLiveIn(Reg);
10043 }
10044
Dan Gohman14152b42010-07-06 20:24:04 +000010045 // Transfer the remainder of BB and its successor edges to sinkMBB.
10046 sinkMBB->splice(sinkMBB->begin(), BB,
10047 llvm::next(MachineBasicBlock::iterator(MI)),
10048 BB->end());
10049 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
10050
10051 // Add the true and fallthrough blocks as its successors.
10052 BB->addSuccessor(copy0MBB);
10053 BB->addSuccessor(sinkMBB);
10054
10055 // Create the conditional branch instruction.
10056 unsigned Opc =
10057 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
10058 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
10059
Chris Lattner52600972009-09-02 05:57:00 +000010060 // copy0MBB:
10061 // %FalseValue = ...
10062 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +000010063 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +000010064
Chris Lattner52600972009-09-02 05:57:00 +000010065 // sinkMBB:
10066 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
10067 // ...
Dan Gohman14152b42010-07-06 20:24:04 +000010068 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
10069 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +000010070 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
10071 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
10072
Dan Gohman14152b42010-07-06 20:24:04 +000010073 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +000010074 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +000010075}
10076
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010077MachineBasicBlock *
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010078X86TargetLowering::EmitLoweredWinAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010079 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010080 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10081 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010082
10083 // The lowering is pretty easy: we're just emitting the call to _alloca. The
10084 // non-trivial part is impdef of ESP.
10085 // FIXME: The code should be tweaked as soon as we'll try to do codegen for
10086 // mingw-w64.
10087
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010088 const char *StackProbeSymbol =
10089 Subtarget->isTargetWindows() ? "_chkstk" : "_alloca";
10090
Dan Gohman14152b42010-07-06 20:24:04 +000010091 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010092 .addExternalSymbol(StackProbeSymbol)
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010093 .addReg(X86::EAX, RegState::Implicit)
10094 .addReg(X86::ESP, RegState::Implicit)
10095 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
Anton Korobeynikov9f7f83b2010-08-25 07:50:11 +000010096 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
10097 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010098
Dan Gohman14152b42010-07-06 20:24:04 +000010099 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010100 return BB;
10101}
Chris Lattner52600972009-09-02 05:57:00 +000010102
10103MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +000010104X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
10105 MachineBasicBlock *BB) const {
10106 // This is pretty easy. We're taking the value that we received from
10107 // our load from the relocation, sticking it in either RDI (x86-64)
10108 // or EAX and doing an indirect call. The return value will then
10109 // be in the normal return register.
Michael J. Spencerec38de22010-10-10 22:04:20 +000010110 const X86InstrInfo *TII
Eric Christopher54415362010-06-08 22:04:25 +000010111 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +000010112 DebugLoc DL = MI->getDebugLoc();
10113 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +000010114
10115 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +000010116 assert(MI->getOperand(3).isGlobal() && "This should be a global");
Michael J. Spencerec38de22010-10-10 22:04:20 +000010117
Eric Christopher30ef0e52010-06-03 04:07:48 +000010118 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +000010119 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
10120 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +000010121 .addReg(X86::RIP)
10122 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000010123 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000010124 MI->getOperand(3).getTargetFlags())
10125 .addReg(0);
Eric Christopher722d3152010-09-27 06:01:51 +000010126 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +000010127 addDirectMem(MIB, X86::RDI);
Eric Christopher61025492010-06-15 23:08:42 +000010128 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +000010129 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
10130 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +000010131 .addReg(0)
10132 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000010133 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher61025492010-06-15 23:08:42 +000010134 MI->getOperand(3).getTargetFlags())
10135 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000010136 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000010137 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010138 } else {
Dan Gohman14152b42010-07-06 20:24:04 +000010139 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
10140 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +000010141 .addReg(TII->getGlobalBaseReg(F))
10142 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000010143 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000010144 MI->getOperand(3).getTargetFlags())
10145 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000010146 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000010147 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010148 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000010149
Dan Gohman14152b42010-07-06 20:24:04 +000010150 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +000010151 return BB;
10152}
10153
10154MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +000010155X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010156 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +000010157 switch (MI->getOpcode()) {
10158 default: assert(false && "Unexpected instr type to insert");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000010159 case X86::TAILJMPd64:
10160 case X86::TAILJMPr64:
10161 case X86::TAILJMPm64:
10162 assert(!"TAILJMP64 would not be touched here.");
10163 case X86::TCRETURNdi64:
10164 case X86::TCRETURNri64:
10165 case X86::TCRETURNmi64:
10166 // Defs of TCRETURNxx64 has Win64's callee-saved registers, as subset.
10167 // On AMD64, additional defs should be added before register allocation.
10168 if (!Subtarget->isTargetWin64()) {
10169 MI->addRegisterDefined(X86::RSI);
10170 MI->addRegisterDefined(X86::RDI);
10171 MI->addRegisterDefined(X86::XMM6);
10172 MI->addRegisterDefined(X86::XMM7);
10173 MI->addRegisterDefined(X86::XMM8);
10174 MI->addRegisterDefined(X86::XMM9);
10175 MI->addRegisterDefined(X86::XMM10);
10176 MI->addRegisterDefined(X86::XMM11);
10177 MI->addRegisterDefined(X86::XMM12);
10178 MI->addRegisterDefined(X86::XMM13);
10179 MI->addRegisterDefined(X86::XMM14);
10180 MI->addRegisterDefined(X86::XMM15);
10181 }
10182 return BB;
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010183 case X86::WIN_ALLOCA:
10184 return EmitLoweredWinAlloca(MI, BB);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010185 case X86::TLSCall_32:
10186 case X86::TLSCall_64:
10187 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +000010188 case X86::CMOV_GR8:
Evan Cheng60c07e12006-07-05 22:17:51 +000010189 case X86::CMOV_FR32:
10190 case X86::CMOV_FR64:
10191 case X86::CMOV_V4F32:
10192 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +000010193 case X86::CMOV_V2I64:
Chris Lattner314a1132010-03-14 18:31:44 +000010194 case X86::CMOV_GR16:
10195 case X86::CMOV_GR32:
10196 case X86::CMOV_RFP32:
10197 case X86::CMOV_RFP64:
10198 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010199 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000010200
Dale Johannesen849f2142007-07-03 00:53:03 +000010201 case X86::FP32_TO_INT16_IN_MEM:
10202 case X86::FP32_TO_INT32_IN_MEM:
10203 case X86::FP32_TO_INT64_IN_MEM:
10204 case X86::FP64_TO_INT16_IN_MEM:
10205 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +000010206 case X86::FP64_TO_INT64_IN_MEM:
10207 case X86::FP80_TO_INT16_IN_MEM:
10208 case X86::FP80_TO_INT32_IN_MEM:
10209 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +000010210 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10211 DebugLoc DL = MI->getDebugLoc();
10212
Evan Cheng60c07e12006-07-05 22:17:51 +000010213 // Change the floating point control register to use "round towards zero"
10214 // mode when truncating to an integer value.
10215 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +000010216 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +000010217 addFrameReference(BuildMI(*BB, MI, DL,
10218 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010219
10220 // Load the old value of the high byte of the control word...
10221 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +000010222 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohman14152b42010-07-06 20:24:04 +000010223 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +000010224 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010225
10226 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +000010227 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000010228 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +000010229
10230 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +000010231 addFrameReference(BuildMI(*BB, MI, DL,
10232 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010233
10234 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +000010235 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000010236 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +000010237
10238 // Get the X86 opcode to use.
10239 unsigned Opc;
10240 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000010241 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +000010242 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
10243 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
10244 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
10245 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
10246 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
10247 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +000010248 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
10249 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
10250 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +000010251 }
10252
10253 X86AddressMode AM;
10254 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +000010255 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000010256 AM.BaseType = X86AddressMode::RegBase;
10257 AM.Base.Reg = Op.getReg();
10258 } else {
10259 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +000010260 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +000010261 }
10262 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +000010263 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000010264 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010265 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +000010266 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000010267 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010268 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +000010269 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000010270 AM.GV = Op.getGlobal();
10271 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +000010272 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010273 }
Dan Gohman14152b42010-07-06 20:24:04 +000010274 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010275 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +000010276
10277 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +000010278 addFrameReference(BuildMI(*BB, MI, DL,
10279 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010280
Dan Gohman14152b42010-07-06 20:24:04 +000010281 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +000010282 return BB;
10283 }
Eric Christopherb120ab42009-08-18 22:50:32 +000010284 // String/text processing lowering.
10285 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010286 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000010287 return EmitPCMP(MI, BB, 3, false /* in-mem */);
10288 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010289 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000010290 return EmitPCMP(MI, BB, 3, true /* in-mem */);
10291 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010292 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000010293 return EmitPCMP(MI, BB, 5, false /* in mem */);
10294 case X86::PCMPESTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010295 case X86::VPCMPESTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000010296 return EmitPCMP(MI, BB, 5, true /* in mem */);
10297
Eric Christopher228232b2010-11-30 07:20:12 +000010298 // Thread synchronization.
10299 case X86::MONITOR:
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010300 return EmitMonitor(MI, BB);
Eric Christopher228232b2010-11-30 07:20:12 +000010301 case X86::MWAIT:
10302 return EmitMwait(MI, BB);
10303
Eric Christopherb120ab42009-08-18 22:50:32 +000010304 // Atomic Lowering.
Mon P Wang63307c32008-05-05 19:05:59 +000010305 case X86::ATOMAND32:
10306 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010307 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010308 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010309 X86::NOT32r, X86::EAX,
10310 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000010311 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +000010312 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
10313 X86::OR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010314 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010315 X86::NOT32r, X86::EAX,
10316 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000010317 case X86::ATOMXOR32:
10318 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010319 X86::XOR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010320 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010321 X86::NOT32r, X86::EAX,
10322 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010323 case X86::ATOMNAND32:
10324 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010325 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010326 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010327 X86::NOT32r, X86::EAX,
10328 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +000010329 case X86::ATOMMIN32:
10330 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
10331 case X86::ATOMMAX32:
10332 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
10333 case X86::ATOMUMIN32:
10334 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
10335 case X86::ATOMUMAX32:
10336 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +000010337
10338 case X86::ATOMAND16:
10339 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
10340 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010341 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010342 X86::NOT16r, X86::AX,
10343 X86::GR16RegisterClass);
10344 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +000010345 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010346 X86::OR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010347 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010348 X86::NOT16r, X86::AX,
10349 X86::GR16RegisterClass);
10350 case X86::ATOMXOR16:
10351 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
10352 X86::XOR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010353 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010354 X86::NOT16r, X86::AX,
10355 X86::GR16RegisterClass);
10356 case X86::ATOMNAND16:
10357 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
10358 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010359 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010360 X86::NOT16r, X86::AX,
10361 X86::GR16RegisterClass, true);
10362 case X86::ATOMMIN16:
10363 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
10364 case X86::ATOMMAX16:
10365 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
10366 case X86::ATOMUMIN16:
10367 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
10368 case X86::ATOMUMAX16:
10369 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
10370
10371 case X86::ATOMAND8:
10372 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
10373 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010374 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010375 X86::NOT8r, X86::AL,
10376 X86::GR8RegisterClass);
10377 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +000010378 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010379 X86::OR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010380 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010381 X86::NOT8r, X86::AL,
10382 X86::GR8RegisterClass);
10383 case X86::ATOMXOR8:
10384 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
10385 X86::XOR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010386 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010387 X86::NOT8r, X86::AL,
10388 X86::GR8RegisterClass);
10389 case X86::ATOMNAND8:
10390 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
10391 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010392 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010393 X86::NOT8r, X86::AL,
10394 X86::GR8RegisterClass, true);
10395 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010396 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +000010397 case X86::ATOMAND64:
10398 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010399 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010400 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010401 X86::NOT64r, X86::RAX,
10402 X86::GR64RegisterClass);
10403 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +000010404 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
10405 X86::OR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010406 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010407 X86::NOT64r, X86::RAX,
10408 X86::GR64RegisterClass);
10409 case X86::ATOMXOR64:
10410 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010411 X86::XOR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010412 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010413 X86::NOT64r, X86::RAX,
10414 X86::GR64RegisterClass);
10415 case X86::ATOMNAND64:
10416 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
10417 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010418 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010419 X86::NOT64r, X86::RAX,
10420 X86::GR64RegisterClass, true);
10421 case X86::ATOMMIN64:
10422 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
10423 case X86::ATOMMAX64:
10424 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
10425 case X86::ATOMUMIN64:
10426 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
10427 case X86::ATOMUMAX64:
10428 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010429
10430 // This group does 64-bit operations on a 32-bit host.
10431 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010432 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010433 X86::AND32rr, X86::AND32rr,
10434 X86::AND32ri, X86::AND32ri,
10435 false);
10436 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010437 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010438 X86::OR32rr, X86::OR32rr,
10439 X86::OR32ri, X86::OR32ri,
10440 false);
10441 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010442 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010443 X86::XOR32rr, X86::XOR32rr,
10444 X86::XOR32ri, X86::XOR32ri,
10445 false);
10446 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010447 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010448 X86::AND32rr, X86::AND32rr,
10449 X86::AND32ri, X86::AND32ri,
10450 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010451 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010452 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010453 X86::ADD32rr, X86::ADC32rr,
10454 X86::ADD32ri, X86::ADC32ri,
10455 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010456 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010457 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010458 X86::SUB32rr, X86::SBB32rr,
10459 X86::SUB32ri, X86::SBB32ri,
10460 false);
Dale Johannesen880ae362008-10-03 22:25:52 +000010461 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010462 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +000010463 X86::MOV32rr, X86::MOV32rr,
10464 X86::MOV32ri, X86::MOV32ri,
10465 false);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010466 case X86::VASTART_SAVE_XMM_REGS:
10467 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Dan Gohman320afb82010-10-12 18:00:49 +000010468
10469 case X86::VAARG_64:
10470 return EmitVAARG64WithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000010471 }
10472}
10473
10474//===----------------------------------------------------------------------===//
10475// X86 Optimization Hooks
10476//===----------------------------------------------------------------------===//
10477
Dan Gohman475871a2008-07-27 21:46:04 +000010478void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +000010479 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000010480 APInt &KnownZero,
10481 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +000010482 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +000010483 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010484 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +000010485 assert((Opc >= ISD::BUILTIN_OP_END ||
10486 Opc == ISD::INTRINSIC_WO_CHAIN ||
10487 Opc == ISD::INTRINSIC_W_CHAIN ||
10488 Opc == ISD::INTRINSIC_VOID) &&
10489 "Should use MaskedValueIsZero if you don't know whether Op"
10490 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010491
Dan Gohmanf4f92f52008-02-13 23:07:24 +000010492 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010493 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +000010494 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +000010495 case X86ISD::ADD:
10496 case X86ISD::SUB:
Chris Lattner5b856542010-12-20 00:59:46 +000010497 case X86ISD::ADC:
10498 case X86ISD::SBB:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000010499 case X86ISD::SMUL:
10500 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +000010501 case X86ISD::INC:
10502 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +000010503 case X86ISD::OR:
10504 case X86ISD::XOR:
10505 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000010506 // These nodes' second result is a boolean.
10507 if (Op.getResNo() == 0)
10508 break;
10509 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010510 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000010511 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
10512 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +000010513 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010514 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010515}
Chris Lattner259e97c2006-01-31 19:43:35 +000010516
Owen Andersonbc146b02010-09-21 20:42:50 +000010517unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
10518 unsigned Depth) const {
10519 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
10520 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
10521 return Op.getValueType().getScalarType().getSizeInBits();
Michael J. Spencerec38de22010-10-10 22:04:20 +000010522
Owen Andersonbc146b02010-09-21 20:42:50 +000010523 // Fallback case.
10524 return 1;
10525}
10526
Evan Cheng206ee9d2006-07-07 08:33:52 +000010527/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +000010528/// node is a GlobalAddress + offset.
10529bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +000010530 const GlobalValue* &GA,
10531 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +000010532 if (N->getOpcode() == X86ISD::Wrapper) {
10533 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +000010534 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +000010535 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +000010536 return true;
10537 }
Evan Cheng206ee9d2006-07-07 08:33:52 +000010538 }
Evan Chengad4196b2008-05-12 19:56:52 +000010539 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +000010540}
10541
Evan Cheng206ee9d2006-07-07 08:33:52 +000010542/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
10543/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
10544/// if the load addresses are consecutive, non-overlapping, and in the right
Nate Begemanfdea31a2010-03-24 20:49:50 +000010545/// order.
Dan Gohman475871a2008-07-27 21:46:04 +000010546static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Mon P Wanga0fd0d52010-12-19 23:55:53 +000010547 TargetLowering::DAGCombinerInfo &DCI) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000010548 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000010549 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +000010550
Eli Friedman7a5e5552009-06-07 06:52:44 +000010551 if (VT.getSizeInBits() != 128)
10552 return SDValue();
10553
Mon P Wanga0fd0d52010-12-19 23:55:53 +000010554 // Don't create instructions with illegal types after legalize types has run.
10555 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10556 if (!DCI.isBeforeLegalize() && !TLI.isTypeLegal(VT.getVectorElementType()))
10557 return SDValue();
10558
Nate Begemanfdea31a2010-03-24 20:49:50 +000010559 SmallVector<SDValue, 16> Elts;
10560 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000010561 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +000010562
Nate Begemanfdea31a2010-03-24 20:49:50 +000010563 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000010564}
Evan Chengd880b972008-05-09 21:53:03 +000010565
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +000010566/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
10567/// generation and convert it from being a bunch of shuffles and extracts
10568/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010569static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
10570 const TargetLowering &TLI) {
10571 SDValue InputVector = N->getOperand(0);
10572
10573 // Only operate on vectors of 4 elements, where the alternative shuffling
10574 // gets to be more expensive.
10575 if (InputVector.getValueType() != MVT::v4i32)
10576 return SDValue();
10577
10578 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
10579 // single use which is a sign-extend or zero-extend, and all elements are
10580 // used.
10581 SmallVector<SDNode *, 4> Uses;
10582 unsigned ExtractedElements = 0;
10583 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
10584 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
10585 if (UI.getUse().getResNo() != InputVector.getResNo())
10586 return SDValue();
10587
10588 SDNode *Extract = *UI;
10589 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
10590 return SDValue();
10591
10592 if (Extract->getValueType(0) != MVT::i32)
10593 return SDValue();
10594 if (!Extract->hasOneUse())
10595 return SDValue();
10596 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
10597 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
10598 return SDValue();
10599 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
10600 return SDValue();
10601
10602 // Record which element was extracted.
10603 ExtractedElements |=
10604 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
10605
10606 Uses.push_back(Extract);
10607 }
10608
10609 // If not all the elements were used, this may not be worthwhile.
10610 if (ExtractedElements != 15)
10611 return SDValue();
10612
10613 // Ok, we've now decided to do the transformation.
10614 DebugLoc dl = InputVector.getDebugLoc();
10615
10616 // Store the value to a temporary stack slot.
10617 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000010618 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
10619 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010620
10621 // Replace each use (extract) with a load of the appropriate element.
10622 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
10623 UE = Uses.end(); UI != UE; ++UI) {
10624 SDNode *Extract = *UI;
10625
10626 // Compute the element's address.
10627 SDValue Idx = Extract->getOperand(1);
10628 unsigned EltSize =
10629 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
10630 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
10631 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
10632
Eric Christopher90eb4022010-07-22 00:26:08 +000010633 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, Idx.getValueType(),
Chris Lattner51abfe42010-09-21 06:02:19 +000010634 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010635
10636 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000010637 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000010638 ScalarAddr, MachinePointerInfo(),
10639 false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010640
10641 // Replace the exact with the load.
10642 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
10643 }
10644
10645 // The replacement was made in place; don't return anything.
10646 return SDValue();
10647}
10648
Chris Lattner83e6c992006-10-04 06:57:07 +000010649/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000010650static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner47b4ce82009-03-11 05:48:52 +000010651 const X86Subtarget *Subtarget) {
10652 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +000010653 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000010654 // Get the LHS/RHS of the select.
10655 SDValue LHS = N->getOperand(1);
10656 SDValue RHS = N->getOperand(2);
Eric Christopherfd179292009-08-27 18:07:15 +000010657
Dan Gohman670e5392009-09-21 18:03:22 +000010658 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000010659 // instructions match the semantics of the common C idiom x<y?x:y but not
10660 // x<=y?x:y, because of how they handle negative zero (which can be
10661 // ignored in unsafe-math mode).
Chris Lattner83e6c992006-10-04 06:57:07 +000010662 if (Subtarget->hasSSE2() &&
Owen Anderson825b72b2009-08-11 20:47:22 +000010663 (LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64) &&
Chris Lattner47b4ce82009-03-11 05:48:52 +000010664 Cond.getOpcode() == ISD::SETCC) {
10665 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010666
Chris Lattner47b4ce82009-03-11 05:48:52 +000010667 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000010668 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000010669 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
10670 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000010671 switch (CC) {
10672 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000010673 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000010674 // Converting this to a min would handle NaNs incorrectly, and swapping
10675 // the operands would cause it to handle comparisons between positive
10676 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000010677 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000010678 if (!UnsafeFPMath &&
10679 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
10680 break;
10681 std::swap(LHS, RHS);
10682 }
Dan Gohman670e5392009-09-21 18:03:22 +000010683 Opcode = X86ISD::FMIN;
10684 break;
10685 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000010686 // Converting this to a min would handle comparisons between positive
10687 // and negative zero incorrectly.
10688 if (!UnsafeFPMath &&
10689 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
10690 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010691 Opcode = X86ISD::FMIN;
10692 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000010693 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000010694 // Converting this to a min would handle both negative zeros and NaNs
10695 // incorrectly, but we can swap the operands to fix both.
10696 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000010697 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010698 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000010699 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010700 Opcode = X86ISD::FMIN;
10701 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010702
Dan Gohman670e5392009-09-21 18:03:22 +000010703 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000010704 // Converting this to a max would handle comparisons between positive
10705 // and negative zero incorrectly.
10706 if (!UnsafeFPMath &&
10707 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(LHS))
10708 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010709 Opcode = X86ISD::FMAX;
10710 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000010711 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000010712 // Converting this to a max would handle NaNs incorrectly, and swapping
10713 // the operands would cause it to handle comparisons between positive
10714 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000010715 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000010716 if (!UnsafeFPMath &&
10717 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
10718 break;
10719 std::swap(LHS, RHS);
10720 }
Dan Gohman670e5392009-09-21 18:03:22 +000010721 Opcode = X86ISD::FMAX;
10722 break;
10723 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000010724 // Converting this to a max would handle both negative zeros and NaNs
10725 // incorrectly, but we can swap the operands to fix both.
10726 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000010727 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010728 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010729 case ISD::SETGE:
10730 Opcode = X86ISD::FMAX;
10731 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000010732 }
Dan Gohman670e5392009-09-21 18:03:22 +000010733 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000010734 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
10735 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000010736 switch (CC) {
10737 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000010738 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000010739 // Converting this to a min would handle comparisons between positive
10740 // and negative zero incorrectly, and swapping the operands would
10741 // cause it to handle NaNs incorrectly.
10742 if (!UnsafeFPMath &&
10743 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000010744 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000010745 break;
10746 std::swap(LHS, RHS);
10747 }
Dan Gohman670e5392009-09-21 18:03:22 +000010748 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000010749 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010750 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000010751 // Converting this to a min would handle NaNs incorrectly.
10752 if (!UnsafeFPMath &&
10753 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
10754 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010755 Opcode = X86ISD::FMIN;
10756 break;
10757 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000010758 // Converting this to a min would handle both negative zeros and NaNs
10759 // incorrectly, but we can swap the operands to fix both.
10760 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000010761 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010762 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010763 case ISD::SETGE:
10764 Opcode = X86ISD::FMIN;
10765 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010766
Dan Gohman670e5392009-09-21 18:03:22 +000010767 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000010768 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000010769 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000010770 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010771 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000010772 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010773 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000010774 // Converting this to a max would handle comparisons between positive
10775 // and negative zero incorrectly, and swapping the operands would
10776 // cause it to handle NaNs incorrectly.
10777 if (!UnsafeFPMath &&
10778 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000010779 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000010780 break;
10781 std::swap(LHS, RHS);
10782 }
Dan Gohman670e5392009-09-21 18:03:22 +000010783 Opcode = X86ISD::FMAX;
10784 break;
10785 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000010786 // Converting this to a max would handle both negative zeros and NaNs
10787 // incorrectly, but we can swap the operands to fix both.
10788 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000010789 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010790 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000010791 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010792 Opcode = X86ISD::FMAX;
10793 break;
10794 }
Chris Lattner83e6c992006-10-04 06:57:07 +000010795 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010796
Chris Lattner47b4ce82009-03-11 05:48:52 +000010797 if (Opcode)
10798 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000010799 }
Eric Christopherfd179292009-08-27 18:07:15 +000010800
Chris Lattnerd1980a52009-03-12 06:52:53 +000010801 // If this is a select between two integer constants, try to do some
10802 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000010803 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
10804 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000010805 // Don't do this for crazy integer types.
10806 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
10807 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000010808 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000010809 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000010810
Chris Lattnercee56e72009-03-13 05:53:31 +000010811 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000010812 // Efficiently invertible.
10813 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
10814 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
10815 isa<ConstantSDNode>(Cond.getOperand(1))))) {
10816 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000010817 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000010818 }
Eric Christopherfd179292009-08-27 18:07:15 +000010819
Chris Lattnerd1980a52009-03-12 06:52:53 +000010820 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000010821 if (FalseC->getAPIntValue() == 0 &&
10822 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000010823 if (NeedsCondInvert) // Invert the condition if needed.
10824 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
10825 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000010826
Chris Lattnerd1980a52009-03-12 06:52:53 +000010827 // Zero extend the condition if needed.
10828 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000010829
Chris Lattnercee56e72009-03-13 05:53:31 +000010830 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000010831 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000010832 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000010833 }
Eric Christopherfd179292009-08-27 18:07:15 +000010834
Chris Lattner97a29a52009-03-13 05:22:11 +000010835 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000010836 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +000010837 if (NeedsCondInvert) // Invert the condition if needed.
10838 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
10839 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000010840
Chris Lattner97a29a52009-03-13 05:22:11 +000010841 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000010842 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
10843 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000010844 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000010845 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000010846 }
Eric Christopherfd179292009-08-27 18:07:15 +000010847
Chris Lattnercee56e72009-03-13 05:53:31 +000010848 // Optimize cases that will turn into an LEA instruction. This requires
10849 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000010850 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000010851 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000010852 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000010853
Chris Lattnercee56e72009-03-13 05:53:31 +000010854 bool isFastMultiplier = false;
10855 if (Diff < 10) {
10856 switch ((unsigned char)Diff) {
10857 default: break;
10858 case 1: // result = add base, cond
10859 case 2: // result = lea base( , cond*2)
10860 case 3: // result = lea base(cond, cond*2)
10861 case 4: // result = lea base( , cond*4)
10862 case 5: // result = lea base(cond, cond*4)
10863 case 8: // result = lea base( , cond*8)
10864 case 9: // result = lea base(cond, cond*8)
10865 isFastMultiplier = true;
10866 break;
10867 }
10868 }
Eric Christopherfd179292009-08-27 18:07:15 +000010869
Chris Lattnercee56e72009-03-13 05:53:31 +000010870 if (isFastMultiplier) {
10871 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
10872 if (NeedsCondInvert) // Invert the condition if needed.
10873 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
10874 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000010875
Chris Lattnercee56e72009-03-13 05:53:31 +000010876 // Zero extend the condition if needed.
10877 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
10878 Cond);
10879 // Scale the condition by the difference.
10880 if (Diff != 1)
10881 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
10882 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000010883
Chris Lattnercee56e72009-03-13 05:53:31 +000010884 // Add the base if non-zero.
10885 if (FalseC->getAPIntValue() != 0)
10886 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
10887 SDValue(FalseC, 0));
10888 return Cond;
10889 }
Eric Christopherfd179292009-08-27 18:07:15 +000010890 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000010891 }
10892 }
Eric Christopherfd179292009-08-27 18:07:15 +000010893
Dan Gohman475871a2008-07-27 21:46:04 +000010894 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000010895}
10896
Chris Lattnerd1980a52009-03-12 06:52:53 +000010897/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
10898static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
10899 TargetLowering::DAGCombinerInfo &DCI) {
10900 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +000010901
Chris Lattnerd1980a52009-03-12 06:52:53 +000010902 // If the flag operand isn't dead, don't touch this CMOV.
10903 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
10904 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +000010905
Chris Lattnerd1980a52009-03-12 06:52:53 +000010906 // If this is a select between two integer constants, try to do some
10907 // optimizations. Note that the operands are ordered the opposite of SELECT
10908 // operands.
10909 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
10910 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
10911 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
10912 // larger than FalseC (the false value).
10913 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
Eric Christopherfd179292009-08-27 18:07:15 +000010914
Chris Lattnerd1980a52009-03-12 06:52:53 +000010915 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
10916 CC = X86::GetOppositeBranchCondition(CC);
10917 std::swap(TrueC, FalseC);
10918 }
Eric Christopherfd179292009-08-27 18:07:15 +000010919
Chris Lattnerd1980a52009-03-12 06:52:53 +000010920 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000010921 // This is efficient for any integer data type (including i8/i16) and
10922 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000010923 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
10924 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +000010925 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
10926 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000010927
Chris Lattnerd1980a52009-03-12 06:52:53 +000010928 // Zero extend the condition if needed.
10929 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000010930
Chris Lattnerd1980a52009-03-12 06:52:53 +000010931 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
10932 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000010933 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000010934 if (N->getNumValues() == 2) // Dead flag value?
10935 return DCI.CombineTo(N, Cond, SDValue());
10936 return Cond;
10937 }
Eric Christopherfd179292009-08-27 18:07:15 +000010938
Chris Lattnercee56e72009-03-13 05:53:31 +000010939 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
10940 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000010941 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
10942 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +000010943 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
10944 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000010945
Chris Lattner97a29a52009-03-13 05:22:11 +000010946 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000010947 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
10948 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000010949 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
10950 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000010951
Chris Lattner97a29a52009-03-13 05:22:11 +000010952 if (N->getNumValues() == 2) // Dead flag value?
10953 return DCI.CombineTo(N, Cond, SDValue());
10954 return Cond;
10955 }
Eric Christopherfd179292009-08-27 18:07:15 +000010956
Chris Lattnercee56e72009-03-13 05:53:31 +000010957 // Optimize cases that will turn into an LEA instruction. This requires
10958 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000010959 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000010960 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000010961 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000010962
Chris Lattnercee56e72009-03-13 05:53:31 +000010963 bool isFastMultiplier = false;
10964 if (Diff < 10) {
10965 switch ((unsigned char)Diff) {
10966 default: break;
10967 case 1: // result = add base, cond
10968 case 2: // result = lea base( , cond*2)
10969 case 3: // result = lea base(cond, cond*2)
10970 case 4: // result = lea base( , cond*4)
10971 case 5: // result = lea base(cond, cond*4)
10972 case 8: // result = lea base( , cond*8)
10973 case 9: // result = lea base(cond, cond*8)
10974 isFastMultiplier = true;
10975 break;
10976 }
10977 }
Eric Christopherfd179292009-08-27 18:07:15 +000010978
Chris Lattnercee56e72009-03-13 05:53:31 +000010979 if (isFastMultiplier) {
10980 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
10981 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +000010982 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
10983 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000010984 // Zero extend the condition if needed.
10985 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
10986 Cond);
10987 // Scale the condition by the difference.
10988 if (Diff != 1)
10989 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
10990 DAG.getConstant(Diff, Cond.getValueType()));
10991
10992 // Add the base if non-zero.
10993 if (FalseC->getAPIntValue() != 0)
10994 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
10995 SDValue(FalseC, 0));
10996 if (N->getNumValues() == 2) // Dead flag value?
10997 return DCI.CombineTo(N, Cond, SDValue());
10998 return Cond;
10999 }
Eric Christopherfd179292009-08-27 18:07:15 +000011000 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000011001 }
11002 }
11003 return SDValue();
11004}
11005
11006
Evan Cheng0b0cd912009-03-28 05:57:29 +000011007/// PerformMulCombine - Optimize a single multiply with constant into two
11008/// in order to implement it with two cheaper instructions, e.g.
11009/// LEA + SHL, LEA + LEA.
11010static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
11011 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000011012 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
11013 return SDValue();
11014
Owen Andersone50ed302009-08-10 22:56:29 +000011015 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000011016 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000011017 return SDValue();
11018
11019 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
11020 if (!C)
11021 return SDValue();
11022 uint64_t MulAmt = C->getZExtValue();
11023 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
11024 return SDValue();
11025
11026 uint64_t MulAmt1 = 0;
11027 uint64_t MulAmt2 = 0;
11028 if ((MulAmt % 9) == 0) {
11029 MulAmt1 = 9;
11030 MulAmt2 = MulAmt / 9;
11031 } else if ((MulAmt % 5) == 0) {
11032 MulAmt1 = 5;
11033 MulAmt2 = MulAmt / 5;
11034 } else if ((MulAmt % 3) == 0) {
11035 MulAmt1 = 3;
11036 MulAmt2 = MulAmt / 3;
11037 }
11038 if (MulAmt2 &&
11039 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
11040 DebugLoc DL = N->getDebugLoc();
11041
11042 if (isPowerOf2_64(MulAmt2) &&
11043 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
11044 // If second multiplifer is pow2, issue it first. We want the multiply by
11045 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
11046 // is an add.
11047 std::swap(MulAmt1, MulAmt2);
11048
11049 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000011050 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000011051 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000011052 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000011053 else
Evan Cheng73f24c92009-03-30 21:36:47 +000011054 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000011055 DAG.getConstant(MulAmt1, VT));
11056
Eric Christopherfd179292009-08-27 18:07:15 +000011057 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000011058 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000011059 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000011060 else
Evan Cheng73f24c92009-03-30 21:36:47 +000011061 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000011062 DAG.getConstant(MulAmt2, VT));
11063
11064 // Do not add new nodes to DAG combiner worklist.
11065 DCI.CombineTo(N, NewMul, false);
11066 }
11067 return SDValue();
11068}
11069
Evan Chengad9c0a32009-12-15 00:53:42 +000011070static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
11071 SDValue N0 = N->getOperand(0);
11072 SDValue N1 = N->getOperand(1);
11073 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
11074 EVT VT = N0.getValueType();
11075
11076 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
11077 // since the result of setcc_c is all zero's or all ones.
11078 if (N1C && N0.getOpcode() == ISD::AND &&
11079 N0.getOperand(1).getOpcode() == ISD::Constant) {
11080 SDValue N00 = N0.getOperand(0);
11081 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
11082 ((N00.getOpcode() == ISD::ANY_EXTEND ||
11083 N00.getOpcode() == ISD::ZERO_EXTEND) &&
11084 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
11085 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
11086 APInt ShAmt = N1C->getAPIntValue();
11087 Mask = Mask.shl(ShAmt);
11088 if (Mask != 0)
11089 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
11090 N00, DAG.getConstant(Mask, VT));
11091 }
11092 }
11093
11094 return SDValue();
11095}
Evan Cheng0b0cd912009-03-28 05:57:29 +000011096
Nate Begeman740ab032009-01-26 00:52:55 +000011097/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
11098/// when possible.
11099static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
11100 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +000011101 EVT VT = N->getValueType(0);
11102 if (!VT.isVector() && VT.isInteger() &&
11103 N->getOpcode() == ISD::SHL)
11104 return PerformSHLCombine(N, DAG);
11105
Nate Begeman740ab032009-01-26 00:52:55 +000011106 // On X86 with SSE2 support, we can transform this to a vector shift if
11107 // all elements are shifted by the same amount. We can't do this in legalize
11108 // because the a constant vector is typically transformed to a constant pool
11109 // so we have no knowledge of the shift amount.
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011110 if (!Subtarget->hasSSE2())
11111 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000011112
Owen Anderson825b72b2009-08-11 20:47:22 +000011113 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011114 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000011115
Mon P Wang3becd092009-01-28 08:12:05 +000011116 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +000011117 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +000011118 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +000011119 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +000011120 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
11121 unsigned NumElts = VT.getVectorNumElements();
11122 unsigned i = 0;
11123 for (; i != NumElts; ++i) {
11124 SDValue Arg = ShAmtOp.getOperand(i);
11125 if (Arg.getOpcode() == ISD::UNDEF) continue;
11126 BaseShAmt = Arg;
11127 break;
11128 }
11129 for (; i != NumElts; ++i) {
11130 SDValue Arg = ShAmtOp.getOperand(i);
11131 if (Arg.getOpcode() == ISD::UNDEF) continue;
11132 if (Arg != BaseShAmt) {
11133 return SDValue();
11134 }
11135 }
11136 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +000011137 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +000011138 SDValue InVec = ShAmtOp.getOperand(0);
11139 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
11140 unsigned NumElts = InVec.getValueType().getVectorNumElements();
11141 unsigned i = 0;
11142 for (; i != NumElts; ++i) {
11143 SDValue Arg = InVec.getOperand(i);
11144 if (Arg.getOpcode() == ISD::UNDEF) continue;
11145 BaseShAmt = Arg;
11146 break;
11147 }
11148 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
11149 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +000011150 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +000011151 if (C->getZExtValue() == SplatIdx)
11152 BaseShAmt = InVec.getOperand(1);
11153 }
11154 }
11155 if (BaseShAmt.getNode() == 0)
11156 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
11157 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +000011158 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011159 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000011160
Mon P Wangefa42202009-09-03 19:56:25 +000011161 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +000011162 if (EltVT.bitsGT(MVT::i32))
11163 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
11164 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +000011165 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +000011166
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011167 // The shift amount is identical so we can do a vector shift.
11168 SDValue ValOp = N->getOperand(0);
11169 switch (N->getOpcode()) {
11170 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000011171 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011172 break;
11173 case ISD::SHL:
Owen Anderson825b72b2009-08-11 20:47:22 +000011174 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011175 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011176 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011177 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011178 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011179 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011180 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011181 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011182 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011183 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011184 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011185 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011186 break;
11187 case ISD::SRA:
Owen Anderson825b72b2009-08-11 20:47:22 +000011188 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011189 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011190 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011191 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011192 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011193 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011194 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011195 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011196 break;
11197 case ISD::SRL:
Owen Anderson825b72b2009-08-11 20:47:22 +000011198 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011199 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011200 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011201 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011202 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011203 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011204 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011205 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011206 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011207 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011208 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011209 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011210 break;
Nate Begeman740ab032009-01-26 00:52:55 +000011211 }
11212 return SDValue();
11213}
11214
Nate Begemanb65c1752010-12-17 22:55:37 +000011215
11216static SDValue PerformAndCombine(SDNode *N, SelectionDAG &DAG,
11217 TargetLowering::DAGCombinerInfo &DCI,
11218 const X86Subtarget *Subtarget) {
11219 if (DCI.isBeforeLegalizeOps())
11220 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011221
Nate Begemanb65c1752010-12-17 22:55:37 +000011222 // Want to form PANDN nodes, in the hopes of then easily combining them with
11223 // OR and AND nodes to form PBLEND/PSIGN.
11224 EVT VT = N->getValueType(0);
11225 if (VT != MVT::v2i64)
11226 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011227
Nate Begemanb65c1752010-12-17 22:55:37 +000011228 SDValue N0 = N->getOperand(0);
11229 SDValue N1 = N->getOperand(1);
11230 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011231
Nate Begemanb65c1752010-12-17 22:55:37 +000011232 // Check LHS for vnot
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011233 if (N0.getOpcode() == ISD::XOR &&
Nate Begemanb65c1752010-12-17 22:55:37 +000011234 ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode()))
11235 return DAG.getNode(X86ISD::PANDN, DL, VT, N0.getOperand(0), N1);
11236
11237 // Check RHS for vnot
11238 if (N1.getOpcode() == ISD::XOR &&
11239 ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode()))
11240 return DAG.getNode(X86ISD::PANDN, DL, VT, N1.getOperand(0), N0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011241
Nate Begemanb65c1752010-12-17 22:55:37 +000011242 return SDValue();
11243}
11244
Evan Cheng760d1942010-01-04 21:22:48 +000011245static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000011246 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000011247 const X86Subtarget *Subtarget) {
Evan Cheng39cfeec2010-04-28 02:25:18 +000011248 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000011249 return SDValue();
11250
Evan Cheng760d1942010-01-04 21:22:48 +000011251 EVT VT = N->getValueType(0);
Nate Begemanb65c1752010-12-17 22:55:37 +000011252 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64 && VT != MVT::v2i64)
Evan Cheng760d1942010-01-04 21:22:48 +000011253 return SDValue();
11254
Evan Cheng760d1942010-01-04 21:22:48 +000011255 SDValue N0 = N->getOperand(0);
11256 SDValue N1 = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011257
Nate Begemanb65c1752010-12-17 22:55:37 +000011258 // look for psign/blend
11259 if (Subtarget->hasSSSE3()) {
11260 if (VT == MVT::v2i64) {
11261 // Canonicalize pandn to RHS
11262 if (N0.getOpcode() == X86ISD::PANDN)
11263 std::swap(N0, N1);
11264 // or (and (m, x), (pandn m, y))
11265 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::PANDN) {
11266 SDValue Mask = N1.getOperand(0);
11267 SDValue X = N1.getOperand(1);
11268 SDValue Y;
11269 if (N0.getOperand(0) == Mask)
11270 Y = N0.getOperand(1);
11271 if (N0.getOperand(1) == Mask)
11272 Y = N0.getOperand(0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011273
Nate Begemanb65c1752010-12-17 22:55:37 +000011274 // Check to see if the mask appeared in both the AND and PANDN and
11275 if (!Y.getNode())
11276 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011277
Nate Begemanb65c1752010-12-17 22:55:37 +000011278 // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them.
11279 if (Mask.getOpcode() != ISD::BITCAST ||
11280 X.getOpcode() != ISD::BITCAST ||
11281 Y.getOpcode() != ISD::BITCAST)
11282 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011283
Nate Begemanb65c1752010-12-17 22:55:37 +000011284 // Look through mask bitcast.
11285 Mask = Mask.getOperand(0);
11286 EVT MaskVT = Mask.getValueType();
11287
11288 // Validate that the Mask operand is a vector sra node. The sra node
11289 // will be an intrinsic.
11290 if (Mask.getOpcode() != ISD::INTRINSIC_WO_CHAIN)
11291 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011292
Nate Begemanb65c1752010-12-17 22:55:37 +000011293 // FIXME: what to do for bytes, since there is a psignb/pblendvb, but
11294 // there is no psrai.b
11295 switch (cast<ConstantSDNode>(Mask.getOperand(0))->getZExtValue()) {
11296 case Intrinsic::x86_sse2_psrai_w:
11297 case Intrinsic::x86_sse2_psrai_d:
11298 break;
11299 default: return SDValue();
11300 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011301
Nate Begemanb65c1752010-12-17 22:55:37 +000011302 // Check that the SRA is all signbits.
11303 SDValue SraC = Mask.getOperand(2);
11304 unsigned SraAmt = cast<ConstantSDNode>(SraC)->getZExtValue();
11305 unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits();
11306 if ((SraAmt + 1) != EltBits)
11307 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011308
Nate Begemanb65c1752010-12-17 22:55:37 +000011309 DebugLoc DL = N->getDebugLoc();
11310
11311 // Now we know we at least have a plendvb with the mask val. See if
11312 // we can form a psignb/w/d.
11313 // psign = x.type == y.type == mask.type && y = sub(0, x);
11314 X = X.getOperand(0);
11315 Y = Y.getOperand(0);
11316 if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X &&
11317 ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) &&
11318 X.getValueType() == MaskVT && X.getValueType() == Y.getValueType()){
11319 unsigned Opc = 0;
11320 switch (EltBits) {
11321 case 8: Opc = X86ISD::PSIGNB; break;
11322 case 16: Opc = X86ISD::PSIGNW; break;
11323 case 32: Opc = X86ISD::PSIGND; break;
11324 default: break;
11325 }
11326 if (Opc) {
11327 SDValue Sign = DAG.getNode(Opc, DL, MaskVT, X, Mask.getOperand(1));
11328 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Sign);
11329 }
11330 }
11331 // PBLENDVB only available on SSE 4.1
11332 if (!Subtarget->hasSSE41())
11333 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011334
Nate Begemanb65c1752010-12-17 22:55:37 +000011335 X = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, X);
11336 Y = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Y);
11337 Mask = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Mask);
Nate Begeman672fb622010-12-20 22:04:24 +000011338 Mask = DAG.getNode(X86ISD::PBLENDVB, DL, MVT::v16i8, X, Y, Mask);
Nate Begemanb65c1752010-12-17 22:55:37 +000011339 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Mask);
11340 }
11341 }
11342 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011343
Nate Begemanb65c1752010-12-17 22:55:37 +000011344 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
Evan Cheng760d1942010-01-04 21:22:48 +000011345 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
11346 std::swap(N0, N1);
11347 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
11348 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000011349 if (!N0.hasOneUse() || !N1.hasOneUse())
11350 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000011351
11352 SDValue ShAmt0 = N0.getOperand(1);
11353 if (ShAmt0.getValueType() != MVT::i8)
11354 return SDValue();
11355 SDValue ShAmt1 = N1.getOperand(1);
11356 if (ShAmt1.getValueType() != MVT::i8)
11357 return SDValue();
11358 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
11359 ShAmt0 = ShAmt0.getOperand(0);
11360 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
11361 ShAmt1 = ShAmt1.getOperand(0);
11362
11363 DebugLoc DL = N->getDebugLoc();
11364 unsigned Opc = X86ISD::SHLD;
11365 SDValue Op0 = N0.getOperand(0);
11366 SDValue Op1 = N1.getOperand(0);
11367 if (ShAmt0.getOpcode() == ISD::SUB) {
11368 Opc = X86ISD::SHRD;
11369 std::swap(Op0, Op1);
11370 std::swap(ShAmt0, ShAmt1);
11371 }
11372
Evan Cheng8b1190a2010-04-28 01:18:01 +000011373 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000011374 if (ShAmt1.getOpcode() == ISD::SUB) {
11375 SDValue Sum = ShAmt1.getOperand(0);
11376 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000011377 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
11378 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
11379 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
11380 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000011381 return DAG.getNode(Opc, DL, VT,
11382 Op0, Op1,
11383 DAG.getNode(ISD::TRUNCATE, DL,
11384 MVT::i8, ShAmt0));
11385 }
11386 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
11387 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
11388 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000011389 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000011390 return DAG.getNode(Opc, DL, VT,
11391 N0.getOperand(0), N1.getOperand(0),
11392 DAG.getNode(ISD::TRUNCATE, DL,
11393 MVT::i8, ShAmt0));
11394 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011395
Evan Cheng760d1942010-01-04 21:22:48 +000011396 return SDValue();
11397}
11398
Chris Lattner149a4e52008-02-22 02:09:43 +000011399/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000011400static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000011401 const X86Subtarget *Subtarget) {
Chris Lattner149a4e52008-02-22 02:09:43 +000011402 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
11403 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000011404 // A preferable solution to the general problem is to figure out the right
11405 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000011406
11407 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng7e2ff772008-05-08 00:57:18 +000011408 StoreSDNode *St = cast<StoreSDNode>(N);
Owen Andersone50ed302009-08-10 22:56:29 +000011409 EVT VT = St->getValue().getValueType();
Evan Cheng536e6672009-03-12 05:59:15 +000011410 if (VT.getSizeInBits() != 64)
11411 return SDValue();
11412
Devang Patel578efa92009-06-05 21:57:13 +000011413 const Function *F = DAG.getMachineFunction().getFunction();
11414 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Eric Christopherfd179292009-08-27 18:07:15 +000011415 bool F64IsLegal = !UseSoftFloat && !NoImplicitFloatOps
Devang Patel578efa92009-06-05 21:57:13 +000011416 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +000011417 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000011418 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000011419 isa<LoadSDNode>(St->getValue()) &&
11420 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
11421 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000011422 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000011423 LoadSDNode *Ld = 0;
11424 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000011425 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000011426 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000011427 // Must be a store of a load. We currently handle two cases: the load
11428 // is a direct child, and it's under an intervening TokenFactor. It is
11429 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000011430 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000011431 Ld = cast<LoadSDNode>(St->getChain());
11432 else if (St->getValue().hasOneUse() &&
11433 ChainVal->getOpcode() == ISD::TokenFactor) {
11434 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000011435 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000011436 TokenFactorIndex = i;
11437 Ld = cast<LoadSDNode>(St->getValue());
11438 } else
11439 Ops.push_back(ChainVal->getOperand(i));
11440 }
11441 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000011442
Evan Cheng536e6672009-03-12 05:59:15 +000011443 if (!Ld || !ISD::isNormalLoad(Ld))
11444 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000011445
Evan Cheng536e6672009-03-12 05:59:15 +000011446 // If this is not the MMX case, i.e. we are just turning i64 load/store
11447 // into f64 load/store, avoid the transformation if there are multiple
11448 // uses of the loaded value.
11449 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
11450 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000011451
Evan Cheng536e6672009-03-12 05:59:15 +000011452 DebugLoc LdDL = Ld->getDebugLoc();
11453 DebugLoc StDL = N->getDebugLoc();
11454 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
11455 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
11456 // pair instead.
11457 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000011458 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000011459 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
11460 Ld->getPointerInfo(), Ld->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000011461 Ld->isNonTemporal(), Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000011462 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000011463 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000011464 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000011465 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000011466 Ops.size());
11467 }
Evan Cheng536e6672009-03-12 05:59:15 +000011468 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000011469 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000011470 St->isVolatile(), St->isNonTemporal(),
11471 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000011472 }
Evan Cheng536e6672009-03-12 05:59:15 +000011473
11474 // Otherwise, lower to two pairs of 32-bit loads / stores.
11475 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000011476 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
11477 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000011478
Owen Anderson825b72b2009-08-11 20:47:22 +000011479 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000011480 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000011481 Ld->isVolatile(), Ld->isNonTemporal(),
11482 Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000011483 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000011484 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000011485 Ld->isVolatile(), Ld->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000011486 MinAlign(Ld->getAlignment(), 4));
11487
11488 SDValue NewChain = LoLd.getValue(1);
11489 if (TokenFactorIndex != -1) {
11490 Ops.push_back(LoLd);
11491 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000011492 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000011493 Ops.size());
11494 }
11495
11496 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000011497 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
11498 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000011499
11500 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000011501 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000011502 St->isVolatile(), St->isNonTemporal(),
11503 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000011504 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000011505 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000011506 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000011507 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000011508 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000011509 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000011510 }
Dan Gohman475871a2008-07-27 21:46:04 +000011511 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000011512}
11513
Chris Lattner6cf73262008-01-25 06:14:17 +000011514/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
11515/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000011516static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000011517 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
11518 // F[X]OR(0.0, x) -> x
11519 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000011520 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
11521 if (C->getValueAPF().isPosZero())
11522 return N->getOperand(1);
11523 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
11524 if (C->getValueAPF().isPosZero())
11525 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000011526 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000011527}
11528
11529/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000011530static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000011531 // FAND(0.0, x) -> 0.0
11532 // FAND(x, 0.0) -> 0.0
11533 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
11534 if (C->getValueAPF().isPosZero())
11535 return N->getOperand(0);
11536 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
11537 if (C->getValueAPF().isPosZero())
11538 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000011539 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000011540}
11541
Dan Gohmane5af2d32009-01-29 01:59:02 +000011542static SDValue PerformBTCombine(SDNode *N,
11543 SelectionDAG &DAG,
11544 TargetLowering::DAGCombinerInfo &DCI) {
11545 // BT ignores high bits in the bit index operand.
11546 SDValue Op1 = N->getOperand(1);
11547 if (Op1.hasOneUse()) {
11548 unsigned BitWidth = Op1.getValueSizeInBits();
11549 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
11550 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000011551 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
11552 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000011553 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000011554 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
11555 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
11556 DCI.CommitTargetLoweringOpt(TLO);
11557 }
11558 return SDValue();
11559}
Chris Lattner83e6c992006-10-04 06:57:07 +000011560
Eli Friedman7a5e5552009-06-07 06:52:44 +000011561static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
11562 SDValue Op = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011563 if (Op.getOpcode() == ISD::BITCAST)
Eli Friedman7a5e5552009-06-07 06:52:44 +000011564 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000011565 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000011566 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000011567 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000011568 OpVT.getVectorElementType().getSizeInBits()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011569 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Eli Friedman7a5e5552009-06-07 06:52:44 +000011570 }
11571 return SDValue();
11572}
11573
Evan Cheng2e489c42009-12-16 00:53:11 +000011574static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG) {
11575 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
11576 // (and (i32 x86isd::setcc_carry), 1)
11577 // This eliminates the zext. This transformation is necessary because
11578 // ISD::SETCC is always legalized to i8.
11579 DebugLoc dl = N->getDebugLoc();
11580 SDValue N0 = N->getOperand(0);
11581 EVT VT = N->getValueType(0);
11582 if (N0.getOpcode() == ISD::AND &&
11583 N0.hasOneUse() &&
11584 N0.getOperand(0).hasOneUse()) {
11585 SDValue N00 = N0.getOperand(0);
11586 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
11587 return SDValue();
11588 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
11589 if (!C || C->getZExtValue() != 1)
11590 return SDValue();
11591 return DAG.getNode(ISD::AND, dl, VT,
11592 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
11593 N00.getOperand(0), N00.getOperand(1)),
11594 DAG.getConstant(1, VT));
11595 }
11596
11597 return SDValue();
11598}
11599
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011600// Optimize RES = X86ISD::SETCC CONDCODE, EFLAG_INPUT
11601static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG &DAG) {
11602 unsigned X86CC = N->getConstantOperandVal(0);
11603 SDValue EFLAG = N->getOperand(1);
11604 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011605
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011606 // Materialize "setb reg" as "sbb reg,reg", since it can be extended without
11607 // a zext and produces an all-ones bit which is more useful than 0/1 in some
11608 // cases.
11609 if (X86CC == X86::COND_B)
11610 return DAG.getNode(ISD::AND, DL, MVT::i8,
11611 DAG.getNode(X86ISD::SETCC_CARRY, DL, MVT::i8,
11612 DAG.getConstant(X86CC, MVT::i8), EFLAG),
11613 DAG.getConstant(1, MVT::i8));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011614
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011615 return SDValue();
11616}
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011617
Chris Lattner23a01992010-12-20 01:37:09 +000011618// Optimize RES, EFLAGS = X86ISD::ADC LHS, RHS, EFLAGS
11619static SDValue PerformADCCombine(SDNode *N, SelectionDAG &DAG,
11620 X86TargetLowering::DAGCombinerInfo &DCI) {
11621 // If the LHS and RHS of the ADC node are zero, then it can't overflow and
11622 // the result is either zero or one (depending on the input carry bit).
11623 // Strength reduce this down to a "set on carry" aka SETCC_CARRY&1.
11624 if (X86::isZeroNode(N->getOperand(0)) &&
11625 X86::isZeroNode(N->getOperand(1)) &&
11626 // We don't have a good way to replace an EFLAGS use, so only do this when
11627 // dead right now.
11628 SDValue(N, 1).use_empty()) {
11629 DebugLoc DL = N->getDebugLoc();
11630 EVT VT = N->getValueType(0);
11631 SDValue CarryOut = DAG.getConstant(0, N->getValueType(1));
11632 SDValue Res1 = DAG.getNode(ISD::AND, DL, VT,
11633 DAG.getNode(X86ISD::SETCC_CARRY, DL, VT,
11634 DAG.getConstant(X86::COND_B,MVT::i8),
11635 N->getOperand(2)),
11636 DAG.getConstant(1, VT));
11637 return DCI.CombineTo(N, Res1, CarryOut);
11638 }
11639
11640 return SDValue();
11641}
11642
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000011643// fold (add Y, (sete X, 0)) -> adc 0, Y
11644// (add Y, (setne X, 0)) -> sbb -1, Y
11645// (sub (sete X, 0), Y) -> sbb 0, Y
11646// (sub (setne X, 0), Y) -> adc -1, Y
11647static SDValue OptimizeConditonalInDecrement(SDNode *N, SelectionDAG &DAG) {
11648 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011649
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000011650 // Look through ZExts.
11651 SDValue Ext = N->getOperand(N->getOpcode() == ISD::SUB ? 1 : 0);
11652 if (Ext.getOpcode() != ISD::ZERO_EXTEND || !Ext.hasOneUse())
11653 return SDValue();
11654
11655 SDValue SetCC = Ext.getOperand(0);
11656 if (SetCC.getOpcode() != X86ISD::SETCC || !SetCC.hasOneUse())
11657 return SDValue();
11658
11659 X86::CondCode CC = (X86::CondCode)SetCC.getConstantOperandVal(0);
11660 if (CC != X86::COND_E && CC != X86::COND_NE)
11661 return SDValue();
11662
11663 SDValue Cmp = SetCC.getOperand(1);
11664 if (Cmp.getOpcode() != X86ISD::CMP || !Cmp.hasOneUse() ||
Chris Lattner9cd3da42011-01-16 02:56:53 +000011665 !X86::isZeroNode(Cmp.getOperand(1)) ||
11666 !Cmp.getOperand(0).getValueType().isInteger())
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000011667 return SDValue();
11668
11669 SDValue CmpOp0 = Cmp.getOperand(0);
11670 SDValue NewCmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32, CmpOp0,
11671 DAG.getConstant(1, CmpOp0.getValueType()));
11672
11673 SDValue OtherVal = N->getOperand(N->getOpcode() == ISD::SUB ? 0 : 1);
11674 if (CC == X86::COND_NE)
11675 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::ADC : X86ISD::SBB,
11676 DL, OtherVal.getValueType(), OtherVal,
11677 DAG.getConstant(-1ULL, OtherVal.getValueType()), NewCmp);
11678 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::SBB : X86ISD::ADC,
11679 DL, OtherVal.getValueType(), OtherVal,
11680 DAG.getConstant(0, OtherVal.getValueType()), NewCmp);
11681}
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011682
Dan Gohman475871a2008-07-27 21:46:04 +000011683SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000011684 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000011685 SelectionDAG &DAG = DCI.DAG;
11686 switch (N->getOpcode()) {
11687 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000011688 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011689 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, *this);
Chris Lattneraf723b92008-01-25 05:46:26 +000011690 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +000011691 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000011692 case ISD::ADD:
11693 case ISD::SUB: return OptimizeConditonalInDecrement(N, DAG);
Chris Lattner23a01992010-12-20 01:37:09 +000011694 case X86ISD::ADC: return PerformADCCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000011695 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000011696 case ISD::SHL:
11697 case ISD::SRA:
11698 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Nate Begemanb65c1752010-12-17 22:55:37 +000011699 case ISD::AND: return PerformAndCombine(N, DAG, DCI, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000011700 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000011701 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +000011702 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000011703 case X86ISD::FOR: return PerformFORCombine(N, DAG);
11704 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000011705 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000011706 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Evan Cheng2e489c42009-12-16 00:53:11 +000011707 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG);
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011708 case X86ISD::SETCC: return PerformSETCCCombine(N, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000011709 case X86ISD::SHUFPS: // Handle all target specific shuffles
11710 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +000011711 case X86ISD::PALIGN:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000011712 case X86ISD::PUNPCKHBW:
11713 case X86ISD::PUNPCKHWD:
11714 case X86ISD::PUNPCKHDQ:
11715 case X86ISD::PUNPCKHQDQ:
11716 case X86ISD::UNPCKHPS:
11717 case X86ISD::UNPCKHPD:
11718 case X86ISD::PUNPCKLBW:
11719 case X86ISD::PUNPCKLWD:
11720 case X86ISD::PUNPCKLDQ:
11721 case X86ISD::PUNPCKLQDQ:
11722 case X86ISD::UNPCKLPS:
11723 case X86ISD::UNPCKLPD:
11724 case X86ISD::MOVHLPS:
11725 case X86ISD::MOVLHPS:
11726 case X86ISD::PSHUFD:
11727 case X86ISD::PSHUFHW:
11728 case X86ISD::PSHUFLW:
11729 case X86ISD::MOVSS:
11730 case X86ISD::MOVSD:
Mon P Wanga0fd0d52010-12-19 23:55:53 +000011731 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, DCI);
Evan Cheng206ee9d2006-07-07 08:33:52 +000011732 }
11733
Dan Gohman475871a2008-07-27 21:46:04 +000011734 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000011735}
11736
Evan Chenge5b51ac2010-04-17 06:13:15 +000011737/// isTypeDesirableForOp - Return true if the target has native support for
11738/// the specified value type and it is 'desirable' to use the type for the
11739/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
11740/// instruction encodings are longer and some i16 instructions are slow.
11741bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
11742 if (!isTypeLegal(VT))
11743 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000011744 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000011745 return true;
11746
11747 switch (Opc) {
11748 default:
11749 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000011750 case ISD::LOAD:
11751 case ISD::SIGN_EXTEND:
11752 case ISD::ZERO_EXTEND:
11753 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000011754 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000011755 case ISD::SRL:
11756 case ISD::SUB:
11757 case ISD::ADD:
11758 case ISD::MUL:
11759 case ISD::AND:
11760 case ISD::OR:
11761 case ISD::XOR:
11762 return false;
11763 }
11764}
11765
11766/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000011767/// beneficial for dag combiner to promote the specified node. If true, it
11768/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000011769bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000011770 EVT VT = Op.getValueType();
11771 if (VT != MVT::i16)
11772 return false;
11773
Evan Cheng4c26e932010-04-19 19:29:22 +000011774 bool Promote = false;
11775 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000011776 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000011777 default: break;
11778 case ISD::LOAD: {
11779 LoadSDNode *LD = cast<LoadSDNode>(Op);
11780 // If the non-extending load has a single use and it's not live out, then it
11781 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000011782 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
11783 Op.hasOneUse()*/) {
11784 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
11785 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
11786 // The only case where we'd want to promote LOAD (rather then it being
11787 // promoted as an operand is when it's only use is liveout.
11788 if (UI->getOpcode() != ISD::CopyToReg)
11789 return false;
11790 }
11791 }
Evan Cheng4c26e932010-04-19 19:29:22 +000011792 Promote = true;
11793 break;
11794 }
11795 case ISD::SIGN_EXTEND:
11796 case ISD::ZERO_EXTEND:
11797 case ISD::ANY_EXTEND:
11798 Promote = true;
11799 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000011800 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000011801 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000011802 SDValue N0 = Op.getOperand(0);
11803 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000011804 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000011805 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000011806 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000011807 break;
11808 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000011809 case ISD::ADD:
11810 case ISD::MUL:
11811 case ISD::AND:
11812 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000011813 case ISD::XOR:
11814 Commute = true;
11815 // fallthrough
11816 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000011817 SDValue N0 = Op.getOperand(0);
11818 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000011819 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000011820 return false;
11821 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000011822 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000011823 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000011824 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000011825 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000011826 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000011827 }
11828 }
11829
11830 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000011831 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000011832}
11833
Evan Cheng60c07e12006-07-05 22:17:51 +000011834//===----------------------------------------------------------------------===//
11835// X86 Inline Assembly Support
11836//===----------------------------------------------------------------------===//
11837
Chris Lattnerb8105652009-07-20 17:51:36 +000011838bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
11839 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattnerb8105652009-07-20 17:51:36 +000011840
11841 std::string AsmStr = IA->getAsmString();
11842
11843 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000011844 SmallVector<StringRef, 4> AsmPieces;
Peter Collingbourne98361182010-11-13 19:54:23 +000011845 SplitString(AsmStr, AsmPieces, ";\n");
Chris Lattnerb8105652009-07-20 17:51:36 +000011846
11847 switch (AsmPieces.size()) {
11848 default: return false;
11849 case 1:
11850 AsmStr = AsmPieces[0];
11851 AsmPieces.clear();
11852 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
11853
Evan Cheng55d42002011-01-08 01:24:27 +000011854 // FIXME: this should verify that we are targetting a 486 or better. If not,
11855 // we will turn this bswap into something that will be lowered to logical ops
11856 // instead of emitting the bswap asm. For now, we don't support 486 or lower
11857 // so don't worry about this.
Chris Lattnerb8105652009-07-20 17:51:36 +000011858 // bswap $0
11859 if (AsmPieces.size() == 2 &&
11860 (AsmPieces[0] == "bswap" ||
11861 AsmPieces[0] == "bswapq" ||
11862 AsmPieces[0] == "bswapl") &&
11863 (AsmPieces[1] == "$0" ||
11864 AsmPieces[1] == "${0:q}")) {
11865 // No need to check constraints, nothing other than the equivalent of
11866 // "=r,0" would be valid here.
Evan Cheng55d42002011-01-08 01:24:27 +000011867 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
11868 if (!Ty || Ty->getBitWidth() % 16 != 0)
11869 return false;
11870 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000011871 }
11872 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000011873 if (CI->getType()->isIntegerTy(16) &&
Chris Lattnerb8105652009-07-20 17:51:36 +000011874 AsmPieces.size() == 3 &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000011875 (AsmPieces[0] == "rorw" || AsmPieces[0] == "rolw") &&
Chris Lattnerb8105652009-07-20 17:51:36 +000011876 AsmPieces[1] == "$$8," &&
11877 AsmPieces[2] == "${0:w}" &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000011878 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
11879 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000011880 const std::string &ConstraintsStr = IA->getConstraintString();
11881 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000011882 std::sort(AsmPieces.begin(), AsmPieces.end());
11883 if (AsmPieces.size() == 4 &&
11884 AsmPieces[0] == "~{cc}" &&
11885 AsmPieces[1] == "~{dirflag}" &&
11886 AsmPieces[2] == "~{flags}" &&
11887 AsmPieces[3] == "~{fpsr}") {
Evan Cheng55d42002011-01-08 01:24:27 +000011888 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
11889 if (!Ty || Ty->getBitWidth() % 16 != 0)
11890 return false;
11891 return IntrinsicLowering::LowerToByteSwap(CI);
Dan Gohman0ef701e2010-03-04 19:58:08 +000011892 }
Chris Lattnerb8105652009-07-20 17:51:36 +000011893 }
11894 break;
11895 case 3:
Peter Collingbourne948cf022010-11-13 19:54:30 +000011896 if (CI->getType()->isIntegerTy(32) &&
11897 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
11898 SmallVector<StringRef, 4> Words;
11899 SplitString(AsmPieces[0], Words, " \t,");
11900 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
11901 Words[2] == "${0:w}") {
11902 Words.clear();
11903 SplitString(AsmPieces[1], Words, " \t,");
11904 if (Words.size() == 3 && Words[0] == "rorl" && Words[1] == "$$16" &&
11905 Words[2] == "$0") {
11906 Words.clear();
11907 SplitString(AsmPieces[2], Words, " \t,");
11908 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
11909 Words[2] == "${0:w}") {
11910 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000011911 const std::string &ConstraintsStr = IA->getConstraintString();
11912 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Peter Collingbourne948cf022010-11-13 19:54:30 +000011913 std::sort(AsmPieces.begin(), AsmPieces.end());
11914 if (AsmPieces.size() == 4 &&
11915 AsmPieces[0] == "~{cc}" &&
11916 AsmPieces[1] == "~{dirflag}" &&
11917 AsmPieces[2] == "~{flags}" &&
11918 AsmPieces[3] == "~{fpsr}") {
Evan Cheng55d42002011-01-08 01:24:27 +000011919 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
11920 if (!Ty || Ty->getBitWidth() % 16 != 0)
11921 return false;
11922 return IntrinsicLowering::LowerToByteSwap(CI);
Peter Collingbourne948cf022010-11-13 19:54:30 +000011923 }
11924 }
11925 }
11926 }
11927 }
Evan Cheng55d42002011-01-08 01:24:27 +000011928
11929 if (CI->getType()->isIntegerTy(64)) {
11930 InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
11931 if (Constraints.size() >= 2 &&
11932 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
11933 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
11934 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
11935 SmallVector<StringRef, 4> Words;
11936 SplitString(AsmPieces[0], Words, " \t");
11937 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
Chris Lattnerb8105652009-07-20 17:51:36 +000011938 Words.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000011939 SplitString(AsmPieces[1], Words, " \t");
11940 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
11941 Words.clear();
11942 SplitString(AsmPieces[2], Words, " \t,");
11943 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
11944 Words[2] == "%edx") {
11945 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
11946 if (!Ty || Ty->getBitWidth() % 16 != 0)
11947 return false;
11948 return IntrinsicLowering::LowerToByteSwap(CI);
11949 }
Chris Lattnerb8105652009-07-20 17:51:36 +000011950 }
11951 }
11952 }
11953 }
11954 break;
11955 }
11956 return false;
11957}
11958
11959
11960
Chris Lattnerf4dff842006-07-11 02:54:03 +000011961/// getConstraintType - Given a constraint letter, return the type of
11962/// constraint it is for this target.
11963X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000011964X86TargetLowering::getConstraintType(const std::string &Constraint) const {
11965 if (Constraint.size() == 1) {
11966 switch (Constraint[0]) {
Chris Lattner4234f572007-03-25 02:14:49 +000011967 case 'R':
Chris Lattner4234f572007-03-25 02:14:49 +000011968 case 'q':
11969 case 'Q':
John Thompson44ab89e2010-10-29 17:29:13 +000011970 case 'f':
11971 case 't':
11972 case 'u':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000011973 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +000011974 case 'x':
Chris Lattner4234f572007-03-25 02:14:49 +000011975 case 'Y':
11976 return C_RegisterClass;
John Thompson44ab89e2010-10-29 17:29:13 +000011977 case 'a':
11978 case 'b':
11979 case 'c':
11980 case 'd':
11981 case 'S':
11982 case 'D':
11983 case 'A':
11984 return C_Register;
11985 case 'I':
11986 case 'J':
11987 case 'K':
11988 case 'L':
11989 case 'M':
11990 case 'N':
11991 case 'G':
11992 case 'C':
Dale Johannesen78e3e522009-02-12 20:58:09 +000011993 case 'e':
11994 case 'Z':
11995 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000011996 default:
11997 break;
11998 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000011999 }
Chris Lattner4234f572007-03-25 02:14:49 +000012000 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000012001}
12002
John Thompson44ab89e2010-10-29 17:29:13 +000012003/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +000012004/// This object must already have been set up with the operand type
12005/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +000012006TargetLowering::ConstraintWeight
12007 X86TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +000012008 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +000012009 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012010 Value *CallOperandVal = info.CallOperandVal;
12011 // If we don't have a value, we can't do a match,
12012 // but allow it at the lowest weight.
12013 if (CallOperandVal == NULL)
John Thompson44ab89e2010-10-29 17:29:13 +000012014 return CW_Default;
12015 const Type *type = CallOperandVal->getType();
John Thompsoneac6e1d2010-09-13 18:15:37 +000012016 // Look at the constraint type.
12017 switch (*constraint) {
12018 default:
John Thompson44ab89e2010-10-29 17:29:13 +000012019 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
12020 case 'R':
12021 case 'q':
12022 case 'Q':
12023 case 'a':
12024 case 'b':
12025 case 'c':
12026 case 'd':
12027 case 'S':
12028 case 'D':
12029 case 'A':
12030 if (CallOperandVal->getType()->isIntegerTy())
12031 weight = CW_SpecificReg;
12032 break;
12033 case 'f':
12034 case 't':
12035 case 'u':
12036 if (type->isFloatingPointTy())
12037 weight = CW_SpecificReg;
12038 break;
12039 case 'y':
Chris Lattner2a786eb2010-12-19 20:19:20 +000012040 if (type->isX86_MMXTy() && Subtarget->hasMMX())
John Thompson44ab89e2010-10-29 17:29:13 +000012041 weight = CW_SpecificReg;
12042 break;
12043 case 'x':
12044 case 'Y':
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012045 if ((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasXMM())
John Thompson44ab89e2010-10-29 17:29:13 +000012046 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012047 break;
12048 case 'I':
12049 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
12050 if (C->getZExtValue() <= 31)
John Thompson44ab89e2010-10-29 17:29:13 +000012051 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012052 }
12053 break;
John Thompson44ab89e2010-10-29 17:29:13 +000012054 case 'J':
12055 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12056 if (C->getZExtValue() <= 63)
12057 weight = CW_Constant;
12058 }
12059 break;
12060 case 'K':
12061 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12062 if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f))
12063 weight = CW_Constant;
12064 }
12065 break;
12066 case 'L':
12067 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12068 if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff))
12069 weight = CW_Constant;
12070 }
12071 break;
12072 case 'M':
12073 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12074 if (C->getZExtValue() <= 3)
12075 weight = CW_Constant;
12076 }
12077 break;
12078 case 'N':
12079 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12080 if (C->getZExtValue() <= 0xff)
12081 weight = CW_Constant;
12082 }
12083 break;
12084 case 'G':
12085 case 'C':
12086 if (dyn_cast<ConstantFP>(CallOperandVal)) {
12087 weight = CW_Constant;
12088 }
12089 break;
12090 case 'e':
12091 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12092 if ((C->getSExtValue() >= -0x80000000LL) &&
12093 (C->getSExtValue() <= 0x7fffffffLL))
12094 weight = CW_Constant;
12095 }
12096 break;
12097 case 'Z':
12098 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12099 if (C->getZExtValue() <= 0xffffffff)
12100 weight = CW_Constant;
12101 }
12102 break;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012103 }
12104 return weight;
12105}
12106
Dale Johannesenba2a0b92008-01-29 02:21:21 +000012107/// LowerXConstraint - try to replace an X constraint, which matches anything,
12108/// with another that has more specific requirements based on the type of the
12109/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000012110const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000012111LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000012112 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
12113 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000012114 if (ConstraintVT.isFloatingPoint()) {
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012115 if (Subtarget->hasXMMInt())
Chris Lattner5e764232008-04-26 23:02:14 +000012116 return "Y";
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012117 if (Subtarget->hasXMM())
Chris Lattner5e764232008-04-26 23:02:14 +000012118 return "x";
12119 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012120
Chris Lattner5e764232008-04-26 23:02:14 +000012121 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000012122}
12123
Chris Lattner48884cd2007-08-25 00:47:38 +000012124/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
12125/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000012126void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattner48884cd2007-08-25 00:47:38 +000012127 char Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000012128 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000012129 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000012130 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000012131
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012132 switch (Constraint) {
12133 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000012134 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000012135 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000012136 if (C->getZExtValue() <= 31) {
12137 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000012138 break;
12139 }
Devang Patel84f7fd22007-03-17 00:13:28 +000012140 }
Chris Lattner48884cd2007-08-25 00:47:38 +000012141 return;
Evan Cheng364091e2008-09-22 23:57:37 +000012142 case 'J':
12143 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000012144 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000012145 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
12146 break;
12147 }
12148 }
12149 return;
12150 case 'K':
12151 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000012152 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +000012153 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
12154 break;
12155 }
12156 }
12157 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000012158 case 'N':
12159 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000012160 if (C->getZExtValue() <= 255) {
12161 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000012162 break;
12163 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000012164 }
Chris Lattner48884cd2007-08-25 00:47:38 +000012165 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000012166 case 'e': {
12167 // 32-bit signed value
12168 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000012169 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
12170 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000012171 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000012172 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000012173 break;
12174 }
12175 // FIXME gcc accepts some relocatable values here too, but only in certain
12176 // memory models; it's complicated.
12177 }
12178 return;
12179 }
12180 case 'Z': {
12181 // 32-bit unsigned value
12182 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000012183 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
12184 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000012185 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
12186 break;
12187 }
12188 }
12189 // FIXME gcc accepts some relocatable values here too, but only in certain
12190 // memory models; it's complicated.
12191 return;
12192 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000012193 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012194 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000012195 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000012196 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000012197 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000012198 break;
12199 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012200
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000012201 // In any sort of PIC mode addresses need to be computed at runtime by
12202 // adding in a register or some sort of table lookup. These can't
12203 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000012204 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000012205 return;
12206
Chris Lattnerdc43a882007-05-03 16:52:29 +000012207 // If we are in non-pic codegen mode, we allow the address of a global (with
12208 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000012209 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000012210 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000012211
Chris Lattner49921962009-05-08 18:23:14 +000012212 // Match either (GA), (GA+C), (GA+C1+C2), etc.
12213 while (1) {
12214 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
12215 Offset += GA->getOffset();
12216 break;
12217 } else if (Op.getOpcode() == ISD::ADD) {
12218 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
12219 Offset += C->getZExtValue();
12220 Op = Op.getOperand(0);
12221 continue;
12222 }
12223 } else if (Op.getOpcode() == ISD::SUB) {
12224 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
12225 Offset += -C->getZExtValue();
12226 Op = Op.getOperand(0);
12227 continue;
12228 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000012229 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000012230
Chris Lattner49921962009-05-08 18:23:14 +000012231 // Otherwise, this isn't something we can handle, reject it.
12232 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000012233 }
Eric Christopherfd179292009-08-27 18:07:15 +000012234
Dan Gohman46510a72010-04-15 01:51:59 +000012235 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000012236 // If we require an extra load to get this address, as in PIC mode, we
12237 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000012238 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
12239 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000012240 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000012241
Devang Patel0d881da2010-07-06 22:08:15 +000012242 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
12243 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000012244 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012245 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000012246 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012247
Gabor Greifba36cb52008-08-28 21:40:38 +000012248 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000012249 Ops.push_back(Result);
12250 return;
12251 }
Dale Johannesen1784d162010-06-25 21:55:36 +000012252 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012253}
12254
Chris Lattner259e97c2006-01-31 19:43:35 +000012255std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +000012256getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000012257 EVT VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +000012258 if (Constraint.size() == 1) {
12259 // FIXME: not handling fp-stack yet!
Chris Lattner259e97c2006-01-31 19:43:35 +000012260 switch (Constraint[0]) { // GCC X86 Constraint Letters
Chris Lattnerf4dff842006-07-11 02:54:03 +000012261 default: break; // Unknown constraint letter
Evan Cheng47e9fab2009-07-17 22:13:25 +000012262 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
12263 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000012264 if (VT == MVT::i32)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012265 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX,
12266 X86::ESI, X86::EDI, X86::R8D, X86::R9D,
12267 X86::R10D,X86::R11D,X86::R12D,
12268 X86::R13D,X86::R14D,X86::R15D,
12269 X86::EBP, X86::ESP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012270 else if (VT == MVT::i16)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012271 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX,
12272 X86::SI, X86::DI, X86::R8W,X86::R9W,
12273 X86::R10W,X86::R11W,X86::R12W,
12274 X86::R13W,X86::R14W,X86::R15W,
12275 X86::BP, X86::SP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012276 else if (VT == MVT::i8)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012277 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL,
12278 X86::SIL, X86::DIL, X86::R8B,X86::R9B,
12279 X86::R10B,X86::R11B,X86::R12B,
12280 X86::R13B,X86::R14B,X86::R15B,
12281 X86::BPL, X86::SPL, 0);
12282
Owen Anderson825b72b2009-08-11 20:47:22 +000012283 else if (VT == MVT::i64)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012284 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX,
12285 X86::RSI, X86::RDI, X86::R8, X86::R9,
12286 X86::R10, X86::R11, X86::R12,
12287 X86::R13, X86::R14, X86::R15,
12288 X86::RBP, X86::RSP, 0);
12289
12290 break;
12291 }
Eric Christopherfd179292009-08-27 18:07:15 +000012292 // 32-bit fallthrough
Chris Lattner259e97c2006-01-31 19:43:35 +000012293 case 'Q': // Q_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +000012294 if (VT == MVT::i32)
Chris Lattner80a7ecc2006-05-06 00:29:37 +000012295 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012296 else if (VT == MVT::i16)
Chris Lattner80a7ecc2006-05-06 00:29:37 +000012297 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012298 else if (VT == MVT::i8)
Evan Cheng12914382007-08-13 23:27:11 +000012299 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012300 else if (VT == MVT::i64)
Chris Lattner03e6c702007-11-04 06:51:12 +000012301 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
12302 break;
Chris Lattner259e97c2006-01-31 19:43:35 +000012303 }
12304 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012305
Chris Lattner1efa40f2006-02-22 00:56:39 +000012306 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +000012307}
Chris Lattnerf76d1802006-07-31 23:26:50 +000012308
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012309std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000012310X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000012311 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000012312 // First, see if this is a constraint that directly corresponds to an LLVM
12313 // register class.
12314 if (Constraint.size() == 1) {
12315 // GCC Constraint Letters
12316 switch (Constraint[0]) {
12317 default: break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000012318 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000012319 case 'l': // INDEX_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +000012320 if (VT == MVT::i8)
Chris Lattner0f65cad2007-04-09 05:49:22 +000012321 return std::make_pair(0U, X86::GR8RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012322 if (VT == MVT::i16)
Chris Lattner1fa71982008-10-17 18:15:05 +000012323 return std::make_pair(0U, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012324 if (VT == MVT::i32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +000012325 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +000012326 return std::make_pair(0U, X86::GR64RegisterClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000012327 case 'R': // LEGACY_REGS
12328 if (VT == MVT::i8)
12329 return std::make_pair(0U, X86::GR8_NOREXRegisterClass);
12330 if (VT == MVT::i16)
12331 return std::make_pair(0U, X86::GR16_NOREXRegisterClass);
12332 if (VT == MVT::i32 || !Subtarget->is64Bit())
12333 return std::make_pair(0U, X86::GR32_NOREXRegisterClass);
12334 return std::make_pair(0U, X86::GR64_NOREXRegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000012335 case 'f': // FP Stack registers.
12336 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
12337 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000012338 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000012339 return std::make_pair(0U, X86::RFP32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012340 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000012341 return std::make_pair(0U, X86::RFP64RegisterClass);
12342 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000012343 case 'y': // MMX_REGS if MMX allowed.
12344 if (!Subtarget->hasMMX()) break;
12345 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000012346 case 'Y': // SSE_REGS if SSE2 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012347 if (!Subtarget->hasXMMInt()) break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000012348 // FALL THROUGH.
12349 case 'x': // SSE_REGS if SSE1 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012350 if (!Subtarget->hasXMM()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000012351
Owen Anderson825b72b2009-08-11 20:47:22 +000012352 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000012353 default: break;
12354 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000012355 case MVT::f32:
12356 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +000012357 return std::make_pair(0U, X86::FR32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012358 case MVT::f64:
12359 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +000012360 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000012361 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000012362 case MVT::v16i8:
12363 case MVT::v8i16:
12364 case MVT::v4i32:
12365 case MVT::v2i64:
12366 case MVT::v4f32:
12367 case MVT::v2f64:
Chris Lattner0f65cad2007-04-09 05:49:22 +000012368 return std::make_pair(0U, X86::VR128RegisterClass);
12369 }
Chris Lattnerad043e82007-04-09 05:11:28 +000012370 break;
12371 }
12372 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012373
Chris Lattnerf76d1802006-07-31 23:26:50 +000012374 // Use the default implementation in TargetLowering to convert the register
12375 // constraint into a member of a register class.
12376 std::pair<unsigned, const TargetRegisterClass*> Res;
12377 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000012378
12379 // Not found as a standard register?
12380 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000012381 // Map st(0) -> st(7) -> ST0
12382 if (Constraint.size() == 7 && Constraint[0] == '{' &&
12383 tolower(Constraint[1]) == 's' &&
12384 tolower(Constraint[2]) == 't' &&
12385 Constraint[3] == '(' &&
12386 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
12387 Constraint[5] == ')' &&
12388 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000012389
Chris Lattner56d77c72009-09-13 22:41:48 +000012390 Res.first = X86::ST0+Constraint[4]-'0';
12391 Res.second = X86::RFP80RegisterClass;
12392 return Res;
12393 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000012394
Chris Lattner56d77c72009-09-13 22:41:48 +000012395 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000012396 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000012397 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +000012398 Res.second = X86::RFP80RegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000012399 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000012400 }
Chris Lattner56d77c72009-09-13 22:41:48 +000012401
12402 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000012403 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000012404 Res.first = X86::EFLAGS;
12405 Res.second = X86::CCRRegisterClass;
12406 return Res;
12407 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000012408
Dale Johannesen330169f2008-11-13 21:52:36 +000012409 // 'A' means EAX + EDX.
12410 if (Constraint == "A") {
12411 Res.first = X86::EAX;
Dan Gohman68a31c22009-07-30 17:02:08 +000012412 Res.second = X86::GR32_ADRegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000012413 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000012414 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000012415 return Res;
12416 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012417
Chris Lattnerf76d1802006-07-31 23:26:50 +000012418 // Otherwise, check to see if this is a register class of the wrong value
12419 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
12420 // turn into {ax},{dx}.
12421 if (Res.second->hasType(VT))
12422 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012423
Chris Lattnerf76d1802006-07-31 23:26:50 +000012424 // All of the single-register GCC register classes map their values onto
12425 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
12426 // really want an 8-bit or 32-bit register, map to the appropriate register
12427 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +000012428 if (Res.second == X86::GR16RegisterClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000012429 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000012430 unsigned DestReg = 0;
12431 switch (Res.first) {
12432 default: break;
12433 case X86::AX: DestReg = X86::AL; break;
12434 case X86::DX: DestReg = X86::DL; break;
12435 case X86::CX: DestReg = X86::CL; break;
12436 case X86::BX: DestReg = X86::BL; break;
12437 }
12438 if (DestReg) {
12439 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000012440 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000012441 }
Owen Anderson825b72b2009-08-11 20:47:22 +000012442 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000012443 unsigned DestReg = 0;
12444 switch (Res.first) {
12445 default: break;
12446 case X86::AX: DestReg = X86::EAX; break;
12447 case X86::DX: DestReg = X86::EDX; break;
12448 case X86::CX: DestReg = X86::ECX; break;
12449 case X86::BX: DestReg = X86::EBX; break;
12450 case X86::SI: DestReg = X86::ESI; break;
12451 case X86::DI: DestReg = X86::EDI; break;
12452 case X86::BP: DestReg = X86::EBP; break;
12453 case X86::SP: DestReg = X86::ESP; break;
12454 }
12455 if (DestReg) {
12456 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000012457 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000012458 }
Owen Anderson825b72b2009-08-11 20:47:22 +000012459 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000012460 unsigned DestReg = 0;
12461 switch (Res.first) {
12462 default: break;
12463 case X86::AX: DestReg = X86::RAX; break;
12464 case X86::DX: DestReg = X86::RDX; break;
12465 case X86::CX: DestReg = X86::RCX; break;
12466 case X86::BX: DestReg = X86::RBX; break;
12467 case X86::SI: DestReg = X86::RSI; break;
12468 case X86::DI: DestReg = X86::RDI; break;
12469 case X86::BP: DestReg = X86::RBP; break;
12470 case X86::SP: DestReg = X86::RSP; break;
12471 }
12472 if (DestReg) {
12473 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000012474 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000012475 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000012476 }
Chris Lattner6ba50a92008-08-26 06:19:02 +000012477 } else if (Res.second == X86::FR32RegisterClass ||
12478 Res.second == X86::FR64RegisterClass ||
12479 Res.second == X86::VR128RegisterClass) {
12480 // Handle references to XMM physical registers that got mapped into the
12481 // wrong class. This can happen with constraints like {xmm0} where the
12482 // target independent register mapper will just pick the first match it can
12483 // find, ignoring the required type.
Owen Anderson825b72b2009-08-11 20:47:22 +000012484 if (VT == MVT::f32)
Chris Lattner6ba50a92008-08-26 06:19:02 +000012485 Res.second = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +000012486 else if (VT == MVT::f64)
Chris Lattner6ba50a92008-08-26 06:19:02 +000012487 Res.second = X86::FR64RegisterClass;
12488 else if (X86::VR128RegisterClass->hasType(VT))
12489 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000012490 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012491
Chris Lattnerf76d1802006-07-31 23:26:50 +000012492 return Res;
12493}