blob: aa1301fd4591f8ce4ef85b1fb0ba3b5e1d9770bb [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());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001547
1548 // Allocate shadow area for Win64
1549 if (IsWin64) {
1550 CCInfo.AllocateStack(32, 8);
1551 }
1552
Duncan Sands45907662010-10-31 13:21:44 +00001553 CCInfo.AnalyzeFormalArguments(Ins, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001554
Chris Lattnerf39f7712007-02-28 05:46:49 +00001555 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001556 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001557 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1558 CCValAssign &VA = ArgLocs[i];
1559 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1560 // places.
1561 assert(VA.getValNo() != LastVal &&
1562 "Don't support value assigned to multiple locs yet");
1563 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001564
Chris Lattnerf39f7712007-02-28 05:46:49 +00001565 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001566 EVT RegVT = VA.getLocVT();
Devang Patel8a84e442009-01-05 17:31:22 +00001567 TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001568 if (RegVT == MVT::i32)
Chris Lattnerf39f7712007-02-28 05:46:49 +00001569 RC = X86::GR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001570 else if (Is64Bit && RegVT == MVT::i64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001571 RC = X86::GR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001572 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001573 RC = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001574 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001575 RC = X86::FR64RegisterClass;
Bruno Cardoso Lopesac098352010-08-05 23:35:51 +00001576 else if (RegVT.isVector() && RegVT.getSizeInBits() == 256)
1577 RC = X86::VR256RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001578 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001579 RC = X86::VR128RegisterClass;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001580 else if (RegVT == MVT::x86mmx)
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001581 RC = X86::VR64RegisterClass;
1582 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001583 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001584
Devang Patele9a7ea62011-01-31 21:38:14 +00001585 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC, dl);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001586 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001587
Chris Lattnerf39f7712007-02-28 05:46:49 +00001588 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1589 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1590 // right size.
1591 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001592 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001593 DAG.getValueType(VA.getValVT()));
1594 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001595 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001596 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001597 else if (VA.getLocInfo() == CCValAssign::BCvt)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001598 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001599
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001600 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001601 // Handle MMX values passed in XMM regs.
1602 if (RegVT.isVector()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001603 ArgValue = DAG.getNode(X86ISD::MOVDQ2Q, dl, VA.getValVT(),
1604 ArgValue);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001605 } else
1606 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001607 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001608 } else {
1609 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001610 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001611 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001612
1613 // If value is passed via pointer - do a load.
1614 if (VA.getLocInfo() == CCValAssign::Indirect)
Chris Lattner51abfe42010-09-21 06:02:19 +00001615 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue,
1616 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001617
Dan Gohman98ca4f22009-08-05 01:29:28 +00001618 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001619 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001620
Dan Gohman61a92132008-04-21 23:59:07 +00001621 // The x86-64 ABI for returning structs by value requires that we copy
1622 // the sret argument into %rax for the return. Save the argument into
1623 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001624 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001625 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1626 unsigned Reg = FuncInfo->getSRetReturnReg();
1627 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001628 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001629 FuncInfo->setSRetReturnReg(Reg);
1630 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001631 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001632 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001633 }
1634
Chris Lattnerf39f7712007-02-28 05:46:49 +00001635 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00001636 // Align stack specially for tail calls.
1637 if (FuncIsMadeTailCallSafe(CallConv))
Gordon Henriksenae636f82008-01-03 16:47:34 +00001638 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001639
Evan Cheng1bc78042006-04-26 01:20:17 +00001640 // If the function takes variable number of arguments, make a frame index for
1641 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001642 if (isVarArg) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001643 if (!IsWin64 && (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
1644 CallConv != CallingConv::X86_ThisCall))) {
Jakob Stoklund Olesenb2eeed72010-07-29 17:42:27 +00001645 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,true));
Gordon Henriksen86737662008-01-05 16:56:59 +00001646 }
1647 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001648 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1649
1650 // FIXME: We should really autogenerate these arrays
1651 static const unsigned GPR64ArgRegsWin64[] = {
1652 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001653 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001654 static const unsigned GPR64ArgRegs64Bit[] = {
1655 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1656 };
1657 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001658 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1659 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1660 };
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001661 const unsigned *GPR64ArgRegs;
1662 unsigned NumXMMRegs = 0;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001663
1664 if (IsWin64) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001665 // The XMM registers which might contain var arg parameters are shadowed
1666 // in their paired GPR. So we only need to save the GPR to their home
1667 // slots.
1668 TotalNumIntRegs = 4;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001669 GPR64ArgRegs = GPR64ArgRegsWin64;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001670 } else {
1671 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1672 GPR64ArgRegs = GPR64ArgRegs64Bit;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001673
1674 NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs64Bit, TotalNumXMMRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001675 }
1676 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1677 TotalNumIntRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001678
Devang Patel578efa92009-06-05 21:57:13 +00001679 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001680 assert(!(NumXMMRegs && !Subtarget->hasXMM()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001681 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001682 assert(!(NumXMMRegs && UseSoftFloat && NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00001683 "SSE register cannot be used when SSE is disabled!");
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001684 if (UseSoftFloat || NoImplicitFloatOps || !Subtarget->hasXMM())
Torok Edwin3f142c32009-02-01 18:15:56 +00001685 // Kernel mode asks for SSE to be disabled, so don't push them
1686 // on the stack.
1687 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001688
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001689 if (IsWin64) {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001690 const TargetFrameLowering &TFI = *getTargetMachine().getFrameLowering();
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001691 // Get to the caller-allocated home save location. Add 8 to account
1692 // for the return address.
1693 int HomeOffset = TFI.getOffsetOfLocalArea() + 8;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001694 FuncInfo->setRegSaveFrameIndex(
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001695 MFI->CreateFixedObject(1, NumIntRegs * 8 + HomeOffset, false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001696 FuncInfo->setVarArgsFrameIndex(FuncInfo->getRegSaveFrameIndex());
1697 } else {
1698 // For X86-64, if there are vararg parameters that are passed via
1699 // registers, then we must store them to their spots on the stack so they
1700 // may be loaded by deferencing the result of va_next.
1701 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
1702 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
1703 FuncInfo->setRegSaveFrameIndex(
1704 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
Dan Gohman1e93df62010-04-17 14:41:14 +00001705 false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001706 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001707
Gordon Henriksen86737662008-01-05 16:56:59 +00001708 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001709 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00001710 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
1711 getPointerTy());
1712 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001713 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00001714 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
1715 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00001716 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
Devang Patele9a7ea62011-01-31 21:38:14 +00001717 X86::GR64RegisterClass, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00001718 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00001719 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001720 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001721 MachinePointerInfo::getFixedStack(
1722 FuncInfo->getRegSaveFrameIndex(), Offset),
1723 false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001724 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001725 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001726 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001727
Dan Gohmanface41a2009-08-16 21:24:25 +00001728 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
1729 // Now store the XMM (fp + vector) parameter registers.
1730 SmallVector<SDValue, 11> SaveXMMOps;
1731 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001732
Devang Patele9a7ea62011-01-31 21:38:14 +00001733 unsigned AL = MF.addLiveIn(X86::AL, X86::GR8RegisterClass, dl);
Dan Gohmanface41a2009-08-16 21:24:25 +00001734 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
1735 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001736
Dan Gohman1e93df62010-04-17 14:41:14 +00001737 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1738 FuncInfo->getRegSaveFrameIndex()));
1739 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1740 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00001741
Dan Gohmanface41a2009-08-16 21:24:25 +00001742 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001743 unsigned VReg = MF.addLiveIn(XMMArgRegs64Bit[NumXMMRegs],
Devang Patele9a7ea62011-01-31 21:38:14 +00001744 X86::VR128RegisterClass, dl);
Dan Gohmanface41a2009-08-16 21:24:25 +00001745 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
1746 SaveXMMOps.push_back(Val);
1747 }
1748 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
1749 MVT::Other,
1750 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00001751 }
Dan Gohmanface41a2009-08-16 21:24:25 +00001752
1753 if (!MemOps.empty())
1754 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1755 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001756 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001757 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001758
Gordon Henriksen86737662008-01-05 16:56:59 +00001759 // Some CCs need callee pop.
Dan Gohman4d3d6e12010-05-27 18:43:40 +00001760 if (Subtarget->IsCalleePop(isVarArg, CallConv)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001761 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001762 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00001763 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001764 // If this is an sret function, the return should pop the hidden pointer.
Chris Lattner29689432010-03-11 00:22:57 +00001765 if (!Is64Bit && !IsTailCallConvention(CallConv) && ArgsAreStructReturn(Ins))
Dan Gohman1e93df62010-04-17 14:41:14 +00001766 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001767 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001768
Gordon Henriksen86737662008-01-05 16:56:59 +00001769 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001770 // RegSaveFrameIndex is X86-64 only.
1771 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Anton Korobeynikovded05e32010-05-16 09:08:45 +00001772 if (CallConv == CallingConv::X86_FastCall ||
1773 CallConv == CallingConv::X86_ThisCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00001774 // fastcc functions can't have varargs.
1775 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00001776 }
Evan Cheng25caf632006-05-23 21:06:34 +00001777
Dan Gohman98ca4f22009-08-05 01:29:28 +00001778 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001779}
1780
Dan Gohman475871a2008-07-27 21:46:04 +00001781SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001782X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
1783 SDValue StackPtr, SDValue Arg,
1784 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00001785 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001786 ISD::ArgFlagsTy Flags) const {
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001787 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00001788 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00001789 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001790 if (Flags.isByVal())
Dale Johannesendd64c412009-02-04 00:33:20 +00001791 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001792
1793 return DAG.getStore(Chain, dl, Arg, PtrOff,
1794 MachinePointerInfo::getStack(LocMemOffset),
David Greene67c9d422010-02-15 16:53:33 +00001795 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00001796}
1797
Bill Wendling64e87322009-01-16 19:25:27 +00001798/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001799/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00001800SDValue
1801X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00001802 SDValue &OutRetAddr, SDValue Chain,
1803 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00001804 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001805 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00001806 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001807 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00001808
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001809 // Load the "old" Return address.
Chris Lattner51abfe42010-09-21 06:02:19 +00001810 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, MachinePointerInfo(),
1811 false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001812 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001813}
1814
1815/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1816/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00001817static SDValue
1818EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00001819 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00001820 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001821 // Store the return address to the appropriate stack slot.
1822 if (!FPDiff) return Chain;
1823 // Calculate the new stack slot for the return address.
1824 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00001825 int NewReturnAddrFI =
Evan Chenged2ae132010-07-03 00:40:23 +00001826 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false);
Owen Anderson825b72b2009-08-11 20:47:22 +00001827 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00001828 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001829 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00001830 MachinePointerInfo::getFixedStack(NewReturnAddrFI),
David Greene67c9d422010-02-15 16:53:33 +00001831 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001832 return Chain;
1833}
1834
Dan Gohman98ca4f22009-08-05 01:29:28 +00001835SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001836X86TargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001837 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001838 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001839 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001840 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001841 const SmallVectorImpl<ISD::InputArg> &Ins,
1842 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001843 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001844 MachineFunction &MF = DAG.getMachineFunction();
1845 bool Is64Bit = Subtarget->is64Bit();
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00001846 bool IsWin64 = Subtarget->isTargetWin64();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001847 bool IsStructRet = CallIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00001848 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001849
Evan Cheng5f941932010-02-05 02:21:12 +00001850 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00001851 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00001852 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1853 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001854 Outs, OutVals, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00001855
1856 // Sibcalls are automatically detected tailcalls which do not require
1857 // ABI changes.
Dan Gohman1797ed52010-02-08 20:27:50 +00001858 if (!GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00001859 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00001860
1861 if (isTailCall)
1862 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00001863 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00001864
Chris Lattner29689432010-03-11 00:22:57 +00001865 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1866 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001867
Chris Lattner638402b2007-02-28 07:00:42 +00001868 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00001869 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001870 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1871 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001872
1873 // Allocate shadow area for Win64
1874 if (IsWin64) {
1875 CCInfo.AllocateStack(32, 8);
1876 }
1877
Duncan Sands45907662010-10-31 13:21:44 +00001878 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001879
Chris Lattner423c5f42007-02-28 05:31:48 +00001880 // Get a count of how many bytes are to be pushed on the stack.
1881 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00001882 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00001883 // This is a sibcall. The memory operands are available in caller's
1884 // own caller's stack.
1885 NumBytes = 0;
Chris Lattner29689432010-03-11 00:22:57 +00001886 else if (GuaranteedTailCallOpt && IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00001887 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001888
Gordon Henriksen86737662008-01-05 16:56:59 +00001889 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00001890 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001891 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00001892 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00001893 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1894 FPDiff = NumBytesCallerPushed - NumBytes;
1895
1896 // Set the delta of movement of the returnaddr stackslot.
1897 // But only set if delta is greater than previous delta.
1898 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1899 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1900 }
1901
Evan Chengf22f9b32010-02-06 03:28:46 +00001902 if (!IsSibcall)
1903 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001904
Dan Gohman475871a2008-07-27 21:46:04 +00001905 SDValue RetAddrFrIdx;
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001906 // Load return adress for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00001907 if (isTailCall && FPDiff)
1908 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
1909 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00001910
Dan Gohman475871a2008-07-27 21:46:04 +00001911 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
1912 SmallVector<SDValue, 8> MemOpChains;
1913 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00001914
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001915 // Walk the register/memloc assignments, inserting copies/loads. In the case
1916 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00001917 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1918 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001919 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00001920 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00001921 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00001922 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00001923
Chris Lattner423c5f42007-02-28 05:31:48 +00001924 // Promote the value if needed.
1925 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001926 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00001927 case CCValAssign::Full: break;
1928 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001929 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001930 break;
1931 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001932 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001933 break;
1934 case CCValAssign::AExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001935 if (RegVT.isVector() && RegVT.getSizeInBits() == 128) {
1936 // Special case: passing MMX values in XMM registers.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001937 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00001938 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
1939 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001940 } else
1941 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
1942 break;
1943 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001944 Arg = DAG.getNode(ISD::BITCAST, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001945 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001946 case CCValAssign::Indirect: {
1947 // Store the argument.
1948 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00001949 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001950 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00001951 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00001952 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001953 Arg = SpillSlot;
1954 break;
1955 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00001956 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001957
Chris Lattner423c5f42007-02-28 05:31:48 +00001958 if (VA.isRegLoc()) {
1959 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00001960 if (isVarArg && IsWin64) {
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00001961 // Win64 ABI requires argument XMM reg to be copied to the corresponding
1962 // shadow reg if callee is a varargs function.
1963 unsigned ShadowReg = 0;
1964 switch (VA.getLocReg()) {
1965 case X86::XMM0: ShadowReg = X86::RCX; break;
1966 case X86::XMM1: ShadowReg = X86::RDX; break;
1967 case X86::XMM2: ShadowReg = X86::R8; break;
1968 case X86::XMM3: ShadowReg = X86::R9; break;
1969 }
1970 if (ShadowReg)
1971 RegsToPass.push_back(std::make_pair(ShadowReg, Arg));
1972 }
Evan Chengf22f9b32010-02-06 03:28:46 +00001973 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00001974 assert(VA.isMemLoc());
1975 if (StackPtr.getNode() == 0)
1976 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
1977 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1978 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001979 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001980 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001981
Evan Cheng32fe1032006-05-25 00:59:30 +00001982 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001983 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001984 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001985
Evan Cheng347d5f72006-04-28 21:29:37 +00001986 // Build a sequence of copy-to-reg nodes chained together with token chain
1987 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001988 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001989 // Tail call byval lowering might overwrite argument registers so in case of
1990 // tail call optimization the copies to registers are lowered later.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001991 if (!isTailCall)
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001992 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001993 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00001994 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001995 InFlag = Chain.getValue(1);
1996 }
Gordon Henriksen86737662008-01-05 16:56:59 +00001997
Chris Lattner88e1fd52009-07-09 04:24:46 +00001998 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00001999 // ELF / PIC requires GOT in the EBX register before function calls via PLT
2000 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002001 if (!isTailCall) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002002 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
2003 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00002004 DebugLoc(), getPointerTy()),
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002005 InFlag);
2006 InFlag = Chain.getValue(1);
2007 } else {
2008 // If we are tail calling and generating PIC/GOT style code load the
2009 // address of the callee into ECX. The value in ecx is used as target of
2010 // the tail jump. This is done to circumvent the ebx/callee-saved problem
2011 // for tail calls on PIC/GOT architectures. Normally we would just put the
2012 // address of GOT into ebx and then call target@PLT. But for tail calls
2013 // ebx would be restored (since ebx is callee saved) before jumping to the
2014 // target@PLT.
2015
2016 // Note: The actual moving to ECX is done further down.
2017 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
2018 if (G && !G->getGlobal()->hasHiddenVisibility() &&
2019 !G->getGlobal()->hasProtectedVisibility())
2020 Callee = LowerGlobalAddress(Callee, DAG);
2021 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00002022 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002023 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002024 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002025
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002026 if (Is64Bit && isVarArg && !IsWin64) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002027 // From AMD64 ABI document:
2028 // For calls that may call functions that use varargs or stdargs
2029 // (prototype-less calls or calls to functions containing ellipsis (...) in
2030 // the declaration) %al is used as hidden argument to specify the number
2031 // of SSE registers used. The contents of %al do not need to match exactly
2032 // the number of registers, but must be an ubound on the number of SSE
2033 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002034
Gordon Henriksen86737662008-01-05 16:56:59 +00002035 // Count the number of XMM registers allocated.
2036 static const unsigned XMMArgRegs[] = {
2037 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2038 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2039 };
2040 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00002041 assert((Subtarget->hasXMM() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00002042 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00002043
Dale Johannesendd64c412009-02-04 00:33:20 +00002044 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Owen Anderson825b72b2009-08-11 20:47:22 +00002045 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002046 InFlag = Chain.getValue(1);
2047 }
2048
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002049
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002050 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002051 if (isTailCall) {
2052 // Force all the incoming stack arguments to be loaded from the stack
2053 // before any new outgoing arguments are stored to the stack, because the
2054 // outgoing stack slots may alias the incoming argument stack slots, and
2055 // the alias isn't otherwise explicit. This is slightly more conservative
2056 // than necessary, because it means that each store effectively depends
2057 // on every argument instead of just those arguments it would clobber.
2058 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2059
Dan Gohman475871a2008-07-27 21:46:04 +00002060 SmallVector<SDValue, 8> MemOpChains2;
2061 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002062 int FI = 0;
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002063 // Do not flag preceeding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00002064 InFlag = SDValue();
Dan Gohman1797ed52010-02-08 20:27:50 +00002065 if (GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002066 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2067 CCValAssign &VA = ArgLocs[i];
2068 if (VA.isRegLoc())
2069 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002070 assert(VA.isMemLoc());
Dan Gohmanc9403652010-07-07 15:54:55 +00002071 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002072 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002073 // Create frame index.
2074 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002075 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002076 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002077 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002078
Duncan Sands276dcbd2008-03-21 09:14:45 +00002079 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002080 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002081 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002082 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00002083 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00002084 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002085 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002086
Dan Gohman98ca4f22009-08-05 01:29:28 +00002087 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2088 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002089 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002090 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002091 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002092 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002093 DAG.getStore(ArgChain, dl, Arg, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002094 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002095 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002096 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002097 }
2098 }
2099
2100 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002101 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002102 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002103
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002104 // Copy arguments to their registers.
2105 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002106 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002107 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002108 InFlag = Chain.getValue(1);
2109 }
Dan Gohman475871a2008-07-27 21:46:04 +00002110 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002111
Gordon Henriksen86737662008-01-05 16:56:59 +00002112 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002113 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00002114 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002115 }
2116
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002117 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2118 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2119 // In the 64-bit large code model, we have to make all calls
2120 // through a register, since the call instruction's 32-bit
2121 // pc-relative offset may not be large enough to hold the whole
2122 // address.
2123 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002124 // If the callee is a GlobalAddress node (quite common, every direct call
2125 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2126 // it.
2127
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002128 // We should use extra load for direct calls to dllimported functions in
2129 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002130 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002131 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002132 unsigned char OpFlags = 0;
Eric Christopherfd179292009-08-27 18:07:15 +00002133
Chris Lattner48a7d022009-07-09 05:02:21 +00002134 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2135 // external symbols most go through the PLT in PIC mode. If the symbol
2136 // has hidden or protected visibility, or if it is static or local, then
2137 // we don't need to use the PLT - we can directly call it.
2138 if (Subtarget->isTargetELF() &&
2139 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002140 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002141 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002142 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner80945782010-09-27 06:34:01 +00002143 (GV->isDeclaration() || GV->isWeakForLinker()) &&
2144 Subtarget->getDarwinVers() < 9) {
Chris Lattner74e726e2009-07-09 05:27:35 +00002145 // PC-relative references to external symbols should go through $stub,
2146 // unless we're building with the leopard linker or later, which
2147 // automatically synthesizes these stubs.
2148 OpFlags = X86II::MO_DARWIN_STUB;
2149 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002150
Devang Patel0d881da2010-07-06 22:08:15 +00002151 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002152 G->getOffset(), OpFlags);
2153 }
Bill Wendling056292f2008-09-16 21:48:12 +00002154 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002155 unsigned char OpFlags = 0;
2156
Evan Cheng1bf891a2010-12-01 22:59:46 +00002157 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to
2158 // external symbols should go through the PLT.
2159 if (Subtarget->isTargetELF() &&
2160 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
2161 OpFlags = X86II::MO_PLT;
2162 } else if (Subtarget->isPICStyleStubAny() &&
2163 Subtarget->getDarwinVers() < 9) {
2164 // PC-relative references to external symbols should go through $stub,
2165 // unless we're building with the leopard linker or later, which
2166 // automatically synthesizes these stubs.
2167 OpFlags = X86II::MO_DARWIN_STUB;
Chris Lattner74e726e2009-07-09 05:27:35 +00002168 }
Eric Christopherfd179292009-08-27 18:07:15 +00002169
Chris Lattner48a7d022009-07-09 05:02:21 +00002170 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2171 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002172 }
2173
Chris Lattnerd96d0722007-02-25 06:40:16 +00002174 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002175 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002176 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002177
Evan Chengf22f9b32010-02-06 03:28:46 +00002178 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002179 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2180 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002181 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002182 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002183
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002184 Ops.push_back(Chain);
2185 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002186
Dan Gohman98ca4f22009-08-05 01:29:28 +00002187 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002188 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002189
Gordon Henriksen86737662008-01-05 16:56:59 +00002190 // Add argument registers to the end of the list so that they are known live
2191 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002192 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2193 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2194 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002195
Evan Cheng586ccac2008-03-18 23:36:35 +00002196 // Add an implicit use GOT pointer in EBX.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002197 if (!isTailCall && Subtarget->isPICStyleGOT())
Evan Cheng586ccac2008-03-18 23:36:35 +00002198 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
2199
Anton Korobeynikov3a1e54a2010-08-17 21:06:07 +00002200 // Add an implicit use of AL for non-Windows x86 64-bit vararg functions.
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002201 if (Is64Bit && isVarArg && !IsWin64)
Owen Anderson825b72b2009-08-11 20:47:22 +00002202 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
Evan Cheng586ccac2008-03-18 23:36:35 +00002203
Gabor Greifba36cb52008-08-28 21:40:38 +00002204 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002205 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002206
Dan Gohman98ca4f22009-08-05 01:29:28 +00002207 if (isTailCall) {
Dale Johannesen88004c22010-06-05 00:30:45 +00002208 // We used to do:
2209 //// If this is the first return lowered for this function, add the regs
2210 //// to the liveout set for the function.
2211 // This isn't right, although it's probably harmless on x86; liveouts
2212 // should be computed from returns not tail calls. Consider a void
2213 // function making a tail call to a function returning int.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002214 return DAG.getNode(X86ISD::TC_RETURN, dl,
2215 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002216 }
2217
Dale Johannesenace16102009-02-03 19:33:06 +00002218 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002219 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002220
Chris Lattner2d297092006-05-23 18:50:38 +00002221 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002222 unsigned NumBytesForCalleeToPush;
Dan Gohman4d3d6e12010-05-27 18:43:40 +00002223 if (Subtarget->IsCalleePop(isVarArg, CallConv))
Gordon Henriksen86737662008-01-05 16:56:59 +00002224 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Chris Lattner29689432010-03-11 00:22:57 +00002225 else if (!Is64Bit && !IsTailCallConvention(CallConv) && IsStructRet)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002226 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002227 // pops the hidden struct pointer, so we have to push it back.
2228 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002229 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002230 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002231 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002232
Gordon Henriksenae636f82008-01-03 16:47:34 +00002233 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002234 if (!IsSibcall) {
2235 Chain = DAG.getCALLSEQ_END(Chain,
2236 DAG.getIntPtrConstant(NumBytes, true),
2237 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2238 true),
2239 InFlag);
2240 InFlag = Chain.getValue(1);
2241 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002242
Chris Lattner3085e152007-02-25 08:59:22 +00002243 // Handle result values, copying them out of physregs into vregs that we
2244 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002245 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2246 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002247}
2248
Evan Cheng25ab6902006-09-08 06:48:29 +00002249
2250//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002251// Fast Calling Convention (tail call) implementation
2252//===----------------------------------------------------------------------===//
2253
2254// Like std call, callee cleans arguments, convention except that ECX is
2255// reserved for storing the tail called function address. Only 2 registers are
2256// free for argument passing (inreg). Tail call optimization is performed
2257// provided:
2258// * tailcallopt is enabled
2259// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002260// On X86_64 architecture with GOT-style position independent code only local
2261// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002262// To keep the stack aligned according to platform abi the function
2263// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2264// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002265// If a tail called function callee has more arguments than the caller the
2266// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002267// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002268// original REtADDR, but before the saved framepointer or the spilled registers
2269// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2270// stack layout:
2271// arg1
2272// arg2
2273// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002274// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002275// move area ]
2276// (possible EBP)
2277// ESI
2278// EDI
2279// local1 ..
2280
2281/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2282/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002283unsigned
2284X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2285 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002286 MachineFunction &MF = DAG.getMachineFunction();
2287 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002288 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002289 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002290 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002291 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002292 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002293 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2294 // Number smaller than 12 so just add the difference.
2295 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2296 } else {
2297 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002298 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002299 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002300 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002301 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002302}
2303
Evan Cheng5f941932010-02-05 02:21:12 +00002304/// MatchingStackOffset - Return true if the given stack call argument is
2305/// already available in the same position (relatively) of the caller's
2306/// incoming argument stack.
2307static
2308bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2309 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2310 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002311 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2312 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002313 if (Arg.getOpcode() == ISD::CopyFromReg) {
2314 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00002315 if (!TargetRegisterInfo::isVirtualRegister(VR))
Evan Cheng5f941932010-02-05 02:21:12 +00002316 return false;
2317 MachineInstr *Def = MRI->getVRegDef(VR);
2318 if (!Def)
2319 return false;
2320 if (!Flags.isByVal()) {
2321 if (!TII->isLoadFromStackSlot(Def, FI))
2322 return false;
2323 } else {
2324 unsigned Opcode = Def->getOpcode();
2325 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2326 Def->getOperand(1).isFI()) {
2327 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002328 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002329 } else
2330 return false;
2331 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002332 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2333 if (Flags.isByVal())
2334 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002335 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002336 // define @foo(%struct.X* %A) {
2337 // tail call @bar(%struct.X* byval %A)
2338 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002339 return false;
2340 SDValue Ptr = Ld->getBasePtr();
2341 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2342 if (!FINode)
2343 return false;
2344 FI = FINode->getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002345 } else
2346 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002347
Evan Cheng4cae1332010-03-05 08:38:04 +00002348 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002349 if (!MFI->isFixedObjectIndex(FI))
2350 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002351 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002352}
2353
Dan Gohman98ca4f22009-08-05 01:29:28 +00002354/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2355/// for tail call optimization. Targets which want to do tail call
2356/// optimization should implement this function.
2357bool
2358X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002359 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002360 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002361 bool isCalleeStructRet,
2362 bool isCallerStructRet,
Evan Chengb1712452010-01-27 06:25:16 +00002363 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002364 const SmallVectorImpl<SDValue> &OutVals,
Evan Chengb1712452010-01-27 06:25:16 +00002365 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002366 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002367 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002368 CalleeCC != CallingConv::C)
2369 return false;
2370
Evan Cheng7096ae42010-01-29 06:45:59 +00002371 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002372 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002373 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Cheng13617962010-04-30 01:12:32 +00002374 CallingConv::ID CallerCC = CallerF->getCallingConv();
2375 bool CCMatch = CallerCC == CalleeCC;
2376
Dan Gohman1797ed52010-02-08 20:27:50 +00002377 if (GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002378 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002379 return true;
2380 return false;
2381 }
2382
Dale Johannesen2f05cc02010-05-28 23:24:28 +00002383 // Look for obvious safe cases to perform tail call optimization that do not
2384 // require ABI changes. This is what gcc calls sibcall.
Evan Chengb2c92902010-02-02 02:22:50 +00002385
Evan Cheng2c12cb42010-03-26 16:26:03 +00002386 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2387 // emit a special epilogue.
2388 if (RegInfo->needsStackRealignment(MF))
2389 return false;
2390
Eric Christopher90eb4022010-07-22 00:26:08 +00002391 // Do not sibcall optimize vararg calls unless the call site is not passing
2392 // any arguments.
Evan Cheng3c262ee2010-03-26 02:13:13 +00002393 if (isVarArg && !Outs.empty())
Evan Cheng843bd692010-01-31 06:44:49 +00002394 return false;
2395
Evan Chenga375d472010-03-15 18:54:48 +00002396 // Also avoid sibcall optimization if either caller or callee uses struct
2397 // return semantics.
2398 if (isCalleeStructRet || isCallerStructRet)
2399 return false;
2400
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002401 // If the call result is in ST0 / ST1, it needs to be popped off the x87 stack.
2402 // Therefore if it's not used by the call it is not safe to optimize this into
2403 // a sibcall.
2404 bool Unused = false;
2405 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2406 if (!Ins[i].Used) {
2407 Unused = true;
2408 break;
2409 }
2410 }
2411 if (Unused) {
2412 SmallVector<CCValAssign, 16> RVLocs;
2413 CCState CCInfo(CalleeCC, false, getTargetMachine(),
2414 RVLocs, *DAG.getContext());
2415 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002416 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002417 CCValAssign &VA = RVLocs[i];
2418 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2419 return false;
2420 }
2421 }
2422
Evan Cheng13617962010-04-30 01:12:32 +00002423 // If the calling conventions do not match, then we'd better make sure the
2424 // results are returned in the same way as what the caller expects.
2425 if (!CCMatch) {
2426 SmallVector<CCValAssign, 16> RVLocs1;
2427 CCState CCInfo1(CalleeCC, false, getTargetMachine(),
2428 RVLocs1, *DAG.getContext());
2429 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2430
2431 SmallVector<CCValAssign, 16> RVLocs2;
2432 CCState CCInfo2(CallerCC, false, getTargetMachine(),
2433 RVLocs2, *DAG.getContext());
2434 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2435
2436 if (RVLocs1.size() != RVLocs2.size())
2437 return false;
2438 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2439 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2440 return false;
2441 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2442 return false;
2443 if (RVLocs1[i].isRegLoc()) {
2444 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2445 return false;
2446 } else {
2447 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2448 return false;
2449 }
2450 }
2451 }
2452
Evan Chenga6bff982010-01-30 01:22:00 +00002453 // If the callee takes no arguments then go on to check the results of the
2454 // call.
2455 if (!Outs.empty()) {
2456 // Check if stack adjustment is needed. For now, do not do this if any
2457 // argument is passed on the stack.
2458 SmallVector<CCValAssign, 16> ArgLocs;
2459 CCState CCInfo(CalleeCC, isVarArg, getTargetMachine(),
2460 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002461
2462 // Allocate shadow area for Win64
2463 if (Subtarget->isTargetWin64()) {
2464 CCInfo.AllocateStack(32, 8);
2465 }
2466
Duncan Sands45907662010-10-31 13:21:44 +00002467 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Evan Chengb2c92902010-02-02 02:22:50 +00002468 if (CCInfo.getNextStackOffset()) {
2469 MachineFunction &MF = DAG.getMachineFunction();
2470 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2471 return false;
Evan Chengb2c92902010-02-02 02:22:50 +00002472
2473 // Check if the arguments are already laid out in the right way as
2474 // the caller's fixed stack objects.
2475 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002476 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2477 const X86InstrInfo *TII =
2478 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002479 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2480 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00002481 SDValue Arg = OutVals[i];
Evan Chengb2c92902010-02-02 02:22:50 +00002482 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002483 if (VA.getLocInfo() == CCValAssign::Indirect)
2484 return false;
2485 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002486 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2487 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002488 return false;
2489 }
2490 }
2491 }
Evan Cheng9c044672010-05-29 01:35:22 +00002492
2493 // If the tailcall address may be in a register, then make sure it's
2494 // possible to register allocate for it. In 32-bit, the call address can
2495 // only target EAX, EDX, or ECX since the tail call must be scheduled after
Evan Chengdedd9742010-07-14 06:44:01 +00002496 // callee-saved registers are restored. These happen to be the same
2497 // registers used to pass 'inreg' arguments so watch out for those.
2498 if (!Subtarget->is64Bit() &&
2499 !isa<GlobalAddressSDNode>(Callee) &&
Evan Cheng9c044672010-05-29 01:35:22 +00002500 !isa<ExternalSymbolSDNode>(Callee)) {
Evan Cheng9c044672010-05-29 01:35:22 +00002501 unsigned NumInRegs = 0;
2502 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2503 CCValAssign &VA = ArgLocs[i];
Evan Chengdedd9742010-07-14 06:44:01 +00002504 if (!VA.isRegLoc())
2505 continue;
2506 unsigned Reg = VA.getLocReg();
2507 switch (Reg) {
2508 default: break;
2509 case X86::EAX: case X86::EDX: case X86::ECX:
2510 if (++NumInRegs == 3)
Evan Cheng9c044672010-05-29 01:35:22 +00002511 return false;
Evan Chengdedd9742010-07-14 06:44:01 +00002512 break;
Evan Cheng9c044672010-05-29 01:35:22 +00002513 }
2514 }
2515 }
Evan Chenga6bff982010-01-30 01:22:00 +00002516 }
Evan Chengb1712452010-01-27 06:25:16 +00002517
Dale Johannesend155d7e2010-10-25 22:17:05 +00002518 // An stdcall caller is expected to clean up its arguments; the callee
Dale Johannesen0e034562010-11-12 00:43:18 +00002519 // isn't going to do that.
Dale Johannesend155d7e2010-10-25 22:17:05 +00002520 if (!CCMatch && CallerCC==CallingConv::X86_StdCall)
2521 return false;
2522
Evan Cheng86809cc2010-02-03 03:28:02 +00002523 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002524}
2525
Dan Gohman3df24e62008-09-03 23:12:08 +00002526FastISel *
Dan Gohmana4160c32010-07-07 16:29:44 +00002527X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
2528 return X86::createFastISel(funcInfo);
Dan Gohmand9f3c482008-08-19 21:32:53 +00002529}
2530
2531
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002532//===----------------------------------------------------------------------===//
2533// Other Lowering Hooks
2534//===----------------------------------------------------------------------===//
2535
Bruno Cardoso Lopese654b562010-09-01 00:51:36 +00002536static bool MayFoldLoad(SDValue Op) {
2537 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
2538}
2539
2540static bool MayFoldIntoStore(SDValue Op) {
2541 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
2542}
2543
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002544static bool isTargetShuffle(unsigned Opcode) {
2545 switch(Opcode) {
2546 default: return false;
2547 case X86ISD::PSHUFD:
2548 case X86ISD::PSHUFHW:
2549 case X86ISD::PSHUFLW:
2550 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002551 case X86ISD::PALIGN:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002552 case X86ISD::SHUFPS:
2553 case X86ISD::MOVLHPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002554 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002555 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002556 case X86ISD::MOVLPS:
2557 case X86ISD::MOVLPD:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002558 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002559 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002560 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002561 case X86ISD::MOVSS:
2562 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002563 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002564 case X86ISD::UNPCKLPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002565 case X86ISD::PUNPCKLWD:
2566 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002567 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002568 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002569 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002570 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002571 case X86ISD::PUNPCKHWD:
2572 case X86ISD::PUNPCKHBW:
2573 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002574 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002575 return true;
2576 }
2577 return false;
2578}
2579
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002580static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002581 SDValue V1, SelectionDAG &DAG) {
2582 switch(Opc) {
2583 default: llvm_unreachable("Unknown x86 shuffle node");
2584 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002585 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002586 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002587 return DAG.getNode(Opc, dl, VT, V1);
2588 }
2589
2590 return SDValue();
2591}
2592
2593static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00002594 SDValue V1, unsigned TargetMask, SelectionDAG &DAG) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002595 switch(Opc) {
2596 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002597 case X86ISD::PSHUFD:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002598 case X86ISD::PSHUFHW:
2599 case X86ISD::PSHUFLW:
2600 return DAG.getNode(Opc, dl, VT, V1, DAG.getConstant(TargetMask, MVT::i8));
2601 }
2602
2603 return SDValue();
2604}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002605
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002606static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2607 SDValue V1, SDValue V2, unsigned TargetMask, SelectionDAG &DAG) {
2608 switch(Opc) {
2609 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002610 case X86ISD::PALIGN:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002611 case X86ISD::SHUFPD:
2612 case X86ISD::SHUFPS:
2613 return DAG.getNode(Opc, dl, VT, V1, V2,
2614 DAG.getConstant(TargetMask, MVT::i8));
2615 }
2616 return SDValue();
2617}
2618
2619static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2620 SDValue V1, SDValue V2, SelectionDAG &DAG) {
2621 switch(Opc) {
2622 default: llvm_unreachable("Unknown x86 shuffle node");
2623 case X86ISD::MOVLHPS:
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00002624 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002625 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002626 case X86ISD::MOVLPS:
2627 case X86ISD::MOVLPD:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002628 case X86ISD::MOVSS:
2629 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002630 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002631 case X86ISD::UNPCKLPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002632 case X86ISD::PUNPCKLWD:
2633 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002634 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002635 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002636 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002637 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002638 case X86ISD::PUNPCKHWD:
2639 case X86ISD::PUNPCKHBW:
2640 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002641 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002642 return DAG.getNode(Opc, dl, VT, V1, V2);
2643 }
2644 return SDValue();
2645}
2646
Dan Gohmand858e902010-04-17 15:26:15 +00002647SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002648 MachineFunction &MF = DAG.getMachineFunction();
2649 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2650 int ReturnAddrIndex = FuncInfo->getRAIndex();
2651
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002652 if (ReturnAddrIndex == 0) {
2653 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002654 uint64_t SlotSize = TD->getPointerSize();
David Greene3f2bf852009-11-12 20:49:22 +00002655 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00002656 false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002657 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002658 }
2659
Evan Cheng25ab6902006-09-08 06:48:29 +00002660 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002661}
2662
2663
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002664bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
2665 bool hasSymbolicDisplacement) {
2666 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00002667 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002668 return false;
2669
2670 // If we don't have a symbolic displacement - we don't have any extra
2671 // restrictions.
2672 if (!hasSymbolicDisplacement)
2673 return true;
2674
2675 // FIXME: Some tweaks might be needed for medium code model.
2676 if (M != CodeModel::Small && M != CodeModel::Kernel)
2677 return false;
2678
2679 // For small code model we assume that latest object is 16MB before end of 31
2680 // bits boundary. We may also accept pretty large negative constants knowing
2681 // that all objects are in the positive half of address space.
2682 if (M == CodeModel::Small && Offset < 16*1024*1024)
2683 return true;
2684
2685 // For kernel code model we know that all object resist in the negative half
2686 // of 32bits address space. We may not accept negative offsets, since they may
2687 // be just off and we may accept pretty large positive ones.
2688 if (M == CodeModel::Kernel && Offset > 0)
2689 return true;
2690
2691 return false;
2692}
2693
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002694/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
2695/// specific condition code, returning the condition code and the LHS/RHS of the
2696/// comparison to make.
2697static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2698 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00002699 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002700 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2701 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2702 // X > -1 -> X == 0, jump !sign.
2703 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002704 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002705 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2706 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002707 return X86::COND_S;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002708 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00002709 // X < 1 -> X <= 0
2710 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002711 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002712 }
Chris Lattnerf9570512006-09-13 03:22:10 +00002713 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002714
Evan Chengd9558e02006-01-06 00:43:03 +00002715 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002716 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002717 case ISD::SETEQ: return X86::COND_E;
2718 case ISD::SETGT: return X86::COND_G;
2719 case ISD::SETGE: return X86::COND_GE;
2720 case ISD::SETLT: return X86::COND_L;
2721 case ISD::SETLE: return X86::COND_LE;
2722 case ISD::SETNE: return X86::COND_NE;
2723 case ISD::SETULT: return X86::COND_B;
2724 case ISD::SETUGT: return X86::COND_A;
2725 case ISD::SETULE: return X86::COND_BE;
2726 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00002727 }
Chris Lattner4c78e022008-12-23 23:42:27 +00002728 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002729
Chris Lattner4c78e022008-12-23 23:42:27 +00002730 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00002731
Chris Lattner4c78e022008-12-23 23:42:27 +00002732 // If LHS is a foldable load, but RHS is not, flip the condition.
Rafael Espindolaf297c932011-02-03 03:58:05 +00002733 if (ISD::isNON_EXTLoad(LHS.getNode()) &&
2734 !ISD::isNON_EXTLoad(RHS.getNode())) {
Chris Lattner4c78e022008-12-23 23:42:27 +00002735 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
2736 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00002737 }
2738
Chris Lattner4c78e022008-12-23 23:42:27 +00002739 switch (SetCCOpcode) {
2740 default: break;
2741 case ISD::SETOLT:
2742 case ISD::SETOLE:
2743 case ISD::SETUGT:
2744 case ISD::SETUGE:
2745 std::swap(LHS, RHS);
2746 break;
2747 }
2748
2749 // On a floating point condition, the flags are set as follows:
2750 // ZF PF CF op
2751 // 0 | 0 | 0 | X > Y
2752 // 0 | 0 | 1 | X < Y
2753 // 1 | 0 | 0 | X == Y
2754 // 1 | 1 | 1 | unordered
2755 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002756 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00002757 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002758 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00002759 case ISD::SETOLT: // flipped
2760 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002761 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00002762 case ISD::SETOLE: // flipped
2763 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002764 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002765 case ISD::SETUGT: // flipped
2766 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002767 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00002768 case ISD::SETUGE: // flipped
2769 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002770 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002771 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002772 case ISD::SETNE: return X86::COND_NE;
2773 case ISD::SETUO: return X86::COND_P;
2774 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00002775 case ISD::SETOEQ:
2776 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00002777 }
Evan Chengd9558e02006-01-06 00:43:03 +00002778}
2779
Evan Cheng4a460802006-01-11 00:33:36 +00002780/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2781/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00002782/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00002783static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00002784 switch (X86CC) {
2785 default:
2786 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00002787 case X86::COND_B:
2788 case X86::COND_BE:
2789 case X86::COND_E:
2790 case X86::COND_P:
2791 case X86::COND_A:
2792 case X86::COND_AE:
2793 case X86::COND_NE:
2794 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00002795 return true;
2796 }
2797}
2798
Evan Chengeb2f9692009-10-27 19:56:55 +00002799/// isFPImmLegal - Returns true if the target can instruction select the
2800/// specified FP immediate natively. If false, the legalizer will
2801/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00002802bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00002803 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
2804 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
2805 return true;
2806 }
2807 return false;
2808}
2809
Nate Begeman9008ca62009-04-27 18:41:29 +00002810/// isUndefOrInRange - Return true if Val is undef or if its value falls within
2811/// the specified range (L, H].
2812static bool isUndefOrInRange(int Val, int Low, int Hi) {
2813 return (Val < 0) || (Val >= Low && Val < Hi);
2814}
2815
2816/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
2817/// specified value.
2818static bool isUndefOrEqual(int Val, int CmpVal) {
2819 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00002820 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00002821 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00002822}
2823
Nate Begeman9008ca62009-04-27 18:41:29 +00002824/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
2825/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
2826/// the second operand.
Owen Andersone50ed302009-08-10 22:56:29 +00002827static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00002828 if (VT == MVT::v4f32 || VT == MVT::v4i32 )
Nate Begeman9008ca62009-04-27 18:41:29 +00002829 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002830 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00002831 return (Mask[0] < 2 && Mask[1] < 2);
2832 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002833}
2834
Nate Begeman9008ca62009-04-27 18:41:29 +00002835bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002836 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002837 N->getMask(M);
2838 return ::isPSHUFDMask(M, N->getValueType(0));
2839}
Evan Cheng0188ecb2006-03-22 18:59:22 +00002840
Nate Begeman9008ca62009-04-27 18:41:29 +00002841/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
2842/// is suitable for input to PSHUFHW.
Owen Andersone50ed302009-08-10 22:56:29 +00002843static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002844 if (VT != MVT::v8i16)
Evan Cheng0188ecb2006-03-22 18:59:22 +00002845 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002846
Nate Begeman9008ca62009-04-27 18:41:29 +00002847 // Lower quadword copied in order or undef.
2848 for (int i = 0; i != 4; ++i)
2849 if (Mask[i] >= 0 && Mask[i] != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00002850 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002851
Evan Cheng506d3df2006-03-29 23:07:14 +00002852 // Upper quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002853 for (int i = 4; i != 8; ++i)
2854 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Evan Cheng506d3df2006-03-29 23:07:14 +00002855 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002856
Evan Cheng506d3df2006-03-29 23:07:14 +00002857 return true;
2858}
2859
Nate Begeman9008ca62009-04-27 18:41:29 +00002860bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002861 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002862 N->getMask(M);
2863 return ::isPSHUFHWMask(M, N->getValueType(0));
2864}
Evan Cheng506d3df2006-03-29 23:07:14 +00002865
Nate Begeman9008ca62009-04-27 18:41:29 +00002866/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
2867/// is suitable for input to PSHUFLW.
Owen Andersone50ed302009-08-10 22:56:29 +00002868static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002869 if (VT != MVT::v8i16)
Evan Cheng506d3df2006-03-29 23:07:14 +00002870 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002871
Rafael Espindola15684b22009-04-24 12:40:33 +00002872 // Upper quadword copied in order.
Nate Begeman9008ca62009-04-27 18:41:29 +00002873 for (int i = 4; i != 8; ++i)
2874 if (Mask[i] >= 0 && Mask[i] != i)
Rafael Espindola15684b22009-04-24 12:40:33 +00002875 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002876
Rafael Espindola15684b22009-04-24 12:40:33 +00002877 // Lower quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002878 for (int i = 0; i != 4; ++i)
2879 if (Mask[i] >= 4)
Rafael Espindola15684b22009-04-24 12:40:33 +00002880 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002881
Rafael Espindola15684b22009-04-24 12:40:33 +00002882 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002883}
2884
Nate Begeman9008ca62009-04-27 18:41:29 +00002885bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002886 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002887 N->getMask(M);
2888 return ::isPSHUFLWMask(M, N->getValueType(0));
2889}
2890
Nate Begemana09008b2009-10-19 02:17:23 +00002891/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
2892/// is suitable for input to PALIGNR.
2893static bool isPALIGNRMask(const SmallVectorImpl<int> &Mask, EVT VT,
2894 bool hasSSSE3) {
2895 int i, e = VT.getVectorNumElements();
Michael J. Spencerec38de22010-10-10 22:04:20 +00002896
Nate Begemana09008b2009-10-19 02:17:23 +00002897 // Do not handle v2i64 / v2f64 shuffles with palignr.
2898 if (e < 4 || !hasSSSE3)
2899 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00002900
Nate Begemana09008b2009-10-19 02:17:23 +00002901 for (i = 0; i != e; ++i)
2902 if (Mask[i] >= 0)
2903 break;
Michael J. Spencerec38de22010-10-10 22:04:20 +00002904
Nate Begemana09008b2009-10-19 02:17:23 +00002905 // All undef, not a palignr.
2906 if (i == e)
2907 return false;
2908
2909 // Determine if it's ok to perform a palignr with only the LHS, since we
2910 // don't have access to the actual shuffle elements to see if RHS is undef.
2911 bool Unary = Mask[i] < (int)e;
2912 bool NeedsUnary = false;
2913
2914 int s = Mask[i] - i;
Michael J. Spencerec38de22010-10-10 22:04:20 +00002915
Nate Begemana09008b2009-10-19 02:17:23 +00002916 // Check the rest of the elements to see if they are consecutive.
2917 for (++i; i != e; ++i) {
2918 int m = Mask[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00002919 if (m < 0)
Nate Begemana09008b2009-10-19 02:17:23 +00002920 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00002921
Nate Begemana09008b2009-10-19 02:17:23 +00002922 Unary = Unary && (m < (int)e);
2923 NeedsUnary = NeedsUnary || (m < s);
2924
2925 if (NeedsUnary && !Unary)
2926 return false;
2927 if (Unary && m != ((s+i) & (e-1)))
2928 return false;
2929 if (!Unary && m != (s+i))
2930 return false;
2931 }
2932 return true;
2933}
2934
2935bool X86::isPALIGNRMask(ShuffleVectorSDNode *N) {
2936 SmallVector<int, 8> M;
2937 N->getMask(M);
2938 return ::isPALIGNRMask(M, N->getValueType(0), true);
2939}
2940
Evan Cheng14aed5e2006-03-24 01:18:28 +00002941/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2942/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Owen Andersone50ed302009-08-10 22:56:29 +00002943static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002944 int NumElems = VT.getVectorNumElements();
2945 if (NumElems != 2 && NumElems != 4)
2946 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002947
Nate Begeman9008ca62009-04-27 18:41:29 +00002948 int Half = NumElems / 2;
2949 for (int i = 0; i < Half; ++i)
2950 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002951 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002952 for (int i = Half; i < NumElems; ++i)
2953 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002954 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002955
Evan Cheng14aed5e2006-03-24 01:18:28 +00002956 return true;
2957}
2958
Nate Begeman9008ca62009-04-27 18:41:29 +00002959bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
2960 SmallVector<int, 8> M;
2961 N->getMask(M);
2962 return ::isSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002963}
2964
Evan Cheng213d2cf2007-05-17 18:45:50 +00002965/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00002966/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2967/// half elements to come from vector 1 (which would equal the dest.) and
2968/// the upper half to come from vector 2.
Owen Andersone50ed302009-08-10 22:56:29 +00002969static bool isCommutedSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002970 int NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00002971
2972 if (NumElems != 2 && NumElems != 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00002973 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002974
Nate Begeman9008ca62009-04-27 18:41:29 +00002975 int Half = NumElems / 2;
2976 for (int i = 0; i < Half; ++i)
2977 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002978 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002979 for (int i = Half; i < NumElems; ++i)
2980 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002981 return false;
2982 return true;
2983}
2984
Nate Begeman9008ca62009-04-27 18:41:29 +00002985static bool isCommutedSHUFP(ShuffleVectorSDNode *N) {
2986 SmallVector<int, 8> M;
2987 N->getMask(M);
2988 return isCommutedSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002989}
2990
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002991/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2992/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00002993bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
2994 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002995 return false;
2996
Evan Cheng2064a2b2006-03-28 06:50:32 +00002997 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman9008ca62009-04-27 18:41:29 +00002998 return isUndefOrEqual(N->getMaskElt(0), 6) &&
2999 isUndefOrEqual(N->getMaskElt(1), 7) &&
3000 isUndefOrEqual(N->getMaskElt(2), 2) &&
3001 isUndefOrEqual(N->getMaskElt(3), 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00003002}
3003
Nate Begeman0b10b912009-11-07 23:17:15 +00003004/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
3005/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
3006/// <2, 3, 2, 3>
3007bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
3008 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Michael J. Spencerec38de22010-10-10 22:04:20 +00003009
Nate Begeman0b10b912009-11-07 23:17:15 +00003010 if (NumElems != 4)
3011 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003012
Nate Begeman0b10b912009-11-07 23:17:15 +00003013 return isUndefOrEqual(N->getMaskElt(0), 2) &&
3014 isUndefOrEqual(N->getMaskElt(1), 3) &&
3015 isUndefOrEqual(N->getMaskElt(2), 2) &&
3016 isUndefOrEqual(N->getMaskElt(3), 3);
3017}
3018
Evan Cheng5ced1d82006-04-06 23:23:56 +00003019/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
3020/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman9008ca62009-04-27 18:41:29 +00003021bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
3022 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003023
Evan Cheng5ced1d82006-04-06 23:23:56 +00003024 if (NumElems != 2 && NumElems != 4)
3025 return false;
3026
Evan Chengc5cdff22006-04-07 21:53:05 +00003027 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003028 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003029 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003030
Evan Chengc5cdff22006-04-07 21:53:05 +00003031 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003032 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003033 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003034
3035 return true;
3036}
3037
Nate Begeman0b10b912009-11-07 23:17:15 +00003038/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
3039/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
3040bool X86::isMOVLHPSMask(ShuffleVectorSDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003041 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003042
Evan Cheng5ced1d82006-04-06 23:23:56 +00003043 if (NumElems != 2 && NumElems != 4)
3044 return false;
3045
Evan Chengc5cdff22006-04-07 21:53:05 +00003046 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003047 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003048 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003049
Nate Begeman9008ca62009-04-27 18:41:29 +00003050 for (unsigned i = 0; i < NumElems/2; ++i)
3051 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003052 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003053
3054 return true;
3055}
3056
Evan Cheng0038e592006-03-28 00:39:58 +00003057/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
3058/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Owen Andersone50ed302009-08-10 22:56:29 +00003059static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003060 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003061 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003062 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng0038e592006-03-28 00:39:58 +00003063 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003064
Nate Begeman9008ca62009-04-27 18:41:29 +00003065 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
3066 int BitI = Mask[i];
3067 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00003068 if (!isUndefOrEqual(BitI, j))
3069 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00003070 if (V2IsSplat) {
Mon P Wang7bcaefa2009-02-04 01:16:59 +00003071 if (!isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003072 return false;
3073 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00003074 if (!isUndefOrEqual(BitI1, j + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003075 return false;
3076 }
Evan Cheng0038e592006-03-28 00:39:58 +00003077 }
Evan Cheng0038e592006-03-28 00:39:58 +00003078 return true;
3079}
3080
Nate Begeman9008ca62009-04-27 18:41:29 +00003081bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3082 SmallVector<int, 8> M;
3083 N->getMask(M);
3084 return ::isUNPCKLMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003085}
3086
Evan Cheng4fcb9222006-03-28 02:43:26 +00003087/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3088/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Eric Christopherfd179292009-08-27 18:07:15 +00003089static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003090 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003091 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003092 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng4fcb9222006-03-28 02:43:26 +00003093 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003094
Nate Begeman9008ca62009-04-27 18:41:29 +00003095 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
3096 int BitI = Mask[i];
3097 int BitI1 = Mask[i+1];
Chris Lattner5a88b832007-02-25 07:10:00 +00003098 if (!isUndefOrEqual(BitI, j + NumElts/2))
Evan Chengc5cdff22006-04-07 21:53:05 +00003099 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00003100 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00003101 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003102 return false;
3103 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00003104 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003105 return false;
3106 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003107 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003108 return true;
3109}
3110
Nate Begeman9008ca62009-04-27 18:41:29 +00003111bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3112 SmallVector<int, 8> M;
3113 N->getMask(M);
3114 return ::isUNPCKHMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003115}
3116
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003117/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3118/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3119/// <0, 0, 1, 1>
Owen Andersone50ed302009-08-10 22:56:29 +00003120static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003121 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003122 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003123 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003124
Nate Begeman9008ca62009-04-27 18:41:29 +00003125 for (int i = 0, j = 0; i != NumElems; i += 2, ++j) {
3126 int BitI = Mask[i];
3127 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00003128 if (!isUndefOrEqual(BitI, j))
3129 return false;
3130 if (!isUndefOrEqual(BitI1, j))
3131 return false;
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003132 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003133 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003134}
3135
Nate Begeman9008ca62009-04-27 18:41:29 +00003136bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
3137 SmallVector<int, 8> M;
3138 N->getMask(M);
3139 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
3140}
3141
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003142/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3143/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3144/// <2, 2, 3, 3>
Owen Andersone50ed302009-08-10 22:56:29 +00003145static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003146 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003147 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
3148 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003149
Nate Begeman9008ca62009-04-27 18:41:29 +00003150 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
3151 int BitI = Mask[i];
3152 int BitI1 = Mask[i+1];
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003153 if (!isUndefOrEqual(BitI, j))
3154 return false;
3155 if (!isUndefOrEqual(BitI1, j))
3156 return false;
3157 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003158 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003159}
3160
Nate Begeman9008ca62009-04-27 18:41:29 +00003161bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
3162 SmallVector<int, 8> M;
3163 N->getMask(M);
3164 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
3165}
3166
Evan Cheng017dcc62006-04-21 01:05:10 +00003167/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3168/// specifies a shuffle of elements that is suitable for input to MOVSS,
3169/// MOVSD, and MOVD, i.e. setting the lowest element.
Owen Andersone50ed302009-08-10 22:56:29 +00003170static bool isMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003171 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003172 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003173
3174 int NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003175
Nate Begeman9008ca62009-04-27 18:41:29 +00003176 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003177 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003178
Nate Begeman9008ca62009-04-27 18:41:29 +00003179 for (int i = 1; i < NumElts; ++i)
3180 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003181 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003182
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003183 return true;
3184}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003185
Nate Begeman9008ca62009-04-27 18:41:29 +00003186bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
3187 SmallVector<int, 8> M;
3188 N->getMask(M);
3189 return ::isMOVLMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003190}
3191
Evan Cheng017dcc62006-04-21 01:05:10 +00003192/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
3193/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003194/// element of vector 2 and the other elements to come from vector 1 in order.
Owen Andersone50ed302009-08-10 22:56:29 +00003195static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003196 bool V2IsSplat = false, bool V2IsUndef = false) {
3197 int NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003198 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003199 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003200
Nate Begeman9008ca62009-04-27 18:41:29 +00003201 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003202 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003203
Nate Begeman9008ca62009-04-27 18:41:29 +00003204 for (int i = 1; i < NumOps; ++i)
3205 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3206 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3207 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003208 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003209
Evan Cheng39623da2006-04-20 08:58:49 +00003210 return true;
3211}
3212
Nate Begeman9008ca62009-04-27 18:41:29 +00003213static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00003214 bool V2IsUndef = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003215 SmallVector<int, 8> M;
3216 N->getMask(M);
3217 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00003218}
3219
Evan Chengd9539472006-04-14 21:59:03 +00003220/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3221/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003222bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N) {
3223 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003224 return false;
3225
3226 // Expect 1, 1, 3, 3
Rafael Espindola15684b22009-04-24 12:40:33 +00003227 for (unsigned i = 0; i < 2; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003228 int Elt = N->getMaskElt(i);
3229 if (Elt >= 0 && Elt != 1)
3230 return false;
Rafael Espindola15684b22009-04-24 12:40:33 +00003231 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003232
3233 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003234 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003235 int Elt = N->getMaskElt(i);
3236 if (Elt >= 0 && Elt != 3)
3237 return false;
3238 if (Elt == 3)
3239 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003240 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003241 // Don't use movshdup if it can be done with a shufps.
Nate Begeman9008ca62009-04-27 18:41:29 +00003242 // FIXME: verify that matching u, u, 3, 3 is what we want.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003243 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003244}
3245
3246/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3247/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003248bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N) {
3249 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003250 return false;
3251
3252 // Expect 0, 0, 2, 2
Nate Begeman9008ca62009-04-27 18:41:29 +00003253 for (unsigned i = 0; i < 2; ++i)
3254 if (N->getMaskElt(i) > 0)
3255 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003256
3257 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003258 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003259 int Elt = N->getMaskElt(i);
3260 if (Elt >= 0 && Elt != 2)
3261 return false;
3262 if (Elt == 2)
3263 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003264 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003265 // Don't use movsldup if it can be done with a shufps.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003266 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003267}
3268
Evan Cheng0b457f02008-09-25 20:50:48 +00003269/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3270/// specifies a shuffle of elements that is suitable for input to MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003271bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
3272 int e = N->getValueType(0).getVectorNumElements() / 2;
Eric Christopherfd179292009-08-27 18:07:15 +00003273
Nate Begeman9008ca62009-04-27 18:41:29 +00003274 for (int i = 0; i < e; ++i)
3275 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003276 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003277 for (int i = 0; i < e; ++i)
3278 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003279 return false;
3280 return true;
3281}
3282
David Greenec38a03e2011-02-03 15:50:00 +00003283/// isVEXTRACTF128Index - Return true if the specified
3284/// EXTRACT_SUBVECTOR operand specifies a vector extract that is
3285/// suitable for input to VEXTRACTF128.
3286bool X86::isVEXTRACTF128Index(SDNode *N) {
3287 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
3288 return false;
3289
3290 // The index should be aligned on a 128-bit boundary.
3291 uint64_t Index =
3292 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
3293
3294 unsigned VL = N->getValueType(0).getVectorNumElements();
3295 unsigned VBits = N->getValueType(0).getSizeInBits();
3296 unsigned ElSize = VBits / VL;
3297 bool Result = (Index * ElSize) % 128 == 0;
3298
3299 return Result;
3300}
3301
David Greeneccacdc12011-02-04 16:08:29 +00003302/// isVINSERTF128Index - Return true if the specified INSERT_SUBVECTOR
3303/// operand specifies a subvector insert that is suitable for input to
3304/// VINSERTF128.
3305bool X86::isVINSERTF128Index(SDNode *N) {
3306 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
3307 return false;
3308
3309 // The index should be aligned on a 128-bit boundary.
3310 uint64_t Index =
3311 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
3312
3313 unsigned VL = N->getValueType(0).getVectorNumElements();
3314 unsigned VBits = N->getValueType(0).getSizeInBits();
3315 unsigned ElSize = VBits / VL;
3316 bool Result = (Index * ElSize) % 128 == 0;
3317
3318 return Result;
3319}
3320
Evan Cheng63d33002006-03-22 08:01:21 +00003321/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003322/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Evan Cheng63d33002006-03-22 08:01:21 +00003323unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003324 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3325 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
3326
Evan Chengb9df0ca2006-03-22 02:53:00 +00003327 unsigned Shift = (NumOperands == 4) ? 2 : 1;
3328 unsigned Mask = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00003329 for (int i = 0; i < NumOperands; ++i) {
3330 int Val = SVOp->getMaskElt(NumOperands-i-1);
3331 if (Val < 0) Val = 0;
Evan Cheng14aed5e2006-03-24 01:18:28 +00003332 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00003333 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00003334 if (i != NumOperands - 1)
3335 Mask <<= Shift;
3336 }
Evan Cheng63d33002006-03-22 08:01:21 +00003337 return Mask;
3338}
3339
Evan Cheng506d3df2006-03-29 23:07:14 +00003340/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003341/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003342unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003343 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003344 unsigned Mask = 0;
3345 // 8 nodes, but we only care about the last 4.
3346 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003347 int Val = SVOp->getMaskElt(i);
3348 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00003349 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00003350 if (i != 4)
3351 Mask <<= 2;
3352 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003353 return Mask;
3354}
3355
3356/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003357/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003358unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003359 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003360 unsigned Mask = 0;
3361 // 8 nodes, but we only care about the first 4.
3362 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003363 int Val = SVOp->getMaskElt(i);
3364 if (Val >= 0)
3365 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00003366 if (i != 0)
3367 Mask <<= 2;
3368 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003369 return Mask;
3370}
3371
Nate Begemana09008b2009-10-19 02:17:23 +00003372/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
3373/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
3374unsigned X86::getShufflePALIGNRImmediate(SDNode *N) {
3375 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3376 EVT VVT = N->getValueType(0);
3377 unsigned EltSize = VVT.getVectorElementType().getSizeInBits() >> 3;
3378 int Val = 0;
3379
3380 unsigned i, e;
3381 for (i = 0, e = VVT.getVectorNumElements(); i != e; ++i) {
3382 Val = SVOp->getMaskElt(i);
3383 if (Val >= 0)
3384 break;
3385 }
3386 return (Val - i) * EltSize;
3387}
3388
David Greenec38a03e2011-02-03 15:50:00 +00003389/// getExtractVEXTRACTF128Immediate - Return the appropriate immediate
3390/// to extract the specified EXTRACT_SUBVECTOR index with VEXTRACTF128
3391/// instructions.
3392unsigned X86::getExtractVEXTRACTF128Immediate(SDNode *N) {
3393 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
3394 llvm_unreachable("Illegal extract subvector for VEXTRACTF128");
3395
3396 uint64_t Index =
3397 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
3398
3399 EVT VecVT = N->getOperand(0).getValueType();
3400 EVT ElVT = VecVT.getVectorElementType();
3401
3402 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
3403
3404 return Index / NumElemsPerChunk;
3405}
3406
David Greeneccacdc12011-02-04 16:08:29 +00003407/// getInsertVINSERTF128Immediate - Return the appropriate immediate
3408/// to insert at the specified INSERT_SUBVECTOR index with VINSERTF128
3409/// instructions.
3410unsigned X86::getInsertVINSERTF128Immediate(SDNode *N) {
3411 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
3412 llvm_unreachable("Illegal insert subvector for VINSERTF128");
3413
3414 uint64_t Index =
NAKAMURA Takumi27635382011-02-05 15:10:54 +00003415 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
David Greeneccacdc12011-02-04 16:08:29 +00003416
3417 EVT VecVT = N->getValueType(0);
3418 EVT ElVT = VecVT.getVectorElementType();
3419
3420 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
3421
3422 return Index / NumElemsPerChunk;
3423}
3424
Evan Cheng37b73872009-07-30 08:33:02 +00003425/// isZeroNode - Returns true if Elt is a constant zero or a floating point
3426/// constant +0.0.
3427bool X86::isZeroNode(SDValue Elt) {
3428 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmane368b462010-06-18 14:22:04 +00003429 cast<ConstantSDNode>(Elt)->isNullValue()) ||
Evan Cheng37b73872009-07-30 08:33:02 +00003430 (isa<ConstantFPSDNode>(Elt) &&
3431 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
3432}
3433
Nate Begeman9008ca62009-04-27 18:41:29 +00003434/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
3435/// their permute mask.
3436static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
3437 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003438 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003439 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00003440 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00003441
Nate Begeman5a5ca152009-04-29 05:20:52 +00003442 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003443 int idx = SVOp->getMaskElt(i);
3444 if (idx < 0)
3445 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003446 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003447 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003448 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003449 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003450 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003451 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
3452 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003453}
3454
Evan Cheng779ccea2007-12-07 21:30:01 +00003455/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3456/// the two vector operands have swapped position.
Owen Andersone50ed302009-08-10 22:56:29 +00003457static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00003458 unsigned NumElems = VT.getVectorNumElements();
3459 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003460 int idx = Mask[i];
3461 if (idx < 0)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003462 continue;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003463 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003464 Mask[i] = idx + NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003465 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003466 Mask[i] = idx - NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003467 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003468}
3469
Evan Cheng533a0aa2006-04-19 20:35:22 +00003470/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
3471/// match movhlps. The lower half elements should come from upper half of
3472/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003473/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00003474static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
3475 if (Op->getValueType(0).getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00003476 return false;
3477 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003478 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003479 return false;
3480 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003481 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003482 return false;
3483 return true;
3484}
3485
Evan Cheng5ced1d82006-04-06 23:23:56 +00003486/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00003487/// is promoted to a vector. It also returns the LoadSDNode by reference if
3488/// required.
3489static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00003490 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
3491 return false;
3492 N = N->getOperand(0).getNode();
3493 if (!ISD::isNON_EXTLoad(N))
3494 return false;
3495 if (LD)
3496 *LD = cast<LoadSDNode>(N);
3497 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003498}
3499
Evan Cheng533a0aa2006-04-19 20:35:22 +00003500/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
3501/// match movlp{s|d}. The lower half elements should come from lower half of
3502/// V1 (and in order), and the upper half elements should come from the upper
3503/// half of V2 (and in order). And since V1 will become the source of the
3504/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003505static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
3506 ShuffleVectorSDNode *Op) {
Evan Cheng466685d2006-10-09 20:57:25 +00003507 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003508 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00003509 // Is V2 is a vector load, don't do this transformation. We will try to use
3510 // load folding shufps op.
3511 if (ISD::isNON_EXTLoad(V2))
3512 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003513
Nate Begeman5a5ca152009-04-29 05:20:52 +00003514 unsigned NumElems = Op->getValueType(0).getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003515
Evan Cheng533a0aa2006-04-19 20:35:22 +00003516 if (NumElems != 2 && NumElems != 4)
3517 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003518 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003519 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003520 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003521 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003522 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003523 return false;
3524 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003525}
3526
Evan Cheng39623da2006-04-20 08:58:49 +00003527/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
3528/// all the same.
3529static bool isSplatVector(SDNode *N) {
3530 if (N->getOpcode() != ISD::BUILD_VECTOR)
3531 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003532
Dan Gohman475871a2008-07-27 21:46:04 +00003533 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00003534 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
3535 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003536 return false;
3537 return true;
3538}
3539
Evan Cheng213d2cf2007-05-17 18:45:50 +00003540/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00003541/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00003542/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00003543static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00003544 SDValue V1 = N->getOperand(0);
3545 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003546 unsigned NumElems = N->getValueType(0).getVectorNumElements();
3547 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003548 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003549 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003550 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00003551 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
3552 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003553 if (Opc != ISD::BUILD_VECTOR ||
3554 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00003555 return false;
3556 } else if (Idx >= 0) {
3557 unsigned Opc = V1.getOpcode();
3558 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
3559 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003560 if (Opc != ISD::BUILD_VECTOR ||
3561 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00003562 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00003563 }
3564 }
3565 return true;
3566}
3567
3568/// getZeroVector - Returns a vector of specified type with all zero elements.
3569///
Owen Andersone50ed302009-08-10 22:56:29 +00003570static SDValue getZeroVector(EVT VT, bool HasSSE2, SelectionDAG &DAG,
Dale Johannesenace16102009-02-03 19:33:06 +00003571 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003572 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003573
Dale Johannesen0488fb62010-09-30 23:57:10 +00003574 // Always build SSE zero vectors as <4 x i32> bitcasted
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003575 // to their dest type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00003576 SDValue Vec;
Dale Johannesen0488fb62010-09-30 23:57:10 +00003577 if (VT.getSizeInBits() == 128) { // SSE
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003578 if (HasSSE2) { // SSE2
3579 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3580 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
3581 } else { // SSE1
3582 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
3583 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
3584 }
3585 } else if (VT.getSizeInBits() == 256) { // AVX
3586 // 256-bit logic and arithmetic instructions in AVX are
3587 // all floating-point, no support for integer ops. Default
3588 // to emitting fp zeroed vectors then.
Owen Anderson825b72b2009-08-11 20:47:22 +00003589 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003590 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
3591 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops, 8);
Evan Chengf0df0312008-05-15 08:39:06 +00003592 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003593 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00003594}
3595
Chris Lattner8a594482007-11-25 00:24:49 +00003596/// getOnesVector - Returns a vector of specified type with all bits set.
3597///
Owen Andersone50ed302009-08-10 22:56:29 +00003598static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003599 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003600
Chris Lattner8a594482007-11-25 00:24:49 +00003601 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
3602 // type. This ensures they get CSE'd.
Owen Anderson825b72b2009-08-11 20:47:22 +00003603 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00003604 SDValue Vec;
Dale Johannesen0488fb62010-09-30 23:57:10 +00003605 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003606 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00003607}
3608
3609
Evan Cheng39623da2006-04-20 08:58:49 +00003610/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
3611/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00003612static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003613 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003614 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003615
Evan Cheng39623da2006-04-20 08:58:49 +00003616 bool Changed = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003617 SmallVector<int, 8> MaskVec;
3618 SVOp->getMask(MaskVec);
Eric Christopherfd179292009-08-27 18:07:15 +00003619
Nate Begeman5a5ca152009-04-29 05:20:52 +00003620 for (unsigned i = 0; i != NumElems; ++i) {
3621 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003622 MaskVec[i] = NumElems;
3623 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00003624 }
Evan Cheng39623da2006-04-20 08:58:49 +00003625 }
Evan Cheng39623da2006-04-20 08:58:49 +00003626 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00003627 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
3628 SVOp->getOperand(1), &MaskVec[0]);
3629 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00003630}
3631
Evan Cheng017dcc62006-04-21 01:05:10 +00003632/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
3633/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00003634static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003635 SDValue V2) {
3636 unsigned NumElems = VT.getVectorNumElements();
3637 SmallVector<int, 8> Mask;
3638 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00003639 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003640 Mask.push_back(i);
3641 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00003642}
3643
Nate Begeman9008ca62009-04-27 18:41:29 +00003644/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003645static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003646 SDValue V2) {
3647 unsigned NumElems = VT.getVectorNumElements();
3648 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00003649 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003650 Mask.push_back(i);
3651 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00003652 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003653 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00003654}
3655
Nate Begeman9008ca62009-04-27 18:41:29 +00003656/// getUnpackhMask - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003657static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003658 SDValue V2) {
3659 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00003660 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00003661 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00003662 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003663 Mask.push_back(i + Half);
3664 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00003665 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003666 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00003667}
3668
Bruno Cardoso Lopesbb0a9482010-08-13 17:50:47 +00003669/// PromoteSplat - Promote a splat of v4i32, v8i16 or v16i8 to v4f32.
3670static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003671 EVT PVT = MVT::v4f32;
Owen Andersone50ed302009-08-10 22:56:29 +00003672 EVT VT = SV->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00003673 DebugLoc dl = SV->getDebugLoc();
3674 SDValue V1 = SV->getOperand(0);
3675 int NumElems = VT.getVectorNumElements();
3676 int EltNo = SV->getSplatIndex();
Rafael Espindola15684b22009-04-24 12:40:33 +00003677
Nate Begeman9008ca62009-04-27 18:41:29 +00003678 // unpack elements to the correct location
3679 while (NumElems > 4) {
3680 if (EltNo < NumElems/2) {
3681 V1 = getUnpackl(DAG, dl, VT, V1, V1);
3682 } else {
3683 V1 = getUnpackh(DAG, dl, VT, V1, V1);
3684 EltNo -= NumElems/2;
3685 }
3686 NumElems >>= 1;
3687 }
Eric Christopherfd179292009-08-27 18:07:15 +00003688
Nate Begeman9008ca62009-04-27 18:41:29 +00003689 // Perform the splat.
3690 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003691 V1 = DAG.getNode(ISD::BITCAST, dl, PVT, V1);
Nate Begeman9008ca62009-04-27 18:41:29 +00003692 V1 = DAG.getVectorShuffle(PVT, dl, V1, DAG.getUNDEF(PVT), &SplatMask[0]);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003693 return DAG.getNode(ISD::BITCAST, dl, VT, V1);
Evan Chengc575ca22006-04-17 20:43:08 +00003694}
3695
Evan Chengba05f722006-04-21 23:03:30 +00003696/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00003697/// vector of zero or undef vector. This produces a shuffle where the low
3698/// element of V2 is swizzled into the zero/undef vector, landing at element
3699/// 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 +00003700static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Chengf0df0312008-05-15 08:39:06 +00003701 bool isZero, bool HasSSE2,
3702 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003703 EVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00003704 SDValue V1 = isZero
Nate Begeman9008ca62009-04-27 18:41:29 +00003705 ? getZeroVector(VT, HasSSE2, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
3706 unsigned NumElems = VT.getVectorNumElements();
3707 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00003708 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003709 // If this is the insertion idx, put the low elt of V2 here.
3710 MaskVec.push_back(i == Idx ? NumElems : i);
3711 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00003712}
3713
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003714/// getShuffleScalarElt - Returns the scalar element that will make up the ith
3715/// element of the result of the vector shuffle.
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003716SDValue getShuffleScalarElt(SDNode *N, int Index, SelectionDAG &DAG,
3717 unsigned Depth) {
3718 if (Depth == 6)
3719 return SDValue(); // Limit search depth.
3720
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003721 SDValue V = SDValue(N, 0);
3722 EVT VT = V.getValueType();
3723 unsigned Opcode = V.getOpcode();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003724
3725 // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
3726 if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
3727 Index = SV->getMaskElt(Index);
3728
3729 if (Index < 0)
3730 return DAG.getUNDEF(VT.getVectorElementType());
3731
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003732 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003733 SDValue NewV = (Index < NumElems) ? SV->getOperand(0) : SV->getOperand(1);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003734 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG, Depth+1);
Evan Chengf26ffe92008-05-29 08:22:04 +00003735 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003736
3737 // Recurse into target specific vector shuffles to find scalars.
3738 if (isTargetShuffle(Opcode)) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003739 int NumElems = VT.getVectorNumElements();
3740 SmallVector<unsigned, 16> ShuffleMask;
3741 SDValue ImmN;
3742
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003743 switch(Opcode) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003744 case X86ISD::SHUFPS:
3745 case X86ISD::SHUFPD:
3746 ImmN = N->getOperand(N->getNumOperands()-1);
3747 DecodeSHUFPSMask(NumElems,
3748 cast<ConstantSDNode>(ImmN)->getZExtValue(),
3749 ShuffleMask);
3750 break;
3751 case X86ISD::PUNPCKHBW:
3752 case X86ISD::PUNPCKHWD:
3753 case X86ISD::PUNPCKHDQ:
3754 case X86ISD::PUNPCKHQDQ:
3755 DecodePUNPCKHMask(NumElems, ShuffleMask);
3756 break;
3757 case X86ISD::UNPCKHPS:
3758 case X86ISD::UNPCKHPD:
3759 DecodeUNPCKHPMask(NumElems, ShuffleMask);
3760 break;
3761 case X86ISD::PUNPCKLBW:
3762 case X86ISD::PUNPCKLWD:
3763 case X86ISD::PUNPCKLDQ:
3764 case X86ISD::PUNPCKLQDQ:
3765 DecodePUNPCKLMask(NumElems, ShuffleMask);
3766 break;
3767 case X86ISD::UNPCKLPS:
3768 case X86ISD::UNPCKLPD:
3769 DecodeUNPCKLPMask(NumElems, ShuffleMask);
3770 break;
3771 case X86ISD::MOVHLPS:
3772 DecodeMOVHLPSMask(NumElems, ShuffleMask);
3773 break;
3774 case X86ISD::MOVLHPS:
3775 DecodeMOVLHPSMask(NumElems, ShuffleMask);
3776 break;
3777 case X86ISD::PSHUFD:
3778 ImmN = N->getOperand(N->getNumOperands()-1);
3779 DecodePSHUFMask(NumElems,
3780 cast<ConstantSDNode>(ImmN)->getZExtValue(),
3781 ShuffleMask);
3782 break;
3783 case X86ISD::PSHUFHW:
3784 ImmN = N->getOperand(N->getNumOperands()-1);
3785 DecodePSHUFHWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
3786 ShuffleMask);
3787 break;
3788 case X86ISD::PSHUFLW:
3789 ImmN = N->getOperand(N->getNumOperands()-1);
3790 DecodePSHUFLWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
3791 ShuffleMask);
3792 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003793 case X86ISD::MOVSS:
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00003794 case X86ISD::MOVSD: {
3795 // The index 0 always comes from the first element of the second source,
3796 // this is why MOVSS and MOVSD are used in the first place. The other
3797 // elements come from the other positions of the first source vector.
3798 unsigned OpNum = (Index == 0) ? 1 : 0;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003799 return getShuffleScalarElt(V.getOperand(OpNum).getNode(), Index, DAG,
3800 Depth+1);
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00003801 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003802 default:
3803 assert("not implemented for target shuffle node");
3804 return SDValue();
3805 }
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003806
3807 Index = ShuffleMask[Index];
3808 if (Index < 0)
3809 return DAG.getUNDEF(VT.getVectorElementType());
3810
3811 SDValue NewV = (Index < NumElems) ? N->getOperand(0) : N->getOperand(1);
3812 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG,
3813 Depth+1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003814 }
3815
3816 // Actual nodes that may contain scalar elements
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003817 if (Opcode == ISD::BITCAST) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003818 V = V.getOperand(0);
3819 EVT SrcVT = V.getValueType();
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003820 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003821
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003822 if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003823 return SDValue();
3824 }
3825
3826 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
3827 return (Index == 0) ? V.getOperand(0)
3828 : DAG.getUNDEF(VT.getVectorElementType());
3829
3830 if (V.getOpcode() == ISD::BUILD_VECTOR)
3831 return V.getOperand(Index);
3832
3833 return SDValue();
3834}
3835
3836/// getNumOfConsecutiveZeros - Return the number of elements of a vector
3837/// shuffle operation which come from a consecutively from a zero. The
3838/// search can start in two diferent directions, from left or right.
3839static
3840unsigned getNumOfConsecutiveZeros(SDNode *N, int NumElems,
3841 bool ZerosFromLeft, SelectionDAG &DAG) {
3842 int i = 0;
3843
3844 while (i < NumElems) {
3845 unsigned Index = ZerosFromLeft ? i : NumElems-i-1;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003846 SDValue Elt = getShuffleScalarElt(N, Index, DAG, 0);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003847 if (!(Elt.getNode() &&
3848 (Elt.getOpcode() == ISD::UNDEF || X86::isZeroNode(Elt))))
3849 break;
3850 ++i;
3851 }
3852
3853 return i;
3854}
3855
3856/// isShuffleMaskConsecutive - Check if the shuffle mask indicies from MaskI to
3857/// MaskE correspond consecutively to elements from one of the vector operands,
3858/// starting from its index OpIdx. Also tell OpNum which source vector operand.
3859static
3860bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp, int MaskI, int MaskE,
3861 int OpIdx, int NumElems, unsigned &OpNum) {
3862 bool SeenV1 = false;
3863 bool SeenV2 = false;
3864
3865 for (int i = MaskI; i <= MaskE; ++i, ++OpIdx) {
3866 int Idx = SVOp->getMaskElt(i);
3867 // Ignore undef indicies
3868 if (Idx < 0)
3869 continue;
3870
3871 if (Idx < NumElems)
3872 SeenV1 = true;
3873 else
3874 SeenV2 = true;
3875
3876 // Only accept consecutive elements from the same vector
3877 if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
3878 return false;
3879 }
3880
3881 OpNum = SeenV1 ? 0 : 1;
3882 return true;
3883}
3884
3885/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
3886/// logical left shift of a vector.
3887static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
3888 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
3889 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
3890 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
3891 false /* check zeros from right */, DAG);
3892 unsigned OpSrc;
3893
3894 if (!NumZeros)
3895 return false;
3896
3897 // Considering the elements in the mask that are not consecutive zeros,
3898 // check if they consecutively come from only one of the source vectors.
3899 //
3900 // V1 = {X, A, B, C} 0
3901 // \ \ \ /
3902 // vector_shuffle V1, V2 <1, 2, 3, X>
3903 //
3904 if (!isShuffleMaskConsecutive(SVOp,
3905 0, // Mask Start Index
3906 NumElems-NumZeros-1, // Mask End Index
3907 NumZeros, // Where to start looking in the src vector
3908 NumElems, // Number of elements in vector
3909 OpSrc)) // Which source operand ?
3910 return false;
3911
3912 isLeft = false;
3913 ShAmt = NumZeros;
3914 ShVal = SVOp->getOperand(OpSrc);
3915 return true;
3916}
3917
3918/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
3919/// logical left shift of a vector.
3920static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
3921 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
3922 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
3923 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
3924 true /* check zeros from left */, DAG);
3925 unsigned OpSrc;
3926
3927 if (!NumZeros)
3928 return false;
3929
3930 // Considering the elements in the mask that are not consecutive zeros,
3931 // check if they consecutively come from only one of the source vectors.
3932 //
3933 // 0 { A, B, X, X } = V2
3934 // / \ / /
3935 // vector_shuffle V1, V2 <X, X, 4, 5>
3936 //
3937 if (!isShuffleMaskConsecutive(SVOp,
3938 NumZeros, // Mask Start Index
3939 NumElems-1, // Mask End Index
3940 0, // Where to start looking in the src vector
3941 NumElems, // Number of elements in vector
3942 OpSrc)) // Which source operand ?
3943 return false;
3944
3945 isLeft = true;
3946 ShAmt = NumZeros;
3947 ShVal = SVOp->getOperand(OpSrc);
3948 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00003949}
3950
3951/// isVectorShift - Returns true if the shuffle can be implemented as a
3952/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003953static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00003954 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003955 if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
3956 isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
3957 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00003958
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003959 return false;
Evan Chengf26ffe92008-05-29 08:22:04 +00003960}
3961
Evan Chengc78d3b42006-04-24 18:01:45 +00003962/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
3963///
Dan Gohman475871a2008-07-27 21:46:04 +00003964static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00003965 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00003966 SelectionDAG &DAG,
3967 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003968 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00003969 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00003970
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003971 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003972 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003973 bool First = true;
3974 for (unsigned i = 0; i < 16; ++i) {
3975 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
3976 if (ThisIsNonZero && First) {
3977 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003978 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00003979 else
Owen Anderson825b72b2009-08-11 20:47:22 +00003980 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00003981 First = false;
3982 }
3983
3984 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00003985 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003986 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
3987 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003988 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003989 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00003990 }
3991 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003992 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
3993 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
3994 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00003995 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003996 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00003997 } else
3998 ThisElt = LastElt;
3999
Gabor Greifba36cb52008-08-28 21:40:38 +00004000 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00004001 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00004002 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00004003 }
4004 }
4005
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004006 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00004007}
4008
Bill Wendlinga348c562007-03-22 18:42:45 +00004009/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00004010///
Dan Gohman475871a2008-07-27 21:46:04 +00004011static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00004012 unsigned NumNonZero, unsigned NumZero,
4013 SelectionDAG &DAG,
4014 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004015 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00004016 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004017
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004018 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004019 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004020 bool First = true;
4021 for (unsigned i = 0; i < 8; ++i) {
4022 bool isNonZero = (NonZeros & (1 << i)) != 0;
4023 if (isNonZero) {
4024 if (First) {
4025 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004026 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004027 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004028 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004029 First = false;
4030 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004031 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004032 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00004033 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00004034 }
4035 }
4036
4037 return V;
4038}
4039
Evan Chengf26ffe92008-05-29 08:22:04 +00004040/// getVShift - Return a vector logical shift node.
4041///
Owen Andersone50ed302009-08-10 22:56:29 +00004042static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00004043 unsigned NumBits, SelectionDAG &DAG,
4044 const TargetLowering &TLI, DebugLoc dl) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004045 EVT ShVT = MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00004046 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004047 SrcOp = DAG.getNode(ISD::BITCAST, dl, ShVT, SrcOp);
4048 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004049 DAG.getNode(Opc, dl, ShVT, SrcOp,
Gabor Greif327ef032008-08-28 23:19:51 +00004050 DAG.getConstant(NumBits, TLI.getShiftAmountTy())));
Evan Chengf26ffe92008-05-29 08:22:04 +00004051}
4052
Dan Gohman475871a2008-07-27 21:46:04 +00004053SDValue
Evan Chengc3630942009-12-09 21:00:30 +00004054X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00004055 SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00004056
Evan Chengc3630942009-12-09 21:00:30 +00004057 // Check if the scalar load can be widened into a vector load. And if
4058 // the address is "base + cst" see if the cst can be "absorbed" into
4059 // the shuffle mask.
4060 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
4061 SDValue Ptr = LD->getBasePtr();
4062 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
4063 return SDValue();
4064 EVT PVT = LD->getValueType(0);
4065 if (PVT != MVT::i32 && PVT != MVT::f32)
4066 return SDValue();
4067
4068 int FI = -1;
4069 int64_t Offset = 0;
4070 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
4071 FI = FINode->getIndex();
4072 Offset = 0;
4073 } else if (Ptr.getOpcode() == ISD::ADD &&
4074 isa<ConstantSDNode>(Ptr.getOperand(1)) &&
4075 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4076 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4077 Offset = Ptr.getConstantOperandVal(1);
4078 Ptr = Ptr.getOperand(0);
4079 } else {
4080 return SDValue();
4081 }
4082
4083 SDValue Chain = LD->getChain();
4084 // Make sure the stack object alignment is at least 16.
4085 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
4086 if (DAG.InferPtrAlignment(Ptr) < 16) {
4087 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00004088 // Can't change the alignment. FIXME: It's possible to compute
4089 // the exact stack offset and reference FI + adjust offset instead.
4090 // If someone *really* cares about this. That's the way to implement it.
4091 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004092 } else {
4093 MFI->setObjectAlignment(FI, 16);
4094 }
4095 }
4096
4097 // (Offset % 16) must be multiple of 4. Then address is then
4098 // Ptr + (Offset & ~15).
4099 if (Offset < 0)
4100 return SDValue();
4101 if ((Offset % 16) & 3)
4102 return SDValue();
4103 int64_t StartOffset = Offset & ~15;
4104 if (StartOffset)
4105 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
4106 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
4107
4108 int EltNo = (Offset - StartOffset) >> 2;
4109 int Mask[4] = { EltNo, EltNo, EltNo, EltNo };
4110 EVT VT = (PVT == MVT::i32) ? MVT::v4i32 : MVT::v4f32;
Chris Lattner51abfe42010-09-21 06:02:19 +00004111 SDValue V1 = DAG.getLoad(VT, dl, Chain, Ptr,
4112 LD->getPointerInfo().getWithOffset(StartOffset),
David Greene67c9d422010-02-15 16:53:33 +00004113 false, false, 0);
Evan Chengc3630942009-12-09 21:00:30 +00004114 // Canonicalize it to a v4i32 shuffle.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004115 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, V1);
4116 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Chengc3630942009-12-09 21:00:30 +00004117 DAG.getVectorShuffle(MVT::v4i32, dl, V1,
Chris Lattner51abfe42010-09-21 06:02:19 +00004118 DAG.getUNDEF(MVT::v4i32),&Mask[0]));
Evan Chengc3630942009-12-09 21:00:30 +00004119 }
4120
4121 return SDValue();
4122}
4123
Michael J. Spencerec38de22010-10-10 22:04:20 +00004124/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
4125/// vector of type 'VT', see if the elements can be replaced by a single large
Nate Begeman1449f292010-03-24 22:19:06 +00004126/// load which has the same value as a build_vector whose operands are 'elts'.
4127///
4128/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
Michael J. Spencerec38de22010-10-10 22:04:20 +00004129///
Nate Begeman1449f292010-03-24 22:19:06 +00004130/// FIXME: we'd also like to handle the case where the last elements are zero
4131/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
4132/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004133static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
Chris Lattner88641552010-09-22 00:34:38 +00004134 DebugLoc &DL, SelectionDAG &DAG) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00004135 EVT EltVT = VT.getVectorElementType();
4136 unsigned NumElems = Elts.size();
Michael J. Spencerec38de22010-10-10 22:04:20 +00004137
Nate Begemanfdea31a2010-03-24 20:49:50 +00004138 LoadSDNode *LDBase = NULL;
4139 unsigned LastLoadedElt = -1U;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004140
Nate Begeman1449f292010-03-24 22:19:06 +00004141 // For each element in the initializer, see if we've found a load or an undef.
Michael J. Spencerec38de22010-10-10 22:04:20 +00004142 // If we don't find an initial load element, or later load elements are
Nate Begeman1449f292010-03-24 22:19:06 +00004143 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004144 for (unsigned i = 0; i < NumElems; ++i) {
4145 SDValue Elt = Elts[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00004146
Nate Begemanfdea31a2010-03-24 20:49:50 +00004147 if (!Elt.getNode() ||
4148 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
4149 return SDValue();
4150 if (!LDBase) {
4151 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
4152 return SDValue();
4153 LDBase = cast<LoadSDNode>(Elt.getNode());
4154 LastLoadedElt = i;
4155 continue;
4156 }
4157 if (Elt.getOpcode() == ISD::UNDEF)
4158 continue;
4159
4160 LoadSDNode *LD = cast<LoadSDNode>(Elt);
4161 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
4162 return SDValue();
4163 LastLoadedElt = i;
4164 }
Nate Begeman1449f292010-03-24 22:19:06 +00004165
4166 // If we have found an entire vector of loads and undefs, then return a large
4167 // load of the entire vector width starting at the base pointer. If we found
4168 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004169 if (LastLoadedElt == NumElems - 1) {
4170 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
Chris Lattner88641552010-09-22 00:34:38 +00004171 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004172 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004173 LDBase->isVolatile(), LDBase->isNonTemporal(), 0);
Chris Lattner88641552010-09-22 00:34:38 +00004174 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004175 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004176 LDBase->isVolatile(), LDBase->isNonTemporal(),
4177 LDBase->getAlignment());
4178 } else if (NumElems == 4 && LastLoadedElt == 1) {
4179 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
4180 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
Chris Lattner88641552010-09-22 00:34:38 +00004181 SDValue ResNode = DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys,
4182 Ops, 2, MVT::i32,
4183 LDBase->getMemOperand());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004184 return DAG.getNode(ISD::BITCAST, DL, VT, ResNode);
Nate Begemanfdea31a2010-03-24 20:49:50 +00004185 }
4186 return SDValue();
4187}
4188
Evan Chengc3630942009-12-09 21:00:30 +00004189SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004190X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004191 DebugLoc dl = Op.getDebugLoc();
Chris Lattner6e80e442010-08-28 17:15:43 +00004192 // All zero's are handled with pxor in SSE2 and above, xorps in SSE1.
4193 // All one's are handled with pcmpeqd. In AVX, zero's are handled with
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004194 // vpxor in 128-bit and xor{pd,ps} in 256-bit, but no 256 version of pcmpeqd
4195 // is present, so AllOnes is ignored.
4196 if (ISD::isBuildVectorAllZeros(Op.getNode()) ||
4197 (Op.getValueType().getSizeInBits() != 256 &&
4198 ISD::isBuildVectorAllOnes(Op.getNode()))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004199 // Canonicalize this to <4 x i32> (SSE) to
Chris Lattner8a594482007-11-25 00:24:49 +00004200 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
4201 // eliminated on x86-32 hosts.
Dale Johannesen0488fb62010-09-30 23:57:10 +00004202 if (Op.getValueType() == MVT::v4i32)
Chris Lattner8a594482007-11-25 00:24:49 +00004203 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004204
Gabor Greifba36cb52008-08-28 21:40:38 +00004205 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00004206 return getOnesVector(Op.getValueType(), DAG, dl);
4207 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00004208 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004209
Owen Andersone50ed302009-08-10 22:56:29 +00004210 EVT VT = Op.getValueType();
4211 EVT ExtVT = VT.getVectorElementType();
4212 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004213
4214 unsigned NumElems = Op.getNumOperands();
4215 unsigned NumZero = 0;
4216 unsigned NumNonZero = 0;
4217 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004218 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00004219 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004220 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00004221 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00004222 if (Elt.getOpcode() == ISD::UNDEF)
4223 continue;
4224 Values.insert(Elt);
4225 if (Elt.getOpcode() != ISD::Constant &&
4226 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004227 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00004228 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00004229 NumZero++;
4230 else {
4231 NonZeros |= (1 << i);
4232 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004233 }
4234 }
4235
Chris Lattner97a2a562010-08-26 05:24:29 +00004236 // All undef vector. Return an UNDEF. All zero vectors were handled above.
4237 if (NumNonZero == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00004238 return DAG.getUNDEF(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004239
Chris Lattner67f453a2008-03-09 05:42:06 +00004240 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00004241 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004242 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00004243 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00004244
Chris Lattner62098042008-03-09 01:05:04 +00004245 // If this is an insertion of an i64 value on x86-32, and if the top bits of
4246 // the value are obviously zero, truncate the value to i32 and do the
4247 // insertion that way. Only do this if the value is non-constant or if the
4248 // value is a constant being inserted into element 0. It is cheaper to do
4249 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00004250 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00004251 (!IsAllConstants || Idx == 0)) {
4252 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004253 // Handle SSE only.
4254 assert(VT == MVT::v2i64 && "Expected an SSE value type!");
4255 EVT VecVT = MVT::v4i32;
4256 unsigned VecElts = 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00004257
Chris Lattner62098042008-03-09 01:05:04 +00004258 // Truncate the value (which may itself be a constant) to i32, and
4259 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00004260 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00004261 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00004262 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
4263 Subtarget->hasSSE2(), DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00004264
Chris Lattner62098042008-03-09 01:05:04 +00004265 // Now we have our 32-bit value zero extended in the low element of
4266 // a vector. If Idx != 0, swizzle it into place.
4267 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004268 SmallVector<int, 4> Mask;
4269 Mask.push_back(Idx);
4270 for (unsigned i = 1; i != VecElts; ++i)
4271 Mask.push_back(i);
4272 Item = DAG.getVectorShuffle(VecVT, dl, Item,
Eric Christopherfd179292009-08-27 18:07:15 +00004273 DAG.getUNDEF(Item.getValueType()),
Nate Begeman9008ca62009-04-27 18:41:29 +00004274 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00004275 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004276 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00004277 }
4278 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004279
Chris Lattner19f79692008-03-08 22:59:52 +00004280 // If we have a constant or non-constant insertion into the low element of
4281 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
4282 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00004283 // depending on what the source datatype is.
4284 if (Idx == 0) {
4285 if (NumZero == 0) {
4286 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Owen Anderson825b72b2009-08-11 20:47:22 +00004287 } else if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
4288 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Eli Friedman10415532009-06-06 06:05:10 +00004289 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
4290 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
4291 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget->hasSSE2(),
4292 DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00004293 } else if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
4294 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
Dale Johannesen0488fb62010-09-30 23:57:10 +00004295 assert(VT.getSizeInBits() == 128 && "Expected an SSE value type!");
4296 EVT MiddleVT = MVT::v4i32;
Eli Friedman10415532009-06-06 06:05:10 +00004297 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MiddleVT, Item);
4298 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
4299 Subtarget->hasSSE2(), DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004300 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Eli Friedman10415532009-06-06 06:05:10 +00004301 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004302 }
Evan Chengf26ffe92008-05-29 08:22:04 +00004303
4304 // Is it a vector logical left shift?
4305 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00004306 X86::isZeroNode(Op.getOperand(0)) &&
4307 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004308 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00004309 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00004310 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004311 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00004312 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004313 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004314
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004315 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00004316 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004317
Chris Lattner19f79692008-03-08 22:59:52 +00004318 // Otherwise, if this is a vector with i32 or f32 elements, and the element
4319 // is a non-constant being inserted into an element other than the low one,
4320 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
4321 // movd/movss) to move this into the low element, then shuffle it into
4322 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004323 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00004324 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00004325
Evan Cheng0db9fe62006-04-25 20:13:52 +00004326 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00004327 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
4328 Subtarget->hasSSE2(), DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00004329 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004330 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00004331 MaskVec.push_back(i == Idx ? 0 : 1);
4332 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004333 }
4334 }
4335
Chris Lattner67f453a2008-03-09 05:42:06 +00004336 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00004337 if (Values.size() == 1) {
4338 if (EVTBits == 32) {
4339 // Instead of a shuffle like this:
4340 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
4341 // Check if it's possible to issue this instead.
4342 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
4343 unsigned Idx = CountTrailingZeros_32(NonZeros);
4344 SDValue Item = Op.getOperand(Idx);
4345 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
4346 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
4347 }
Dan Gohman475871a2008-07-27 21:46:04 +00004348 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004349 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004350
Dan Gohmana3941172007-07-24 22:55:08 +00004351 // A vector full of immediates; various special cases are already
4352 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004353 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00004354 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00004355
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00004356 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00004357 if (EVTBits == 64) {
4358 if (NumNonZero == 1) {
4359 // One half is zero or undef.
4360 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00004361 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00004362 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00004363 return getShuffleVectorZeroOrUndef(V2, Idx, true,
4364 Subtarget->hasSSE2(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00004365 }
Dan Gohman475871a2008-07-27 21:46:04 +00004366 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00004367 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004368
4369 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00004370 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00004371 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00004372 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004373 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004374 }
4375
Bill Wendling826f36f2007-03-28 00:57:11 +00004376 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00004377 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Chris Lattner97a2a562010-08-26 05:24:29 +00004378 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004379 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004380 }
4381
4382 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00004383 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00004384 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004385 if (NumElems == 4 && NumZero > 0) {
4386 for (unsigned i = 0; i < 4; ++i) {
4387 bool isZero = !(NonZeros & (1 << i));
4388 if (isZero)
Dale Johannesenace16102009-02-03 19:33:06 +00004389 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004390 else
Dale Johannesenace16102009-02-03 19:33:06 +00004391 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004392 }
4393
4394 for (unsigned i = 0; i < 2; ++i) {
4395 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
4396 default: break;
4397 case 0:
4398 V[i] = V[i*2]; // Must be a zero vector.
4399 break;
4400 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00004401 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004402 break;
4403 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00004404 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004405 break;
4406 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00004407 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004408 break;
4409 }
4410 }
4411
Nate Begeman9008ca62009-04-27 18:41:29 +00004412 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004413 bool Reverse = (NonZeros & 0x3) == 2;
4414 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004415 MaskVec.push_back(Reverse ? 1-i : i);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004416 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
4417 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004418 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
4419 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004420 }
4421
Nate Begemanfdea31a2010-03-24 20:49:50 +00004422 if (Values.size() > 1 && VT.getSizeInBits() == 128) {
4423 // Check for a build vector of consecutive loads.
4424 for (unsigned i = 0; i < NumElems; ++i)
4425 V[i] = Op.getOperand(i);
Michael J. Spencerec38de22010-10-10 22:04:20 +00004426
Nate Begemanfdea31a2010-03-24 20:49:50 +00004427 // Check for elements which are consecutive loads.
4428 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
4429 if (LD.getNode())
4430 return LD;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004431
4432 // For SSE 4.1, use insertps to put the high elements into the low element.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004433 if (getSubtarget()->hasSSE41()) {
Chris Lattner24faf612010-08-28 17:59:08 +00004434 SDValue Result;
4435 if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
4436 Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
4437 else
4438 Result = DAG.getUNDEF(VT);
Michael J. Spencerec38de22010-10-10 22:04:20 +00004439
Chris Lattner24faf612010-08-28 17:59:08 +00004440 for (unsigned i = 1; i < NumElems; ++i) {
4441 if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
4442 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
Nate Begeman9008ca62009-04-27 18:41:29 +00004443 Op.getOperand(i), DAG.getIntPtrConstant(i));
Chris Lattner24faf612010-08-28 17:59:08 +00004444 }
4445 return Result;
Nate Begeman9008ca62009-04-27 18:41:29 +00004446 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00004447
Chris Lattner6e80e442010-08-28 17:15:43 +00004448 // Otherwise, expand into a number of unpckl*, start by extending each of
4449 // our (non-undef) elements to the full vector width with the element in the
4450 // bottom slot of the vector (which generates no code for SSE).
4451 for (unsigned i = 0; i < NumElems; ++i) {
4452 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
4453 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
4454 else
4455 V[i] = DAG.getUNDEF(VT);
4456 }
4457
4458 // Next, we iteratively mix elements, e.g. for v4f32:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004459 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
4460 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
4461 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Chris Lattner6e80e442010-08-28 17:15:43 +00004462 unsigned EltStride = NumElems >> 1;
4463 while (EltStride != 0) {
Chris Lattner3ddcc432010-08-28 17:28:30 +00004464 for (unsigned i = 0; i < EltStride; ++i) {
4465 // If V[i+EltStride] is undef and this is the first round of mixing,
4466 // then it is safe to just drop this shuffle: V[i] is already in the
4467 // right place, the one element (since it's the first round) being
4468 // inserted as undef can be dropped. This isn't safe for successive
4469 // rounds because they will permute elements within both vectors.
4470 if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
4471 EltStride == NumElems/2)
4472 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004473
Chris Lattner6e80e442010-08-28 17:15:43 +00004474 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
Chris Lattner3ddcc432010-08-28 17:28:30 +00004475 }
Chris Lattner6e80e442010-08-28 17:15:43 +00004476 EltStride >>= 1;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004477 }
4478 return V[0];
4479 }
Dan Gohman475871a2008-07-27 21:46:04 +00004480 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004481}
4482
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004483SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004484X86TargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const {
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004485 // We support concatenate two MMX registers and place them in a MMX
4486 // register. This is better than doing a stack convert.
4487 DebugLoc dl = Op.getDebugLoc();
4488 EVT ResVT = Op.getValueType();
4489 assert(Op.getNumOperands() == 2);
4490 assert(ResVT == MVT::v2i64 || ResVT == MVT::v4i32 ||
4491 ResVT == MVT::v8i16 || ResVT == MVT::v16i8);
4492 int Mask[2];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004493 SDValue InVec = DAG.getNode(ISD::BITCAST,dl, MVT::v1i64, Op.getOperand(0));
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004494 SDValue VecOp = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
4495 InVec = Op.getOperand(1);
4496 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4497 unsigned NumElts = ResVT.getVectorNumElements();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004498 VecOp = DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004499 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, ResVT, VecOp,
4500 InVec.getOperand(0), DAG.getIntPtrConstant(NumElts/2+1));
4501 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004502 InVec = DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, InVec);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004503 SDValue VecOp2 = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
4504 Mask[0] = 0; Mask[1] = 2;
4505 VecOp = DAG.getVectorShuffle(MVT::v2i64, dl, VecOp, VecOp2, Mask);
4506 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004507 return DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004508}
4509
Nate Begemanb9a47b82009-02-23 08:49:38 +00004510// v8i16 shuffles - Prefer shuffles in the following order:
4511// 1. [all] pshuflw, pshufhw, optional move
4512// 2. [ssse3] 1 x pshufb
4513// 3. [ssse3] 2 x pshufb + 1 x por
4514// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004515SDValue
4516X86TargetLowering::LowerVECTOR_SHUFFLEv8i16(SDValue Op,
4517 SelectionDAG &DAG) const {
4518 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00004519 SDValue V1 = SVOp->getOperand(0);
4520 SDValue V2 = SVOp->getOperand(1);
4521 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00004522 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00004523
Nate Begemanb9a47b82009-02-23 08:49:38 +00004524 // Determine if more than 1 of the words in each of the low and high quadwords
4525 // of the result come from the same quadword of one of the two inputs. Undef
4526 // mask values count as coming from any quadword, for better codegen.
4527 SmallVector<unsigned, 4> LoQuad(4);
4528 SmallVector<unsigned, 4> HiQuad(4);
4529 BitVector InputQuads(4);
4530 for (unsigned i = 0; i < 8; ++i) {
4531 SmallVectorImpl<unsigned> &Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00004532 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004533 MaskVals.push_back(EltIdx);
4534 if (EltIdx < 0) {
4535 ++Quad[0];
4536 ++Quad[1];
4537 ++Quad[2];
4538 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00004539 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004540 }
4541 ++Quad[EltIdx / 4];
4542 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00004543 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004544
Nate Begemanb9a47b82009-02-23 08:49:38 +00004545 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00004546 unsigned MaxQuad = 1;
4547 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004548 if (LoQuad[i] > MaxQuad) {
4549 BestLoQuad = i;
4550 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00004551 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004552 }
4553
Nate Begemanb9a47b82009-02-23 08:49:38 +00004554 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00004555 MaxQuad = 1;
4556 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004557 if (HiQuad[i] > MaxQuad) {
4558 BestHiQuad = i;
4559 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00004560 }
4561 }
4562
Nate Begemanb9a47b82009-02-23 08:49:38 +00004563 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00004564 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00004565 // single pshufb instruction is necessary. If There are more than 2 input
4566 // quads, disable the next transformation since it does not help SSSE3.
4567 bool V1Used = InputQuads[0] || InputQuads[1];
4568 bool V2Used = InputQuads[2] || InputQuads[3];
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004569 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004570 if (InputQuads.count() == 2 && V1Used && V2Used) {
4571 BestLoQuad = InputQuads.find_first();
4572 BestHiQuad = InputQuads.find_next(BestLoQuad);
4573 }
4574 if (InputQuads.count() > 2) {
4575 BestLoQuad = -1;
4576 BestHiQuad = -1;
4577 }
4578 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004579
Nate Begemanb9a47b82009-02-23 08:49:38 +00004580 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
4581 // the shuffle mask. If a quad is scored as -1, that means that it contains
4582 // words from all 4 input quadwords.
4583 SDValue NewV;
4584 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004585 SmallVector<int, 8> MaskV;
4586 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
4587 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
Eric Christopherfd179292009-08-27 18:07:15 +00004588 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004589 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V1),
4590 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V2), &MaskV[0]);
4591 NewV = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00004592
Nate Begemanb9a47b82009-02-23 08:49:38 +00004593 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
4594 // source words for the shuffle, to aid later transformations.
4595 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00004596 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00004597 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004598 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00004599 if (idx != (int)i)
4600 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004601 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00004602 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004603 AllWordsInNewV = false;
4604 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00004605 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004606
Nate Begemanb9a47b82009-02-23 08:49:38 +00004607 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
4608 if (AllWordsInNewV) {
4609 for (int i = 0; i != 8; ++i) {
4610 int idx = MaskVals[i];
4611 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00004612 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004613 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004614 if ((idx != i) && idx < 4)
4615 pshufhw = false;
4616 if ((idx != i) && idx > 3)
4617 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00004618 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00004619 V1 = NewV;
4620 V2Used = false;
4621 BestLoQuad = 0;
4622 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004623 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004624
Nate Begemanb9a47b82009-02-23 08:49:38 +00004625 // If we've eliminated the use of V2, and the new mask is a pshuflw or
4626 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00004627 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00004628 unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
4629 unsigned TargetMask = 0;
4630 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00004631 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00004632 TargetMask = pshufhw ? X86::getShufflePSHUFHWImmediate(NewV.getNode()):
4633 X86::getShufflePSHUFLWImmediate(NewV.getNode());
4634 V1 = NewV.getOperand(0);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00004635 return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
Evan Cheng14b32e12007-12-11 01:46:18 +00004636 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004637 }
Eric Christopherfd179292009-08-27 18:07:15 +00004638
Nate Begemanb9a47b82009-02-23 08:49:38 +00004639 // If we have SSSE3, and all words of the result are from 1 input vector,
4640 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
4641 // is present, fall back to case 4.
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004642 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004643 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00004644
Nate Begemanb9a47b82009-02-23 08:49:38 +00004645 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00004646 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00004647 // mask, and elements that come from V1 in the V2 mask, so that the two
4648 // results can be OR'd together.
4649 bool TwoInputs = V1Used && V2Used;
4650 for (unsigned i = 0; i != 8; ++i) {
4651 int EltIdx = MaskVals[i] * 2;
4652 if (TwoInputs && (EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004653 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4654 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004655 continue;
4656 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004657 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
4658 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004659 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004660 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004661 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00004662 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004663 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004664 if (!TwoInputs)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004665 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004666
Nate Begemanb9a47b82009-02-23 08:49:38 +00004667 // Calculate the shuffle mask for the second input, shuffle it, and
4668 // OR it with the first shuffled input.
4669 pshufbMask.clear();
4670 for (unsigned i = 0; i != 8; ++i) {
4671 int EltIdx = MaskVals[i] * 2;
4672 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004673 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4674 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004675 continue;
4676 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004677 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
4678 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004679 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004680 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00004681 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00004682 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004683 MVT::v16i8, &pshufbMask[0], 16));
4684 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004685 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004686 }
4687
4688 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
4689 // and update MaskVals with new element order.
4690 BitVector InOrder(8);
4691 if (BestLoQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004692 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004693 for (int i = 0; i != 4; ++i) {
4694 int idx = MaskVals[i];
4695 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004696 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004697 InOrder.set(i);
4698 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004699 MaskV.push_back(idx & 3);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004700 InOrder.set(i);
4701 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004702 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004703 }
4704 }
4705 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004706 MaskV.push_back(i);
Owen Anderson825b72b2009-08-11 20:47:22 +00004707 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00004708 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00004709
4710 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3())
4711 NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
4712 NewV.getOperand(0),
4713 X86::getShufflePSHUFLWImmediate(NewV.getNode()),
4714 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004715 }
Eric Christopherfd179292009-08-27 18:07:15 +00004716
Nate Begemanb9a47b82009-02-23 08:49:38 +00004717 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
4718 // and update MaskVals with the new element order.
4719 if (BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004720 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004721 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004722 MaskV.push_back(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004723 for (unsigned i = 4; i != 8; ++i) {
4724 int idx = MaskVals[i];
4725 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004726 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004727 InOrder.set(i);
4728 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004729 MaskV.push_back((idx & 3) + 4);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004730 InOrder.set(i);
4731 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004732 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004733 }
4734 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004735 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00004736 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00004737
4738 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3())
4739 NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
4740 NewV.getOperand(0),
4741 X86::getShufflePSHUFHWImmediate(NewV.getNode()),
4742 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004743 }
Eric Christopherfd179292009-08-27 18:07:15 +00004744
Nate Begemanb9a47b82009-02-23 08:49:38 +00004745 // In case BestHi & BestLo were both -1, which means each quadword has a word
4746 // from each of the four input quadwords, calculate the InOrder bitvector now
4747 // before falling through to the insert/extract cleanup.
4748 if (BestLoQuad == -1 && BestHiQuad == -1) {
4749 NewV = V1;
4750 for (int i = 0; i != 8; ++i)
4751 if (MaskVals[i] < 0 || MaskVals[i] == i)
4752 InOrder.set(i);
4753 }
Eric Christopherfd179292009-08-27 18:07:15 +00004754
Nate Begemanb9a47b82009-02-23 08:49:38 +00004755 // The other elements are put in the right place using pextrw and pinsrw.
4756 for (unsigned i = 0; i != 8; ++i) {
4757 if (InOrder[i])
4758 continue;
4759 int EltIdx = MaskVals[i];
4760 if (EltIdx < 0)
4761 continue;
4762 SDValue ExtOp = (EltIdx < 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00004763 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004764 DAG.getIntPtrConstant(EltIdx))
Owen Anderson825b72b2009-08-11 20:47:22 +00004765 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004766 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00004767 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004768 DAG.getIntPtrConstant(i));
4769 }
4770 return NewV;
4771}
4772
4773// v16i8 shuffles - Prefer shuffles in the following order:
4774// 1. [ssse3] 1 x pshufb
4775// 2. [ssse3] 2 x pshufb + 1 x por
4776// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
4777static
Nate Begeman9008ca62009-04-27 18:41:29 +00004778SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00004779 SelectionDAG &DAG,
4780 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004781 SDValue V1 = SVOp->getOperand(0);
4782 SDValue V2 = SVOp->getOperand(1);
4783 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00004784 SmallVector<int, 16> MaskVals;
Nate Begeman9008ca62009-04-27 18:41:29 +00004785 SVOp->getMask(MaskVals);
Eric Christopherfd179292009-08-27 18:07:15 +00004786
Nate Begemanb9a47b82009-02-23 08:49:38 +00004787 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00004788 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00004789 // present, fall back to case 3.
4790 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
4791 bool V1Only = true;
4792 bool V2Only = true;
4793 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004794 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00004795 if (EltIdx < 0)
4796 continue;
4797 if (EltIdx < 16)
4798 V2Only = false;
4799 else
4800 V1Only = false;
4801 }
Eric Christopherfd179292009-08-27 18:07:15 +00004802
Nate Begemanb9a47b82009-02-23 08:49:38 +00004803 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
4804 if (TLI.getSubtarget()->hasSSSE3()) {
4805 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00004806
Nate Begemanb9a47b82009-02-23 08:49:38 +00004807 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00004808 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00004809 //
4810 // Otherwise, we have elements from both input vectors, and must zero out
4811 // elements that come from V2 in the first mask, and V1 in the second mask
4812 // so that we can OR them together.
4813 bool TwoInputs = !(V1Only || V2Only);
4814 for (unsigned i = 0; i != 16; ++i) {
4815 int EltIdx = MaskVals[i];
4816 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004817 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004818 continue;
4819 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004820 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004821 }
4822 // If all the elements are from V2, assign it to V1 and return after
4823 // building the first pshufb.
4824 if (V2Only)
4825 V1 = V2;
Owen Anderson825b72b2009-08-11 20:47:22 +00004826 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00004827 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004828 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004829 if (!TwoInputs)
4830 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00004831
Nate Begemanb9a47b82009-02-23 08:49:38 +00004832 // Calculate the shuffle mask for the second input, shuffle it, and
4833 // OR it with the first shuffled input.
4834 pshufbMask.clear();
4835 for (unsigned i = 0; i != 16; ++i) {
4836 int EltIdx = MaskVals[i];
4837 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004838 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004839 continue;
4840 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004841 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004842 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004843 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00004844 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004845 MVT::v16i8, &pshufbMask[0], 16));
4846 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004847 }
Eric Christopherfd179292009-08-27 18:07:15 +00004848
Nate Begemanb9a47b82009-02-23 08:49:38 +00004849 // No SSSE3 - Calculate in place words and then fix all out of place words
4850 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
4851 // the 16 different words that comprise the two doublequadword input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004852 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
4853 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004854 SDValue NewV = V2Only ? V2 : V1;
4855 for (int i = 0; i != 8; ++i) {
4856 int Elt0 = MaskVals[i*2];
4857 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00004858
Nate Begemanb9a47b82009-02-23 08:49:38 +00004859 // This word of the result is all undef, skip it.
4860 if (Elt0 < 0 && Elt1 < 0)
4861 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004862
Nate Begemanb9a47b82009-02-23 08:49:38 +00004863 // This word of the result is already in the correct place, skip it.
4864 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
4865 continue;
4866 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
4867 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004868
Nate Begemanb9a47b82009-02-23 08:49:38 +00004869 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
4870 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
4871 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00004872
4873 // If Elt0 and Elt1 are defined, are consecutive, and can be load
4874 // using a single extract together, load it and store it.
4875 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004876 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00004877 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00004878 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00004879 DAG.getIntPtrConstant(i));
4880 continue;
4881 }
4882
Nate Begemanb9a47b82009-02-23 08:49:38 +00004883 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00004884 // source byte is not also odd, shift the extracted word left 8 bits
4885 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00004886 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004887 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004888 DAG.getIntPtrConstant(Elt1 / 2));
4889 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004890 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004891 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00004892 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004893 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
4894 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004895 }
4896 // If Elt0 is defined, extract it from the appropriate source. If the
4897 // source byte is not also even, shift the extracted word right 8 bits. If
4898 // Elt1 was also defined, OR the extracted values together before
4899 // inserting them in the result.
4900 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004901 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004902 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
4903 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004904 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004905 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00004906 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004907 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
4908 DAG.getConstant(0x00FF, MVT::i16));
4909 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00004910 : InsElt0;
4911 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004912 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004913 DAG.getIntPtrConstant(i));
4914 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004915 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00004916}
4917
Evan Cheng7a831ce2007-12-15 03:00:47 +00004918/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00004919/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00004920/// done when every pair / quad of shuffle mask elements point to elements in
4921/// the right sequence. e.g.
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00004922/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
Evan Cheng14b32e12007-12-11 01:46:18 +00004923static
Nate Begeman9008ca62009-04-27 18:41:29 +00004924SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00004925 SelectionDAG &DAG, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00004926 EVT VT = SVOp->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00004927 SDValue V1 = SVOp->getOperand(0);
4928 SDValue V2 = SVOp->getOperand(1);
4929 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00004930 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00004931 EVT NewVT;
Owen Anderson825b72b2009-08-11 20:47:22 +00004932 switch (VT.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004933 default: assert(false && "Unexpected!");
Owen Anderson825b72b2009-08-11 20:47:22 +00004934 case MVT::v4f32: NewVT = MVT::v2f64; break;
4935 case MVT::v4i32: NewVT = MVT::v2i64; break;
4936 case MVT::v8i16: NewVT = MVT::v4i32; break;
4937 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00004938 }
4939
Nate Begeman9008ca62009-04-27 18:41:29 +00004940 int Scale = NumElems / NewWidth;
4941 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00004942 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004943 int StartIdx = -1;
4944 for (int j = 0; j < Scale; ++j) {
4945 int EltIdx = SVOp->getMaskElt(i+j);
4946 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00004947 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00004948 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00004949 StartIdx = EltIdx - (EltIdx % Scale);
4950 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00004951 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00004952 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004953 if (StartIdx == -1)
4954 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00004955 else
Nate Begeman9008ca62009-04-27 18:41:29 +00004956 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004957 }
4958
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004959 V1 = DAG.getNode(ISD::BITCAST, dl, NewVT, V1);
4960 V2 = DAG.getNode(ISD::BITCAST, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00004961 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004962}
4963
Evan Chengd880b972008-05-09 21:53:03 +00004964/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00004965///
Owen Andersone50ed302009-08-10 22:56:29 +00004966static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00004967 SDValue SrcOp, SelectionDAG &DAG,
4968 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004969 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004970 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00004971 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00004972 LD = dyn_cast<LoadSDNode>(SrcOp);
4973 if (!LD) {
4974 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
4975 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00004976 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Duncan Sandscdfad362010-11-03 12:17:33 +00004977 if ((ExtVT != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00004978 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004979 SrcOp.getOperand(0).getOpcode() == ISD::BITCAST &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00004980 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004981 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00004982 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004983 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004984 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
4985 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
4986 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00004987 SrcOp.getOperand(0)
4988 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00004989 }
4990 }
4991 }
4992
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004993 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004994 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004995 DAG.getNode(ISD::BITCAST, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00004996 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00004997}
4998
Evan Chengace3c172008-07-22 21:13:36 +00004999/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
5000/// shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00005001static SDValue
Nate Begeman9008ca62009-04-27 18:41:29 +00005002LowerVECTOR_SHUFFLE_4wide(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
5003 SDValue V1 = SVOp->getOperand(0);
5004 SDValue V2 = SVOp->getOperand(1);
5005 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005006 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00005007
Evan Chengace3c172008-07-22 21:13:36 +00005008 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00005009 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00005010 SmallVector<int, 8> Mask1(4U, -1);
5011 SmallVector<int, 8> PermMask;
5012 SVOp->getMask(PermMask);
5013
Evan Chengace3c172008-07-22 21:13:36 +00005014 unsigned NumHi = 0;
5015 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00005016 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005017 int Idx = PermMask[i];
5018 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00005019 Locs[i] = std::make_pair(-1, -1);
5020 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005021 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
5022 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00005023 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00005024 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005025 NumLo++;
5026 } else {
5027 Locs[i] = std::make_pair(1, NumHi);
5028 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00005029 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005030 NumHi++;
5031 }
5032 }
5033 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005034
Evan Chengace3c172008-07-22 21:13:36 +00005035 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005036 // If no more than two elements come from either vector. This can be
5037 // implemented with two shuffles. First shuffle gather the elements.
5038 // The second shuffle, which takes the first shuffle as both of its
5039 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00005040 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005041
Nate Begeman9008ca62009-04-27 18:41:29 +00005042 SmallVector<int, 8> Mask2(4U, -1);
Eric Christopherfd179292009-08-27 18:07:15 +00005043
Evan Chengace3c172008-07-22 21:13:36 +00005044 for (unsigned i = 0; i != 4; ++i) {
5045 if (Locs[i].first == -1)
5046 continue;
5047 else {
5048 unsigned Idx = (i < 2) ? 0 : 4;
5049 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00005050 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005051 }
5052 }
5053
Nate Begeman9008ca62009-04-27 18:41:29 +00005054 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005055 } else if (NumLo == 3 || NumHi == 3) {
5056 // Otherwise, we must have three elements from one vector, call it X, and
5057 // one element from the other, call it Y. First, use a shufps to build an
5058 // intermediate vector with the one element from Y and the element from X
5059 // that will be in the same half in the final destination (the indexes don't
5060 // matter). Then, use a shufps to build the final vector, taking the half
5061 // containing the element from Y from the intermediate, and the other half
5062 // from X.
5063 if (NumHi == 3) {
5064 // Normalize it so the 3 elements come from V1.
Nate Begeman9008ca62009-04-27 18:41:29 +00005065 CommuteVectorShuffleMask(PermMask, VT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005066 std::swap(V1, V2);
5067 }
5068
5069 // Find the element from V2.
5070 unsigned HiIndex;
5071 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005072 int Val = PermMask[HiIndex];
5073 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005074 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005075 if (Val >= 4)
5076 break;
5077 }
5078
Nate Begeman9008ca62009-04-27 18:41:29 +00005079 Mask1[0] = PermMask[HiIndex];
5080 Mask1[1] = -1;
5081 Mask1[2] = PermMask[HiIndex^1];
5082 Mask1[3] = -1;
5083 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005084
5085 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005086 Mask1[0] = PermMask[0];
5087 Mask1[1] = PermMask[1];
5088 Mask1[2] = HiIndex & 1 ? 6 : 4;
5089 Mask1[3] = HiIndex & 1 ? 4 : 6;
5090 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005091 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005092 Mask1[0] = HiIndex & 1 ? 2 : 0;
5093 Mask1[1] = HiIndex & 1 ? 0 : 2;
5094 Mask1[2] = PermMask[2];
5095 Mask1[3] = PermMask[3];
5096 if (Mask1[2] >= 0)
5097 Mask1[2] += 4;
5098 if (Mask1[3] >= 0)
5099 Mask1[3] += 4;
5100 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005101 }
Evan Chengace3c172008-07-22 21:13:36 +00005102 }
5103
5104 // Break it into (shuffle shuffle_hi, shuffle_lo).
5105 Locs.clear();
Nate Begeman9008ca62009-04-27 18:41:29 +00005106 SmallVector<int,8> LoMask(4U, -1);
5107 SmallVector<int,8> HiMask(4U, -1);
5108
5109 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00005110 unsigned MaskIdx = 0;
5111 unsigned LoIdx = 0;
5112 unsigned HiIdx = 2;
5113 for (unsigned i = 0; i != 4; ++i) {
5114 if (i == 2) {
5115 MaskPtr = &HiMask;
5116 MaskIdx = 1;
5117 LoIdx = 0;
5118 HiIdx = 2;
5119 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005120 int Idx = PermMask[i];
5121 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00005122 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00005123 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00005124 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00005125 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005126 LoIdx++;
5127 } else {
5128 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00005129 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005130 HiIdx++;
5131 }
5132 }
5133
Nate Begeman9008ca62009-04-27 18:41:29 +00005134 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
5135 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
5136 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00005137 for (unsigned i = 0; i != 4; ++i) {
5138 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005139 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00005140 } else {
5141 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00005142 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00005143 }
5144 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005145 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00005146}
5147
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005148static bool MayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005149 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005150 V = V.getOperand(0);
5151 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
5152 V = V.getOperand(0);
5153 if (MayFoldLoad(V))
5154 return true;
5155 return false;
5156}
5157
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005158// FIXME: the version above should always be used. Since there's
5159// a bug where several vector shuffles can't be folded because the
5160// DAG is not updated during lowering and a node claims to have two
5161// uses while it only has one, use this version, and let isel match
5162// another instruction if the load really happens to have more than
5163// one use. Remove this version after this bug get fixed.
Evan Cheng835580f2010-10-07 20:50:20 +00005164// rdar://8434668, PR8156
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005165static bool RelaxedMayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005166 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005167 V = V.getOperand(0);
5168 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
5169 V = V.getOperand(0);
5170 if (ISD::isNormalLoad(V.getNode()))
5171 return true;
5172 return false;
5173}
5174
5175/// CanFoldShuffleIntoVExtract - Check if the current shuffle is used by
5176/// a vector extract, and if both can be later optimized into a single load.
5177/// This is done in visitEXTRACT_VECTOR_ELT and the conditions are checked
5178/// here because otherwise a target specific shuffle node is going to be
5179/// emitted for this shuffle, and the optimization not done.
5180/// FIXME: This is probably not the best approach, but fix the problem
5181/// until the right path is decided.
5182static
5183bool CanXFormVExtractWithShuffleIntoLoad(SDValue V, SelectionDAG &DAG,
5184 const TargetLowering &TLI) {
5185 EVT VT = V.getValueType();
5186 ShuffleVectorSDNode *SVOp = dyn_cast<ShuffleVectorSDNode>(V);
5187
5188 // Be sure that the vector shuffle is present in a pattern like this:
5189 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), c) -> (f32 load $addr)
5190 if (!V.hasOneUse())
5191 return false;
5192
5193 SDNode *N = *V.getNode()->use_begin();
5194 if (N->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
5195 return false;
5196
5197 SDValue EltNo = N->getOperand(1);
5198 if (!isa<ConstantSDNode>(EltNo))
5199 return false;
5200
5201 // If the bit convert changed the number of elements, it is unsafe
5202 // to examine the mask.
5203 bool HasShuffleIntoBitcast = false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005204 if (V.getOpcode() == ISD::BITCAST) {
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005205 EVT SrcVT = V.getOperand(0).getValueType();
5206 if (SrcVT.getVectorNumElements() != VT.getVectorNumElements())
5207 return false;
5208 V = V.getOperand(0);
5209 HasShuffleIntoBitcast = true;
5210 }
5211
5212 // Select the input vector, guarding against out of range extract vector.
5213 unsigned NumElems = VT.getVectorNumElements();
5214 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
5215 int Idx = (Elt > NumElems) ? -1 : SVOp->getMaskElt(Elt);
5216 V = (Idx < (int)NumElems) ? V.getOperand(0) : V.getOperand(1);
5217
5218 // Skip one more bit_convert if necessary
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005219 if (V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005220 V = V.getOperand(0);
5221
5222 if (ISD::isNormalLoad(V.getNode())) {
5223 // Is the original load suitable?
5224 LoadSDNode *LN0 = cast<LoadSDNode>(V);
5225
5226 // FIXME: avoid the multi-use bug that is preventing lots of
5227 // of foldings to be detected, this is still wrong of course, but
5228 // give the temporary desired behavior, and if it happens that
5229 // the load has real more uses, during isel it will not fold, and
5230 // will generate poor code.
5231 if (!LN0 || LN0->isVolatile()) // || !LN0->hasOneUse()
5232 return false;
5233
5234 if (!HasShuffleIntoBitcast)
5235 return true;
5236
5237 // If there's a bitcast before the shuffle, check if the load type and
5238 // alignment is valid.
5239 unsigned Align = LN0->getAlignment();
5240 unsigned NewAlign =
5241 TLI.getTargetData()->getABITypeAlignment(
5242 VT.getTypeForEVT(*DAG.getContext()));
5243
5244 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
5245 return false;
5246 }
5247
5248 return true;
5249}
5250
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005251static
Evan Cheng835580f2010-10-07 20:50:20 +00005252SDValue getMOVDDup(SDValue &Op, DebugLoc &dl, SDValue V1, SelectionDAG &DAG) {
5253 EVT VT = Op.getValueType();
5254
5255 // Canonizalize to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005256 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1);
5257 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng835580f2010-10-07 20:50:20 +00005258 getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64,
5259 V1, DAG));
5260}
5261
5262static
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005263SDValue getMOVLowToHigh(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG,
5264 bool HasSSE2) {
5265 SDValue V1 = Op.getOperand(0);
5266 SDValue V2 = Op.getOperand(1);
5267 EVT VT = Op.getValueType();
5268
5269 assert(VT != MVT::v2i64 && "unsupported shuffle type");
5270
5271 if (HasSSE2 && VT == MVT::v2f64)
5272 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
5273
5274 // v4f32 or v4i32
5275 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V2, DAG);
5276}
5277
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00005278static
5279SDValue getMOVHighToLow(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG) {
5280 SDValue V1 = Op.getOperand(0);
5281 SDValue V2 = Op.getOperand(1);
5282 EVT VT = Op.getValueType();
5283
5284 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
5285 "unsupported shuffle type");
5286
5287 if (V2.getOpcode() == ISD::UNDEF)
5288 V2 = V1;
5289
5290 // v4i32 or v4f32
5291 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
5292}
5293
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005294static
5295SDValue getMOVLP(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG, bool HasSSE2) {
5296 SDValue V1 = Op.getOperand(0);
5297 SDValue V2 = Op.getOperand(1);
5298 EVT VT = Op.getValueType();
5299 unsigned NumElems = VT.getVectorNumElements();
5300
5301 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
5302 // operand of these instructions is only memory, so check if there's a
5303 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
5304 // same masks.
5305 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005306
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00005307 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005308 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005309 CanFoldLoad = true;
5310
5311 // When V1 is a load, it can be folded later into a store in isel, example:
5312 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
5313 // turns into:
5314 // (MOVLPSmr addr:$src1, VR128:$src2)
5315 // So, recognize this potential and also use MOVLPS or MOVLPD
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005316 if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005317 CanFoldLoad = true;
5318
5319 if (CanFoldLoad) {
5320 if (HasSSE2 && NumElems == 2)
5321 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
5322
5323 if (NumElems == 4)
5324 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
5325 }
5326
5327 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5328 // movl and movlp will both match v2i64, but v2i64 is never matched by
5329 // movl earlier because we make it strict to avoid messing with the movlp load
5330 // folding logic (see the code above getMOVLP call). Match it here then,
5331 // this is horrible, but will stay like this until we move all shuffle
5332 // matching to x86 specific nodes. Note that for the 1st condition all
5333 // types are matched with movsd.
5334 if ((HasSSE2 && NumElems == 2) || !X86::isMOVLMask(SVOp))
5335 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
5336 else if (HasSSE2)
5337 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
5338
5339
5340 assert(VT != MVT::v4i32 && "unsupported shuffle type");
5341
5342 // Invert the operand order and use SHUFPS to match it.
5343 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V2, V1,
5344 X86::getShuffleSHUFImmediate(SVOp), DAG);
5345}
5346
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005347static inline unsigned getUNPCKLOpcode(EVT VT) {
5348 switch(VT.getSimpleVT().SimpleTy) {
5349 case MVT::v4i32: return X86ISD::PUNPCKLDQ;
5350 case MVT::v2i64: return X86ISD::PUNPCKLQDQ;
5351 case MVT::v4f32: return X86ISD::UNPCKLPS;
5352 case MVT::v2f64: return X86ISD::UNPCKLPD;
5353 case MVT::v16i8: return X86ISD::PUNPCKLBW;
5354 case MVT::v8i16: return X86ISD::PUNPCKLWD;
5355 default:
5356 llvm_unreachable("Unknow type for unpckl");
5357 }
5358 return 0;
5359}
5360
5361static inline unsigned getUNPCKHOpcode(EVT VT) {
5362 switch(VT.getSimpleVT().SimpleTy) {
5363 case MVT::v4i32: return X86ISD::PUNPCKHDQ;
5364 case MVT::v2i64: return X86ISD::PUNPCKHQDQ;
5365 case MVT::v4f32: return X86ISD::UNPCKHPS;
5366 case MVT::v2f64: return X86ISD::UNPCKHPD;
5367 case MVT::v16i8: return X86ISD::PUNPCKHBW;
5368 case MVT::v8i16: return X86ISD::PUNPCKHWD;
5369 default:
5370 llvm_unreachable("Unknow type for unpckh");
5371 }
5372 return 0;
5373}
5374
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005375static
5376SDValue NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG,
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005377 const TargetLowering &TLI,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005378 const X86Subtarget *Subtarget) {
5379 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5380 EVT VT = Op.getValueType();
5381 DebugLoc dl = Op.getDebugLoc();
5382 SDValue V1 = Op.getOperand(0);
5383 SDValue V2 = Op.getOperand(1);
5384
5385 if (isZeroShuffle(SVOp))
5386 return getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
5387
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005388 // Handle splat operations
5389 if (SVOp->isSplat()) {
5390 // Special case, this is the only place now where it's
5391 // allowed to return a vector_shuffle operation without
5392 // using a target specific node, because *hopefully* it
5393 // will be optimized away by the dag combiner.
5394 if (VT.getVectorNumElements() <= 4 &&
5395 CanXFormVExtractWithShuffleIntoLoad(Op, DAG, TLI))
5396 return Op;
5397
5398 // Handle splats by matching through known masks
5399 if (VT.getVectorNumElements() <= 4)
5400 return SDValue();
5401
Evan Cheng835580f2010-10-07 20:50:20 +00005402 // Canonicalize all of the remaining to v4f32.
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005403 return PromoteSplat(SVOp, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005404 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005405
5406 // If the shuffle can be profitably rewritten as a narrower shuffle, then
5407 // do it!
5408 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
5409 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5410 if (NewOp.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005411 return DAG.getNode(ISD::BITCAST, dl, VT, NewOp);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005412 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
5413 // FIXME: Figure out a cleaner way to do this.
5414 // Try to make use of movq to zero out the top part.
5415 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
5416 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5417 if (NewOp.getNode()) {
5418 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
5419 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
5420 DAG, Subtarget, dl);
5421 }
5422 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
5423 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5424 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
5425 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
5426 DAG, Subtarget, dl);
5427 }
5428 }
5429 return SDValue();
5430}
5431
Dan Gohman475871a2008-07-27 21:46:04 +00005432SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005433X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00005434 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00005435 SDValue V1 = Op.getOperand(0);
5436 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00005437 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005438 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00005439 unsigned NumElems = VT.getVectorNumElements();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005440 bool isMMX = VT.getSizeInBits() == 64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005441 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
5442 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00005443 bool V1IsSplat = false;
5444 bool V2IsSplat = false;
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005445 bool HasSSE2 = Subtarget->hasSSE2() || Subtarget->hasAVX();
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00005446 bool HasSSE3 = Subtarget->hasSSE3() || Subtarget->hasAVX();
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00005447 bool HasSSSE3 = Subtarget->hasSSSE3() || Subtarget->hasAVX();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005448 MachineFunction &MF = DAG.getMachineFunction();
5449 bool OptForSize = MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005450
Dale Johannesen0488fb62010-09-30 23:57:10 +00005451 // Shuffle operations on MMX not supported.
5452 if (isMMX)
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00005453 return Op;
5454
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005455 // Vector shuffle lowering takes 3 steps:
5456 //
5457 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
5458 // narrowing and commutation of operands should be handled.
5459 // 2) Matching of shuffles with known shuffle masks to x86 target specific
5460 // shuffle nodes.
5461 // 3) Rewriting of unmatched masks into new generic shuffle operations,
5462 // so the shuffle can be broken into other shuffles and the legalizer can
5463 // try the lowering again.
5464 //
5465 // The general ideia is that no vector_shuffle operation should be left to
5466 // be matched during isel, all of them must be converted to a target specific
5467 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00005468
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005469 // Normalize the input vectors. Here splats, zeroed vectors, profitable
5470 // narrowing and commutation of operands should be handled. The actual code
5471 // doesn't include all of those, work in progress...
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005472 SDValue NewOp = NormalizeVectorShuffle(Op, DAG, *this, Subtarget);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005473 if (NewOp.getNode())
5474 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00005475
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005476 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
5477 // unpckh_undef). Only use pshufd if speed is more important than size.
5478 if (OptForSize && X86::isUNPCKL_v_undef_Mask(SVOp))
5479 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5480 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V1, DAG);
5481 if (OptForSize && X86::isUNPCKH_v_undef_Mask(SVOp))
5482 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5483 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00005484
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005485 if (X86::isMOVDDUPMask(SVOp) && HasSSE3 && V2IsUndef &&
Dale Johannesen0488fb62010-09-30 23:57:10 +00005486 RelaxedMayFoldVectorLoad(V1))
Evan Cheng835580f2010-10-07 20:50:20 +00005487 return getMOVDDup(Op, dl, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005488
Dale Johannesen0488fb62010-09-30 23:57:10 +00005489 if (X86::isMOVHLPS_v_undef_Mask(SVOp))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005490 return getMOVHighToLow(Op, dl, DAG);
5491
5492 // Use to match splats
5493 if (HasSSE2 && X86::isUNPCKHMask(SVOp) && V2IsUndef &&
5494 (VT == MVT::v2f64 || VT == MVT::v2i64))
5495 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
5496
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005497 if (X86::isPSHUFDMask(SVOp)) {
5498 // The actual implementation will match the mask in the if above and then
5499 // during isel it can match several different instructions, not only pshufd
5500 // as its name says, sad but true, emulate the behavior for now...
5501 if (X86::isMOVDDUPMask(SVOp) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
5502 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
5503
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005504 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
5505
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005506 if (HasSSE2 && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005507 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
5508
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005509 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005510 return getTargetShuffleNode(X86ISD::SHUFPD, dl, VT, V1, V1,
5511 TargetMask, DAG);
5512
5513 if (VT == MVT::v4f32)
5514 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V1, V1,
5515 TargetMask, DAG);
5516 }
Eric Christopherfd179292009-08-27 18:07:15 +00005517
Evan Chengf26ffe92008-05-29 08:22:04 +00005518 // Check if this can be converted into a logical shift.
5519 bool isLeft = false;
5520 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00005521 SDValue ShVal;
Nate Begeman9008ca62009-04-27 18:41:29 +00005522 bool isShift = getSubtarget()->hasSSE2() &&
Evan Chengc3630942009-12-09 21:00:30 +00005523 isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00005524 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00005525 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00005526 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00005527 EVT EltVT = VT.getVectorElementType();
5528 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00005529 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005530 }
Eric Christopherfd179292009-08-27 18:07:15 +00005531
Nate Begeman9008ca62009-04-27 18:41:29 +00005532 if (X86::isMOVLMask(SVOp)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005533 if (V1IsUndef)
5534 return V2;
Gabor Greifba36cb52008-08-28 21:40:38 +00005535 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00005536 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Dale Johannesen0488fb62010-09-30 23:57:10 +00005537 if (!X86::isMOVLPMask(SVOp)) {
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005538 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00005539 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
5540
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005541 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00005542 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
5543 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00005544 }
Eric Christopherfd179292009-08-27 18:07:15 +00005545
Nate Begeman9008ca62009-04-27 18:41:29 +00005546 // FIXME: fold these into legal mask.
Dale Johannesen0488fb62010-09-30 23:57:10 +00005547 if (X86::isMOVLHPSMask(SVOp) && !X86::isUNPCKLMask(SVOp))
5548 return getMOVLowToHigh(Op, dl, DAG, HasSSE2);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005549
Dale Johannesen0488fb62010-09-30 23:57:10 +00005550 if (X86::isMOVHLPSMask(SVOp))
5551 return getMOVHighToLow(Op, dl, DAG);
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00005552
Dale Johannesen0488fb62010-09-30 23:57:10 +00005553 if (X86::isMOVSHDUPMask(SVOp) && HasSSE3 && V2IsUndef && NumElems == 4)
5554 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00005555
Dale Johannesen0488fb62010-09-30 23:57:10 +00005556 if (X86::isMOVSLDUPMask(SVOp) && HasSSE3 && V2IsUndef && NumElems == 4)
5557 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00005558
Dale Johannesen0488fb62010-09-30 23:57:10 +00005559 if (X86::isMOVLPMask(SVOp))
5560 return getMOVLP(Op, dl, DAG, HasSSE2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005561
Nate Begeman9008ca62009-04-27 18:41:29 +00005562 if (ShouldXformToMOVHLPS(SVOp) ||
5563 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
5564 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005565
Evan Chengf26ffe92008-05-29 08:22:04 +00005566 if (isShift) {
5567 // No better options. Use a vshl / vsrl.
Dan Gohman8a55ce42009-09-23 21:02:20 +00005568 EVT EltVT = VT.getVectorElementType();
5569 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00005570 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005571 }
Eric Christopherfd179292009-08-27 18:07:15 +00005572
Evan Cheng9eca5e82006-10-25 21:49:50 +00005573 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00005574 // FIXME: This should also accept a bitcast of a splat? Be careful, not
5575 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00005576 V1IsSplat = isSplatVector(V1.getNode());
5577 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00005578
Chris Lattner8a594482007-11-25 00:24:49 +00005579 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00005580 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005581 Op = CommuteVectorShuffle(SVOp, DAG);
5582 SVOp = cast<ShuffleVectorSDNode>(Op);
5583 V1 = SVOp->getOperand(0);
5584 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00005585 std::swap(V1IsSplat, V2IsSplat);
5586 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00005587 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00005588 }
5589
Nate Begeman9008ca62009-04-27 18:41:29 +00005590 if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) {
5591 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00005592 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00005593 return V1;
5594 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
5595 // the instruction selector will not match, so get a canonical MOVL with
5596 // swapped operands to undo the commute.
5597 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00005598 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005599
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005600 if (X86::isUNPCKLMask(SVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005601 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005602
5603 if (X86::isUNPCKHMask(SVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005604 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V2, DAG);
Evan Chenge1113032006-10-04 18:33:38 +00005605
Evan Cheng9bbbb982006-10-25 20:48:19 +00005606 if (V2IsSplat) {
5607 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005608 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00005609 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00005610 SDValue NewMask = NormalizeMask(SVOp, DAG);
5611 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
5612 if (NSVOp != SVOp) {
5613 if (X86::isUNPCKLMask(NSVOp, true)) {
5614 return NewMask;
5615 } else if (X86::isUNPCKHMask(NSVOp, true)) {
5616 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005617 }
5618 }
5619 }
5620
Evan Cheng9eca5e82006-10-25 21:49:50 +00005621 if (Commuted) {
5622 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00005623 // FIXME: this seems wrong.
5624 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
5625 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005626
5627 if (X86::isUNPCKLMask(NewSVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005628 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V2, V1, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005629
5630 if (X86::isUNPCKHMask(NewSVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005631 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V2, V1, DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00005632 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005633
Nate Begeman9008ca62009-04-27 18:41:29 +00005634 // Normalize the node to match x86 shuffle ops if needed
Dale Johannesen0488fb62010-09-30 23:57:10 +00005635 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp))
Nate Begeman9008ca62009-04-27 18:41:29 +00005636 return CommuteVectorShuffle(SVOp, DAG);
5637
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00005638 // The checks below are all present in isShuffleMaskLegal, but they are
5639 // inlined here right now to enable us to directly emit target specific
5640 // nodes, and remove one by one until they don't return Op anymore.
5641 SmallVector<int, 16> M;
5642 SVOp->getMask(M);
5643
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00005644 if (isPALIGNRMask(M, VT, HasSSSE3))
5645 return getTargetShuffleNode(X86ISD::PALIGN, dl, VT, V1, V2,
5646 X86::getShufflePALIGNRImmediate(SVOp),
5647 DAG);
5648
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00005649 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
5650 SVOp->getSplatIndex() == 0 && V2IsUndef) {
5651 if (VT == MVT::v2f64)
5652 return getTargetShuffleNode(X86ISD::UNPCKLPD, dl, VT, V1, V1, DAG);
5653 if (VT == MVT::v2i64)
5654 return getTargetShuffleNode(X86ISD::PUNPCKLQDQ, dl, VT, V1, V1, DAG);
5655 }
5656
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00005657 if (isPSHUFHWMask(M, VT))
5658 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
5659 X86::getShufflePSHUFHWImmediate(SVOp),
5660 DAG);
5661
5662 if (isPSHUFLWMask(M, VT))
5663 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
5664 X86::getShufflePSHUFLWImmediate(SVOp),
5665 DAG);
5666
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00005667 if (isSHUFPMask(M, VT)) {
5668 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
5669 if (VT == MVT::v4f32 || VT == MVT::v4i32)
5670 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V1, V2,
5671 TargetMask, DAG);
5672 if (VT == MVT::v2f64 || VT == MVT::v2i64)
5673 return getTargetShuffleNode(X86ISD::SHUFPD, dl, VT, V1, V2,
5674 TargetMask, DAG);
5675 }
5676
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005677 if (X86::isUNPCKL_v_undef_Mask(SVOp))
5678 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5679 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V1, DAG);
5680 if (X86::isUNPCKH_v_undef_Mask(SVOp))
5681 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5682 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
5683
Evan Cheng14b32e12007-12-11 01:46:18 +00005684 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
Owen Anderson825b72b2009-08-11 20:47:22 +00005685 if (VT == MVT::v8i16) {
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005686 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00005687 if (NewOp.getNode())
Evan Cheng14b32e12007-12-11 01:46:18 +00005688 return NewOp;
5689 }
5690
Owen Anderson825b72b2009-08-11 20:47:22 +00005691 if (VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005692 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005693 if (NewOp.getNode())
5694 return NewOp;
5695 }
Eric Christopherfd179292009-08-27 18:07:15 +00005696
Dale Johannesen0488fb62010-09-30 23:57:10 +00005697 // Handle all 4 wide cases with a number of shuffles.
5698 if (NumElems == 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00005699 return LowerVECTOR_SHUFFLE_4wide(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005700
Dan Gohman475871a2008-07-27 21:46:04 +00005701 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005702}
5703
Dan Gohman475871a2008-07-27 21:46:04 +00005704SDValue
5705X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00005706 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005707 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005708 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005709 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005710 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005711 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00005712 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005713 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00005714 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005715 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00005716 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
5717 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
5718 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005719 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
5720 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005721 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005722 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00005723 Op.getOperand(0)),
5724 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00005725 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005726 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00005727 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005728 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00005729 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Owen Anderson825b72b2009-08-11 20:47:22 +00005730 } else if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00005731 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
5732 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00005733 // result has a single use which is a store or a bitcast to i32. And in
5734 // the case of a store, it's not worth it if the index is a constant 0,
5735 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00005736 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00005737 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00005738 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00005739 if ((User->getOpcode() != ISD::STORE ||
5740 (isa<ConstantSDNode>(Op.getOperand(1)) &&
5741 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005742 (User->getOpcode() != ISD::BITCAST ||
Owen Anderson825b72b2009-08-11 20:47:22 +00005743 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00005744 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00005745 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005746 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00005747 Op.getOperand(0)),
5748 Op.getOperand(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005749 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Extract);
Owen Anderson825b72b2009-08-11 20:47:22 +00005750 } else if (VT == MVT::i32) {
Mon P Wangf0fcdd82009-01-15 21:10:20 +00005751 // ExtractPS works with constant index.
5752 if (isa<ConstantSDNode>(Op.getOperand(1)))
5753 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00005754 }
Dan Gohman475871a2008-07-27 21:46:04 +00005755 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00005756}
5757
5758
Dan Gohman475871a2008-07-27 21:46:04 +00005759SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005760X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
5761 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005762 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00005763 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005764
Evan Cheng62a3f152008-03-24 21:52:23 +00005765 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005766 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00005767 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00005768 return Res;
5769 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00005770
Owen Andersone50ed302009-08-10 22:56:29 +00005771 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005772 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005773 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005774 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00005775 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005776 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00005777 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005778 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
5779 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005780 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005781 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00005782 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005783 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00005784 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00005785 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00005786 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00005787 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00005788 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00005789 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005790 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005791 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005792 if (Idx == 0)
5793 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00005794
Evan Cheng0db9fe62006-04-25 20:13:52 +00005795 // SHUFPS the element to the lowest double word, then movss.
Nate Begeman9008ca62009-04-27 18:41:29 +00005796 int Mask[4] = { Idx, -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00005797 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00005798 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00005799 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00005800 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00005801 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00005802 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00005803 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
5804 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
5805 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005806 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005807 if (Idx == 0)
5808 return Op;
5809
5810 // UNPCKHPD the element to the lowest double word, then movsd.
5811 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
5812 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00005813 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00005814 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00005815 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00005816 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00005817 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00005818 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005819 }
5820
Dan Gohman475871a2008-07-27 21:46:04 +00005821 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005822}
5823
Dan Gohman475871a2008-07-27 21:46:04 +00005824SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005825X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op,
5826 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005827 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00005828 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005829 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00005830
Dan Gohman475871a2008-07-27 21:46:04 +00005831 SDValue N0 = Op.getOperand(0);
5832 SDValue N1 = Op.getOperand(1);
5833 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00005834
Dan Gohman8a55ce42009-09-23 21:02:20 +00005835 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00005836 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00005837 unsigned Opc;
5838 if (VT == MVT::v8i16)
5839 Opc = X86ISD::PINSRW;
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00005840 else if (VT == MVT::v16i8)
5841 Opc = X86ISD::PINSRB;
5842 else
5843 Opc = X86ISD::PINSRB;
5844
Nate Begeman14d12ca2008-02-11 04:19:36 +00005845 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
5846 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00005847 if (N1.getValueType() != MVT::i32)
5848 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
5849 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005850 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00005851 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00005852 } else if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00005853 // Bits [7:6] of the constant are the source select. This will always be
5854 // zero here. The DAG Combiner may combine an extract_elt index into these
5855 // bits. For example (insert (extract, 3), 2) could be matched by putting
5856 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00005857 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00005858 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00005859 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00005860 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005861 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00005862 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00005863 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00005864 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00005865 } else if (EltVT == MVT::i32 && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00005866 // PINSR* works with constant index.
5867 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00005868 }
Dan Gohman475871a2008-07-27 21:46:04 +00005869 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00005870}
5871
Dan Gohman475871a2008-07-27 21:46:04 +00005872SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005873X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005874 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00005875 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00005876
5877 if (Subtarget->hasSSE41())
5878 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
5879
Dan Gohman8a55ce42009-09-23 21:02:20 +00005880 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00005881 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00005882
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005883 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00005884 SDValue N0 = Op.getOperand(0);
5885 SDValue N1 = Op.getOperand(1);
5886 SDValue N2 = Op.getOperand(2);
Evan Cheng794405e2007-12-12 07:55:34 +00005887
Dan Gohman8a55ce42009-09-23 21:02:20 +00005888 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00005889 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
5890 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00005891 if (N1.getValueType() != MVT::i32)
5892 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
5893 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005894 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesen0488fb62010-09-30 23:57:10 +00005895 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005896 }
Dan Gohman475871a2008-07-27 21:46:04 +00005897 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005898}
5899
Dan Gohman475871a2008-07-27 21:46:04 +00005900SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005901X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005902 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00005903
Chris Lattnerf172ecd2010-07-04 23:07:25 +00005904 if (Op.getValueType() == MVT::v1i64 &&
5905 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00005906 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00005907
Owen Anderson825b72b2009-08-11 20:47:22 +00005908 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Dale Johannesen0488fb62010-09-30 23:57:10 +00005909 assert(Op.getValueType().getSimpleVT().getSizeInBits() == 128 &&
5910 "Expected an SSE type!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005911 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(),
Dale Johannesen0488fb62010-09-30 23:57:10 +00005912 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005913}
5914
David Greene91585092011-01-26 15:38:49 +00005915// Lower a node with an EXTRACT_SUBVECTOR opcode. This may result in
5916// a simple subregister reference or explicit instructions to grab
5917// upper bits of a vector.
5918SDValue
5919X86TargetLowering::LowerEXTRACT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
5920 if (Subtarget->hasAVX()) {
5921 // TODO
5922 }
5923 return SDValue();
5924}
5925
David Greenecfe33c42011-01-26 19:13:22 +00005926// Lower a node with an INSERT_SUBVECTOR opcode. This may result in a
5927// simple superregister reference or explicit instructions to insert
5928// the upper bits of a vector.
5929SDValue
5930X86TargetLowering::LowerINSERT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
5931 if (Subtarget->hasAVX()) {
5932 DebugLoc dl = Op.getNode()->getDebugLoc();
5933 SDValue Vec = Op.getNode()->getOperand(0);
5934 SDValue SubVec = Op.getNode()->getOperand(1);
5935 SDValue Idx = Op.getNode()->getOperand(2);
5936
5937 if (Op.getNode()->getValueType(0).getSizeInBits() == 256
5938 && SubVec.getNode()->getValueType(0).getSizeInBits() == 128) {
5939 // TODO
5940 }
5941 }
5942 return SDValue();
5943}
5944
Bill Wendling056292f2008-09-16 21:48:12 +00005945// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
5946// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
5947// one of the above mentioned nodes. It has to be wrapped because otherwise
5948// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
5949// be used to form addressing mode. These wrapped nodes will be selected
5950// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00005951SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005952X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005953 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00005954
Chris Lattner41621a22009-06-26 19:22:52 +00005955 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
5956 // global base reg.
5957 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00005958 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005959 CodeModel::Model M = getTargetMachine().getCodeModel();
5960
Chris Lattner4f066492009-07-11 20:29:19 +00005961 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005962 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00005963 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00005964 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005965 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00005966 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005967 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00005968
Evan Cheng1606e8e2009-03-13 07:51:59 +00005969 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00005970 CP->getAlignment(),
5971 CP->getOffset(), OpFlag);
5972 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00005973 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00005974 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00005975 if (OpFlag) {
5976 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00005977 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00005978 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00005979 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005980 }
5981
5982 return Result;
5983}
5984
Dan Gohmand858e902010-04-17 15:26:15 +00005985SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00005986 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00005987
Chris Lattner18c59872009-06-27 04:16:01 +00005988 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
5989 // global base reg.
5990 unsigned char OpFlag = 0;
5991 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005992 CodeModel::Model M = getTargetMachine().getCodeModel();
5993
Chris Lattner4f066492009-07-11 20:29:19 +00005994 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005995 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00005996 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00005997 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005998 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00005999 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006000 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00006001
Chris Lattner18c59872009-06-27 04:16:01 +00006002 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
6003 OpFlag);
6004 DebugLoc DL = JT->getDebugLoc();
6005 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00006006
Chris Lattner18c59872009-06-27 04:16:01 +00006007 // With PIC, the address is actually $g + Offset.
Chris Lattner1e61e692010-11-15 02:46:57 +00006008 if (OpFlag)
Chris Lattner18c59872009-06-27 04:16:01 +00006009 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
6010 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006011 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00006012 Result);
Eric Christopherfd179292009-08-27 18:07:15 +00006013
Chris Lattner18c59872009-06-27 04:16:01 +00006014 return Result;
6015}
6016
6017SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006018X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00006019 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00006020
Chris Lattner18c59872009-06-27 04:16:01 +00006021 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6022 // global base reg.
6023 unsigned char OpFlag = 0;
6024 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006025 CodeModel::Model M = getTargetMachine().getCodeModel();
6026
Chris Lattner4f066492009-07-11 20:29:19 +00006027 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006028 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00006029 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00006030 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006031 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00006032 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006033 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00006034
Chris Lattner18c59872009-06-27 04:16:01 +00006035 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00006036
Chris Lattner18c59872009-06-27 04:16:01 +00006037 DebugLoc DL = Op.getDebugLoc();
6038 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00006039
6040
Chris Lattner18c59872009-06-27 04:16:01 +00006041 // With PIC, the address is actually $g + Offset.
6042 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00006043 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00006044 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
6045 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006046 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00006047 Result);
6048 }
Eric Christopherfd179292009-08-27 18:07:15 +00006049
Chris Lattner18c59872009-06-27 04:16:01 +00006050 return Result;
6051}
6052
Dan Gohman475871a2008-07-27 21:46:04 +00006053SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006054X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00006055 // Create the TargetBlockAddressAddress node.
6056 unsigned char OpFlags =
6057 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00006058 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00006059 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Dan Gohman29cbade2009-11-20 23:18:13 +00006060 DebugLoc dl = Op.getDebugLoc();
6061 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(),
6062 /*isTarget=*/true, OpFlags);
6063
Dan Gohmanf705adb2009-10-30 01:28:02 +00006064 if (Subtarget->isPICStyleRIPRel() &&
6065 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00006066 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
6067 else
6068 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00006069
Dan Gohman29cbade2009-11-20 23:18:13 +00006070 // With PIC, the address is actually $g + Offset.
6071 if (isGlobalRelativeToPICBase(OpFlags)) {
6072 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
6073 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
6074 Result);
6075 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00006076
6077 return Result;
6078}
6079
6080SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00006081X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00006082 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00006083 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00006084 // Create the TargetGlobalAddress node, folding in the constant
6085 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00006086 unsigned char OpFlags =
6087 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006088 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00006089 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006090 if (OpFlags == X86II::MO_NO_FLAG &&
6091 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00006092 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00006093 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00006094 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00006095 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00006096 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00006097 }
Eric Christopherfd179292009-08-27 18:07:15 +00006098
Chris Lattner4f066492009-07-11 20:29:19 +00006099 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006100 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00006101 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
6102 else
6103 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00006104
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006105 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00006106 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00006107 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
6108 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006109 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006110 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006111
Chris Lattner36c25012009-07-10 07:34:39 +00006112 // For globals that require a load from a stub to get the address, emit the
6113 // load.
6114 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00006115 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00006116 MachinePointerInfo::getGOT(), false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006117
Dan Gohman6520e202008-10-18 02:06:02 +00006118 // If there was a non-zero offset that we didn't fold, create an explicit
6119 // addition for it.
6120 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00006121 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00006122 DAG.getConstant(Offset, getPointerTy()));
6123
Evan Cheng0db9fe62006-04-25 20:13:52 +00006124 return Result;
6125}
6126
Evan Chengda43bcf2008-09-24 00:05:32 +00006127SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006128X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00006129 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00006130 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006131 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00006132}
6133
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006134static SDValue
6135GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00006136 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Chris Lattnerb903bed2009-06-26 21:20:29 +00006137 unsigned char OperandFlags) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006138 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006139 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006140 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00006141 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006142 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00006143 GA->getOffset(),
6144 OperandFlags);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006145 if (InFlag) {
6146 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00006147 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006148 } else {
6149 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00006150 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006151 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006152
6153 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00006154 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006155
Rafael Espindola15f1b662009-04-24 12:59:40 +00006156 SDValue Flag = Chain.getValue(1);
6157 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006158}
6159
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006160// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00006161static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006162LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006163 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00006164 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00006165 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
6166 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006167 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006168 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006169 InFlag = Chain.getValue(1);
6170
Chris Lattnerb903bed2009-06-26 21:20:29 +00006171 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006172}
6173
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006174// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00006175static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006176LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006177 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00006178 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
6179 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006180}
6181
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006182// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
6183// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00006184static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006185 const EVT PtrVT, TLSModel::Model model,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00006186 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00006187 DebugLoc dl = GA->getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00006188
Chris Lattnerf93b90c2010-09-22 04:39:11 +00006189 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
6190 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
6191 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00006192
Michael J. Spencerec38de22010-10-10 22:04:20 +00006193 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Chris Lattnerf93b90c2010-09-22 04:39:11 +00006194 DAG.getIntPtrConstant(0),
6195 MachinePointerInfo(Ptr), false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00006196
Chris Lattnerb903bed2009-06-26 21:20:29 +00006197 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00006198 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
6199 // initialexec.
6200 unsigned WrapperKind = X86ISD::Wrapper;
6201 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00006202 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Chris Lattner18c59872009-06-27 04:16:01 +00006203 } else if (is64Bit) {
6204 assert(model == TLSModel::InitialExec);
6205 OperandFlags = X86II::MO_GOTTPOFF;
6206 WrapperKind = X86ISD::WrapperRIP;
6207 } else {
6208 assert(model == TLSModel::InitialExec);
6209 OperandFlags = X86II::MO_INDNTPOFF;
Chris Lattnerb903bed2009-06-26 21:20:29 +00006210 }
Eric Christopherfd179292009-08-27 18:07:15 +00006211
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006212 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
6213 // exec)
Michael J. Spencerec38de22010-10-10 22:04:20 +00006214 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Devang Patel0d881da2010-07-06 22:08:15 +00006215 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00006216 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00006217 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00006218
Rafael Espindola9a580232009-02-27 13:37:18 +00006219 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00006220 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00006221 MachinePointerInfo::getGOT(), false, false, 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00006222
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006223 // The address of the thread local variable is the add of the thread
6224 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00006225 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006226}
6227
Dan Gohman475871a2008-07-27 21:46:04 +00006228SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006229X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00006230
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006231 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00006232 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00006233
Eric Christopher30ef0e52010-06-03 04:07:48 +00006234 if (Subtarget->isTargetELF()) {
6235 // TODO: implement the "local dynamic" model
6236 // TODO: implement the "initial exec"model for pic executables
Michael J. Spencerec38de22010-10-10 22:04:20 +00006237
Eric Christopher30ef0e52010-06-03 04:07:48 +00006238 // If GV is an alias then use the aliasee for determining
6239 // thread-localness.
6240 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
6241 GV = GA->resolveAliasedGlobal(false);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006242
6243 TLSModel::Model model
Eric Christopher30ef0e52010-06-03 04:07:48 +00006244 = getTLSModel(GV, getTargetMachine().getRelocationModel());
Michael J. Spencerec38de22010-10-10 22:04:20 +00006245
Eric Christopher30ef0e52010-06-03 04:07:48 +00006246 switch (model) {
6247 case TLSModel::GeneralDynamic:
6248 case TLSModel::LocalDynamic: // not implemented
6249 if (Subtarget->is64Bit())
6250 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
6251 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Michael J. Spencerec38de22010-10-10 22:04:20 +00006252
Eric Christopher30ef0e52010-06-03 04:07:48 +00006253 case TLSModel::InitialExec:
6254 case TLSModel::LocalExec:
6255 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
6256 Subtarget->is64Bit());
6257 }
6258 } else if (Subtarget->isTargetDarwin()) {
6259 // Darwin only has one model of TLS. Lower to that.
6260 unsigned char OpFlag = 0;
6261 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
6262 X86ISD::WrapperRIP : X86ISD::Wrapper;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006263
Eric Christopher30ef0e52010-06-03 04:07:48 +00006264 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6265 // global base reg.
6266 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
6267 !Subtarget->is64Bit();
6268 if (PIC32)
6269 OpFlag = X86II::MO_TLVP_PIC_BASE;
6270 else
6271 OpFlag = X86II::MO_TLVP;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006272 DebugLoc DL = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00006273 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopherd8c05362010-12-09 06:25:53 +00006274 GA->getValueType(0),
Eric Christopher30ef0e52010-06-03 04:07:48 +00006275 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00006276 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006277
Eric Christopher30ef0e52010-06-03 04:07:48 +00006278 // With PIC32, the address is actually $g + Offset.
6279 if (PIC32)
6280 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
6281 DAG.getNode(X86ISD::GlobalBaseReg,
6282 DebugLoc(), getPointerTy()),
6283 Offset);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006284
Eric Christopher30ef0e52010-06-03 04:07:48 +00006285 // Lowering the machine isd will make sure everything is in the right
6286 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00006287 SDValue Chain = DAG.getEntryNode();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006288 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Eric Christopherd8c05362010-12-09 06:25:53 +00006289 SDValue Args[] = { Chain, Offset };
6290 Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006291
Eric Christopher30ef0e52010-06-03 04:07:48 +00006292 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
6293 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
6294 MFI->setAdjustsStack(true);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00006295
Eric Christopher30ef0e52010-06-03 04:07:48 +00006296 // And our return value (tls address) is in the standard call return value
6297 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00006298 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
6299 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy());
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006300 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00006301
Eric Christopher30ef0e52010-06-03 04:07:48 +00006302 assert(false &&
6303 "TLS not implemented for this target.");
Eric Christopherfd179292009-08-27 18:07:15 +00006304
Torok Edwinc23197a2009-07-14 16:55:14 +00006305 llvm_unreachable("Unreachable");
Chris Lattner5867de12009-04-01 22:14:45 +00006306 return SDValue();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006307}
6308
Evan Cheng0db9fe62006-04-25 20:13:52 +00006309
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006310/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michelfdc40a02009-02-17 22:15:04 +00006311/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00006312SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman4c1fa612008-03-03 22:22:09 +00006313 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00006314 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006315 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006316 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006317 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00006318 SDValue ShOpLo = Op.getOperand(0);
6319 SDValue ShOpHi = Op.getOperand(1);
6320 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00006321 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00006322 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00006323 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00006324
Dan Gohman475871a2008-07-27 21:46:04 +00006325 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006326 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00006327 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
6328 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006329 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00006330 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
6331 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006332 }
Evan Chenge3413162006-01-09 18:33:28 +00006333
Owen Anderson825b72b2009-08-11 20:47:22 +00006334 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
6335 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00006336 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00006337 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00006338
Dan Gohman475871a2008-07-27 21:46:04 +00006339 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00006340 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00006341 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
6342 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00006343
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006344 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00006345 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
6346 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006347 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00006348 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
6349 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006350 }
6351
Dan Gohman475871a2008-07-27 21:46:04 +00006352 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00006353 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006354}
Evan Chenga3195e82006-01-12 22:54:21 +00006355
Dan Gohmand858e902010-04-17 15:26:15 +00006356SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
6357 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006358 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00006359
Dale Johannesen0488fb62010-09-30 23:57:10 +00006360 if (SrcVT.isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00006361 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00006362
Owen Anderson825b72b2009-08-11 20:47:22 +00006363 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00006364 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00006365
Eli Friedman36df4992009-05-27 00:47:34 +00006366 // These are really Legal; return the operand so the caller accepts it as
6367 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00006368 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00006369 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00006370 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00006371 Subtarget->is64Bit()) {
6372 return Op;
6373 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006374
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006375 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006376 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006377 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00006378 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006379 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00006380 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00006381 StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006382 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006383 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00006384 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
6385}
Evan Cheng0db9fe62006-04-25 20:13:52 +00006386
Owen Andersone50ed302009-08-10 22:56:29 +00006387SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Michael J. Spencerec38de22010-10-10 22:04:20 +00006388 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00006389 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006390 // Build the FILD
Chris Lattner492a43e2010-09-22 01:28:21 +00006391 DebugLoc DL = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00006392 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00006393 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006394 if (useSSE)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006395 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Glue);
Chris Lattner5a88b832007-02-25 07:10:00 +00006396 else
Owen Anderson825b72b2009-08-11 20:47:22 +00006397 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006398
Chris Lattner492a43e2010-09-22 01:28:21 +00006399 unsigned ByteSize = SrcVT.getSizeInBits()/8;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006400
Chris Lattner492a43e2010-09-22 01:28:21 +00006401 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
6402 MachineMemOperand *MMO =
6403 DAG.getMachineFunction()
6404 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6405 MachineMemOperand::MOLoad, ByteSize, ByteSize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006406
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006407 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Chris Lattner492a43e2010-09-22 01:28:21 +00006408 SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
6409 X86ISD::FILD, DL,
6410 Tys, Ops, array_lengthof(Ops),
6411 SrcVT, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006412
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006413 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006414 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00006415 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006416
6417 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
6418 // shouldn't be necessary except that RFP cannot be live across
6419 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006420 MachineFunction &MF = DAG.getMachineFunction();
Bob Wilsoneafca4e2010-09-22 17:35:14 +00006421 unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
6422 int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006423 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00006424 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006425 SDValue Ops[] = {
6426 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
6427 };
Chris Lattner492a43e2010-09-22 01:28:21 +00006428 MachineMemOperand *MMO =
6429 DAG.getMachineFunction()
6430 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
Bob Wilsoneafca4e2010-09-22 17:35:14 +00006431 MachineMemOperand::MOStore, SSFISize, SSFISize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006432
Chris Lattner492a43e2010-09-22 01:28:21 +00006433 Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
6434 Ops, array_lengthof(Ops),
6435 Op.getValueType(), MMO);
6436 Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006437 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006438 false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006439 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006440
Evan Cheng0db9fe62006-04-25 20:13:52 +00006441 return Result;
6442}
6443
Bill Wendling8b8a6362009-01-17 03:56:04 +00006444// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00006445SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
6446 SelectionDAG &DAG) const {
Bill Wendling8b8a6362009-01-17 03:56:04 +00006447 // This algorithm is not obvious. Here it is in C code, more or less:
6448 /*
6449 double uint64_to_double( uint32_t hi, uint32_t lo ) {
6450 static const __m128i exp = { 0x4330000045300000ULL, 0 };
6451 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00006452
Bill Wendling8b8a6362009-01-17 03:56:04 +00006453 // Copy ints to xmm registers.
6454 __m128i xh = _mm_cvtsi32_si128( hi );
6455 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00006456
Bill Wendling8b8a6362009-01-17 03:56:04 +00006457 // Combine into low half of a single xmm register.
6458 __m128i x = _mm_unpacklo_epi32( xh, xl );
6459 __m128d d;
6460 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00006461
Bill Wendling8b8a6362009-01-17 03:56:04 +00006462 // Merge in appropriate exponents to give the integer bits the right
6463 // magnitude.
6464 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00006465
Bill Wendling8b8a6362009-01-17 03:56:04 +00006466 // Subtract away the biases to deal with the IEEE-754 double precision
6467 // implicit 1.
6468 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00006469
Bill Wendling8b8a6362009-01-17 03:56:04 +00006470 // All conversions up to here are exact. The correctly rounded result is
6471 // calculated using the current rounding mode using the following
6472 // horizontal add.
6473 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
6474 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
6475 // store doesn't really need to be here (except
6476 // maybe to zero the other double)
6477 return sd;
6478 }
6479 */
Dale Johannesen040225f2008-10-21 23:07:49 +00006480
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006481 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00006482 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00006483
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006484 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00006485 std::vector<Constant*> CV0;
Owen Andersoneed707b2009-07-24 23:12:02 +00006486 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x45300000)));
6487 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x43300000)));
6488 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
6489 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
Owen Andersonaf7ec972009-07-28 21:19:26 +00006490 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006491 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006492
Bill Wendling8b8a6362009-01-17 03:56:04 +00006493 std::vector<Constant*> CV1;
Owen Andersona90b3dc2009-07-15 21:51:10 +00006494 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006495 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
Owen Andersona90b3dc2009-07-15 21:51:10 +00006496 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006497 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Owen Andersonaf7ec972009-07-28 21:19:26 +00006498 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006499 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006500
Owen Anderson825b72b2009-08-11 20:47:22 +00006501 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6502 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00006503 Op.getOperand(0),
6504 DAG.getIntPtrConstant(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006505 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6506 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00006507 Op.getOperand(0),
6508 DAG.getIntPtrConstant(0)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006509 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
6510 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Chris Lattnere8639032010-09-21 06:22:23 +00006511 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006512 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00006513 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006514 SDValue XR2F = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Unpck2);
Owen Anderson825b72b2009-08-11 20:47:22 +00006515 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00006516 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006517 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00006518 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006519
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006520 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman9008ca62009-04-27 18:41:29 +00006521 int ShufMask[2] = { 1, -1 };
Owen Anderson825b72b2009-08-11 20:47:22 +00006522 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
6523 DAG.getUNDEF(MVT::v2f64), ShufMask);
6524 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
6525 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006526 DAG.getIntPtrConstant(0));
6527}
6528
Bill Wendling8b8a6362009-01-17 03:56:04 +00006529// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00006530SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
6531 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006532 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00006533 // FP constant to bias correct the final result.
6534 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00006535 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006536
6537 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00006538 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6539 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Bill Wendling8b8a6362009-01-17 03:56:04 +00006540 Op.getOperand(0),
6541 DAG.getIntPtrConstant(0)));
6542
Owen Anderson825b72b2009-08-11 20:47:22 +00006543 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006544 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00006545 DAG.getIntPtrConstant(0));
6546
6547 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00006548 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006549 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00006550 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006551 MVT::v2f64, Load)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006552 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00006553 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006554 MVT::v2f64, Bias)));
6555 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006556 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00006557 DAG.getIntPtrConstant(0));
6558
6559 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00006560 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006561
6562 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00006563 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00006564
Owen Anderson825b72b2009-08-11 20:47:22 +00006565 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006566 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00006567 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00006568 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006569 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00006570 }
6571
6572 // Handle final rounding.
6573 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00006574}
6575
Dan Gohmand858e902010-04-17 15:26:15 +00006576SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
6577 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00006578 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006579 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00006580
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006581 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00006582 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
6583 // the optimization here.
6584 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00006585 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00006586
Owen Andersone50ed302009-08-10 22:56:29 +00006587 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006588 EVT DstVT = Op.getValueType();
6589 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00006590 return LowerUINT_TO_FP_i64(Op, DAG);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006591 else if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00006592 return LowerUINT_TO_FP_i32(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00006593
6594 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00006595 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006596 if (SrcVT == MVT::i32) {
6597 SDValue WordOff = DAG.getConstant(4, getPointerTy());
6598 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
6599 getPointerTy(), StackSlot, WordOff);
6600 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006601 StackSlot, MachinePointerInfo(),
6602 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006603 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006604 OffsetSlot, MachinePointerInfo(),
6605 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006606 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
6607 return Fild;
6608 }
6609
6610 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
6611 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006612 StackSlot, MachinePointerInfo(),
6613 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006614 // For i64 source, we need to add the appropriate power of 2 if the input
6615 // was negative. This is the same as the optimization in
6616 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
6617 // we must be careful to do the computation in x87 extended precision, not
6618 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00006619 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
6620 MachineMemOperand *MMO =
6621 DAG.getMachineFunction()
6622 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6623 MachineMemOperand::MOLoad, 8, 8);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006624
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006625 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
6626 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Chris Lattner492a43e2010-09-22 01:28:21 +00006627 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, 3,
6628 MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006629
6630 APInt FF(32, 0x5F800000ULL);
6631
6632 // Check whether the sign bit is set.
6633 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
6634 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
6635 ISD::SETLT);
6636
6637 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
6638 SDValue FudgePtr = DAG.getConstantPool(
6639 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
6640 getPointerTy());
6641
6642 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
6643 SDValue Zero = DAG.getIntPtrConstant(0);
6644 SDValue Four = DAG.getIntPtrConstant(4);
6645 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
6646 Zero, Four);
6647 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
6648
6649 // Load the value out, extending it from f32 to f80.
6650 // FIXME: Avoid the extend by constructing the right constant pool?
Evan Chengbcc80172010-07-07 22:15:37 +00006651 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, MVT::f80, dl, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00006652 FudgePtr, MachinePointerInfo::getConstantPool(),
6653 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006654 // Extend everything to 80 bits to force it to be done on x87.
6655 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
6656 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00006657}
6658
Dan Gohman475871a2008-07-27 21:46:04 +00006659std::pair<SDValue,SDValue> X86TargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00006660FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) const {
Chris Lattner07290932010-09-22 01:05:16 +00006661 DebugLoc DL = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00006662
Owen Andersone50ed302009-08-10 22:56:29 +00006663 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00006664
6665 if (!IsSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006666 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
6667 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00006668 }
6669
Owen Anderson825b72b2009-08-11 20:47:22 +00006670 assert(DstTy.getSimpleVT() <= MVT::i64 &&
6671 DstTy.getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00006672 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00006673
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006674 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00006675 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00006676 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00006677 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00006678 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00006679 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00006680 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00006681 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006682
Evan Cheng87c89352007-10-15 20:11:21 +00006683 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
6684 // stack slot.
6685 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00006686 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00006687 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006688 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00006689
Michael J. Spencerec38de22010-10-10 22:04:20 +00006690
6691
Evan Cheng0db9fe62006-04-25 20:13:52 +00006692 unsigned Opc;
Owen Anderson825b72b2009-08-11 20:47:22 +00006693 switch (DstTy.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00006694 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
Owen Anderson825b72b2009-08-11 20:47:22 +00006695 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
6696 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
6697 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006698 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006699
Dan Gohman475871a2008-07-27 21:46:04 +00006700 SDValue Chain = DAG.getEntryNode();
6701 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00006702 EVT TheVT = Op.getOperand(0).getValueType();
6703 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006704 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00006705 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006706 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006707 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00006708 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00006709 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00006710 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00006711 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00006712
Chris Lattner492a43e2010-09-22 01:28:21 +00006713 MachineMemOperand *MMO =
6714 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6715 MachineMemOperand::MOLoad, MemSize, MemSize);
6716 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, 3,
6717 DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006718 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00006719 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006720 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
6721 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00006722
Chris Lattner07290932010-09-22 01:05:16 +00006723 MachineMemOperand *MMO =
6724 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6725 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006726
Evan Cheng0db9fe62006-04-25 20:13:52 +00006727 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00006728 SDValue Ops[] = { Chain, Value, StackSlot };
Chris Lattner07290932010-09-22 01:05:16 +00006729 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
6730 Ops, 3, DstTy, MMO);
Evan Chengd9558e02006-01-06 00:43:03 +00006731
Chris Lattner27a6c732007-11-24 07:07:01 +00006732 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006733}
6734
Dan Gohmand858e902010-04-17 15:26:15 +00006735SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
6736 SelectionDAG &DAG) const {
Dale Johannesen0488fb62010-09-30 23:57:10 +00006737 if (Op.getValueType().isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00006738 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00006739
Eli Friedman948e95a2009-05-23 09:59:16 +00006740 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00006741 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00006742 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
6743 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00006744
Chris Lattner27a6c732007-11-24 07:07:01 +00006745 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006746 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00006747 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00006748}
6749
Dan Gohmand858e902010-04-17 15:26:15 +00006750SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
6751 SelectionDAG &DAG) const {
Eli Friedman948e95a2009-05-23 09:59:16 +00006752 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
6753 SDValue FIST = Vals.first, StackSlot = Vals.second;
6754 assert(FIST.getNode() && "Unexpected failure");
6755
6756 // Load the result.
6757 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00006758 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00006759}
6760
Dan Gohmand858e902010-04-17 15:26:15 +00006761SDValue X86TargetLowering::LowerFABS(SDValue Op,
6762 SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00006763 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006764 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00006765 EVT VT = Op.getValueType();
6766 EVT EltVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00006767 if (VT.isVector())
6768 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006769 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00006770 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006771 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00006772 CV.push_back(C);
6773 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006774 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006775 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00006776 CV.push_back(C);
6777 CV.push_back(C);
6778 CV.push_back(C);
6779 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006780 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00006781 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006782 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006783 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00006784 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006785 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006786 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006787}
6788
Dan Gohmand858e902010-04-17 15:26:15 +00006789SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00006790 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006791 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00006792 EVT VT = Op.getValueType();
6793 EVT EltVT = VT;
Duncan Sandsda9ad382009-09-06 19:29:07 +00006794 if (VT.isVector())
Duncan Sands83ec4b62008-06-06 12:08:01 +00006795 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006796 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00006797 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006798 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00006799 CV.push_back(C);
6800 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006801 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006802 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00006803 CV.push_back(C);
6804 CV.push_back(C);
6805 CV.push_back(C);
6806 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006807 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00006808 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006809 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006810 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00006811 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006812 false, false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006813 if (VT.isVector()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006814 return DAG.getNode(ISD::BITCAST, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00006815 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006816 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00006817 Op.getOperand(0)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006818 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00006819 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00006820 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00006821 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006822}
6823
Dan Gohmand858e902010-04-17 15:26:15 +00006824SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00006825 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00006826 SDValue Op0 = Op.getOperand(0);
6827 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006828 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00006829 EVT VT = Op.getValueType();
6830 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00006831
6832 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00006833 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006834 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00006835 SrcVT = VT;
6836 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00006837 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00006838 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006839 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00006840 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00006841 }
6842
6843 // At this point the operands and the result should have the same
6844 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00006845
Evan Cheng68c47cb2007-01-05 07:55:56 +00006846 // First get the sign bit of second operand.
6847 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00006848 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006849 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
6850 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00006851 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006852 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
6853 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
6854 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
6855 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00006856 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00006857 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006858 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006859 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00006860 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006861 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006862 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00006863
6864 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00006865 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006866 // Op0 is MVT::f32, Op1 is MVT::f64.
6867 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
6868 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
6869 DAG.getConstant(32, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006870 SignBit = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, SignBit);
Owen Anderson825b72b2009-08-11 20:47:22 +00006871 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00006872 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00006873 }
6874
Evan Cheng73d6cf12007-01-05 21:37:56 +00006875 // Clear first operand sign bit.
6876 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00006877 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006878 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
6879 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00006880 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006881 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
6882 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
6883 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
6884 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00006885 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00006886 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006887 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006888 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00006889 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006890 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006891 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00006892
6893 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00006894 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00006895}
6896
Dan Gohman076aee32009-03-04 19:44:21 +00006897/// Emit nodes that will be selected as "test Op0,Op0", or something
6898/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00006899SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00006900 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00006901 DebugLoc dl = Op.getDebugLoc();
6902
Dan Gohman31125812009-03-07 01:58:32 +00006903 // CF and OF aren't always set the way we want. Determine which
6904 // of these we need.
6905 bool NeedCF = false;
6906 bool NeedOF = false;
6907 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006908 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00006909 case X86::COND_A: case X86::COND_AE:
6910 case X86::COND_B: case X86::COND_BE:
6911 NeedCF = true;
6912 break;
6913 case X86::COND_G: case X86::COND_GE:
6914 case X86::COND_L: case X86::COND_LE:
6915 case X86::COND_O: case X86::COND_NO:
6916 NeedOF = true;
6917 break;
Dan Gohman31125812009-03-07 01:58:32 +00006918 }
6919
Dan Gohman076aee32009-03-04 19:44:21 +00006920 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00006921 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
6922 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006923 if (Op.getResNo() != 0 || NeedOF || NeedCF)
6924 // Emit a CMP with 0, which is the TEST pattern.
6925 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
6926 DAG.getConstant(0, Op.getValueType()));
6927
6928 unsigned Opcode = 0;
6929 unsigned NumOperands = 0;
6930 switch (Op.getNode()->getOpcode()) {
6931 case ISD::ADD:
6932 // Due to an isel shortcoming, be conservative if this add is likely to be
6933 // selected as part of a load-modify-store instruction. When the root node
6934 // in a match is a store, isel doesn't know how to remap non-chain non-flag
6935 // uses of other nodes in the match, such as the ADD in this case. This
6936 // leads to the ADD being left around and reselected, with the result being
6937 // two adds in the output. Alas, even if none our users are stores, that
6938 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
6939 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
6940 // climbing the DAG back to the root, and it doesn't seem to be worth the
6941 // effort.
6942 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Dan Gohman076aee32009-03-04 19:44:21 +00006943 UE = Op.getNode()->use_end(); UI != UE; ++UI)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006944 if (UI->getOpcode() != ISD::CopyToReg && UI->getOpcode() != ISD::SETCC)
6945 goto default_case;
6946
6947 if (ConstantSDNode *C =
6948 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
6949 // An add of one will be selected as an INC.
6950 if (C->getAPIntValue() == 1) {
6951 Opcode = X86ISD::INC;
6952 NumOperands = 1;
6953 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00006954 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006955
6956 // An add of negative one (subtract of one) will be selected as a DEC.
6957 if (C->getAPIntValue().isAllOnesValue()) {
6958 Opcode = X86ISD::DEC;
6959 NumOperands = 1;
6960 break;
6961 }
Dan Gohman076aee32009-03-04 19:44:21 +00006962 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006963
6964 // Otherwise use a regular EFLAGS-setting add.
6965 Opcode = X86ISD::ADD;
6966 NumOperands = 2;
6967 break;
6968 case ISD::AND: {
6969 // If the primary and result isn't used, don't bother using X86ISD::AND,
6970 // because a TEST instruction will be better.
6971 bool NonFlagUse = false;
6972 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
6973 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
6974 SDNode *User = *UI;
6975 unsigned UOpNo = UI.getOperandNo();
6976 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
6977 // Look pass truncate.
6978 UOpNo = User->use_begin().getOperandNo();
6979 User = *User->use_begin();
6980 }
6981
6982 if (User->getOpcode() != ISD::BRCOND &&
6983 User->getOpcode() != ISD::SETCC &&
6984 (User->getOpcode() != ISD::SELECT || UOpNo != 0)) {
6985 NonFlagUse = true;
6986 break;
6987 }
Dan Gohman076aee32009-03-04 19:44:21 +00006988 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006989
6990 if (!NonFlagUse)
6991 break;
6992 }
6993 // FALL THROUGH
6994 case ISD::SUB:
6995 case ISD::OR:
6996 case ISD::XOR:
6997 // Due to the ISEL shortcoming noted above, be conservative if this op is
6998 // likely to be selected as part of a load-modify-store instruction.
6999 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
7000 UE = Op.getNode()->use_end(); UI != UE; ++UI)
7001 if (UI->getOpcode() == ISD::STORE)
7002 goto default_case;
7003
7004 // Otherwise use a regular EFLAGS-setting instruction.
7005 switch (Op.getNode()->getOpcode()) {
7006 default: llvm_unreachable("unexpected operator!");
7007 case ISD::SUB: Opcode = X86ISD::SUB; break;
7008 case ISD::OR: Opcode = X86ISD::OR; break;
7009 case ISD::XOR: Opcode = X86ISD::XOR; break;
7010 case ISD::AND: Opcode = X86ISD::AND; break;
7011 }
7012
7013 NumOperands = 2;
7014 break;
7015 case X86ISD::ADD:
7016 case X86ISD::SUB:
7017 case X86ISD::INC:
7018 case X86ISD::DEC:
7019 case X86ISD::OR:
7020 case X86ISD::XOR:
7021 case X86ISD::AND:
7022 return SDValue(Op.getNode(), 1);
7023 default:
7024 default_case:
7025 break;
Dan Gohman076aee32009-03-04 19:44:21 +00007026 }
7027
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007028 if (Opcode == 0)
7029 // Emit a CMP with 0, which is the TEST pattern.
7030 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
7031 DAG.getConstant(0, Op.getValueType()));
7032
7033 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
7034 SmallVector<SDValue, 4> Ops;
7035 for (unsigned i = 0; i != NumOperands; ++i)
7036 Ops.push_back(Op.getOperand(i));
7037
7038 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
7039 DAG.ReplaceAllUsesWith(Op, New);
7040 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00007041}
7042
7043/// Emit nodes that will be selected as "cmp Op0,Op1", or something
7044/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00007045SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00007046 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00007047 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
7048 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00007049 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00007050
7051 DebugLoc dl = Op0.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00007052 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00007053}
7054
Evan Chengd40d03e2010-01-06 19:38:29 +00007055/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
7056/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00007057SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
7058 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00007059 SDValue Op0 = And.getOperand(0);
7060 SDValue Op1 = And.getOperand(1);
7061 if (Op0.getOpcode() == ISD::TRUNCATE)
7062 Op0 = Op0.getOperand(0);
7063 if (Op1.getOpcode() == ISD::TRUNCATE)
7064 Op1 = Op1.getOperand(0);
7065
Evan Chengd40d03e2010-01-06 19:38:29 +00007066 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00007067 if (Op1.getOpcode() == ISD::SHL)
7068 std::swap(Op0, Op1);
7069 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00007070 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
7071 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00007072 // If we looked past a truncate, check that it's only truncating away
7073 // known zeros.
7074 unsigned BitWidth = Op0.getValueSizeInBits();
7075 unsigned AndBitWidth = And.getValueSizeInBits();
7076 if (BitWidth > AndBitWidth) {
7077 APInt Mask = APInt::getAllOnesValue(BitWidth), Zeros, Ones;
7078 DAG.ComputeMaskedBits(Op0, Mask, Zeros, Ones);
7079 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
7080 return SDValue();
7081 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00007082 LHS = Op1;
7083 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00007084 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00007085 } else if (Op1.getOpcode() == ISD::Constant) {
7086 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
7087 SDValue AndLHS = Op0;
Evan Chengd40d03e2010-01-06 19:38:29 +00007088 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
7089 LHS = AndLHS.getOperand(0);
7090 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00007091 }
Evan Chengd40d03e2010-01-06 19:38:29 +00007092 }
Evan Cheng0488db92007-09-25 01:57:46 +00007093
Evan Chengd40d03e2010-01-06 19:38:29 +00007094 if (LHS.getNode()) {
Evan Chenge5b51ac2010-04-17 06:13:15 +00007095 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00007096 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00007097 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00007098 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00007099 // Also promote i16 to i32 for performance / code size reason.
7100 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00007101 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00007102 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00007103
Evan Chengd40d03e2010-01-06 19:38:29 +00007104 // If the operand types disagree, extend the shift amount to match. Since
7105 // BT ignores high bits (like shifts) we can use anyextend.
7106 if (LHS.getValueType() != RHS.getValueType())
7107 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00007108
Evan Chengd40d03e2010-01-06 19:38:29 +00007109 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
7110 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
7111 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
7112 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00007113 }
7114
Evan Cheng54de3ea2010-01-05 06:52:31 +00007115 return SDValue();
7116}
7117
Dan Gohmand858e902010-04-17 15:26:15 +00007118SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng54de3ea2010-01-05 06:52:31 +00007119 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
7120 SDValue Op0 = Op.getOperand(0);
7121 SDValue Op1 = Op.getOperand(1);
7122 DebugLoc dl = Op.getDebugLoc();
7123 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
7124
7125 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00007126 // Lower (X & (1 << N)) == 0 to BT(X, N).
7127 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
7128 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Chris Lattner481eebc2010-12-19 21:23:48 +00007129 if (Op0.getOpcode() == ISD::AND && Op0.hasOneUse() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00007130 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane368b462010-06-18 14:22:04 +00007131 cast<ConstantSDNode>(Op1)->isNullValue() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00007132 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
7133 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
7134 if (NewSetCC.getNode())
7135 return NewSetCC;
7136 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00007137
Chris Lattner481eebc2010-12-19 21:23:48 +00007138 // Look for X == 0, X == 1, X != 0, or X != 1. We can simplify some forms of
7139 // these.
7140 if (Op1.getOpcode() == ISD::Constant &&
Evan Cheng2c755ba2010-02-27 07:36:59 +00007141 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
7142 cast<ConstantSDNode>(Op1)->isNullValue()) &&
7143 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007144
Chris Lattner481eebc2010-12-19 21:23:48 +00007145 // If the input is a setcc, then reuse the input setcc or use a new one with
7146 // the inverted condition.
7147 if (Op0.getOpcode() == X86ISD::SETCC) {
7148 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
7149 bool Invert = (CC == ISD::SETNE) ^
7150 cast<ConstantSDNode>(Op1)->isNullValue();
7151 if (!Invert) return Op0;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007152
Evan Cheng2c755ba2010-02-27 07:36:59 +00007153 CCode = X86::GetOppositeBranchCondition(CCode);
Chris Lattner481eebc2010-12-19 21:23:48 +00007154 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
7155 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
7156 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00007157 }
7158
Evan Chenge5b51ac2010-04-17 06:13:15 +00007159 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00007160 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00007161 if (X86CC == X86::COND_INVALID)
7162 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00007163
Chris Lattnerc19d1c32010-12-19 22:08:31 +00007164 SDValue EFLAGS = EmitCmp(Op0, Op1, X86CC, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00007165 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattnerc19d1c32010-12-19 22:08:31 +00007166 DAG.getConstant(X86CC, MVT::i8), EFLAGS);
Evan Cheng0488db92007-09-25 01:57:46 +00007167}
7168
Dan Gohmand858e902010-04-17 15:26:15 +00007169SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00007170 SDValue Cond;
7171 SDValue Op0 = Op.getOperand(0);
7172 SDValue Op1 = Op.getOperand(1);
7173 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00007174 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00007175 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
7176 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007177 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00007178
7179 if (isFP) {
7180 unsigned SSECC = 8;
Owen Andersone50ed302009-08-10 22:56:29 +00007181 EVT VT0 = Op0.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00007182 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
7183 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00007184 bool Swap = false;
7185
7186 switch (SetCCOpcode) {
7187 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00007188 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00007189 case ISD::SETEQ: SSECC = 0; break;
Scott Michelfdc40a02009-02-17 22:15:04 +00007190 case ISD::SETOGT:
Nate Begeman30a0de92008-07-17 16:51:19 +00007191 case ISD::SETGT: Swap = true; // Fallthrough
7192 case ISD::SETLT:
7193 case ISD::SETOLT: SSECC = 1; break;
7194 case ISD::SETOGE:
7195 case ISD::SETGE: Swap = true; // Fallthrough
7196 case ISD::SETLE:
7197 case ISD::SETOLE: SSECC = 2; break;
7198 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00007199 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00007200 case ISD::SETNE: SSECC = 4; break;
7201 case ISD::SETULE: Swap = true;
7202 case ISD::SETUGE: SSECC = 5; break;
7203 case ISD::SETULT: Swap = true;
7204 case ISD::SETUGT: SSECC = 6; break;
7205 case ISD::SETO: SSECC = 7; break;
7206 }
7207 if (Swap)
7208 std::swap(Op0, Op1);
7209
Nate Begemanfb8ead02008-07-25 19:05:58 +00007210 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00007211 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00007212 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00007213 SDValue UNORD, EQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00007214 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
7215 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00007216 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00007217 }
7218 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00007219 SDValue ORD, NEQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00007220 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
7221 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00007222 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00007223 }
Torok Edwinc23197a2009-07-14 16:55:14 +00007224 llvm_unreachable("Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00007225 }
7226 // Handle all other FP comparisons here.
Owen Anderson825b72b2009-08-11 20:47:22 +00007227 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00007228 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007229
Nate Begeman30a0de92008-07-17 16:51:19 +00007230 // We are handling one of the integer comparisons here. Since SSE only has
7231 // GT and EQ comparisons for integer, swapping operands and multiple
7232 // operations may be required for some comparisons.
7233 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
7234 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00007235
Owen Anderson825b72b2009-08-11 20:47:22 +00007236 switch (VT.getSimpleVT().SimpleTy) {
Nate Begeman30a0de92008-07-17 16:51:19 +00007237 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007238 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007239 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007240 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
7241 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00007242 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007243
Nate Begeman30a0de92008-07-17 16:51:19 +00007244 switch (SetCCOpcode) {
7245 default: break;
7246 case ISD::SETNE: Invert = true;
7247 case ISD::SETEQ: Opc = EQOpc; break;
7248 case ISD::SETLT: Swap = true;
7249 case ISD::SETGT: Opc = GTOpc; break;
7250 case ISD::SETGE: Swap = true;
7251 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
7252 case ISD::SETULT: Swap = true;
7253 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
7254 case ISD::SETUGE: Swap = true;
7255 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
7256 }
7257 if (Swap)
7258 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007259
Nate Begeman30a0de92008-07-17 16:51:19 +00007260 // Since SSE has no unsigned integer comparisons, we need to flip the sign
7261 // bits of the inputs before performing those operations.
7262 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00007263 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00007264 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
7265 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00007266 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00007267 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
7268 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00007269 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
7270 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00007271 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007272
Dale Johannesenace16102009-02-03 19:33:06 +00007273 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00007274
7275 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00007276 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00007277 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00007278
Nate Begeman30a0de92008-07-17 16:51:19 +00007279 return Result;
7280}
Evan Cheng0488db92007-09-25 01:57:46 +00007281
Evan Cheng370e5342008-12-03 08:38:43 +00007282// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00007283static bool isX86LogicalCmp(SDValue Op) {
7284 unsigned Opc = Op.getNode()->getOpcode();
7285 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
7286 return true;
7287 if (Op.getResNo() == 1 &&
7288 (Opc == X86ISD::ADD ||
7289 Opc == X86ISD::SUB ||
Chris Lattner5b856542010-12-20 00:59:46 +00007290 Opc == X86ISD::ADC ||
7291 Opc == X86ISD::SBB ||
Dan Gohman076aee32009-03-04 19:44:21 +00007292 Opc == X86ISD::SMUL ||
7293 Opc == X86ISD::UMUL ||
7294 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00007295 Opc == X86ISD::DEC ||
7296 Opc == X86ISD::OR ||
7297 Opc == X86ISD::XOR ||
7298 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00007299 return true;
7300
Chris Lattner9637d5b2010-12-05 07:49:54 +00007301 if (Op.getResNo() == 2 && Opc == X86ISD::UMUL)
7302 return true;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007303
Dan Gohman076aee32009-03-04 19:44:21 +00007304 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00007305}
7306
Chris Lattnera2b56002010-12-05 01:23:24 +00007307static bool isZero(SDValue V) {
7308 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
7309 return C && C->isNullValue();
7310}
7311
Chris Lattner96908b12010-12-05 02:00:51 +00007312static bool isAllOnes(SDValue V) {
7313 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
7314 return C && C->isAllOnesValue();
7315}
7316
Dan Gohmand858e902010-04-17 15:26:15 +00007317SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00007318 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00007319 SDValue Cond = Op.getOperand(0);
Chris Lattnera2b56002010-12-05 01:23:24 +00007320 SDValue Op1 = Op.getOperand(1);
7321 SDValue Op2 = Op.getOperand(2);
7322 DebugLoc DL = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00007323 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00007324
Dan Gohman1a492952009-10-20 16:22:37 +00007325 if (Cond.getOpcode() == ISD::SETCC) {
7326 SDValue NewCond = LowerSETCC(Cond, DAG);
7327 if (NewCond.getNode())
7328 Cond = NewCond;
7329 }
Evan Cheng734503b2006-09-11 02:19:56 +00007330
Chris Lattnera2b56002010-12-05 01:23:24 +00007331 // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00007332 // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
Chris Lattnera2b56002010-12-05 01:23:24 +00007333 // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00007334 // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007335 if (Cond.getOpcode() == X86ISD::SETCC &&
Chris Lattner96908b12010-12-05 02:00:51 +00007336 Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
7337 isZero(Cond.getOperand(1).getOperand(1))) {
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007338 SDValue Cmp = Cond.getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007339
Chris Lattnera2b56002010-12-05 01:23:24 +00007340 unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007341
7342 if ((isAllOnes(Op1) || isAllOnes(Op2)) &&
Chris Lattner96908b12010-12-05 02:00:51 +00007343 (CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
7344 SDValue Y = isAllOnes(Op2) ? Op1 : Op2;
Chris Lattnera2b56002010-12-05 01:23:24 +00007345
7346 SDValue CmpOp0 = Cmp.getOperand(0);
7347 Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32,
7348 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007349
Chris Lattner96908b12010-12-05 02:00:51 +00007350 SDValue Res = // Res = 0 or -1.
Chris Lattnera2b56002010-12-05 01:23:24 +00007351 DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
7352 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007353
Chris Lattner96908b12010-12-05 02:00:51 +00007354 if (isAllOnes(Op1) != (CondCode == X86::COND_E))
7355 Res = DAG.getNOT(DL, Res, Res.getValueType());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007356
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007357 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
Chris Lattnera2b56002010-12-05 01:23:24 +00007358 if (N2C == 0 || !N2C->isNullValue())
7359 Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
7360 return Res;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007361 }
7362 }
7363
Chris Lattnera2b56002010-12-05 01:23:24 +00007364 // Look past (and (setcc_carry (cmp ...)), 1).
Evan Chengad9c0a32009-12-15 00:53:42 +00007365 if (Cond.getOpcode() == ISD::AND &&
7366 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
7367 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00007368 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00007369 Cond = Cond.getOperand(0);
7370 }
7371
Evan Cheng3f41d662007-10-08 22:16:29 +00007372 // If condition flag is set by a X86ISD::CMP, then use it as the condition
7373 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00007374 if (Cond.getOpcode() == X86ISD::SETCC ||
7375 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00007376 CC = Cond.getOperand(0);
7377
Dan Gohman475871a2008-07-27 21:46:04 +00007378 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00007379 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00007380 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00007381
Evan Cheng3f41d662007-10-08 22:16:29 +00007382 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007383 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00007384 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00007385 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00007386
Chris Lattnerd1980a52009-03-12 06:52:53 +00007387 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
7388 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00007389 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00007390 addTest = false;
7391 }
7392 }
7393
7394 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007395 // Look pass the truncate.
7396 if (Cond.getOpcode() == ISD::TRUNCATE)
7397 Cond = Cond.getOperand(0);
7398
7399 // We know the result of AND is compared against zero. Try to match
7400 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00007401 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Chris Lattnera2b56002010-12-05 01:23:24 +00007402 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG);
Evan Chengd40d03e2010-01-06 19:38:29 +00007403 if (NewSetCC.getNode()) {
7404 CC = NewSetCC.getOperand(0);
7405 Cond = NewSetCC.getOperand(1);
7406 addTest = false;
7407 }
7408 }
7409 }
7410
7411 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007412 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00007413 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00007414 }
7415
Benjamin Kramere915ff32010-12-22 23:09:28 +00007416 // a < b ? -1 : 0 -> RES = ~setcc_carry
7417 // a < b ? 0 : -1 -> RES = setcc_carry
7418 // a >= b ? -1 : 0 -> RES = setcc_carry
7419 // a >= b ? 0 : -1 -> RES = ~setcc_carry
7420 if (Cond.getOpcode() == X86ISD::CMP) {
7421 unsigned CondCode = cast<ConstantSDNode>(CC)->getZExtValue();
7422
7423 if ((CondCode == X86::COND_AE || CondCode == X86::COND_B) &&
7424 (isAllOnes(Op1) || isAllOnes(Op2)) && (isZero(Op1) || isZero(Op2))) {
7425 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
7426 DAG.getConstant(X86::COND_B, MVT::i8), Cond);
7427 if (isAllOnes(Op1) != (CondCode == X86::COND_B))
7428 return DAG.getNOT(DL, Res, Res.getValueType());
7429 return Res;
7430 }
7431 }
7432
Evan Cheng0488db92007-09-25 01:57:46 +00007433 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
7434 // condition is true.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007435 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007436 SDValue Ops[] = { Op2, Op1, CC, Cond };
Chris Lattnera2b56002010-12-05 01:23:24 +00007437 return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00007438}
7439
Evan Cheng370e5342008-12-03 08:38:43 +00007440// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
7441// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
7442// from the AND / OR.
7443static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
7444 Opc = Op.getOpcode();
7445 if (Opc != ISD::OR && Opc != ISD::AND)
7446 return false;
7447 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
7448 Op.getOperand(0).hasOneUse() &&
7449 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
7450 Op.getOperand(1).hasOneUse());
7451}
7452
Evan Cheng961d6d42009-02-02 08:19:07 +00007453// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
7454// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00007455static bool isXor1OfSetCC(SDValue Op) {
7456 if (Op.getOpcode() != ISD::XOR)
7457 return false;
7458 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
7459 if (N1C && N1C->getAPIntValue() == 1) {
7460 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
7461 Op.getOperand(0).hasOneUse();
7462 }
7463 return false;
7464}
7465
Dan Gohmand858e902010-04-17 15:26:15 +00007466SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00007467 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00007468 SDValue Chain = Op.getOperand(0);
7469 SDValue Cond = Op.getOperand(1);
7470 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007471 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00007472 SDValue CC;
Evan Cheng734503b2006-09-11 02:19:56 +00007473
Dan Gohman1a492952009-10-20 16:22:37 +00007474 if (Cond.getOpcode() == ISD::SETCC) {
7475 SDValue NewCond = LowerSETCC(Cond, DAG);
7476 if (NewCond.getNode())
7477 Cond = NewCond;
7478 }
Chris Lattnere55484e2008-12-25 05:34:37 +00007479#if 0
7480 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00007481 else if (Cond.getOpcode() == X86ISD::ADD ||
7482 Cond.getOpcode() == X86ISD::SUB ||
7483 Cond.getOpcode() == X86ISD::SMUL ||
7484 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00007485 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00007486#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00007487
Evan Chengad9c0a32009-12-15 00:53:42 +00007488 // Look pass (and (setcc_carry (cmp ...)), 1).
7489 if (Cond.getOpcode() == ISD::AND &&
7490 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
7491 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00007492 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00007493 Cond = Cond.getOperand(0);
7494 }
7495
Evan Cheng3f41d662007-10-08 22:16:29 +00007496 // If condition flag is set by a X86ISD::CMP, then use it as the condition
7497 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00007498 if (Cond.getOpcode() == X86ISD::SETCC ||
7499 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00007500 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007501
Dan Gohman475871a2008-07-27 21:46:04 +00007502 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00007503 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00007504 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00007505 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00007506 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00007507 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00007508 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00007509 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00007510 default: break;
7511 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00007512 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00007513 // These can only come from an arithmetic instruction with overflow,
7514 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00007515 Cond = Cond.getNode()->getOperand(1);
7516 addTest = false;
7517 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00007518 }
Evan Cheng0488db92007-09-25 01:57:46 +00007519 }
Evan Cheng370e5342008-12-03 08:38:43 +00007520 } else {
7521 unsigned CondOpc;
7522 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
7523 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00007524 if (CondOpc == ISD::OR) {
7525 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
7526 // two branches instead of an explicit OR instruction with a
7527 // separate test.
7528 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00007529 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00007530 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007531 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00007532 Chain, Dest, CC, Cmp);
7533 CC = Cond.getOperand(1).getOperand(0);
7534 Cond = Cmp;
7535 addTest = false;
7536 }
7537 } else { // ISD::AND
7538 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
7539 // two branches instead of an explicit AND instruction with a
7540 // separate test. However, we only do this if this block doesn't
7541 // have a fall-through edge, because this requires an explicit
7542 // jmp when the condition is false.
7543 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00007544 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00007545 Op.getNode()->hasOneUse()) {
7546 X86::CondCode CCode =
7547 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
7548 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007549 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00007550 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00007551 // Look for an unconditional branch following this conditional branch.
7552 // We need this because we need to reverse the successors in order
7553 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00007554 if (User->getOpcode() == ISD::BR) {
7555 SDValue FalseBB = User->getOperand(1);
7556 SDNode *NewBR =
7557 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00007558 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00007559 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00007560 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00007561
Dale Johannesene4d209d2009-02-03 20:21:25 +00007562 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00007563 Chain, Dest, CC, Cmp);
7564 X86::CondCode CCode =
7565 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
7566 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007567 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00007568 Cond = Cmp;
7569 addTest = false;
7570 }
7571 }
Dan Gohman279c22e2008-10-21 03:29:32 +00007572 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00007573 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
7574 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
7575 // It should be transformed during dag combiner except when the condition
7576 // is set by a arithmetics with overflow node.
7577 X86::CondCode CCode =
7578 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
7579 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007580 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00007581 Cond = Cond.getOperand(0).getOperand(1);
7582 addTest = false;
Dan Gohman279c22e2008-10-21 03:29:32 +00007583 }
Evan Cheng0488db92007-09-25 01:57:46 +00007584 }
7585
7586 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007587 // Look pass the truncate.
7588 if (Cond.getOpcode() == ISD::TRUNCATE)
7589 Cond = Cond.getOperand(0);
7590
7591 // We know the result of AND is compared against zero. Try to match
7592 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00007593 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007594 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
7595 if (NewSetCC.getNode()) {
7596 CC = NewSetCC.getOperand(0);
7597 Cond = NewSetCC.getOperand(1);
7598 addTest = false;
7599 }
7600 }
7601 }
7602
7603 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007604 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00007605 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00007606 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00007607 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00007608 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00007609}
7610
Anton Korobeynikove060b532007-04-17 19:34:00 +00007611
7612// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
7613// Calls to _alloca is needed to probe the stack when allocating more than 4k
7614// bytes in one go. Touching the stack at 4K increments is necessary to ensure
7615// that the guard pages used by the OS virtual memory manager are allocated in
7616// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00007617SDValue
7618X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00007619 SelectionDAG &DAG) const {
Duncan Sands1e1ca0b2010-10-21 16:02:12 +00007620 assert((Subtarget->isTargetCygMing() || Subtarget->isTargetWindows()) &&
Michael J. Spencere9c253e2010-10-21 01:41:01 +00007621 "This should be used only on Windows targets");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007622 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007623
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007624 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00007625 SDValue Chain = Op.getOperand(0);
7626 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007627 // FIXME: Ensure alignment here
7628
Dan Gohman475871a2008-07-27 21:46:04 +00007629 SDValue Flag;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007630
Owen Anderson825b72b2009-08-11 20:47:22 +00007631 EVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007632
Dale Johannesendd64c412009-02-04 00:33:20 +00007633 Chain = DAG.getCopyToReg(Chain, dl, X86::EAX, Size, Flag);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00007634 Flag = Chain.getValue(1);
7635
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007636 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00007637
Michael J. Spencere9c253e2010-10-21 01:41:01 +00007638 Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag);
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00007639 Flag = Chain.getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007640
Dale Johannesendd64c412009-02-04 00:33:20 +00007641 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007642
Dan Gohman475871a2008-07-27 21:46:04 +00007643 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Dale Johannesene4d209d2009-02-03 20:21:25 +00007644 return DAG.getMergeValues(Ops1, 2, dl);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007645}
7646
Dan Gohmand858e902010-04-17 15:26:15 +00007647SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00007648 MachineFunction &MF = DAG.getMachineFunction();
7649 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
7650
Dan Gohman69de1932008-02-06 22:27:42 +00007651 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00007652 DebugLoc DL = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00007653
Anton Korobeynikove7beda12010-10-03 22:52:07 +00007654 if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00007655 // vastart just stores the address of the VarArgsFrameIndex slot into the
7656 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +00007657 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
7658 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00007659 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
7660 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007661 }
7662
7663 // __va_list_tag:
7664 // gp_offset (0 - 6 * 8)
7665 // fp_offset (48 - 48 + 8 * 16)
7666 // overflow_arg_area (point to parameters coming in memory).
7667 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00007668 SmallVector<SDValue, 8> MemOps;
7669 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00007670 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00007671 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00007672 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
7673 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007674 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007675 MemOps.push_back(Store);
7676
7677 // Store fp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00007678 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007679 FIN, DAG.getIntPtrConstant(4));
Chris Lattner8026a9d2010-09-21 17:50:43 +00007680 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00007681 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
7682 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007683 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007684 MemOps.push_back(Store);
7685
7686 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +00007687 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007688 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +00007689 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
7690 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00007691 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
7692 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +00007693 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007694 MemOps.push_back(Store);
7695
7696 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +00007697 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007698 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +00007699 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
7700 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00007701 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
7702 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007703 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +00007704 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00007705 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00007706}
7707
Dan Gohmand858e902010-04-17 15:26:15 +00007708SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman320afb82010-10-12 18:00:49 +00007709 assert(Subtarget->is64Bit() &&
7710 "LowerVAARG only handles 64-bit va_arg!");
7711 assert((Subtarget->isTargetLinux() ||
7712 Subtarget->isTargetDarwin()) &&
7713 "Unhandled target in LowerVAARG");
7714 assert(Op.getNode()->getNumOperands() == 4);
7715 SDValue Chain = Op.getOperand(0);
7716 SDValue SrcPtr = Op.getOperand(1);
7717 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
7718 unsigned Align = Op.getConstantOperandVal(3);
7719 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9018e832008-05-10 01:26:14 +00007720
Dan Gohman320afb82010-10-12 18:00:49 +00007721 EVT ArgVT = Op.getNode()->getValueType(0);
7722 const Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
7723 uint32_t ArgSize = getTargetData()->getTypeAllocSize(ArgTy);
7724 uint8_t ArgMode;
7725
7726 // Decide which area this value should be read from.
7727 // TODO: Implement the AMD64 ABI in its entirety. This simple
7728 // selection mechanism works only for the basic types.
7729 if (ArgVT == MVT::f80) {
7730 llvm_unreachable("va_arg for f80 not yet implemented");
7731 } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) {
7732 ArgMode = 2; // Argument passed in XMM register. Use fp_offset.
7733 } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) {
7734 ArgMode = 1; // Argument passed in GPR64 register(s). Use gp_offset.
7735 } else {
7736 llvm_unreachable("Unhandled argument type in LowerVAARG");
7737 }
7738
7739 if (ArgMode == 2) {
7740 // Sanity Check: Make sure using fp_offset makes sense.
Michael J. Spencer87b86652010-10-19 07:32:42 +00007741 assert(!UseSoftFloat &&
Eric Christopher52b45052010-10-12 19:44:17 +00007742 !(DAG.getMachineFunction()
7743 .getFunction()->hasFnAttr(Attribute::NoImplicitFloat)) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00007744 Subtarget->hasXMM());
Dan Gohman320afb82010-10-12 18:00:49 +00007745 }
7746
7747 // Insert VAARG_64 node into the DAG
7748 // VAARG_64 returns two values: Variable Argument Address, Chain
7749 SmallVector<SDValue, 11> InstOps;
7750 InstOps.push_back(Chain);
7751 InstOps.push_back(SrcPtr);
7752 InstOps.push_back(DAG.getConstant(ArgSize, MVT::i32));
7753 InstOps.push_back(DAG.getConstant(ArgMode, MVT::i8));
7754 InstOps.push_back(DAG.getConstant(Align, MVT::i32));
7755 SDVTList VTs = DAG.getVTList(getPointerTy(), MVT::Other);
7756 SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl,
7757 VTs, &InstOps[0], InstOps.size(),
7758 MVT::i64,
7759 MachinePointerInfo(SV),
7760 /*Align=*/0,
7761 /*Volatile=*/false,
7762 /*ReadMem=*/true,
7763 /*WriteMem=*/true);
7764 Chain = VAARG.getValue(1);
7765
7766 // Load the next argument and return it
7767 return DAG.getLoad(ArgVT, dl,
7768 Chain,
7769 VAARG,
7770 MachinePointerInfo(),
7771 false, false, 0);
Dan Gohman9018e832008-05-10 01:26:14 +00007772}
7773
Dan Gohmand858e902010-04-17 15:26:15 +00007774SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) const {
Evan Chengae642192007-03-02 23:16:35 +00007775 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00007776 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00007777 SDValue Chain = Op.getOperand(0);
7778 SDValue DstPtr = Op.getOperand(1);
7779 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00007780 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
7781 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Chris Lattnere72f2022010-09-21 05:40:29 +00007782 DebugLoc DL = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00007783
Chris Lattnere72f2022010-09-21 05:40:29 +00007784 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +00007785 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Michael J. Spencerec38de22010-10-10 22:04:20 +00007786 false,
Chris Lattnere72f2022010-09-21 05:40:29 +00007787 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +00007788}
7789
Dan Gohman475871a2008-07-27 21:46:04 +00007790SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007791X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007792 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007793 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007794 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00007795 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00007796 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00007797 case Intrinsic::x86_sse_comieq_ss:
7798 case Intrinsic::x86_sse_comilt_ss:
7799 case Intrinsic::x86_sse_comile_ss:
7800 case Intrinsic::x86_sse_comigt_ss:
7801 case Intrinsic::x86_sse_comige_ss:
7802 case Intrinsic::x86_sse_comineq_ss:
7803 case Intrinsic::x86_sse_ucomieq_ss:
7804 case Intrinsic::x86_sse_ucomilt_ss:
7805 case Intrinsic::x86_sse_ucomile_ss:
7806 case Intrinsic::x86_sse_ucomigt_ss:
7807 case Intrinsic::x86_sse_ucomige_ss:
7808 case Intrinsic::x86_sse_ucomineq_ss:
7809 case Intrinsic::x86_sse2_comieq_sd:
7810 case Intrinsic::x86_sse2_comilt_sd:
7811 case Intrinsic::x86_sse2_comile_sd:
7812 case Intrinsic::x86_sse2_comigt_sd:
7813 case Intrinsic::x86_sse2_comige_sd:
7814 case Intrinsic::x86_sse2_comineq_sd:
7815 case Intrinsic::x86_sse2_ucomieq_sd:
7816 case Intrinsic::x86_sse2_ucomilt_sd:
7817 case Intrinsic::x86_sse2_ucomile_sd:
7818 case Intrinsic::x86_sse2_ucomigt_sd:
7819 case Intrinsic::x86_sse2_ucomige_sd:
7820 case Intrinsic::x86_sse2_ucomineq_sd: {
7821 unsigned Opc = 0;
7822 ISD::CondCode CC = ISD::SETCC_INVALID;
7823 switch (IntNo) {
7824 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007825 case Intrinsic::x86_sse_comieq_ss:
7826 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007827 Opc = X86ISD::COMI;
7828 CC = ISD::SETEQ;
7829 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00007830 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007831 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007832 Opc = X86ISD::COMI;
7833 CC = ISD::SETLT;
7834 break;
7835 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007836 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007837 Opc = X86ISD::COMI;
7838 CC = ISD::SETLE;
7839 break;
7840 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007841 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007842 Opc = X86ISD::COMI;
7843 CC = ISD::SETGT;
7844 break;
7845 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007846 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007847 Opc = X86ISD::COMI;
7848 CC = ISD::SETGE;
7849 break;
7850 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007851 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007852 Opc = X86ISD::COMI;
7853 CC = ISD::SETNE;
7854 break;
7855 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007856 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007857 Opc = X86ISD::UCOMI;
7858 CC = ISD::SETEQ;
7859 break;
7860 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007861 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007862 Opc = X86ISD::UCOMI;
7863 CC = ISD::SETLT;
7864 break;
7865 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007866 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007867 Opc = X86ISD::UCOMI;
7868 CC = ISD::SETLE;
7869 break;
7870 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007871 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007872 Opc = X86ISD::UCOMI;
7873 CC = ISD::SETGT;
7874 break;
7875 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007876 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007877 Opc = X86ISD::UCOMI;
7878 CC = ISD::SETGE;
7879 break;
7880 case Intrinsic::x86_sse_ucomineq_ss:
7881 case Intrinsic::x86_sse2_ucomineq_sd:
7882 Opc = X86ISD::UCOMI;
7883 CC = ISD::SETNE;
7884 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00007885 }
Evan Cheng734503b2006-09-11 02:19:56 +00007886
Dan Gohman475871a2008-07-27 21:46:04 +00007887 SDValue LHS = Op.getOperand(1);
7888 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00007889 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00007890 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00007891 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
7892 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
7893 DAG.getConstant(X86CC, MVT::i8), Cond);
7894 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00007895 }
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007896 // ptest and testp intrinsics. The intrinsic these come from are designed to
7897 // return an integer value, not just an instruction so lower it to the ptest
7898 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00007899 case Intrinsic::x86_sse41_ptestz:
7900 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007901 case Intrinsic::x86_sse41_ptestnzc:
7902 case Intrinsic::x86_avx_ptestz_256:
7903 case Intrinsic::x86_avx_ptestc_256:
7904 case Intrinsic::x86_avx_ptestnzc_256:
7905 case Intrinsic::x86_avx_vtestz_ps:
7906 case Intrinsic::x86_avx_vtestc_ps:
7907 case Intrinsic::x86_avx_vtestnzc_ps:
7908 case Intrinsic::x86_avx_vtestz_pd:
7909 case Intrinsic::x86_avx_vtestc_pd:
7910 case Intrinsic::x86_avx_vtestnzc_pd:
7911 case Intrinsic::x86_avx_vtestz_ps_256:
7912 case Intrinsic::x86_avx_vtestc_ps_256:
7913 case Intrinsic::x86_avx_vtestnzc_ps_256:
7914 case Intrinsic::x86_avx_vtestz_pd_256:
7915 case Intrinsic::x86_avx_vtestc_pd_256:
7916 case Intrinsic::x86_avx_vtestnzc_pd_256: {
7917 bool IsTestPacked = false;
Eric Christopher71c67532009-07-29 00:28:05 +00007918 unsigned X86CC = 0;
7919 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00007920 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007921 case Intrinsic::x86_avx_vtestz_ps:
7922 case Intrinsic::x86_avx_vtestz_pd:
7923 case Intrinsic::x86_avx_vtestz_ps_256:
7924 case Intrinsic::x86_avx_vtestz_pd_256:
7925 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00007926 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007927 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +00007928 // ZF = 1
7929 X86CC = X86::COND_E;
7930 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007931 case Intrinsic::x86_avx_vtestc_ps:
7932 case Intrinsic::x86_avx_vtestc_pd:
7933 case Intrinsic::x86_avx_vtestc_ps_256:
7934 case Intrinsic::x86_avx_vtestc_pd_256:
7935 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00007936 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007937 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00007938 // CF = 1
7939 X86CC = X86::COND_B;
7940 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007941 case Intrinsic::x86_avx_vtestnzc_ps:
7942 case Intrinsic::x86_avx_vtestnzc_pd:
7943 case Intrinsic::x86_avx_vtestnzc_ps_256:
7944 case Intrinsic::x86_avx_vtestnzc_pd_256:
7945 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +00007946 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007947 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00007948 // ZF and CF = 0
7949 X86CC = X86::COND_A;
7950 break;
7951 }
Eric Christopherfd179292009-08-27 18:07:15 +00007952
Eric Christopher71c67532009-07-29 00:28:05 +00007953 SDValue LHS = Op.getOperand(1);
7954 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007955 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
7956 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00007957 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
7958 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
7959 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +00007960 }
Evan Cheng5759f972008-05-04 09:15:50 +00007961
7962 // Fix vector shift instructions where the last operand is a non-immediate
7963 // i32 value.
7964 case Intrinsic::x86_sse2_pslli_w:
7965 case Intrinsic::x86_sse2_pslli_d:
7966 case Intrinsic::x86_sse2_pslli_q:
7967 case Intrinsic::x86_sse2_psrli_w:
7968 case Intrinsic::x86_sse2_psrli_d:
7969 case Intrinsic::x86_sse2_psrli_q:
7970 case Intrinsic::x86_sse2_psrai_w:
7971 case Intrinsic::x86_sse2_psrai_d:
7972 case Intrinsic::x86_mmx_pslli_w:
7973 case Intrinsic::x86_mmx_pslli_d:
7974 case Intrinsic::x86_mmx_pslli_q:
7975 case Intrinsic::x86_mmx_psrli_w:
7976 case Intrinsic::x86_mmx_psrli_d:
7977 case Intrinsic::x86_mmx_psrli_q:
7978 case Intrinsic::x86_mmx_psrai_w:
7979 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00007980 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00007981 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00007982 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00007983
7984 unsigned NewIntNo = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00007985 EVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00007986 switch (IntNo) {
7987 case Intrinsic::x86_sse2_pslli_w:
7988 NewIntNo = Intrinsic::x86_sse2_psll_w;
7989 break;
7990 case Intrinsic::x86_sse2_pslli_d:
7991 NewIntNo = Intrinsic::x86_sse2_psll_d;
7992 break;
7993 case Intrinsic::x86_sse2_pslli_q:
7994 NewIntNo = Intrinsic::x86_sse2_psll_q;
7995 break;
7996 case Intrinsic::x86_sse2_psrli_w:
7997 NewIntNo = Intrinsic::x86_sse2_psrl_w;
7998 break;
7999 case Intrinsic::x86_sse2_psrli_d:
8000 NewIntNo = Intrinsic::x86_sse2_psrl_d;
8001 break;
8002 case Intrinsic::x86_sse2_psrli_q:
8003 NewIntNo = Intrinsic::x86_sse2_psrl_q;
8004 break;
8005 case Intrinsic::x86_sse2_psrai_w:
8006 NewIntNo = Intrinsic::x86_sse2_psra_w;
8007 break;
8008 case Intrinsic::x86_sse2_psrai_d:
8009 NewIntNo = Intrinsic::x86_sse2_psra_d;
8010 break;
8011 default: {
Owen Anderson825b72b2009-08-11 20:47:22 +00008012 ShAmtVT = MVT::v2i32;
Evan Cheng5759f972008-05-04 09:15:50 +00008013 switch (IntNo) {
8014 case Intrinsic::x86_mmx_pslli_w:
8015 NewIntNo = Intrinsic::x86_mmx_psll_w;
8016 break;
8017 case Intrinsic::x86_mmx_pslli_d:
8018 NewIntNo = Intrinsic::x86_mmx_psll_d;
8019 break;
8020 case Intrinsic::x86_mmx_pslli_q:
8021 NewIntNo = Intrinsic::x86_mmx_psll_q;
8022 break;
8023 case Intrinsic::x86_mmx_psrli_w:
8024 NewIntNo = Intrinsic::x86_mmx_psrl_w;
8025 break;
8026 case Intrinsic::x86_mmx_psrli_d:
8027 NewIntNo = Intrinsic::x86_mmx_psrl_d;
8028 break;
8029 case Intrinsic::x86_mmx_psrli_q:
8030 NewIntNo = Intrinsic::x86_mmx_psrl_q;
8031 break;
8032 case Intrinsic::x86_mmx_psrai_w:
8033 NewIntNo = Intrinsic::x86_mmx_psra_w;
8034 break;
8035 case Intrinsic::x86_mmx_psrai_d:
8036 NewIntNo = Intrinsic::x86_mmx_psra_d;
8037 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00008038 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng5759f972008-05-04 09:15:50 +00008039 }
8040 break;
8041 }
8042 }
Mon P Wangefa42202009-09-03 19:56:25 +00008043
8044 // The vector shift intrinsics with scalars uses 32b shift amounts but
8045 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
8046 // to be zero.
8047 SDValue ShOps[4];
8048 ShOps[0] = ShAmt;
8049 ShOps[1] = DAG.getConstant(0, MVT::i32);
8050 if (ShAmtVT == MVT::v4i32) {
8051 ShOps[2] = DAG.getUNDEF(MVT::i32);
8052 ShOps[3] = DAG.getUNDEF(MVT::i32);
8053 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 4);
8054 } else {
8055 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 2);
Dale Johannesen0488fb62010-09-30 23:57:10 +00008056// FIXME this must be lowered to get rid of the invalid type.
Mon P Wangefa42202009-09-03 19:56:25 +00008057 }
8058
Owen Andersone50ed302009-08-10 22:56:29 +00008059 EVT VT = Op.getValueType();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008060 ShAmt = DAG.getNode(ISD::BITCAST, dl, VT, ShAmt);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008061 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008062 DAG.getConstant(NewIntNo, MVT::i32),
Evan Cheng5759f972008-05-04 09:15:50 +00008063 Op.getOperand(1), ShAmt);
8064 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00008065 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008066}
Evan Cheng72261582005-12-20 06:22:03 +00008067
Dan Gohmand858e902010-04-17 15:26:15 +00008068SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
8069 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +00008070 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
8071 MFI->setReturnAddressIsTaken(true);
8072
Bill Wendling64e87322009-01-16 19:25:27 +00008073 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008074 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00008075
8076 if (Depth > 0) {
8077 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
8078 SDValue Offset =
8079 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +00008080 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008081 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00008082 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008083 FrameAddr, Offset),
Chris Lattner51abfe42010-09-21 06:02:19 +00008084 MachinePointerInfo(), false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +00008085 }
8086
8087 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00008088 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00008089 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Chris Lattner51abfe42010-09-21 06:02:19 +00008090 RetAddrFI, MachinePointerInfo(), false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00008091}
8092
Dan Gohmand858e902010-04-17 15:26:15 +00008093SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +00008094 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
8095 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00008096
Owen Andersone50ed302009-08-10 22:56:29 +00008097 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008098 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00008099 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
8100 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00008101 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00008102 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +00008103 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
8104 MachinePointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +00008105 false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00008106 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00008107}
8108
Dan Gohman475871a2008-07-27 21:46:04 +00008109SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008110 SelectionDAG &DAG) const {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00008111 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008112}
8113
Dan Gohmand858e902010-04-17 15:26:15 +00008114SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008115 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00008116 SDValue Chain = Op.getOperand(0);
8117 SDValue Offset = Op.getOperand(1);
8118 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008119 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008120
Dan Gohmand8816272010-08-11 18:14:00 +00008121 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
8122 Subtarget->is64Bit() ? X86::RBP : X86::EBP,
8123 getPointerTy());
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008124 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008125
Dan Gohmand8816272010-08-11 18:14:00 +00008126 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Frame,
8127 DAG.getIntPtrConstant(TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008128 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +00008129 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
8130 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00008131 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008132 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008133
Dale Johannesene4d209d2009-02-03 20:21:25 +00008134 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008135 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008136 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008137}
8138
Dan Gohman475871a2008-07-27 21:46:04 +00008139SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008140 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00008141 SDValue Root = Op.getOperand(0);
8142 SDValue Trmp = Op.getOperand(1); // trampoline
8143 SDValue FPtr = Op.getOperand(2); // nested function
8144 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008145 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008146
Dan Gohman69de1932008-02-06 22:27:42 +00008147 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008148
8149 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00008150 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00008151
8152 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +00008153 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
8154 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +00008155
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00008156 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
8157 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00008158
8159 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
8160
8161 // Load the pointer to the nested function into R11.
8162 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00008163 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00008164 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008165 Addr, MachinePointerInfo(TrmpAddr),
8166 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008167
Owen Anderson825b72b2009-08-11 20:47:22 +00008168 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8169 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008170 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
8171 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +00008172 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00008173
8174 // Load the 'nest' parameter value into R10.
8175 // R10 is specified in X86CallingConv.td
8176 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +00008177 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8178 DAG.getConstant(10, MVT::i64));
8179 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008180 Addr, MachinePointerInfo(TrmpAddr, 10),
8181 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008182
Owen Anderson825b72b2009-08-11 20:47:22 +00008183 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8184 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008185 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
8186 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +00008187 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00008188
8189 // Jump to the nested function.
8190 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +00008191 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8192 DAG.getConstant(20, MVT::i64));
8193 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008194 Addr, MachinePointerInfo(TrmpAddr, 20),
8195 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008196
8197 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +00008198 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8199 DAG.getConstant(22, MVT::i64));
8200 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00008201 MachinePointerInfo(TrmpAddr, 22),
8202 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008203
Dan Gohman475871a2008-07-27 21:46:04 +00008204 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00008205 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008206 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008207 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00008208 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00008209 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00008210 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00008211 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008212
8213 switch (CC) {
8214 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00008215 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +00008216 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00008217 case CallingConv::X86_StdCall: {
8218 // Pass 'nest' parameter in ECX.
8219 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00008220 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008221
8222 // Check that ECX wasn't needed by an 'inreg' parameter.
8223 const FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00008224 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008225
Chris Lattner58d74912008-03-12 17:45:29 +00008226 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00008227 unsigned InRegCount = 0;
8228 unsigned Idx = 1;
8229
8230 for (FunctionType::param_iterator I = FTy->param_begin(),
8231 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00008232 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00008233 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00008234 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008235
8236 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +00008237 report_fatal_error("Nest register in use - reduce number of inreg"
8238 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +00008239 }
8240 }
8241 break;
8242 }
8243 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +00008244 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00008245 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00008246 // Pass 'nest' parameter in EAX.
8247 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00008248 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008249 break;
8250 }
8251
Dan Gohman475871a2008-07-27 21:46:04 +00008252 SDValue OutChains[4];
8253 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008254
Owen Anderson825b72b2009-08-11 20:47:22 +00008255 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8256 DAG.getConstant(10, MVT::i32));
8257 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008258
Chris Lattnera62fe662010-02-05 19:20:30 +00008259 // This is storing the opcode for MOV32ri.
8260 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00008261 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00008262 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008263 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008264 Trmp, MachinePointerInfo(TrmpAddr),
8265 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008266
Owen Anderson825b72b2009-08-11 20:47:22 +00008267 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8268 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008269 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
8270 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +00008271 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008272
Chris Lattnera62fe662010-02-05 19:20:30 +00008273 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +00008274 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8275 DAG.getConstant(5, MVT::i32));
8276 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00008277 MachinePointerInfo(TrmpAddr, 5),
8278 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008279
Owen Anderson825b72b2009-08-11 20:47:22 +00008280 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8281 DAG.getConstant(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008282 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
8283 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +00008284 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008285
Dan Gohman475871a2008-07-27 21:46:04 +00008286 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00008287 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008288 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008289 }
8290}
8291
Dan Gohmand858e902010-04-17 15:26:15 +00008292SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
8293 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008294 /*
8295 The rounding mode is in bits 11:10 of FPSR, and has the following
8296 settings:
8297 00 Round to nearest
8298 01 Round to -inf
8299 10 Round to +inf
8300 11 Round to 0
8301
8302 FLT_ROUNDS, on the other hand, expects the following:
8303 -1 Undefined
8304 0 Round to 0
8305 1 Round to nearest
8306 2 Round to +inf
8307 3 Round to -inf
8308
8309 To perform the conversion, we do:
8310 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
8311 */
8312
8313 MachineFunction &MF = DAG.getMachineFunction();
8314 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00008315 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008316 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00008317 EVT VT = Op.getValueType();
Chris Lattner2156b792010-09-22 01:11:26 +00008318 DebugLoc DL = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008319
8320 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +00008321 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +00008322 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008323
Michael J. Spencerec38de22010-10-10 22:04:20 +00008324
Chris Lattner2156b792010-09-22 01:11:26 +00008325 MachineMemOperand *MMO =
8326 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8327 MachineMemOperand::MOStore, 2, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008328
Chris Lattner2156b792010-09-22 01:11:26 +00008329 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
8330 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
8331 DAG.getVTList(MVT::Other),
8332 Ops, 2, MVT::i16, MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008333
8334 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +00008335 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Chris Lattner51abfe42010-09-21 06:02:19 +00008336 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008337
8338 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00008339 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +00008340 DAG.getNode(ISD::SRL, DL, MVT::i16,
8341 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00008342 CWD, DAG.getConstant(0x800, MVT::i16)),
8343 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00008344 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +00008345 DAG.getNode(ISD::SRL, DL, MVT::i16,
8346 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00008347 CWD, DAG.getConstant(0x400, MVT::i16)),
8348 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008349
Dan Gohman475871a2008-07-27 21:46:04 +00008350 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +00008351 DAG.getNode(ISD::AND, DL, MVT::i16,
8352 DAG.getNode(ISD::ADD, DL, MVT::i16,
8353 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +00008354 DAG.getConstant(1, MVT::i16)),
8355 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008356
8357
Duncan Sands83ec4b62008-06-06 12:08:01 +00008358 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +00008359 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008360}
8361
Dan Gohmand858e902010-04-17 15:26:15 +00008362SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008363 EVT VT = Op.getValueType();
8364 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008365 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008366 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00008367
8368 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008369 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00008370 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +00008371 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00008372 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008373 }
Evan Cheng18efe262007-12-14 02:13:44 +00008374
Evan Cheng152804e2007-12-14 08:30:15 +00008375 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008376 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008377 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00008378
8379 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00008380 SDValue Ops[] = {
8381 Op,
8382 DAG.getConstant(NumBits+NumBits-1, OpVT),
8383 DAG.getConstant(X86::COND_E, MVT::i8),
8384 Op.getValue(1)
8385 };
8386 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00008387
8388 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +00008389 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +00008390
Owen Anderson825b72b2009-08-11 20:47:22 +00008391 if (VT == MVT::i8)
8392 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008393 return Op;
8394}
8395
Dan Gohmand858e902010-04-17 15:26:15 +00008396SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008397 EVT VT = Op.getValueType();
8398 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008399 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008400 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00008401
8402 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008403 if (VT == MVT::i8) {
8404 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00008405 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008406 }
Evan Cheng152804e2007-12-14 08:30:15 +00008407
8408 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008409 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008410 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00008411
8412 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00008413 SDValue Ops[] = {
8414 Op,
8415 DAG.getConstant(NumBits, OpVT),
8416 DAG.getConstant(X86::COND_E, MVT::i8),
8417 Op.getValue(1)
8418 };
8419 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00008420
Owen Anderson825b72b2009-08-11 20:47:22 +00008421 if (VT == MVT::i8)
8422 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008423 return Op;
8424}
8425
Dan Gohmand858e902010-04-17 15:26:15 +00008426SDValue X86TargetLowering::LowerMUL_V2I64(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008427 EVT VT = Op.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00008428 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008429 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00008430
Mon P Wangaf9b9522008-12-18 21:42:19 +00008431 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
8432 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
8433 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
8434 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
8435 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
8436 //
8437 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
8438 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
8439 // return AloBlo + AloBhi + AhiBlo;
8440
8441 SDValue A = Op.getOperand(0);
8442 SDValue B = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008443
Dale Johannesene4d209d2009-02-03 20:21:25 +00008444 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008445 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
8446 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008447 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008448 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
8449 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008450 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008451 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008452 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008453 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008454 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008455 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008456 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008457 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008458 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008459 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008460 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
8461 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008462 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008463 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
8464 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008465 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
8466 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +00008467 return Res;
8468}
8469
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008470SDValue X86TargetLowering::LowerSHL(SDValue Op, SelectionDAG &DAG) const {
8471 EVT VT = Op.getValueType();
8472 DebugLoc dl = Op.getDebugLoc();
8473 SDValue R = Op.getOperand(0);
8474
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008475 LLVMContext *Context = DAG.getContext();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008476
Nate Begeman51409212010-07-28 00:21:48 +00008477 assert(Subtarget->hasSSE41() && "Cannot lower SHL without SSE4.1 or later");
8478
8479 if (VT == MVT::v4i32) {
8480 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8481 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
8482 Op.getOperand(1), DAG.getConstant(23, MVT::i32));
8483
8484 ConstantInt *CI = ConstantInt::get(*Context, APInt(32, 0x3f800000U));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008485
Nate Begeman51409212010-07-28 00:21:48 +00008486 std::vector<Constant*> CV(4, CI);
8487 Constant *C = ConstantVector::get(CV);
8488 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8489 SDValue Addend = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008490 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +00008491 false, false, 16);
8492
8493 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Addend);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008494 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op);
Nate Begeman51409212010-07-28 00:21:48 +00008495 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
8496 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
8497 }
8498 if (VT == MVT::v16i8) {
8499 // a = a << 5;
8500 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8501 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
8502 Op.getOperand(1), DAG.getConstant(5, MVT::i32));
8503
8504 ConstantInt *CM1 = ConstantInt::get(*Context, APInt(8, 15));
8505 ConstantInt *CM2 = ConstantInt::get(*Context, APInt(8, 63));
8506
8507 std::vector<Constant*> CVM1(16, CM1);
8508 std::vector<Constant*> CVM2(16, CM2);
8509 Constant *C = ConstantVector::get(CVM1);
8510 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8511 SDValue M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008512 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +00008513 false, false, 16);
8514
8515 // r = pblendv(r, psllw(r & (char16)15, 4), a);
8516 M = DAG.getNode(ISD::AND, dl, VT, R, M);
8517 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8518 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
8519 DAG.getConstant(4, MVT::i32));
Nate Begeman672fb622010-12-20 22:04:24 +00008520 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT, R, M, Op);
Nate Begeman51409212010-07-28 00:21:48 +00008521 // a += a
8522 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008523
Nate Begeman51409212010-07-28 00:21:48 +00008524 C = ConstantVector::get(CVM2);
8525 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8526 M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008527 MachinePointerInfo::getConstantPool(),
Chris Lattner51abfe42010-09-21 06:02:19 +00008528 false, false, 16);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008529
Nate Begeman51409212010-07-28 00:21:48 +00008530 // r = pblendv(r, psllw(r & (char16)63, 2), a);
8531 M = DAG.getNode(ISD::AND, dl, VT, R, M);
8532 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8533 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
8534 DAG.getConstant(2, MVT::i32));
Nate Begeman672fb622010-12-20 22:04:24 +00008535 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT, R, M, Op);
Nate Begeman51409212010-07-28 00:21:48 +00008536 // a += a
8537 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008538
Nate Begeman51409212010-07-28 00:21:48 +00008539 // return pblendv(r, r+r, a);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008540 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT,
Nate Begeman51409212010-07-28 00:21:48 +00008541 R, DAG.getNode(ISD::ADD, dl, VT, R, R), Op);
8542 return R;
8543 }
8544 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008545}
Mon P Wangaf9b9522008-12-18 21:42:19 +00008546
Dan Gohmand858e902010-04-17 15:26:15 +00008547SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
Bill Wendling74c37652008-12-09 22:08:41 +00008548 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
8549 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +00008550 // looks for this combo and may remove the "setcc" instruction if the "setcc"
8551 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +00008552 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +00008553 SDValue LHS = N->getOperand(0);
8554 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +00008555 unsigned BaseOp = 0;
8556 unsigned Cond = 0;
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008557 DebugLoc DL = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +00008558 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008559 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +00008560 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +00008561 // A subtract of one will be selected as a INC. Note that INC doesn't
8562 // set CF, so we can't do this for UADDO.
8563 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
8564 if (C->getAPIntValue() == 1) {
8565 BaseOp = X86ISD::INC;
8566 Cond = X86::COND_O;
8567 break;
8568 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008569 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +00008570 Cond = X86::COND_O;
8571 break;
8572 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008573 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +00008574 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00008575 break;
8576 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +00008577 // A subtract of one will be selected as a DEC. Note that DEC doesn't
8578 // set CF, so we can't do this for USUBO.
8579 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
8580 if (C->getAPIntValue() == 1) {
8581 BaseOp = X86ISD::DEC;
8582 Cond = X86::COND_O;
8583 break;
8584 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008585 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +00008586 Cond = X86::COND_O;
8587 break;
8588 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008589 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +00008590 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00008591 break;
8592 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00008593 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +00008594 Cond = X86::COND_O;
8595 break;
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008596 case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs
8597 SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0),
8598 MVT::i32);
8599 SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008600
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008601 SDValue SetCC =
8602 DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
8603 DAG.getConstant(X86::COND_O, MVT::i32),
8604 SDValue(Sum.getNode(), 2));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008605
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008606 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
8607 return Sum;
8608 }
Bill Wendling74c37652008-12-09 22:08:41 +00008609 }
Bill Wendling3fafd932008-11-26 22:37:40 +00008610
Bill Wendling61edeb52008-12-02 01:06:39 +00008611 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008612 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008613 SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +00008614
Bill Wendling61edeb52008-12-02 01:06:39 +00008615 SDValue SetCC =
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008616 DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1),
8617 DAG.getConstant(Cond, MVT::i32),
8618 SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +00008619
Bill Wendling61edeb52008-12-02 01:06:39 +00008620 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
8621 return Sum;
Bill Wendling41ea7e72008-11-24 19:21:46 +00008622}
8623
Eric Christopher9a9d2752010-07-22 02:48:34 +00008624SDValue X86TargetLowering::LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG) const{
8625 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00008626
Eric Christopherb6729dc2010-08-04 23:03:04 +00008627 if (!Subtarget->hasSSE2()) {
Eric Christopherc0b2a202010-08-14 21:51:50 +00008628 SDValue Chain = Op.getOperand(0);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008629 SDValue Zero = DAG.getConstant(0,
Eric Christopherb6729dc2010-08-04 23:03:04 +00008630 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Eric Christopherc0b2a202010-08-14 21:51:50 +00008631 SDValue Ops[] = {
8632 DAG.getRegister(X86::ESP, MVT::i32), // Base
8633 DAG.getTargetConstant(1, MVT::i8), // Scale
8634 DAG.getRegister(0, MVT::i32), // Index
8635 DAG.getTargetConstant(0, MVT::i32), // Disp
8636 DAG.getRegister(0, MVT::i32), // Segment.
8637 Zero,
8638 Chain
8639 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00008640 SDNode *Res =
Eric Christopherc0b2a202010-08-14 21:51:50 +00008641 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
8642 array_lengthof(Ops));
8643 return SDValue(Res, 0);
Eric Christopherb6729dc2010-08-04 23:03:04 +00008644 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00008645
Eric Christopher9a9d2752010-07-22 02:48:34 +00008646 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
Chris Lattner132929a2010-08-14 17:26:09 +00008647 if (!isDev)
Eric Christopher9a9d2752010-07-22 02:48:34 +00008648 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008649
Chris Lattner132929a2010-08-14 17:26:09 +00008650 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
8651 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
8652 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
8653 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
Michael J. Spencerec38de22010-10-10 22:04:20 +00008654
Chris Lattner132929a2010-08-14 17:26:09 +00008655 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
8656 if (!Op1 && !Op2 && !Op3 && Op4)
8657 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008658
Chris Lattner132929a2010-08-14 17:26:09 +00008659 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
8660 if (Op1 && !Op2 && !Op3 && !Op4)
8661 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008662
8663 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
Chris Lattner132929a2010-08-14 17:26:09 +00008664 // (MFENCE)>;
8665 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
Eric Christopher9a9d2752010-07-22 02:48:34 +00008666}
8667
Dan Gohmand858e902010-04-17 15:26:15 +00008668SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008669 EVT T = Op.getValueType();
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008670 DebugLoc DL = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +00008671 unsigned Reg = 0;
8672 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00008673 switch(T.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00008674 default:
8675 assert(false && "Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +00008676 case MVT::i8: Reg = X86::AL; size = 1; break;
8677 case MVT::i16: Reg = X86::AX; size = 2; break;
8678 case MVT::i32: Reg = X86::EAX; size = 4; break;
8679 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +00008680 assert(Subtarget->is64Bit() && "Node not type legal!");
8681 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +00008682 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00008683 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008684 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +00008685 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +00008686 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +00008687 Op.getOperand(1),
8688 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +00008689 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +00008690 cpIn.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00008691 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008692 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
8693 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
8694 Ops, 5, T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +00008695 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008696 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +00008697 return cpOut;
8698}
8699
Duncan Sands1607f052008-12-01 11:39:25 +00008700SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008701 SelectionDAG &DAG) const {
Duncan Sands1607f052008-12-01 11:39:25 +00008702 assert(Subtarget->is64Bit() && "Result not type legalized?");
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00008703 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +00008704 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008705 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +00008706 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00008707 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
8708 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +00008709 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +00008710 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
8711 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +00008712 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +00008713 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +00008714 rdx.getValue(1)
8715 };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008716 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008717}
8718
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008719SDValue X86TargetLowering::LowerBITCAST(SDValue Op,
Dale Johannesen7d07b482010-05-21 00:52:33 +00008720 SelectionDAG &DAG) const {
8721 EVT SrcVT = Op.getOperand(0).getValueType();
8722 EVT DstVT = Op.getValueType();
Chris Lattner2a786eb2010-12-19 20:19:20 +00008723 assert(Subtarget->is64Bit() && !Subtarget->hasSSE2() &&
8724 Subtarget->hasMMX() && "Unexpected custom BITCAST");
Michael J. Spencerec38de22010-10-10 22:04:20 +00008725 assert((DstVT == MVT::i64 ||
Dale Johannesen7d07b482010-05-21 00:52:33 +00008726 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008727 "Unexpected custom BITCAST");
Dale Johannesen7d07b482010-05-21 00:52:33 +00008728 // i64 <=> MMX conversions are Legal.
8729 if (SrcVT==MVT::i64 && DstVT.isVector())
8730 return Op;
8731 if (DstVT==MVT::i64 && SrcVT.isVector())
8732 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +00008733 // MMX <=> MMX conversions are Legal.
8734 if (SrcVT.isVector() && DstVT.isVector())
8735 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +00008736 // All other conversions need to be expanded.
8737 return SDValue();
8738}
Chris Lattner5b856542010-12-20 00:59:46 +00008739
Dan Gohmand858e902010-04-17 15:26:15 +00008740SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen71d1bf52008-09-29 22:25:26 +00008741 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +00008742 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008743 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008744 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +00008745 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008746 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008747 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +00008748 Node->getOperand(0),
8749 Node->getOperand(1), negOp,
8750 cast<AtomicSDNode>(Node)->getSrcValue(),
8751 cast<AtomicSDNode>(Node)->getAlignment());
Mon P Wang63307c32008-05-05 19:05:59 +00008752}
8753
Chris Lattner5b856542010-12-20 00:59:46 +00008754static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
8755 EVT VT = Op.getNode()->getValueType(0);
8756
8757 // Let legalize expand this if it isn't a legal type yet.
8758 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
8759 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008760
Chris Lattner5b856542010-12-20 00:59:46 +00008761 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008762
Chris Lattner5b856542010-12-20 00:59:46 +00008763 unsigned Opc;
8764 bool ExtraOp = false;
8765 switch (Op.getOpcode()) {
8766 default: assert(0 && "Invalid code");
8767 case ISD::ADDC: Opc = X86ISD::ADD; break;
8768 case ISD::ADDE: Opc = X86ISD::ADC; ExtraOp = true; break;
8769 case ISD::SUBC: Opc = X86ISD::SUB; break;
8770 case ISD::SUBE: Opc = X86ISD::SBB; ExtraOp = true; break;
8771 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008772
Chris Lattner5b856542010-12-20 00:59:46 +00008773 if (!ExtraOp)
8774 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
8775 Op.getOperand(1));
8776 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
8777 Op.getOperand(1), Op.getOperand(2));
8778}
8779
Evan Cheng0db9fe62006-04-25 20:13:52 +00008780/// LowerOperation - Provide custom lowering hooks for some operations.
8781///
Dan Gohmand858e902010-04-17 15:26:15 +00008782SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00008783 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008784 default: llvm_unreachable("Should not custom lower this!");
Eric Christopher9a9d2752010-07-22 02:48:34 +00008785 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op,DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008786 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
8787 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008788 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00008789 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008790 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
8791 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
8792 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
David Greene91585092011-01-26 15:38:49 +00008793 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op, DAG);
David Greenecfe33c42011-01-26 19:13:22 +00008794 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008795 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
8796 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
8797 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00008798 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +00008799 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +00008800 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008801 case ISD::SHL_PARTS:
8802 case ISD::SRA_PARTS:
8803 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
8804 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00008805 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008806 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00008807 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008808 case ISD::FABS: return LowerFABS(Op, DAG);
8809 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008810 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00008811 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman30a0de92008-07-17 16:51:19 +00008812 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00008813 case ISD::SELECT: return LowerSELECT(Op, DAG);
8814 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008815 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008816 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +00008817 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +00008818 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008819 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00008820 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
8821 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008822 case ISD::FRAME_TO_ARGS_OFFSET:
8823 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008824 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008825 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008826 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +00008827 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +00008828 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
8829 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Mon P Wangaf9b9522008-12-18 21:42:19 +00008830 case ISD::MUL: return LowerMUL_V2I64(Op, DAG);
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008831 case ISD::SHL: return LowerSHL(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +00008832 case ISD::SADDO:
8833 case ISD::UADDO:
8834 case ISD::SSUBO:
8835 case ISD::USUBO:
8836 case ISD::SMULO:
8837 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +00008838 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008839 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Chris Lattner5b856542010-12-20 00:59:46 +00008840 case ISD::ADDC:
8841 case ISD::ADDE:
8842 case ISD::SUBC:
8843 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008844 }
Chris Lattner27a6c732007-11-24 07:07:01 +00008845}
8846
Duncan Sands1607f052008-12-01 11:39:25 +00008847void X86TargetLowering::
8848ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00008849 SelectionDAG &DAG, unsigned NewOp) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008850 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008851 DebugLoc dl = Node->getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00008852 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +00008853
8854 SDValue Chain = Node->getOperand(0);
8855 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00008856 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00008857 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00008858 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00008859 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +00008860 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +00008861 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +00008862 SDValue Result =
8863 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
8864 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +00008865 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +00008866 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00008867 Results.push_back(Result.getValue(2));
8868}
8869
Duncan Sands126d9072008-07-04 11:47:58 +00008870/// ReplaceNodeResults - Replace a node with an illegal result type
8871/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00008872void X86TargetLowering::ReplaceNodeResults(SDNode *N,
8873 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00008874 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +00008875 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +00008876 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +00008877 default:
Duncan Sands1607f052008-12-01 11:39:25 +00008878 assert(false && "Do not know how to custom type legalize this operation!");
8879 return;
Chris Lattner5b856542010-12-20 00:59:46 +00008880 case ISD::ADDC:
8881 case ISD::ADDE:
8882 case ISD::SUBC:
8883 case ISD::SUBE:
8884 // We don't want to expand or promote these.
8885 return;
Duncan Sands1607f052008-12-01 11:39:25 +00008886 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +00008887 std::pair<SDValue,SDValue> Vals =
8888 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +00008889 SDValue FIST = Vals.first, StackSlot = Vals.second;
8890 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +00008891 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +00008892 // Return a load from the stack slot.
Chris Lattner51abfe42010-09-21 06:02:19 +00008893 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
8894 MachinePointerInfo(), false, false, 0));
Duncan Sands1607f052008-12-01 11:39:25 +00008895 }
8896 return;
8897 }
8898 case ISD::READCYCLECOUNTER: {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00008899 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +00008900 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008901 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00008902 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +00008903 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00008904 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00008905 eax.getValue(2));
8906 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
8907 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +00008908 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00008909 Results.push_back(edx.getValue(1));
8910 return;
8911 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008912 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +00008913 EVT T = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008914 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
Duncan Sands1607f052008-12-01 11:39:25 +00008915 SDValue cpInL, cpInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00008916 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
8917 DAG.getConstant(0, MVT::i32));
8918 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
8919 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00008920 cpInL = DAG.getCopyToReg(N->getOperand(0), dl, X86::EAX, cpInL, SDValue());
8921 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl, X86::EDX, cpInH,
Duncan Sands1607f052008-12-01 11:39:25 +00008922 cpInL.getValue(1));
8923 SDValue swapInL, swapInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00008924 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
8925 DAG.getConstant(0, MVT::i32));
8926 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
8927 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00008928 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl, X86::EBX, swapInL,
Duncan Sands1607f052008-12-01 11:39:25 +00008929 cpInH.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00008930 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl, X86::ECX, swapInH,
Duncan Sands1607f052008-12-01 11:39:25 +00008931 swapInL.getValue(1));
8932 SDValue Ops[] = { swapInH.getValue(0),
8933 N->getOperand(1),
8934 swapInH.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00008935 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Andrew Trick1a2cf3b2010-10-11 19:02:04 +00008936 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
8937 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG8_DAG, dl, Tys,
8938 Ops, 3, T, MMO);
Dale Johannesendd64c412009-02-04 00:33:20 +00008939 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl, X86::EAX,
Owen Anderson825b72b2009-08-11 20:47:22 +00008940 MVT::i32, Result.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00008941 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl, X86::EDX,
Owen Anderson825b72b2009-08-11 20:47:22 +00008942 MVT::i32, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +00008943 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Owen Anderson825b72b2009-08-11 20:47:22 +00008944 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00008945 Results.push_back(cpOutH.getValue(1));
8946 return;
8947 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008948 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +00008949 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
8950 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008951 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +00008952 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
8953 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008954 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +00008955 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
8956 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008957 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +00008958 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
8959 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008960 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +00008961 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
8962 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008963 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +00008964 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
8965 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008966 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +00008967 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
8968 return;
Chris Lattner27a6c732007-11-24 07:07:01 +00008969 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00008970}
8971
Evan Cheng72261582005-12-20 06:22:03 +00008972const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
8973 switch (Opcode) {
8974 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +00008975 case X86ISD::BSF: return "X86ISD::BSF";
8976 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +00008977 case X86ISD::SHLD: return "X86ISD::SHLD";
8978 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00008979 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +00008980 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +00008981 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +00008982 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +00008983 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00008984 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00008985 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
8986 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
8987 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00008988 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00008989 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +00008990 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +00008991 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +00008992 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +00008993 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +00008994 case X86ISD::COMI: return "X86ISD::COMI";
8995 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +00008996 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +00008997 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Evan Cheng72261582005-12-20 06:22:03 +00008998 case X86ISD::CMOV: return "X86ISD::CMOV";
8999 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00009000 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00009001 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
9002 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +00009003 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00009004 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +00009005 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +00009006 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +00009007 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +00009008 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
9009 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +00009010 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +00009011 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Nate Begemanb65c1752010-12-17 22:55:37 +00009012 case X86ISD::PANDN: return "X86ISD::PANDN";
9013 case X86ISD::PSIGNB: return "X86ISD::PSIGNB";
9014 case X86ISD::PSIGNW: return "X86ISD::PSIGNW";
9015 case X86ISD::PSIGND: return "X86ISD::PSIGND";
Nate Begeman672fb622010-12-20 22:04:24 +00009016 case X86ISD::PBLENDVB: return "X86ISD::PBLENDVB";
Evan Cheng8ca29322006-11-10 21:43:37 +00009017 case X86ISD::FMAX: return "X86ISD::FMAX";
9018 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +00009019 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
9020 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00009021 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +00009022 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009023 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00009024 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009025 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +00009026 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
9027 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009028 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
9029 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
9030 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
9031 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
9032 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
9033 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +00009034 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
9035 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +00009036 case X86ISD::VSHL: return "X86ISD::VSHL";
9037 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +00009038 case X86ISD::CMPPD: return "X86ISD::CMPPD";
9039 case X86ISD::CMPPS: return "X86ISD::CMPPS";
9040 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
9041 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
9042 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
9043 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
9044 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
9045 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
9046 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
9047 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +00009048 case X86ISD::ADD: return "X86ISD::ADD";
9049 case X86ISD::SUB: return "X86ISD::SUB";
Chris Lattner5b856542010-12-20 00:59:46 +00009050 case X86ISD::ADC: return "X86ISD::ADC";
9051 case X86ISD::SBB: return "X86ISD::SBB";
Bill Wendlingd350e022008-12-12 21:15:41 +00009052 case X86ISD::SMUL: return "X86ISD::SMUL";
9053 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +00009054 case X86ISD::INC: return "X86ISD::INC";
9055 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +00009056 case X86ISD::OR: return "X86ISD::OR";
9057 case X86ISD::XOR: return "X86ISD::XOR";
9058 case X86ISD::AND: return "X86ISD::AND";
Evan Cheng73f24c92009-03-30 21:36:47 +00009059 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +00009060 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009061 case X86ISD::TESTP: return "X86ISD::TESTP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009062 case X86ISD::PALIGN: return "X86ISD::PALIGN";
9063 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
9064 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
9065 case X86ISD::PSHUFHW_LD: return "X86ISD::PSHUFHW_LD";
9066 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
9067 case X86ISD::PSHUFLW_LD: return "X86ISD::PSHUFLW_LD";
9068 case X86ISD::SHUFPS: return "X86ISD::SHUFPS";
9069 case X86ISD::SHUFPD: return "X86ISD::SHUFPD";
9070 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009071 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00009072 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009073 case X86ISD::MOVHLPD: return "X86ISD::MOVHLPD";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00009074 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
9075 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009076 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
9077 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
9078 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
9079 case X86ISD::MOVSHDUP_LD: return "X86ISD::MOVSHDUP_LD";
9080 case X86ISD::MOVSLDUP_LD: return "X86ISD::MOVSLDUP_LD";
9081 case X86ISD::MOVSD: return "X86ISD::MOVSD";
9082 case X86ISD::MOVSS: return "X86ISD::MOVSS";
9083 case X86ISD::UNPCKLPS: return "X86ISD::UNPCKLPS";
9084 case X86ISD::UNPCKLPD: return "X86ISD::UNPCKLPD";
9085 case X86ISD::UNPCKHPS: return "X86ISD::UNPCKHPS";
9086 case X86ISD::UNPCKHPD: return "X86ISD::UNPCKHPD";
9087 case X86ISD::PUNPCKLBW: return "X86ISD::PUNPCKLBW";
9088 case X86ISD::PUNPCKLWD: return "X86ISD::PUNPCKLWD";
9089 case X86ISD::PUNPCKLDQ: return "X86ISD::PUNPCKLDQ";
9090 case X86ISD::PUNPCKLQDQ: return "X86ISD::PUNPCKLQDQ";
9091 case X86ISD::PUNPCKHBW: return "X86ISD::PUNPCKHBW";
9092 case X86ISD::PUNPCKHWD: return "X86ISD::PUNPCKHWD";
9093 case X86ISD::PUNPCKHDQ: return "X86ISD::PUNPCKHDQ";
9094 case X86ISD::PUNPCKHQDQ: return "X86ISD::PUNPCKHQDQ";
Dan Gohmand6708ea2009-08-15 01:38:56 +00009095 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Dan Gohman320afb82010-10-12 18:00:49 +00009096 case X86ISD::VAARG_64: return "X86ISD::VAARG_64";
Michael J. Spencere9c253e2010-10-21 01:41:01 +00009097 case X86ISD::WIN_ALLOCA: return "X86ISD::WIN_ALLOCA";
Evan Cheng72261582005-12-20 06:22:03 +00009098 }
9099}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00009100
Chris Lattnerc9addb72007-03-30 23:15:24 +00009101// isLegalAddressingMode - Return true if the addressing mode represented
9102// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +00009103bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerc9addb72007-03-30 23:15:24 +00009104 const Type *Ty) const {
9105 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009106 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +00009107 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +00009108
Chris Lattnerc9addb72007-03-30 23:15:24 +00009109 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009110 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +00009111 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00009112
Chris Lattnerc9addb72007-03-30 23:15:24 +00009113 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +00009114 unsigned GVFlags =
9115 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009116
Chris Lattnerdfed4132009-07-10 07:38:24 +00009117 // If a reference to this global requires an extra load, we can't fold it.
9118 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +00009119 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009120
Chris Lattnerdfed4132009-07-10 07:38:24 +00009121 // If BaseGV requires a register for the PIC base, we cannot also have a
9122 // BaseReg specified.
9123 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +00009124 return false;
Evan Cheng52787842007-08-01 23:46:47 +00009125
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009126 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +00009127 if ((M != CodeModel::Small || R != Reloc::Static) &&
9128 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009129 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +00009130 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009131
Chris Lattnerc9addb72007-03-30 23:15:24 +00009132 switch (AM.Scale) {
9133 case 0:
9134 case 1:
9135 case 2:
9136 case 4:
9137 case 8:
9138 // These scales always work.
9139 break;
9140 case 3:
9141 case 5:
9142 case 9:
9143 // These scales are formed with basereg+scalereg. Only accept if there is
9144 // no basereg yet.
9145 if (AM.HasBaseReg)
9146 return false;
9147 break;
9148 default: // Other stuff never works.
9149 return false;
9150 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009151
Chris Lattnerc9addb72007-03-30 23:15:24 +00009152 return true;
9153}
9154
9155
Evan Cheng2bd122c2007-10-26 01:56:11 +00009156bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00009157 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +00009158 return false;
Evan Chenge127a732007-10-29 07:57:50 +00009159 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
9160 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00009161 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +00009162 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00009163 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +00009164}
9165
Owen Andersone50ed302009-08-10 22:56:29 +00009166bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +00009167 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009168 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009169 unsigned NumBits1 = VT1.getSizeInBits();
9170 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00009171 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009172 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00009173 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009174}
Evan Cheng2bd122c2007-10-26 01:56:11 +00009175
Dan Gohman97121ba2009-04-08 00:15:30 +00009176bool X86TargetLowering::isZExtFree(const Type *Ty1, const Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00009177 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00009178 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00009179}
9180
Owen Andersone50ed302009-08-10 22:56:29 +00009181bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00009182 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00009183 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00009184}
9185
Owen Andersone50ed302009-08-10 22:56:29 +00009186bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +00009187 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +00009188 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +00009189}
9190
Evan Cheng60c07e12006-07-05 22:17:51 +00009191/// isShuffleMaskLegal - Targets can use this to indicate that they only
9192/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
9193/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
9194/// are assumed to be legal.
9195bool
Eric Christopherfd179292009-08-27 18:07:15 +00009196X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +00009197 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +00009198 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +00009199 if (VT.getSizeInBits() == 64)
Eric Christophercff6f852010-04-15 01:40:20 +00009200 return isPALIGNRMask(M, VT, Subtarget->hasSSSE3());
Nate Begeman9008ca62009-04-27 18:41:29 +00009201
Nate Begemana09008b2009-10-19 02:17:23 +00009202 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +00009203 return (VT.getVectorNumElements() == 2 ||
9204 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
9205 isMOVLMask(M, VT) ||
9206 isSHUFPMask(M, VT) ||
9207 isPSHUFDMask(M, VT) ||
9208 isPSHUFHWMask(M, VT) ||
9209 isPSHUFLWMask(M, VT) ||
Nate Begemana09008b2009-10-19 02:17:23 +00009210 isPALIGNRMask(M, VT, Subtarget->hasSSSE3()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +00009211 isUNPCKLMask(M, VT) ||
9212 isUNPCKHMask(M, VT) ||
9213 isUNPCKL_v_undef_Mask(M, VT) ||
9214 isUNPCKH_v_undef_Mask(M, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00009215}
9216
Dan Gohman7d8143f2008-04-09 20:09:42 +00009217bool
Nate Begeman5a5ca152009-04-29 05:20:52 +00009218X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +00009219 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00009220 unsigned NumElts = VT.getVectorNumElements();
9221 // FIXME: This collection of masks seems suspect.
9222 if (NumElts == 2)
9223 return true;
9224 if (NumElts == 4 && VT.getSizeInBits() == 128) {
9225 return (isMOVLMask(Mask, VT) ||
9226 isCommutedMOVLMask(Mask, VT, true) ||
9227 isSHUFPMask(Mask, VT) ||
9228 isCommutedSHUFPMask(Mask, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00009229 }
9230 return false;
9231}
9232
9233//===----------------------------------------------------------------------===//
9234// X86 Scheduler Hooks
9235//===----------------------------------------------------------------------===//
9236
Mon P Wang63307c32008-05-05 19:05:59 +00009237// private utility function
9238MachineBasicBlock *
9239X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
9240 MachineBasicBlock *MBB,
9241 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009242 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009243 unsigned LoadOpc,
9244 unsigned CXchgOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009245 unsigned notOpc,
9246 unsigned EAXreg,
9247 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009248 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00009249 // For the atomic bitwise operator, we generate
9250 // thisMBB:
9251 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00009252 // ld t1 = [bitinstr.addr]
9253 // op t2 = t1, [bitinstr.val]
9254 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00009255 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
9256 // bz newMBB
9257 // fallthrough -->nextMBB
9258 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9259 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009260 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00009261 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009262
Mon P Wang63307c32008-05-05 19:05:59 +00009263 /// First build the CFG
9264 MachineFunction *F = MBB->getParent();
9265 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009266 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9267 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9268 F->insert(MBBIter, newMBB);
9269 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009270
Dan Gohman14152b42010-07-06 20:24:04 +00009271 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9272 nextMBB->splice(nextMBB->begin(), thisMBB,
9273 llvm::next(MachineBasicBlock::iterator(bInstr)),
9274 thisMBB->end());
9275 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009276
Mon P Wang63307c32008-05-05 19:05:59 +00009277 // Update thisMBB to fall through to newMBB
9278 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009279
Mon P Wang63307c32008-05-05 19:05:59 +00009280 // newMBB jumps to itself and fall through to nextMBB
9281 newMBB->addSuccessor(nextMBB);
9282 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009283
Mon P Wang63307c32008-05-05 19:05:59 +00009284 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009285 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009286 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +00009287 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00009288 MachineOperand& destOper = bInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009289 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00009290 int numArgs = bInstr->getNumOperands() - 1;
9291 for (int i=0; i < numArgs; ++i)
9292 argOpers[i] = &bInstr->getOperand(i+1);
9293
9294 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009295 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009296 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00009297
Dale Johannesen140be2d2008-08-19 18:47:28 +00009298 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009299 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00009300 for (int i=0; i <= lastAddrIndx; ++i)
9301 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009302
Dale Johannesen140be2d2008-08-19 18:47:28 +00009303 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009304 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00009305 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009306 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009307 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009308 tt = t1;
9309
Dale Johannesen140be2d2008-08-19 18:47:28 +00009310 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +00009311 assert((argOpers[valArgIndx]->isReg() ||
9312 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00009313 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +00009314 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009315 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00009316 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009317 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009318 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +00009319 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009320
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009321 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +00009322 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009323
Dale Johannesene4d209d2009-02-03 20:21:25 +00009324 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +00009325 for (int i=0; i <= lastAddrIndx; ++i)
9326 (*MIB).addOperand(*argOpers[i]);
9327 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +00009328 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00009329 (*MIB).setMemRefs(bInstr->memoperands_begin(),
9330 bInstr->memoperands_end());
Mon P Wangf5952662008-07-17 04:54:06 +00009331
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009332 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +00009333 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +00009334
Mon P Wang63307c32008-05-05 19:05:59 +00009335 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009336 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00009337
Dan Gohman14152b42010-07-06 20:24:04 +00009338 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00009339 return nextMBB;
9340}
9341
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00009342// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +00009343MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009344X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
9345 MachineBasicBlock *MBB,
9346 unsigned regOpcL,
9347 unsigned regOpcH,
9348 unsigned immOpcL,
9349 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009350 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009351 // For the atomic bitwise operator, we generate
9352 // thisMBB (instructions are in pairs, except cmpxchg8b)
9353 // ld t1,t2 = [bitinstr.addr]
9354 // newMBB:
9355 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
9356 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +00009357 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009358 // mov ECX, EBX <- t5, t6
9359 // mov EAX, EDX <- t1, t2
9360 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
9361 // mov t3, t4 <- EAX, EDX
9362 // bz newMBB
9363 // result in out1, out2
9364 // fallthrough -->nextMBB
9365
9366 const TargetRegisterClass *RC = X86::GR32RegisterClass;
9367 const unsigned LoadOpc = X86::MOV32rm;
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009368 const unsigned NotOpc = X86::NOT32r;
9369 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9370 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
9371 MachineFunction::iterator MBBIter = MBB;
9372 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009373
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009374 /// First build the CFG
9375 MachineFunction *F = MBB->getParent();
9376 MachineBasicBlock *thisMBB = MBB;
9377 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9378 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9379 F->insert(MBBIter, newMBB);
9380 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009381
Dan Gohman14152b42010-07-06 20:24:04 +00009382 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9383 nextMBB->splice(nextMBB->begin(), thisMBB,
9384 llvm::next(MachineBasicBlock::iterator(bInstr)),
9385 thisMBB->end());
9386 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009387
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009388 // Update thisMBB to fall through to newMBB
9389 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009390
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009391 // newMBB jumps to itself and fall through to nextMBB
9392 newMBB->addSuccessor(nextMBB);
9393 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009394
Dale Johannesene4d209d2009-02-03 20:21:25 +00009395 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009396 // Insert instructions into newMBB based on incoming instruction
9397 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009398 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009399 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009400 MachineOperand& dest1Oper = bInstr->getOperand(0);
9401 MachineOperand& dest2Oper = bInstr->getOperand(1);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009402 MachineOperand* argOpers[2 + X86::AddrNumOperands];
9403 for (int i=0; i < 2 + X86::AddrNumOperands; ++i) {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009404 argOpers[i] = &bInstr->getOperand(i+2);
9405
Dan Gohman71ea4e52010-05-14 21:01:44 +00009406 // We use some of the operands multiple times, so conservatively just
9407 // clear any kill flags that might be present.
9408 if (argOpers[i]->isReg() && argOpers[i]->isUse())
9409 argOpers[i]->setIsKill(false);
9410 }
9411
Evan Chengad5b52f2010-01-08 19:14:57 +00009412 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009413 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +00009414
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009415 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009416 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009417 for (int i=0; i <= lastAddrIndx; ++i)
9418 (*MIB).addOperand(*argOpers[i]);
9419 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009420 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +00009421 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +00009422 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009423 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +00009424 MachineOperand newOp3 = *(argOpers[3]);
9425 if (newOp3.isImm())
9426 newOp3.setImm(newOp3.getImm()+4);
9427 else
9428 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009429 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +00009430 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009431
9432 // t3/4 are defined later, at the bottom of the loop
9433 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
9434 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009435 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009436 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009437 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009438 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
9439
Evan Cheng306b4ca2010-01-08 23:41:50 +00009440 // The subsequent operations should be using the destination registers of
9441 //the PHI instructions.
Scott Michelfdc40a02009-02-17 22:15:04 +00009442 if (invSrc) {
Evan Cheng306b4ca2010-01-08 23:41:50 +00009443 t1 = F->getRegInfo().createVirtualRegister(RC);
9444 t2 = F->getRegInfo().createVirtualRegister(RC);
9445 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t1).addReg(dest1Oper.getReg());
9446 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t2).addReg(dest2Oper.getReg());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009447 } else {
Evan Cheng306b4ca2010-01-08 23:41:50 +00009448 t1 = dest1Oper.getReg();
9449 t2 = dest2Oper.getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009450 }
9451
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009452 int valArgIndx = lastAddrIndx + 1;
9453 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +00009454 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009455 "invalid operand");
9456 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
9457 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009458 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009459 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009460 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009461 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +00009462 if (regOpcL != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00009463 MIB.addReg(t1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009464 (*MIB).addOperand(*argOpers[valArgIndx]);
9465 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00009466 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009467 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00009468 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009469 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009470 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009471 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009472 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +00009473 if (regOpcH != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00009474 MIB.addReg(t2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009475 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
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), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009478 MIB.addReg(t1);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009479 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009480 MIB.addReg(t2);
9481
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009482 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009483 MIB.addReg(t5);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009484 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009485 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +00009486
Dale Johannesene4d209d2009-02-03 20:21:25 +00009487 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009488 for (int i=0; i <= lastAddrIndx; ++i)
9489 (*MIB).addOperand(*argOpers[i]);
9490
9491 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00009492 (*MIB).setMemRefs(bInstr->memoperands_begin(),
9493 bInstr->memoperands_end());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009494
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009495 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009496 MIB.addReg(X86::EAX);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009497 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009498 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +00009499
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009500 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009501 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009502
Dan Gohman14152b42010-07-06 20:24:04 +00009503 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009504 return nextMBB;
9505}
9506
9507// private utility function
9508MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +00009509X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
9510 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009511 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00009512 // For the atomic min/max operator, we generate
9513 // thisMBB:
9514 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00009515 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +00009516 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +00009517 // cmp t1, t2
9518 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +00009519 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00009520 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
9521 // bz newMBB
9522 // fallthrough -->nextMBB
9523 //
9524 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9525 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009526 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00009527 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009528
Mon P Wang63307c32008-05-05 19:05:59 +00009529 /// First build the CFG
9530 MachineFunction *F = MBB->getParent();
9531 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009532 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9533 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9534 F->insert(MBBIter, newMBB);
9535 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009536
Dan Gohman14152b42010-07-06 20:24:04 +00009537 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9538 nextMBB->splice(nextMBB->begin(), thisMBB,
9539 llvm::next(MachineBasicBlock::iterator(mInstr)),
9540 thisMBB->end());
9541 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009542
Mon P Wang63307c32008-05-05 19:05:59 +00009543 // Update thisMBB to fall through to newMBB
9544 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009545
Mon P Wang63307c32008-05-05 19:05:59 +00009546 // newMBB jumps to newMBB and fall through to nextMBB
9547 newMBB->addSuccessor(nextMBB);
9548 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009549
Dale Johannesene4d209d2009-02-03 20:21:25 +00009550 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00009551 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009552 assert(mInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009553 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +00009554 MachineOperand& destOper = mInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009555 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00009556 int numArgs = mInstr->getNumOperands() - 1;
9557 for (int i=0; i < numArgs; ++i)
9558 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009559
Mon P Wang63307c32008-05-05 19:05:59 +00009560 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009561 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009562 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00009563
Mon P Wangab3e7472008-05-05 22:56:23 +00009564 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009565 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00009566 for (int i=0; i <= lastAddrIndx; ++i)
9567 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +00009568
Mon P Wang63307c32008-05-05 19:05:59 +00009569 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +00009570 assert((argOpers[valArgIndx]->isReg() ||
9571 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00009572 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +00009573
9574 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +00009575 if (argOpers[valArgIndx]->isReg())
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009576 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +00009577 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009578 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00009579 (*MIB).addOperand(*argOpers[valArgIndx]);
9580
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009581 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +00009582 MIB.addReg(t1);
9583
Dale Johannesene4d209d2009-02-03 20:21:25 +00009584 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +00009585 MIB.addReg(t1);
9586 MIB.addReg(t2);
9587
9588 // Generate movc
9589 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009590 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +00009591 MIB.addReg(t2);
9592 MIB.addReg(t1);
9593
9594 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +00009595 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +00009596 for (int i=0; i <= lastAddrIndx; ++i)
9597 (*MIB).addOperand(*argOpers[i]);
9598 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +00009599 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00009600 (*MIB).setMemRefs(mInstr->memoperands_begin(),
9601 mInstr->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +00009602
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009603 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +00009604 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +00009605
Mon P Wang63307c32008-05-05 19:05:59 +00009606 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009607 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00009608
Dan Gohman14152b42010-07-06 20:24:04 +00009609 mInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00009610 return nextMBB;
9611}
9612
Eric Christopherf83a5de2009-08-27 18:08:16 +00009613// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00009614// or XMM0_V32I8 in AVX all of this code can be replaced with that
9615// in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +00009616MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +00009617X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +00009618 unsigned numArgs, bool memArg) const {
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00009619 assert((Subtarget->hasSSE42() || Subtarget->hasAVX()) &&
9620 "Target must have SSE4.2 or AVX features enabled");
9621
Eric Christopherb120ab42009-08-18 22:50:32 +00009622 DebugLoc dl = MI->getDebugLoc();
9623 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Eric Christopherb120ab42009-08-18 22:50:32 +00009624 unsigned Opc;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00009625 if (!Subtarget->hasAVX()) {
9626 if (memArg)
9627 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
9628 else
9629 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
9630 } else {
9631 if (memArg)
9632 Opc = numArgs == 3 ? X86::VPCMPISTRM128rm : X86::VPCMPESTRM128rm;
9633 else
9634 Opc = numArgs == 3 ? X86::VPCMPISTRM128rr : X86::VPCMPESTRM128rr;
9635 }
Eric Christopherb120ab42009-08-18 22:50:32 +00009636
Eric Christopher41c902f2010-11-30 08:20:21 +00009637 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Eric Christopherb120ab42009-08-18 22:50:32 +00009638 for (unsigned i = 0; i < numArgs; ++i) {
9639 MachineOperand &Op = MI->getOperand(i+1);
Eric Christopherb120ab42009-08-18 22:50:32 +00009640 if (!(Op.isReg() && Op.isImplicit()))
9641 MIB.addOperand(Op);
9642 }
Eric Christopher41c902f2010-11-30 08:20:21 +00009643 BuildMI(*BB, MI, dl, TII->get(X86::MOVAPSrr), MI->getOperand(0).getReg())
Eric Christopherb120ab42009-08-18 22:50:32 +00009644 .addReg(X86::XMM0);
9645
Dan Gohman14152b42010-07-06 20:24:04 +00009646 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +00009647 return BB;
9648}
9649
9650MachineBasicBlock *
Eric Christopher228232b2010-11-30 07:20:12 +00009651X86TargetLowering::EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +00009652 DebugLoc dl = MI->getDebugLoc();
9653 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009654
Eric Christopher228232b2010-11-30 07:20:12 +00009655 // Address into RAX/EAX, other two args into ECX, EDX.
9656 unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
9657 unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
9658 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg);
9659 for (int i = 0; i < X86::AddrNumOperands; ++i)
Eric Christopher82be2202010-11-30 08:10:28 +00009660 MIB.addOperand(MI->getOperand(i));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009661
Eric Christopher228232b2010-11-30 07:20:12 +00009662 unsigned ValOps = X86::AddrNumOperands;
9663 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
9664 .addReg(MI->getOperand(ValOps).getReg());
9665 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX)
9666 .addReg(MI->getOperand(ValOps+1).getReg());
9667
9668 // The instruction doesn't actually take any operands though.
9669 BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr));
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 *
9676X86TargetLowering::EmitMwait(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +00009677 DebugLoc dl = MI->getDebugLoc();
9678 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009679
Eric Christopher228232b2010-11-30 07:20:12 +00009680 // First arg in ECX, the second in EAX.
9681 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
9682 .addReg(MI->getOperand(0).getReg());
9683 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EAX)
9684 .addReg(MI->getOperand(1).getReg());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009685
Eric Christopher228232b2010-11-30 07:20:12 +00009686 // The instruction doesn't actually take any operands though.
9687 BuildMI(*BB, MI, dl, TII->get(X86::MWAITrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009688
Eric Christopher228232b2010-11-30 07:20:12 +00009689 MI->eraseFromParent(); // The pseudo is gone now.
9690 return BB;
9691}
9692
9693MachineBasicBlock *
Dan Gohman320afb82010-10-12 18:00:49 +00009694X86TargetLowering::EmitVAARG64WithCustomInserter(
9695 MachineInstr *MI,
9696 MachineBasicBlock *MBB) const {
9697 // Emit va_arg instruction on X86-64.
9698
9699 // Operands to this pseudo-instruction:
9700 // 0 ) Output : destination address (reg)
9701 // 1-5) Input : va_list address (addr, i64mem)
9702 // 6 ) ArgSize : Size (in bytes) of vararg type
9703 // 7 ) ArgMode : 0=overflow only, 1=use gp_offset, 2=use fp_offset
9704 // 8 ) Align : Alignment of type
9705 // 9 ) EFLAGS (implicit-def)
9706
9707 assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
9708 assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands");
9709
9710 unsigned DestReg = MI->getOperand(0).getReg();
9711 MachineOperand &Base = MI->getOperand(1);
9712 MachineOperand &Scale = MI->getOperand(2);
9713 MachineOperand &Index = MI->getOperand(3);
9714 MachineOperand &Disp = MI->getOperand(4);
9715 MachineOperand &Segment = MI->getOperand(5);
9716 unsigned ArgSize = MI->getOperand(6).getImm();
9717 unsigned ArgMode = MI->getOperand(7).getImm();
9718 unsigned Align = MI->getOperand(8).getImm();
9719
9720 // Memory Reference
9721 assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand");
9722 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
9723 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
9724
9725 // Machine Information
9726 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9727 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
9728 const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64);
9729 const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32);
9730 DebugLoc DL = MI->getDebugLoc();
9731
9732 // struct va_list {
9733 // i32 gp_offset
9734 // i32 fp_offset
9735 // i64 overflow_area (address)
9736 // i64 reg_save_area (address)
9737 // }
9738 // sizeof(va_list) = 24
9739 // alignment(va_list) = 8
9740
9741 unsigned TotalNumIntRegs = 6;
9742 unsigned TotalNumXMMRegs = 8;
9743 bool UseGPOffset = (ArgMode == 1);
9744 bool UseFPOffset = (ArgMode == 2);
9745 unsigned MaxOffset = TotalNumIntRegs * 8 +
9746 (UseFPOffset ? TotalNumXMMRegs * 16 : 0);
9747
9748 /* Align ArgSize to a multiple of 8 */
9749 unsigned ArgSizeA8 = (ArgSize + 7) & ~7;
9750 bool NeedsAlign = (Align > 8);
9751
9752 MachineBasicBlock *thisMBB = MBB;
9753 MachineBasicBlock *overflowMBB;
9754 MachineBasicBlock *offsetMBB;
9755 MachineBasicBlock *endMBB;
9756
9757 unsigned OffsetDestReg = 0; // Argument address computed by offsetMBB
9758 unsigned OverflowDestReg = 0; // Argument address computed by overflowMBB
9759 unsigned OffsetReg = 0;
9760
9761 if (!UseGPOffset && !UseFPOffset) {
9762 // If we only pull from the overflow region, we don't create a branch.
9763 // We don't need to alter control flow.
9764 OffsetDestReg = 0; // unused
9765 OverflowDestReg = DestReg;
9766
9767 offsetMBB = NULL;
9768 overflowMBB = thisMBB;
9769 endMBB = thisMBB;
9770 } else {
9771 // First emit code to check if gp_offset (or fp_offset) is below the bound.
9772 // If so, pull the argument from reg_save_area. (branch to offsetMBB)
9773 // If not, pull from overflow_area. (branch to overflowMBB)
9774 //
9775 // thisMBB
9776 // | .
9777 // | .
9778 // offsetMBB overflowMBB
9779 // | .
9780 // | .
9781 // endMBB
9782
9783 // Registers for the PHI in endMBB
9784 OffsetDestReg = MRI.createVirtualRegister(AddrRegClass);
9785 OverflowDestReg = MRI.createVirtualRegister(AddrRegClass);
9786
9787 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
9788 MachineFunction *MF = MBB->getParent();
9789 overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB);
9790 offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB);
9791 endMBB = MF->CreateMachineBasicBlock(LLVM_BB);
9792
9793 MachineFunction::iterator MBBIter = MBB;
9794 ++MBBIter;
9795
9796 // Insert the new basic blocks
9797 MF->insert(MBBIter, offsetMBB);
9798 MF->insert(MBBIter, overflowMBB);
9799 MF->insert(MBBIter, endMBB);
9800
9801 // Transfer the remainder of MBB and its successor edges to endMBB.
9802 endMBB->splice(endMBB->begin(), thisMBB,
9803 llvm::next(MachineBasicBlock::iterator(MI)),
9804 thisMBB->end());
9805 endMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
9806
9807 // Make offsetMBB and overflowMBB successors of thisMBB
9808 thisMBB->addSuccessor(offsetMBB);
9809 thisMBB->addSuccessor(overflowMBB);
9810
9811 // endMBB is a successor of both offsetMBB and overflowMBB
9812 offsetMBB->addSuccessor(endMBB);
9813 overflowMBB->addSuccessor(endMBB);
9814
9815 // Load the offset value into a register
9816 OffsetReg = MRI.createVirtualRegister(OffsetRegClass);
9817 BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg)
9818 .addOperand(Base)
9819 .addOperand(Scale)
9820 .addOperand(Index)
9821 .addDisp(Disp, UseFPOffset ? 4 : 0)
9822 .addOperand(Segment)
9823 .setMemRefs(MMOBegin, MMOEnd);
9824
9825 // Check if there is enough room left to pull this argument.
9826 BuildMI(thisMBB, DL, TII->get(X86::CMP32ri))
9827 .addReg(OffsetReg)
9828 .addImm(MaxOffset + 8 - ArgSizeA8);
9829
9830 // Branch to "overflowMBB" if offset >= max
9831 // Fall through to "offsetMBB" otherwise
9832 BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE)))
9833 .addMBB(overflowMBB);
9834 }
9835
9836 // In offsetMBB, emit code to use the reg_save_area.
9837 if (offsetMBB) {
9838 assert(OffsetReg != 0);
9839
9840 // Read the reg_save_area address.
9841 unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass);
9842 BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg)
9843 .addOperand(Base)
9844 .addOperand(Scale)
9845 .addOperand(Index)
9846 .addDisp(Disp, 16)
9847 .addOperand(Segment)
9848 .setMemRefs(MMOBegin, MMOEnd);
9849
9850 // Zero-extend the offset
9851 unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass);
9852 BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64)
9853 .addImm(0)
9854 .addReg(OffsetReg)
9855 .addImm(X86::sub_32bit);
9856
9857 // Add the offset to the reg_save_area to get the final address.
9858 BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg)
9859 .addReg(OffsetReg64)
9860 .addReg(RegSaveReg);
9861
9862 // Compute the offset for the next argument
9863 unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass);
9864 BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg)
9865 .addReg(OffsetReg)
9866 .addImm(UseFPOffset ? 16 : 8);
9867
9868 // Store it back into the va_list.
9869 BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr))
9870 .addOperand(Base)
9871 .addOperand(Scale)
9872 .addOperand(Index)
9873 .addDisp(Disp, UseFPOffset ? 4 : 0)
9874 .addOperand(Segment)
9875 .addReg(NextOffsetReg)
9876 .setMemRefs(MMOBegin, MMOEnd);
9877
9878 // Jump to endMBB
9879 BuildMI(offsetMBB, DL, TII->get(X86::JMP_4))
9880 .addMBB(endMBB);
9881 }
9882
9883 //
9884 // Emit code to use overflow area
9885 //
9886
9887 // Load the overflow_area address into a register.
9888 unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass);
9889 BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg)
9890 .addOperand(Base)
9891 .addOperand(Scale)
9892 .addOperand(Index)
9893 .addDisp(Disp, 8)
9894 .addOperand(Segment)
9895 .setMemRefs(MMOBegin, MMOEnd);
9896
9897 // If we need to align it, do so. Otherwise, just copy the address
9898 // to OverflowDestReg.
9899 if (NeedsAlign) {
9900 // Align the overflow address
9901 assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2");
9902 unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass);
9903
9904 // aligned_addr = (addr + (align-1)) & ~(align-1)
9905 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg)
9906 .addReg(OverflowAddrReg)
9907 .addImm(Align-1);
9908
9909 BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg)
9910 .addReg(TmpReg)
9911 .addImm(~(uint64_t)(Align-1));
9912 } else {
9913 BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg)
9914 .addReg(OverflowAddrReg);
9915 }
9916
9917 // Compute the next overflow address after this argument.
9918 // (the overflow address should be kept 8-byte aligned)
9919 unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass);
9920 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg)
9921 .addReg(OverflowDestReg)
9922 .addImm(ArgSizeA8);
9923
9924 // Store the new overflow address.
9925 BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr))
9926 .addOperand(Base)
9927 .addOperand(Scale)
9928 .addOperand(Index)
9929 .addDisp(Disp, 8)
9930 .addOperand(Segment)
9931 .addReg(NextAddrReg)
9932 .setMemRefs(MMOBegin, MMOEnd);
9933
9934 // If we branched, emit the PHI to the front of endMBB.
9935 if (offsetMBB) {
9936 BuildMI(*endMBB, endMBB->begin(), DL,
9937 TII->get(X86::PHI), DestReg)
9938 .addReg(OffsetDestReg).addMBB(offsetMBB)
9939 .addReg(OverflowDestReg).addMBB(overflowMBB);
9940 }
9941
9942 // Erase the pseudo instruction
9943 MI->eraseFromParent();
9944
9945 return endMBB;
9946}
9947
9948MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +00009949X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
9950 MachineInstr *MI,
9951 MachineBasicBlock *MBB) const {
9952 // Emit code to save XMM registers to the stack. The ABI says that the
9953 // number of registers to save is given in %al, so it's theoretically
9954 // possible to do an indirect jump trick to avoid saving all of them,
9955 // however this code takes a simpler approach and just executes all
9956 // of the stores if %al is non-zero. It's less code, and it's probably
9957 // easier on the hardware branch predictor, and stores aren't all that
9958 // expensive anyway.
9959
9960 // Create the new basic blocks. One block contains all the XMM stores,
9961 // and one block is the final destination regardless of whether any
9962 // stores were performed.
9963 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
9964 MachineFunction *F = MBB->getParent();
9965 MachineFunction::iterator MBBIter = MBB;
9966 ++MBBIter;
9967 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
9968 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
9969 F->insert(MBBIter, XMMSaveMBB);
9970 F->insert(MBBIter, EndMBB);
9971
Dan Gohman14152b42010-07-06 20:24:04 +00009972 // Transfer the remainder of MBB and its successor edges to EndMBB.
9973 EndMBB->splice(EndMBB->begin(), MBB,
9974 llvm::next(MachineBasicBlock::iterator(MI)),
9975 MBB->end());
9976 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
9977
Dan Gohmand6708ea2009-08-15 01:38:56 +00009978 // The original block will now fall through to the XMM save block.
9979 MBB->addSuccessor(XMMSaveMBB);
9980 // The XMMSaveMBB will fall through to the end block.
9981 XMMSaveMBB->addSuccessor(EndMBB);
9982
9983 // Now add the instructions.
9984 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9985 DebugLoc DL = MI->getDebugLoc();
9986
9987 unsigned CountReg = MI->getOperand(0).getReg();
9988 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
9989 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
9990
9991 if (!Subtarget->isTargetWin64()) {
9992 // If %al is 0, branch around the XMM save block.
9993 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009994 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +00009995 MBB->addSuccessor(EndMBB);
9996 }
9997
9998 // In the XMM save block, save all the XMM argument registers.
9999 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
10000 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +000010001 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +000010002 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +000010003 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +000010004 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +000010005 /*Size=*/16, /*Align=*/16);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010006 BuildMI(XMMSaveMBB, DL, TII->get(X86::MOVAPSmr))
10007 .addFrameIndex(RegSaveFrameIndex)
10008 .addImm(/*Scale=*/1)
10009 .addReg(/*IndexReg=*/0)
10010 .addImm(/*Disp=*/Offset)
10011 .addReg(/*Segment=*/0)
10012 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +000010013 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010014 }
10015
Dan Gohman14152b42010-07-06 20:24:04 +000010016 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +000010017
10018 return EndMBB;
10019}
Mon P Wang63307c32008-05-05 19:05:59 +000010020
Evan Cheng60c07e12006-07-05 22:17:51 +000010021MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +000010022X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010023 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +000010024 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10025 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +000010026
Chris Lattner52600972009-09-02 05:57:00 +000010027 // To "insert" a SELECT_CC instruction, we actually have to insert the
10028 // diamond control-flow pattern. The incoming instruction knows the
10029 // destination vreg to set, the condition code register to branch on, the
10030 // true/false values to select between, and a branch opcode to use.
10031 const BasicBlock *LLVM_BB = BB->getBasicBlock();
10032 MachineFunction::iterator It = BB;
10033 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +000010034
Chris Lattner52600972009-09-02 05:57:00 +000010035 // thisMBB:
10036 // ...
10037 // TrueVal = ...
10038 // cmpTY ccX, r1, r2
10039 // bCC copy1MBB
10040 // fallthrough --> copy0MBB
10041 MachineBasicBlock *thisMBB = BB;
10042 MachineFunction *F = BB->getParent();
10043 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
10044 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +000010045 F->insert(It, copy0MBB);
10046 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +000010047
Bill Wendling730c07e2010-06-25 20:48:10 +000010048 // If the EFLAGS register isn't dead in the terminator, then claim that it's
10049 // live into the sink and copy blocks.
10050 const MachineFunction *MF = BB->getParent();
10051 const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo();
10052 BitVector ReservedRegs = TRI->getReservedRegs(*MF);
Bill Wendling730c07e2010-06-25 20:48:10 +000010053
Dan Gohman14152b42010-07-06 20:24:04 +000010054 for (unsigned I = 0, E = MI->getNumOperands(); I != E; ++I) {
10055 const MachineOperand &MO = MI->getOperand(I);
10056 if (!MO.isReg() || !MO.isUse() || MO.isKill()) continue;
Bill Wendling730c07e2010-06-25 20:48:10 +000010057 unsigned Reg = MO.getReg();
10058 if (Reg != X86::EFLAGS) continue;
10059 copy0MBB->addLiveIn(Reg);
10060 sinkMBB->addLiveIn(Reg);
10061 }
10062
Dan Gohman14152b42010-07-06 20:24:04 +000010063 // Transfer the remainder of BB and its successor edges to sinkMBB.
10064 sinkMBB->splice(sinkMBB->begin(), BB,
10065 llvm::next(MachineBasicBlock::iterator(MI)),
10066 BB->end());
10067 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
10068
10069 // Add the true and fallthrough blocks as its successors.
10070 BB->addSuccessor(copy0MBB);
10071 BB->addSuccessor(sinkMBB);
10072
10073 // Create the conditional branch instruction.
10074 unsigned Opc =
10075 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
10076 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
10077
Chris Lattner52600972009-09-02 05:57:00 +000010078 // copy0MBB:
10079 // %FalseValue = ...
10080 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +000010081 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +000010082
Chris Lattner52600972009-09-02 05:57:00 +000010083 // sinkMBB:
10084 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
10085 // ...
Dan Gohman14152b42010-07-06 20:24:04 +000010086 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
10087 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +000010088 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
10089 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
10090
Dan Gohman14152b42010-07-06 20:24:04 +000010091 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +000010092 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +000010093}
10094
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010095MachineBasicBlock *
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010096X86TargetLowering::EmitLoweredWinAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010097 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010098 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10099 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010100
10101 // The lowering is pretty easy: we're just emitting the call to _alloca. The
10102 // non-trivial part is impdef of ESP.
10103 // FIXME: The code should be tweaked as soon as we'll try to do codegen for
10104 // mingw-w64.
10105
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010106 const char *StackProbeSymbol =
10107 Subtarget->isTargetWindows() ? "_chkstk" : "_alloca";
10108
Dan Gohman14152b42010-07-06 20:24:04 +000010109 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010110 .addExternalSymbol(StackProbeSymbol)
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010111 .addReg(X86::EAX, RegState::Implicit)
10112 .addReg(X86::ESP, RegState::Implicit)
10113 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
Anton Korobeynikov9f7f83b2010-08-25 07:50:11 +000010114 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
10115 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010116
Dan Gohman14152b42010-07-06 20:24:04 +000010117 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010118 return BB;
10119}
Chris Lattner52600972009-09-02 05:57:00 +000010120
10121MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +000010122X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
10123 MachineBasicBlock *BB) const {
10124 // This is pretty easy. We're taking the value that we received from
10125 // our load from the relocation, sticking it in either RDI (x86-64)
10126 // or EAX and doing an indirect call. The return value will then
10127 // be in the normal return register.
Michael J. Spencerec38de22010-10-10 22:04:20 +000010128 const X86InstrInfo *TII
Eric Christopher54415362010-06-08 22:04:25 +000010129 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +000010130 DebugLoc DL = MI->getDebugLoc();
10131 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +000010132
10133 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +000010134 assert(MI->getOperand(3).isGlobal() && "This should be a global");
Michael J. Spencerec38de22010-10-10 22:04:20 +000010135
Eric Christopher30ef0e52010-06-03 04:07:48 +000010136 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +000010137 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
10138 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +000010139 .addReg(X86::RIP)
10140 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000010141 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000010142 MI->getOperand(3).getTargetFlags())
10143 .addReg(0);
Eric Christopher722d3152010-09-27 06:01:51 +000010144 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +000010145 addDirectMem(MIB, X86::RDI);
Eric Christopher61025492010-06-15 23:08:42 +000010146 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +000010147 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
10148 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +000010149 .addReg(0)
10150 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000010151 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher61025492010-06-15 23:08:42 +000010152 MI->getOperand(3).getTargetFlags())
10153 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000010154 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000010155 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010156 } else {
Dan Gohman14152b42010-07-06 20:24:04 +000010157 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
10158 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +000010159 .addReg(TII->getGlobalBaseReg(F))
10160 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000010161 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000010162 MI->getOperand(3).getTargetFlags())
10163 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000010164 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000010165 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010166 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000010167
Dan Gohman14152b42010-07-06 20:24:04 +000010168 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +000010169 return BB;
10170}
10171
10172MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +000010173X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010174 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +000010175 switch (MI->getOpcode()) {
10176 default: assert(false && "Unexpected instr type to insert");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000010177 case X86::TAILJMPd64:
10178 case X86::TAILJMPr64:
10179 case X86::TAILJMPm64:
10180 assert(!"TAILJMP64 would not be touched here.");
10181 case X86::TCRETURNdi64:
10182 case X86::TCRETURNri64:
10183 case X86::TCRETURNmi64:
10184 // Defs of TCRETURNxx64 has Win64's callee-saved registers, as subset.
10185 // On AMD64, additional defs should be added before register allocation.
10186 if (!Subtarget->isTargetWin64()) {
10187 MI->addRegisterDefined(X86::RSI);
10188 MI->addRegisterDefined(X86::RDI);
10189 MI->addRegisterDefined(X86::XMM6);
10190 MI->addRegisterDefined(X86::XMM7);
10191 MI->addRegisterDefined(X86::XMM8);
10192 MI->addRegisterDefined(X86::XMM9);
10193 MI->addRegisterDefined(X86::XMM10);
10194 MI->addRegisterDefined(X86::XMM11);
10195 MI->addRegisterDefined(X86::XMM12);
10196 MI->addRegisterDefined(X86::XMM13);
10197 MI->addRegisterDefined(X86::XMM14);
10198 MI->addRegisterDefined(X86::XMM15);
10199 }
10200 return BB;
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010201 case X86::WIN_ALLOCA:
10202 return EmitLoweredWinAlloca(MI, BB);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010203 case X86::TLSCall_32:
10204 case X86::TLSCall_64:
10205 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +000010206 case X86::CMOV_GR8:
Evan Cheng60c07e12006-07-05 22:17:51 +000010207 case X86::CMOV_FR32:
10208 case X86::CMOV_FR64:
10209 case X86::CMOV_V4F32:
10210 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +000010211 case X86::CMOV_V2I64:
Chris Lattner314a1132010-03-14 18:31:44 +000010212 case X86::CMOV_GR16:
10213 case X86::CMOV_GR32:
10214 case X86::CMOV_RFP32:
10215 case X86::CMOV_RFP64:
10216 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010217 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000010218
Dale Johannesen849f2142007-07-03 00:53:03 +000010219 case X86::FP32_TO_INT16_IN_MEM:
10220 case X86::FP32_TO_INT32_IN_MEM:
10221 case X86::FP32_TO_INT64_IN_MEM:
10222 case X86::FP64_TO_INT16_IN_MEM:
10223 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +000010224 case X86::FP64_TO_INT64_IN_MEM:
10225 case X86::FP80_TO_INT16_IN_MEM:
10226 case X86::FP80_TO_INT32_IN_MEM:
10227 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +000010228 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10229 DebugLoc DL = MI->getDebugLoc();
10230
Evan Cheng60c07e12006-07-05 22:17:51 +000010231 // Change the floating point control register to use "round towards zero"
10232 // mode when truncating to an integer value.
10233 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +000010234 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +000010235 addFrameReference(BuildMI(*BB, MI, DL,
10236 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010237
10238 // Load the old value of the high byte of the control word...
10239 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +000010240 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohman14152b42010-07-06 20:24:04 +000010241 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +000010242 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010243
10244 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +000010245 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000010246 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +000010247
10248 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +000010249 addFrameReference(BuildMI(*BB, MI, DL,
10250 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010251
10252 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +000010253 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000010254 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +000010255
10256 // Get the X86 opcode to use.
10257 unsigned Opc;
10258 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000010259 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +000010260 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
10261 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
10262 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
10263 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
10264 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
10265 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +000010266 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
10267 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
10268 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +000010269 }
10270
10271 X86AddressMode AM;
10272 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +000010273 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000010274 AM.BaseType = X86AddressMode::RegBase;
10275 AM.Base.Reg = Op.getReg();
10276 } else {
10277 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +000010278 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +000010279 }
10280 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +000010281 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000010282 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010283 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +000010284 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000010285 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010286 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +000010287 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000010288 AM.GV = Op.getGlobal();
10289 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +000010290 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010291 }
Dan Gohman14152b42010-07-06 20:24:04 +000010292 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010293 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +000010294
10295 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +000010296 addFrameReference(BuildMI(*BB, MI, DL,
10297 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010298
Dan Gohman14152b42010-07-06 20:24:04 +000010299 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +000010300 return BB;
10301 }
Eric Christopherb120ab42009-08-18 22:50:32 +000010302 // String/text processing lowering.
10303 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010304 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000010305 return EmitPCMP(MI, BB, 3, false /* in-mem */);
10306 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010307 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000010308 return EmitPCMP(MI, BB, 3, true /* in-mem */);
10309 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010310 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000010311 return EmitPCMP(MI, BB, 5, false /* in mem */);
10312 case X86::PCMPESTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010313 case X86::VPCMPESTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000010314 return EmitPCMP(MI, BB, 5, true /* in mem */);
10315
Eric Christopher228232b2010-11-30 07:20:12 +000010316 // Thread synchronization.
10317 case X86::MONITOR:
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010318 return EmitMonitor(MI, BB);
Eric Christopher228232b2010-11-30 07:20:12 +000010319 case X86::MWAIT:
10320 return EmitMwait(MI, BB);
10321
Eric Christopherb120ab42009-08-18 22:50:32 +000010322 // Atomic Lowering.
Mon P Wang63307c32008-05-05 19:05:59 +000010323 case X86::ATOMAND32:
10324 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +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);
Mon P Wang63307c32008-05-05 19:05:59 +000010329 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +000010330 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
10331 X86::OR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010332 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010333 X86::NOT32r, X86::EAX,
10334 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000010335 case X86::ATOMXOR32:
10336 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010337 X86::XOR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010338 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010339 X86::NOT32r, X86::EAX,
10340 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010341 case X86::ATOMNAND32:
10342 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010343 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010344 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010345 X86::NOT32r, X86::EAX,
10346 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +000010347 case X86::ATOMMIN32:
10348 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
10349 case X86::ATOMMAX32:
10350 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
10351 case X86::ATOMUMIN32:
10352 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
10353 case X86::ATOMUMAX32:
10354 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +000010355
10356 case X86::ATOMAND16:
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);
10362 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +000010363 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010364 X86::OR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010365 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010366 X86::NOT16r, X86::AX,
10367 X86::GR16RegisterClass);
10368 case X86::ATOMXOR16:
10369 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
10370 X86::XOR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010371 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010372 X86::NOT16r, X86::AX,
10373 X86::GR16RegisterClass);
10374 case X86::ATOMNAND16:
10375 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
10376 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010377 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010378 X86::NOT16r, X86::AX,
10379 X86::GR16RegisterClass, true);
10380 case X86::ATOMMIN16:
10381 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
10382 case X86::ATOMMAX16:
10383 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
10384 case X86::ATOMUMIN16:
10385 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
10386 case X86::ATOMUMAX16:
10387 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
10388
10389 case X86::ATOMAND8:
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);
10395 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +000010396 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010397 X86::OR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010398 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010399 X86::NOT8r, X86::AL,
10400 X86::GR8RegisterClass);
10401 case X86::ATOMXOR8:
10402 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
10403 X86::XOR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010404 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010405 X86::NOT8r, X86::AL,
10406 X86::GR8RegisterClass);
10407 case X86::ATOMNAND8:
10408 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
10409 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010410 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010411 X86::NOT8r, X86::AL,
10412 X86::GR8RegisterClass, true);
10413 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010414 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +000010415 case X86::ATOMAND64:
10416 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010417 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);
10421 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +000010422 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
10423 X86::OR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010424 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010425 X86::NOT64r, X86::RAX,
10426 X86::GR64RegisterClass);
10427 case X86::ATOMXOR64:
10428 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010429 X86::XOR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010430 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010431 X86::NOT64r, X86::RAX,
10432 X86::GR64RegisterClass);
10433 case X86::ATOMNAND64:
10434 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
10435 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010436 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010437 X86::NOT64r, X86::RAX,
10438 X86::GR64RegisterClass, true);
10439 case X86::ATOMMIN64:
10440 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
10441 case X86::ATOMMAX64:
10442 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
10443 case X86::ATOMUMIN64:
10444 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
10445 case X86::ATOMUMAX64:
10446 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010447
10448 // This group does 64-bit operations on a 32-bit host.
10449 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010450 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010451 X86::AND32rr, X86::AND32rr,
10452 X86::AND32ri, X86::AND32ri,
10453 false);
10454 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010455 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010456 X86::OR32rr, X86::OR32rr,
10457 X86::OR32ri, X86::OR32ri,
10458 false);
10459 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010460 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010461 X86::XOR32rr, X86::XOR32rr,
10462 X86::XOR32ri, X86::XOR32ri,
10463 false);
10464 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010465 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010466 X86::AND32rr, X86::AND32rr,
10467 X86::AND32ri, X86::AND32ri,
10468 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010469 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010470 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010471 X86::ADD32rr, X86::ADC32rr,
10472 X86::ADD32ri, X86::ADC32ri,
10473 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010474 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010475 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010476 X86::SUB32rr, X86::SBB32rr,
10477 X86::SUB32ri, X86::SBB32ri,
10478 false);
Dale Johannesen880ae362008-10-03 22:25:52 +000010479 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010480 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +000010481 X86::MOV32rr, X86::MOV32rr,
10482 X86::MOV32ri, X86::MOV32ri,
10483 false);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010484 case X86::VASTART_SAVE_XMM_REGS:
10485 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Dan Gohman320afb82010-10-12 18:00:49 +000010486
10487 case X86::VAARG_64:
10488 return EmitVAARG64WithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000010489 }
10490}
10491
10492//===----------------------------------------------------------------------===//
10493// X86 Optimization Hooks
10494//===----------------------------------------------------------------------===//
10495
Dan Gohman475871a2008-07-27 21:46:04 +000010496void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +000010497 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000010498 APInt &KnownZero,
10499 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +000010500 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +000010501 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010502 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +000010503 assert((Opc >= ISD::BUILTIN_OP_END ||
10504 Opc == ISD::INTRINSIC_WO_CHAIN ||
10505 Opc == ISD::INTRINSIC_W_CHAIN ||
10506 Opc == ISD::INTRINSIC_VOID) &&
10507 "Should use MaskedValueIsZero if you don't know whether Op"
10508 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010509
Dan Gohmanf4f92f52008-02-13 23:07:24 +000010510 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010511 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +000010512 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +000010513 case X86ISD::ADD:
10514 case X86ISD::SUB:
Chris Lattner5b856542010-12-20 00:59:46 +000010515 case X86ISD::ADC:
10516 case X86ISD::SBB:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000010517 case X86ISD::SMUL:
10518 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +000010519 case X86ISD::INC:
10520 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +000010521 case X86ISD::OR:
10522 case X86ISD::XOR:
10523 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000010524 // These nodes' second result is a boolean.
10525 if (Op.getResNo() == 0)
10526 break;
10527 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010528 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000010529 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
10530 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +000010531 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010532 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010533}
Chris Lattner259e97c2006-01-31 19:43:35 +000010534
Owen Andersonbc146b02010-09-21 20:42:50 +000010535unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
10536 unsigned Depth) const {
10537 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
10538 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
10539 return Op.getValueType().getScalarType().getSizeInBits();
Michael J. Spencerec38de22010-10-10 22:04:20 +000010540
Owen Andersonbc146b02010-09-21 20:42:50 +000010541 // Fallback case.
10542 return 1;
10543}
10544
Evan Cheng206ee9d2006-07-07 08:33:52 +000010545/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +000010546/// node is a GlobalAddress + offset.
10547bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +000010548 const GlobalValue* &GA,
10549 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +000010550 if (N->getOpcode() == X86ISD::Wrapper) {
10551 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +000010552 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +000010553 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +000010554 return true;
10555 }
Evan Cheng206ee9d2006-07-07 08:33:52 +000010556 }
Evan Chengad4196b2008-05-12 19:56:52 +000010557 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +000010558}
10559
Evan Cheng206ee9d2006-07-07 08:33:52 +000010560/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
10561/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
10562/// if the load addresses are consecutive, non-overlapping, and in the right
Nate Begemanfdea31a2010-03-24 20:49:50 +000010563/// order.
Dan Gohman475871a2008-07-27 21:46:04 +000010564static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Mon P Wanga0fd0d52010-12-19 23:55:53 +000010565 TargetLowering::DAGCombinerInfo &DCI) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000010566 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000010567 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +000010568
Eli Friedman7a5e5552009-06-07 06:52:44 +000010569 if (VT.getSizeInBits() != 128)
10570 return SDValue();
10571
Mon P Wanga0fd0d52010-12-19 23:55:53 +000010572 // Don't create instructions with illegal types after legalize types has run.
10573 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10574 if (!DCI.isBeforeLegalize() && !TLI.isTypeLegal(VT.getVectorElementType()))
10575 return SDValue();
10576
Nate Begemanfdea31a2010-03-24 20:49:50 +000010577 SmallVector<SDValue, 16> Elts;
10578 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000010579 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +000010580
Nate Begemanfdea31a2010-03-24 20:49:50 +000010581 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000010582}
Evan Chengd880b972008-05-09 21:53:03 +000010583
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +000010584/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
10585/// generation and convert it from being a bunch of shuffles and extracts
10586/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010587static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
10588 const TargetLowering &TLI) {
10589 SDValue InputVector = N->getOperand(0);
10590
10591 // Only operate on vectors of 4 elements, where the alternative shuffling
10592 // gets to be more expensive.
10593 if (InputVector.getValueType() != MVT::v4i32)
10594 return SDValue();
10595
10596 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
10597 // single use which is a sign-extend or zero-extend, and all elements are
10598 // used.
10599 SmallVector<SDNode *, 4> Uses;
10600 unsigned ExtractedElements = 0;
10601 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
10602 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
10603 if (UI.getUse().getResNo() != InputVector.getResNo())
10604 return SDValue();
10605
10606 SDNode *Extract = *UI;
10607 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
10608 return SDValue();
10609
10610 if (Extract->getValueType(0) != MVT::i32)
10611 return SDValue();
10612 if (!Extract->hasOneUse())
10613 return SDValue();
10614 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
10615 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
10616 return SDValue();
10617 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
10618 return SDValue();
10619
10620 // Record which element was extracted.
10621 ExtractedElements |=
10622 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
10623
10624 Uses.push_back(Extract);
10625 }
10626
10627 // If not all the elements were used, this may not be worthwhile.
10628 if (ExtractedElements != 15)
10629 return SDValue();
10630
10631 // Ok, we've now decided to do the transformation.
10632 DebugLoc dl = InputVector.getDebugLoc();
10633
10634 // Store the value to a temporary stack slot.
10635 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000010636 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
10637 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010638
10639 // Replace each use (extract) with a load of the appropriate element.
10640 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
10641 UE = Uses.end(); UI != UE; ++UI) {
10642 SDNode *Extract = *UI;
10643
10644 // Compute the element's address.
10645 SDValue Idx = Extract->getOperand(1);
10646 unsigned EltSize =
10647 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
10648 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
10649 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
10650
Eric Christopher90eb4022010-07-22 00:26:08 +000010651 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, Idx.getValueType(),
Chris Lattner51abfe42010-09-21 06:02:19 +000010652 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010653
10654 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000010655 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000010656 ScalarAddr, MachinePointerInfo(),
10657 false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010658
10659 // Replace the exact with the load.
10660 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
10661 }
10662
10663 // The replacement was made in place; don't return anything.
10664 return SDValue();
10665}
10666
Chris Lattner83e6c992006-10-04 06:57:07 +000010667/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000010668static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner47b4ce82009-03-11 05:48:52 +000010669 const X86Subtarget *Subtarget) {
10670 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +000010671 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000010672 // Get the LHS/RHS of the select.
10673 SDValue LHS = N->getOperand(1);
10674 SDValue RHS = N->getOperand(2);
Eric Christopherfd179292009-08-27 18:07:15 +000010675
Dan Gohman670e5392009-09-21 18:03:22 +000010676 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000010677 // instructions match the semantics of the common C idiom x<y?x:y but not
10678 // x<=y?x:y, because of how they handle negative zero (which can be
10679 // ignored in unsafe-math mode).
Chris Lattner83e6c992006-10-04 06:57:07 +000010680 if (Subtarget->hasSSE2() &&
Owen Anderson825b72b2009-08-11 20:47:22 +000010681 (LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64) &&
Chris Lattner47b4ce82009-03-11 05:48:52 +000010682 Cond.getOpcode() == ISD::SETCC) {
10683 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010684
Chris Lattner47b4ce82009-03-11 05:48:52 +000010685 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000010686 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000010687 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
10688 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000010689 switch (CC) {
10690 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000010691 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000010692 // Converting this to a min would handle NaNs incorrectly, and swapping
10693 // the operands would cause it to handle comparisons between positive
10694 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000010695 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000010696 if (!UnsafeFPMath &&
10697 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
10698 break;
10699 std::swap(LHS, RHS);
10700 }
Dan Gohman670e5392009-09-21 18:03:22 +000010701 Opcode = X86ISD::FMIN;
10702 break;
10703 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000010704 // Converting this to a min would handle comparisons between positive
10705 // and negative zero incorrectly.
10706 if (!UnsafeFPMath &&
10707 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
10708 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010709 Opcode = X86ISD::FMIN;
10710 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000010711 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000010712 // Converting this to a min would handle both negative zeros and NaNs
10713 // incorrectly, but we can swap the operands to fix both.
10714 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000010715 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010716 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000010717 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010718 Opcode = X86ISD::FMIN;
10719 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010720
Dan Gohman670e5392009-09-21 18:03:22 +000010721 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000010722 // Converting this to a max would handle comparisons between positive
10723 // and negative zero incorrectly.
10724 if (!UnsafeFPMath &&
10725 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(LHS))
10726 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010727 Opcode = X86ISD::FMAX;
10728 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000010729 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000010730 // Converting this to a max would handle NaNs incorrectly, and swapping
10731 // the operands would cause it to handle comparisons between positive
10732 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000010733 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000010734 if (!UnsafeFPMath &&
10735 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
10736 break;
10737 std::swap(LHS, RHS);
10738 }
Dan Gohman670e5392009-09-21 18:03:22 +000010739 Opcode = X86ISD::FMAX;
10740 break;
10741 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000010742 // Converting this to a max would handle both negative zeros and NaNs
10743 // incorrectly, but we can swap the operands to fix both.
10744 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000010745 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010746 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010747 case ISD::SETGE:
10748 Opcode = X86ISD::FMAX;
10749 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000010750 }
Dan Gohman670e5392009-09-21 18:03:22 +000010751 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000010752 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
10753 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000010754 switch (CC) {
10755 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000010756 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000010757 // Converting this to a min would handle comparisons between positive
10758 // and negative zero incorrectly, and swapping the operands would
10759 // cause it to handle NaNs incorrectly.
10760 if (!UnsafeFPMath &&
10761 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000010762 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000010763 break;
10764 std::swap(LHS, RHS);
10765 }
Dan Gohman670e5392009-09-21 18:03:22 +000010766 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000010767 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010768 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000010769 // Converting this to a min would handle NaNs incorrectly.
10770 if (!UnsafeFPMath &&
10771 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
10772 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010773 Opcode = X86ISD::FMIN;
10774 break;
10775 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000010776 // Converting this to a min would handle both negative zeros and NaNs
10777 // incorrectly, but we can swap the operands to fix both.
10778 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000010779 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010780 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010781 case ISD::SETGE:
10782 Opcode = X86ISD::FMIN;
10783 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010784
Dan Gohman670e5392009-09-21 18:03:22 +000010785 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000010786 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000010787 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000010788 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010789 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000010790 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010791 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000010792 // Converting this to a max would handle comparisons between positive
10793 // and negative zero incorrectly, and swapping the operands would
10794 // cause it to handle NaNs incorrectly.
10795 if (!UnsafeFPMath &&
10796 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000010797 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000010798 break;
10799 std::swap(LHS, RHS);
10800 }
Dan Gohman670e5392009-09-21 18:03:22 +000010801 Opcode = X86ISD::FMAX;
10802 break;
10803 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000010804 // Converting this to a max would handle both negative zeros and NaNs
10805 // incorrectly, but we can swap the operands to fix both.
10806 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000010807 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010808 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000010809 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010810 Opcode = X86ISD::FMAX;
10811 break;
10812 }
Chris Lattner83e6c992006-10-04 06:57:07 +000010813 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010814
Chris Lattner47b4ce82009-03-11 05:48:52 +000010815 if (Opcode)
10816 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000010817 }
Eric Christopherfd179292009-08-27 18:07:15 +000010818
Chris Lattnerd1980a52009-03-12 06:52:53 +000010819 // If this is a select between two integer constants, try to do some
10820 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000010821 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
10822 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000010823 // Don't do this for crazy integer types.
10824 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
10825 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000010826 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000010827 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000010828
Chris Lattnercee56e72009-03-13 05:53:31 +000010829 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000010830 // Efficiently invertible.
10831 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
10832 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
10833 isa<ConstantSDNode>(Cond.getOperand(1))))) {
10834 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000010835 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000010836 }
Eric Christopherfd179292009-08-27 18:07:15 +000010837
Chris Lattnerd1980a52009-03-12 06:52:53 +000010838 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000010839 if (FalseC->getAPIntValue() == 0 &&
10840 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000010841 if (NeedsCondInvert) // Invert the condition if needed.
10842 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
10843 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000010844
Chris Lattnerd1980a52009-03-12 06:52:53 +000010845 // Zero extend the condition if needed.
10846 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000010847
Chris Lattnercee56e72009-03-13 05:53:31 +000010848 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000010849 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000010850 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000010851 }
Eric Christopherfd179292009-08-27 18:07:15 +000010852
Chris Lattner97a29a52009-03-13 05:22:11 +000010853 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000010854 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +000010855 if (NeedsCondInvert) // Invert the condition if needed.
10856 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
10857 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000010858
Chris Lattner97a29a52009-03-13 05:22:11 +000010859 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000010860 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
10861 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000010862 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000010863 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000010864 }
Eric Christopherfd179292009-08-27 18:07:15 +000010865
Chris Lattnercee56e72009-03-13 05:53:31 +000010866 // Optimize cases that will turn into an LEA instruction. This requires
10867 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000010868 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000010869 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000010870 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000010871
Chris Lattnercee56e72009-03-13 05:53:31 +000010872 bool isFastMultiplier = false;
10873 if (Diff < 10) {
10874 switch ((unsigned char)Diff) {
10875 default: break;
10876 case 1: // result = add base, cond
10877 case 2: // result = lea base( , cond*2)
10878 case 3: // result = lea base(cond, cond*2)
10879 case 4: // result = lea base( , cond*4)
10880 case 5: // result = lea base(cond, cond*4)
10881 case 8: // result = lea base( , cond*8)
10882 case 9: // result = lea base(cond, cond*8)
10883 isFastMultiplier = true;
10884 break;
10885 }
10886 }
Eric Christopherfd179292009-08-27 18:07:15 +000010887
Chris Lattnercee56e72009-03-13 05:53:31 +000010888 if (isFastMultiplier) {
10889 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
10890 if (NeedsCondInvert) // Invert the condition if needed.
10891 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
10892 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000010893
Chris Lattnercee56e72009-03-13 05:53:31 +000010894 // Zero extend the condition if needed.
10895 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
10896 Cond);
10897 // Scale the condition by the difference.
10898 if (Diff != 1)
10899 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
10900 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000010901
Chris Lattnercee56e72009-03-13 05:53:31 +000010902 // Add the base if non-zero.
10903 if (FalseC->getAPIntValue() != 0)
10904 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
10905 SDValue(FalseC, 0));
10906 return Cond;
10907 }
Eric Christopherfd179292009-08-27 18:07:15 +000010908 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000010909 }
10910 }
Eric Christopherfd179292009-08-27 18:07:15 +000010911
Dan Gohman475871a2008-07-27 21:46:04 +000010912 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000010913}
10914
Chris Lattnerd1980a52009-03-12 06:52:53 +000010915/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
10916static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
10917 TargetLowering::DAGCombinerInfo &DCI) {
10918 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +000010919
Chris Lattnerd1980a52009-03-12 06:52:53 +000010920 // If the flag operand isn't dead, don't touch this CMOV.
10921 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
10922 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +000010923
Chris Lattnerd1980a52009-03-12 06:52:53 +000010924 // If this is a select between two integer constants, try to do some
10925 // optimizations. Note that the operands are ordered the opposite of SELECT
10926 // operands.
10927 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
10928 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
10929 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
10930 // larger than FalseC (the false value).
10931 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
Eric Christopherfd179292009-08-27 18:07:15 +000010932
Chris Lattnerd1980a52009-03-12 06:52:53 +000010933 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
10934 CC = X86::GetOppositeBranchCondition(CC);
10935 std::swap(TrueC, FalseC);
10936 }
Eric Christopherfd179292009-08-27 18:07:15 +000010937
Chris Lattnerd1980a52009-03-12 06:52:53 +000010938 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000010939 // This is efficient for any integer data type (including i8/i16) and
10940 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000010941 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
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 Lattnerd1980a52009-03-12 06:52:53 +000010946 // Zero extend the condition if needed.
10947 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000010948
Chris Lattnerd1980a52009-03-12 06:52:53 +000010949 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
10950 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000010951 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +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 Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
10958 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000010959 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
10960 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +000010961 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
10962 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000010963
Chris Lattner97a29a52009-03-13 05:22:11 +000010964 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000010965 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
10966 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000010967 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
10968 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000010969
Chris Lattner97a29a52009-03-13 05:22:11 +000010970 if (N->getNumValues() == 2) // Dead flag value?
10971 return DCI.CombineTo(N, Cond, SDValue());
10972 return Cond;
10973 }
Eric Christopherfd179292009-08-27 18:07:15 +000010974
Chris Lattnercee56e72009-03-13 05:53:31 +000010975 // Optimize cases that will turn into an LEA instruction. This requires
10976 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000010977 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000010978 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000010979 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000010980
Chris Lattnercee56e72009-03-13 05:53:31 +000010981 bool isFastMultiplier = false;
10982 if (Diff < 10) {
10983 switch ((unsigned char)Diff) {
10984 default: break;
10985 case 1: // result = add base, cond
10986 case 2: // result = lea base( , cond*2)
10987 case 3: // result = lea base(cond, cond*2)
10988 case 4: // result = lea base( , cond*4)
10989 case 5: // result = lea base(cond, cond*4)
10990 case 8: // result = lea base( , cond*8)
10991 case 9: // result = lea base(cond, cond*8)
10992 isFastMultiplier = true;
10993 break;
10994 }
10995 }
Eric Christopherfd179292009-08-27 18:07:15 +000010996
Chris Lattnercee56e72009-03-13 05:53:31 +000010997 if (isFastMultiplier) {
10998 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
10999 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +000011000 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
11001 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000011002 // Zero extend the condition if needed.
11003 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
11004 Cond);
11005 // Scale the condition by the difference.
11006 if (Diff != 1)
11007 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
11008 DAG.getConstant(Diff, Cond.getValueType()));
11009
11010 // Add the base if non-zero.
11011 if (FalseC->getAPIntValue() != 0)
11012 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
11013 SDValue(FalseC, 0));
11014 if (N->getNumValues() == 2) // Dead flag value?
11015 return DCI.CombineTo(N, Cond, SDValue());
11016 return Cond;
11017 }
Eric Christopherfd179292009-08-27 18:07:15 +000011018 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000011019 }
11020 }
11021 return SDValue();
11022}
11023
11024
Evan Cheng0b0cd912009-03-28 05:57:29 +000011025/// PerformMulCombine - Optimize a single multiply with constant into two
11026/// in order to implement it with two cheaper instructions, e.g.
11027/// LEA + SHL, LEA + LEA.
11028static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
11029 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000011030 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
11031 return SDValue();
11032
Owen Andersone50ed302009-08-10 22:56:29 +000011033 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000011034 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000011035 return SDValue();
11036
11037 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
11038 if (!C)
11039 return SDValue();
11040 uint64_t MulAmt = C->getZExtValue();
11041 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
11042 return SDValue();
11043
11044 uint64_t MulAmt1 = 0;
11045 uint64_t MulAmt2 = 0;
11046 if ((MulAmt % 9) == 0) {
11047 MulAmt1 = 9;
11048 MulAmt2 = MulAmt / 9;
11049 } else if ((MulAmt % 5) == 0) {
11050 MulAmt1 = 5;
11051 MulAmt2 = MulAmt / 5;
11052 } else if ((MulAmt % 3) == 0) {
11053 MulAmt1 = 3;
11054 MulAmt2 = MulAmt / 3;
11055 }
11056 if (MulAmt2 &&
11057 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
11058 DebugLoc DL = N->getDebugLoc();
11059
11060 if (isPowerOf2_64(MulAmt2) &&
11061 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
11062 // If second multiplifer is pow2, issue it first. We want the multiply by
11063 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
11064 // is an add.
11065 std::swap(MulAmt1, MulAmt2);
11066
11067 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000011068 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000011069 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000011070 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000011071 else
Evan Cheng73f24c92009-03-30 21:36:47 +000011072 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000011073 DAG.getConstant(MulAmt1, VT));
11074
Eric Christopherfd179292009-08-27 18:07:15 +000011075 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000011076 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000011077 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000011078 else
Evan Cheng73f24c92009-03-30 21:36:47 +000011079 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000011080 DAG.getConstant(MulAmt2, VT));
11081
11082 // Do not add new nodes to DAG combiner worklist.
11083 DCI.CombineTo(N, NewMul, false);
11084 }
11085 return SDValue();
11086}
11087
Evan Chengad9c0a32009-12-15 00:53:42 +000011088static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
11089 SDValue N0 = N->getOperand(0);
11090 SDValue N1 = N->getOperand(1);
11091 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
11092 EVT VT = N0.getValueType();
11093
11094 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
11095 // since the result of setcc_c is all zero's or all ones.
11096 if (N1C && N0.getOpcode() == ISD::AND &&
11097 N0.getOperand(1).getOpcode() == ISD::Constant) {
11098 SDValue N00 = N0.getOperand(0);
11099 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
11100 ((N00.getOpcode() == ISD::ANY_EXTEND ||
11101 N00.getOpcode() == ISD::ZERO_EXTEND) &&
11102 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
11103 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
11104 APInt ShAmt = N1C->getAPIntValue();
11105 Mask = Mask.shl(ShAmt);
11106 if (Mask != 0)
11107 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
11108 N00, DAG.getConstant(Mask, VT));
11109 }
11110 }
11111
11112 return SDValue();
11113}
Evan Cheng0b0cd912009-03-28 05:57:29 +000011114
Nate Begeman740ab032009-01-26 00:52:55 +000011115/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
11116/// when possible.
11117static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
11118 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +000011119 EVT VT = N->getValueType(0);
11120 if (!VT.isVector() && VT.isInteger() &&
11121 N->getOpcode() == ISD::SHL)
11122 return PerformSHLCombine(N, DAG);
11123
Nate Begeman740ab032009-01-26 00:52:55 +000011124 // On X86 with SSE2 support, we can transform this to a vector shift if
11125 // all elements are shifted by the same amount. We can't do this in legalize
11126 // because the a constant vector is typically transformed to a constant pool
11127 // so we have no knowledge of the shift amount.
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011128 if (!Subtarget->hasSSE2())
11129 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000011130
Owen Anderson825b72b2009-08-11 20:47:22 +000011131 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011132 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000011133
Mon P Wang3becd092009-01-28 08:12:05 +000011134 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +000011135 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +000011136 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +000011137 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +000011138 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
11139 unsigned NumElts = VT.getVectorNumElements();
11140 unsigned i = 0;
11141 for (; i != NumElts; ++i) {
11142 SDValue Arg = ShAmtOp.getOperand(i);
11143 if (Arg.getOpcode() == ISD::UNDEF) continue;
11144 BaseShAmt = Arg;
11145 break;
11146 }
11147 for (; i != NumElts; ++i) {
11148 SDValue Arg = ShAmtOp.getOperand(i);
11149 if (Arg.getOpcode() == ISD::UNDEF) continue;
11150 if (Arg != BaseShAmt) {
11151 return SDValue();
11152 }
11153 }
11154 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +000011155 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +000011156 SDValue InVec = ShAmtOp.getOperand(0);
11157 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
11158 unsigned NumElts = InVec.getValueType().getVectorNumElements();
11159 unsigned i = 0;
11160 for (; i != NumElts; ++i) {
11161 SDValue Arg = InVec.getOperand(i);
11162 if (Arg.getOpcode() == ISD::UNDEF) continue;
11163 BaseShAmt = Arg;
11164 break;
11165 }
11166 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
11167 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +000011168 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +000011169 if (C->getZExtValue() == SplatIdx)
11170 BaseShAmt = InVec.getOperand(1);
11171 }
11172 }
11173 if (BaseShAmt.getNode() == 0)
11174 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
11175 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +000011176 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011177 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000011178
Mon P Wangefa42202009-09-03 19:56:25 +000011179 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +000011180 if (EltVT.bitsGT(MVT::i32))
11181 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
11182 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +000011183 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +000011184
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011185 // The shift amount is identical so we can do a vector shift.
11186 SDValue ValOp = N->getOperand(0);
11187 switch (N->getOpcode()) {
11188 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000011189 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011190 break;
11191 case ISD::SHL:
Owen Anderson825b72b2009-08-11 20:47:22 +000011192 if (VT == MVT::v2i64)
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_pslli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011195 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011196 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011197 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011198 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011199 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011200 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011201 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011202 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011203 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011204 break;
11205 case ISD::SRA:
Owen Anderson825b72b2009-08-11 20:47:22 +000011206 if (VT == MVT::v4i32)
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_psrai_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011209 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011210 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011211 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011212 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011213 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011214 break;
11215 case ISD::SRL:
Owen Anderson825b72b2009-08-11 20:47:22 +000011216 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011217 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011218 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011219 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011220 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011221 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011222 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011223 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011224 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011225 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011226 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011227 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011228 break;
Nate Begeman740ab032009-01-26 00:52:55 +000011229 }
11230 return SDValue();
11231}
11232
Nate Begemanb65c1752010-12-17 22:55:37 +000011233
11234static SDValue PerformAndCombine(SDNode *N, SelectionDAG &DAG,
11235 TargetLowering::DAGCombinerInfo &DCI,
11236 const X86Subtarget *Subtarget) {
11237 if (DCI.isBeforeLegalizeOps())
11238 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011239
Nate Begemanb65c1752010-12-17 22:55:37 +000011240 // Want to form PANDN nodes, in the hopes of then easily combining them with
11241 // OR and AND nodes to form PBLEND/PSIGN.
11242 EVT VT = N->getValueType(0);
11243 if (VT != MVT::v2i64)
11244 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011245
Nate Begemanb65c1752010-12-17 22:55:37 +000011246 SDValue N0 = N->getOperand(0);
11247 SDValue N1 = N->getOperand(1);
11248 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011249
Nate Begemanb65c1752010-12-17 22:55:37 +000011250 // Check LHS for vnot
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011251 if (N0.getOpcode() == ISD::XOR &&
Nate Begemanb65c1752010-12-17 22:55:37 +000011252 ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode()))
11253 return DAG.getNode(X86ISD::PANDN, DL, VT, N0.getOperand(0), N1);
11254
11255 // Check RHS for vnot
11256 if (N1.getOpcode() == ISD::XOR &&
11257 ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode()))
11258 return DAG.getNode(X86ISD::PANDN, DL, VT, N1.getOperand(0), N0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011259
Nate Begemanb65c1752010-12-17 22:55:37 +000011260 return SDValue();
11261}
11262
Evan Cheng760d1942010-01-04 21:22:48 +000011263static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000011264 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000011265 const X86Subtarget *Subtarget) {
Evan Cheng39cfeec2010-04-28 02:25:18 +000011266 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000011267 return SDValue();
11268
Evan Cheng760d1942010-01-04 21:22:48 +000011269 EVT VT = N->getValueType(0);
Nate Begemanb65c1752010-12-17 22:55:37 +000011270 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64 && VT != MVT::v2i64)
Evan Cheng760d1942010-01-04 21:22:48 +000011271 return SDValue();
11272
Evan Cheng760d1942010-01-04 21:22:48 +000011273 SDValue N0 = N->getOperand(0);
11274 SDValue N1 = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011275
Nate Begemanb65c1752010-12-17 22:55:37 +000011276 // look for psign/blend
11277 if (Subtarget->hasSSSE3()) {
11278 if (VT == MVT::v2i64) {
11279 // Canonicalize pandn to RHS
11280 if (N0.getOpcode() == X86ISD::PANDN)
11281 std::swap(N0, N1);
11282 // or (and (m, x), (pandn m, y))
11283 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::PANDN) {
11284 SDValue Mask = N1.getOperand(0);
11285 SDValue X = N1.getOperand(1);
11286 SDValue Y;
11287 if (N0.getOperand(0) == Mask)
11288 Y = N0.getOperand(1);
11289 if (N0.getOperand(1) == Mask)
11290 Y = N0.getOperand(0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011291
Nate Begemanb65c1752010-12-17 22:55:37 +000011292 // Check to see if the mask appeared in both the AND and PANDN and
11293 if (!Y.getNode())
11294 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011295
Nate Begemanb65c1752010-12-17 22:55:37 +000011296 // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them.
11297 if (Mask.getOpcode() != ISD::BITCAST ||
11298 X.getOpcode() != ISD::BITCAST ||
11299 Y.getOpcode() != ISD::BITCAST)
11300 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011301
Nate Begemanb65c1752010-12-17 22:55:37 +000011302 // Look through mask bitcast.
11303 Mask = Mask.getOperand(0);
11304 EVT MaskVT = Mask.getValueType();
11305
11306 // Validate that the Mask operand is a vector sra node. The sra node
11307 // will be an intrinsic.
11308 if (Mask.getOpcode() != ISD::INTRINSIC_WO_CHAIN)
11309 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011310
Nate Begemanb65c1752010-12-17 22:55:37 +000011311 // FIXME: what to do for bytes, since there is a psignb/pblendvb, but
11312 // there is no psrai.b
11313 switch (cast<ConstantSDNode>(Mask.getOperand(0))->getZExtValue()) {
11314 case Intrinsic::x86_sse2_psrai_w:
11315 case Intrinsic::x86_sse2_psrai_d:
11316 break;
11317 default: return SDValue();
11318 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011319
Nate Begemanb65c1752010-12-17 22:55:37 +000011320 // Check that the SRA is all signbits.
11321 SDValue SraC = Mask.getOperand(2);
11322 unsigned SraAmt = cast<ConstantSDNode>(SraC)->getZExtValue();
11323 unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits();
11324 if ((SraAmt + 1) != EltBits)
11325 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011326
Nate Begemanb65c1752010-12-17 22:55:37 +000011327 DebugLoc DL = N->getDebugLoc();
11328
11329 // Now we know we at least have a plendvb with the mask val. See if
11330 // we can form a psignb/w/d.
11331 // psign = x.type == y.type == mask.type && y = sub(0, x);
11332 X = X.getOperand(0);
11333 Y = Y.getOperand(0);
11334 if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X &&
11335 ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) &&
11336 X.getValueType() == MaskVT && X.getValueType() == Y.getValueType()){
11337 unsigned Opc = 0;
11338 switch (EltBits) {
11339 case 8: Opc = X86ISD::PSIGNB; break;
11340 case 16: Opc = X86ISD::PSIGNW; break;
11341 case 32: Opc = X86ISD::PSIGND; break;
11342 default: break;
11343 }
11344 if (Opc) {
11345 SDValue Sign = DAG.getNode(Opc, DL, MaskVT, X, Mask.getOperand(1));
11346 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Sign);
11347 }
11348 }
11349 // PBLENDVB only available on SSE 4.1
11350 if (!Subtarget->hasSSE41())
11351 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011352
Nate Begemanb65c1752010-12-17 22:55:37 +000011353 X = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, X);
11354 Y = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Y);
11355 Mask = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Mask);
Nate Begeman672fb622010-12-20 22:04:24 +000011356 Mask = DAG.getNode(X86ISD::PBLENDVB, DL, MVT::v16i8, X, Y, Mask);
Nate Begemanb65c1752010-12-17 22:55:37 +000011357 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Mask);
11358 }
11359 }
11360 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011361
Nate Begemanb65c1752010-12-17 22:55:37 +000011362 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
Evan Cheng760d1942010-01-04 21:22:48 +000011363 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
11364 std::swap(N0, N1);
11365 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
11366 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000011367 if (!N0.hasOneUse() || !N1.hasOneUse())
11368 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000011369
11370 SDValue ShAmt0 = N0.getOperand(1);
11371 if (ShAmt0.getValueType() != MVT::i8)
11372 return SDValue();
11373 SDValue ShAmt1 = N1.getOperand(1);
11374 if (ShAmt1.getValueType() != MVT::i8)
11375 return SDValue();
11376 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
11377 ShAmt0 = ShAmt0.getOperand(0);
11378 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
11379 ShAmt1 = ShAmt1.getOperand(0);
11380
11381 DebugLoc DL = N->getDebugLoc();
11382 unsigned Opc = X86ISD::SHLD;
11383 SDValue Op0 = N0.getOperand(0);
11384 SDValue Op1 = N1.getOperand(0);
11385 if (ShAmt0.getOpcode() == ISD::SUB) {
11386 Opc = X86ISD::SHRD;
11387 std::swap(Op0, Op1);
11388 std::swap(ShAmt0, ShAmt1);
11389 }
11390
Evan Cheng8b1190a2010-04-28 01:18:01 +000011391 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000011392 if (ShAmt1.getOpcode() == ISD::SUB) {
11393 SDValue Sum = ShAmt1.getOperand(0);
11394 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000011395 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
11396 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
11397 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
11398 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000011399 return DAG.getNode(Opc, DL, VT,
11400 Op0, Op1,
11401 DAG.getNode(ISD::TRUNCATE, DL,
11402 MVT::i8, ShAmt0));
11403 }
11404 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
11405 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
11406 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000011407 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000011408 return DAG.getNode(Opc, DL, VT,
11409 N0.getOperand(0), N1.getOperand(0),
11410 DAG.getNode(ISD::TRUNCATE, DL,
11411 MVT::i8, ShAmt0));
11412 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011413
Evan Cheng760d1942010-01-04 21:22:48 +000011414 return SDValue();
11415}
11416
Chris Lattner149a4e52008-02-22 02:09:43 +000011417/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000011418static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000011419 const X86Subtarget *Subtarget) {
Chris Lattner149a4e52008-02-22 02:09:43 +000011420 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
11421 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000011422 // A preferable solution to the general problem is to figure out the right
11423 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000011424
11425 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng7e2ff772008-05-08 00:57:18 +000011426 StoreSDNode *St = cast<StoreSDNode>(N);
Owen Andersone50ed302009-08-10 22:56:29 +000011427 EVT VT = St->getValue().getValueType();
Evan Cheng536e6672009-03-12 05:59:15 +000011428 if (VT.getSizeInBits() != 64)
11429 return SDValue();
11430
Devang Patel578efa92009-06-05 21:57:13 +000011431 const Function *F = DAG.getMachineFunction().getFunction();
11432 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Eric Christopherfd179292009-08-27 18:07:15 +000011433 bool F64IsLegal = !UseSoftFloat && !NoImplicitFloatOps
Devang Patel578efa92009-06-05 21:57:13 +000011434 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +000011435 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000011436 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000011437 isa<LoadSDNode>(St->getValue()) &&
11438 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
11439 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000011440 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000011441 LoadSDNode *Ld = 0;
11442 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000011443 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000011444 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000011445 // Must be a store of a load. We currently handle two cases: the load
11446 // is a direct child, and it's under an intervening TokenFactor. It is
11447 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000011448 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000011449 Ld = cast<LoadSDNode>(St->getChain());
11450 else if (St->getValue().hasOneUse() &&
11451 ChainVal->getOpcode() == ISD::TokenFactor) {
11452 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000011453 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000011454 TokenFactorIndex = i;
11455 Ld = cast<LoadSDNode>(St->getValue());
11456 } else
11457 Ops.push_back(ChainVal->getOperand(i));
11458 }
11459 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000011460
Evan Cheng536e6672009-03-12 05:59:15 +000011461 if (!Ld || !ISD::isNormalLoad(Ld))
11462 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000011463
Evan Cheng536e6672009-03-12 05:59:15 +000011464 // If this is not the MMX case, i.e. we are just turning i64 load/store
11465 // into f64 load/store, avoid the transformation if there are multiple
11466 // uses of the loaded value.
11467 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
11468 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000011469
Evan Cheng536e6672009-03-12 05:59:15 +000011470 DebugLoc LdDL = Ld->getDebugLoc();
11471 DebugLoc StDL = N->getDebugLoc();
11472 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
11473 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
11474 // pair instead.
11475 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000011476 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000011477 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
11478 Ld->getPointerInfo(), Ld->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000011479 Ld->isNonTemporal(), Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000011480 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000011481 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000011482 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000011483 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000011484 Ops.size());
11485 }
Evan Cheng536e6672009-03-12 05:59:15 +000011486 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000011487 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000011488 St->isVolatile(), St->isNonTemporal(),
11489 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000011490 }
Evan Cheng536e6672009-03-12 05:59:15 +000011491
11492 // Otherwise, lower to two pairs of 32-bit loads / stores.
11493 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000011494 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
11495 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000011496
Owen Anderson825b72b2009-08-11 20:47:22 +000011497 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000011498 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000011499 Ld->isVolatile(), Ld->isNonTemporal(),
11500 Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000011501 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000011502 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000011503 Ld->isVolatile(), Ld->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000011504 MinAlign(Ld->getAlignment(), 4));
11505
11506 SDValue NewChain = LoLd.getValue(1);
11507 if (TokenFactorIndex != -1) {
11508 Ops.push_back(LoLd);
11509 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000011510 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000011511 Ops.size());
11512 }
11513
11514 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000011515 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
11516 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000011517
11518 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000011519 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000011520 St->isVolatile(), St->isNonTemporal(),
11521 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000011522 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000011523 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000011524 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000011525 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000011526 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000011527 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000011528 }
Dan Gohman475871a2008-07-27 21:46:04 +000011529 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000011530}
11531
Chris Lattner6cf73262008-01-25 06:14:17 +000011532/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
11533/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000011534static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000011535 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
11536 // F[X]OR(0.0, x) -> x
11537 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000011538 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
11539 if (C->getValueAPF().isPosZero())
11540 return N->getOperand(1);
11541 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
11542 if (C->getValueAPF().isPosZero())
11543 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000011544 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000011545}
11546
11547/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000011548static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000011549 // FAND(0.0, x) -> 0.0
11550 // FAND(x, 0.0) -> 0.0
11551 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
11552 if (C->getValueAPF().isPosZero())
11553 return N->getOperand(0);
11554 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
11555 if (C->getValueAPF().isPosZero())
11556 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000011557 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000011558}
11559
Dan Gohmane5af2d32009-01-29 01:59:02 +000011560static SDValue PerformBTCombine(SDNode *N,
11561 SelectionDAG &DAG,
11562 TargetLowering::DAGCombinerInfo &DCI) {
11563 // BT ignores high bits in the bit index operand.
11564 SDValue Op1 = N->getOperand(1);
11565 if (Op1.hasOneUse()) {
11566 unsigned BitWidth = Op1.getValueSizeInBits();
11567 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
11568 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000011569 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
11570 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000011571 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000011572 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
11573 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
11574 DCI.CommitTargetLoweringOpt(TLO);
11575 }
11576 return SDValue();
11577}
Chris Lattner83e6c992006-10-04 06:57:07 +000011578
Eli Friedman7a5e5552009-06-07 06:52:44 +000011579static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
11580 SDValue Op = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011581 if (Op.getOpcode() == ISD::BITCAST)
Eli Friedman7a5e5552009-06-07 06:52:44 +000011582 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000011583 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000011584 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000011585 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000011586 OpVT.getVectorElementType().getSizeInBits()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011587 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Eli Friedman7a5e5552009-06-07 06:52:44 +000011588 }
11589 return SDValue();
11590}
11591
Evan Cheng2e489c42009-12-16 00:53:11 +000011592static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG) {
11593 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
11594 // (and (i32 x86isd::setcc_carry), 1)
11595 // This eliminates the zext. This transformation is necessary because
11596 // ISD::SETCC is always legalized to i8.
11597 DebugLoc dl = N->getDebugLoc();
11598 SDValue N0 = N->getOperand(0);
11599 EVT VT = N->getValueType(0);
11600 if (N0.getOpcode() == ISD::AND &&
11601 N0.hasOneUse() &&
11602 N0.getOperand(0).hasOneUse()) {
11603 SDValue N00 = N0.getOperand(0);
11604 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
11605 return SDValue();
11606 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
11607 if (!C || C->getZExtValue() != 1)
11608 return SDValue();
11609 return DAG.getNode(ISD::AND, dl, VT,
11610 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
11611 N00.getOperand(0), N00.getOperand(1)),
11612 DAG.getConstant(1, VT));
11613 }
11614
11615 return SDValue();
11616}
11617
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011618// Optimize RES = X86ISD::SETCC CONDCODE, EFLAG_INPUT
11619static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG &DAG) {
11620 unsigned X86CC = N->getConstantOperandVal(0);
11621 SDValue EFLAG = N->getOperand(1);
11622 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011623
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011624 // Materialize "setb reg" as "sbb reg,reg", since it can be extended without
11625 // a zext and produces an all-ones bit which is more useful than 0/1 in some
11626 // cases.
11627 if (X86CC == X86::COND_B)
11628 return DAG.getNode(ISD::AND, DL, MVT::i8,
11629 DAG.getNode(X86ISD::SETCC_CARRY, DL, MVT::i8,
11630 DAG.getConstant(X86CC, MVT::i8), EFLAG),
11631 DAG.getConstant(1, MVT::i8));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011632
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011633 return SDValue();
11634}
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011635
Chris Lattner23a01992010-12-20 01:37:09 +000011636// Optimize RES, EFLAGS = X86ISD::ADC LHS, RHS, EFLAGS
11637static SDValue PerformADCCombine(SDNode *N, SelectionDAG &DAG,
11638 X86TargetLowering::DAGCombinerInfo &DCI) {
11639 // If the LHS and RHS of the ADC node are zero, then it can't overflow and
11640 // the result is either zero or one (depending on the input carry bit).
11641 // Strength reduce this down to a "set on carry" aka SETCC_CARRY&1.
11642 if (X86::isZeroNode(N->getOperand(0)) &&
11643 X86::isZeroNode(N->getOperand(1)) &&
11644 // We don't have a good way to replace an EFLAGS use, so only do this when
11645 // dead right now.
11646 SDValue(N, 1).use_empty()) {
11647 DebugLoc DL = N->getDebugLoc();
11648 EVT VT = N->getValueType(0);
11649 SDValue CarryOut = DAG.getConstant(0, N->getValueType(1));
11650 SDValue Res1 = DAG.getNode(ISD::AND, DL, VT,
11651 DAG.getNode(X86ISD::SETCC_CARRY, DL, VT,
11652 DAG.getConstant(X86::COND_B,MVT::i8),
11653 N->getOperand(2)),
11654 DAG.getConstant(1, VT));
11655 return DCI.CombineTo(N, Res1, CarryOut);
11656 }
11657
11658 return SDValue();
11659}
11660
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000011661// fold (add Y, (sete X, 0)) -> adc 0, Y
11662// (add Y, (setne X, 0)) -> sbb -1, Y
11663// (sub (sete X, 0), Y) -> sbb 0, Y
11664// (sub (setne X, 0), Y) -> adc -1, Y
11665static SDValue OptimizeConditonalInDecrement(SDNode *N, SelectionDAG &DAG) {
11666 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011667
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000011668 // Look through ZExts.
11669 SDValue Ext = N->getOperand(N->getOpcode() == ISD::SUB ? 1 : 0);
11670 if (Ext.getOpcode() != ISD::ZERO_EXTEND || !Ext.hasOneUse())
11671 return SDValue();
11672
11673 SDValue SetCC = Ext.getOperand(0);
11674 if (SetCC.getOpcode() != X86ISD::SETCC || !SetCC.hasOneUse())
11675 return SDValue();
11676
11677 X86::CondCode CC = (X86::CondCode)SetCC.getConstantOperandVal(0);
11678 if (CC != X86::COND_E && CC != X86::COND_NE)
11679 return SDValue();
11680
11681 SDValue Cmp = SetCC.getOperand(1);
11682 if (Cmp.getOpcode() != X86ISD::CMP || !Cmp.hasOneUse() ||
Chris Lattner9cd3da42011-01-16 02:56:53 +000011683 !X86::isZeroNode(Cmp.getOperand(1)) ||
11684 !Cmp.getOperand(0).getValueType().isInteger())
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000011685 return SDValue();
11686
11687 SDValue CmpOp0 = Cmp.getOperand(0);
11688 SDValue NewCmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32, CmpOp0,
11689 DAG.getConstant(1, CmpOp0.getValueType()));
11690
11691 SDValue OtherVal = N->getOperand(N->getOpcode() == ISD::SUB ? 0 : 1);
11692 if (CC == X86::COND_NE)
11693 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::ADC : X86ISD::SBB,
11694 DL, OtherVal.getValueType(), OtherVal,
11695 DAG.getConstant(-1ULL, OtherVal.getValueType()), NewCmp);
11696 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::SBB : X86ISD::ADC,
11697 DL, OtherVal.getValueType(), OtherVal,
11698 DAG.getConstant(0, OtherVal.getValueType()), NewCmp);
11699}
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011700
Dan Gohman475871a2008-07-27 21:46:04 +000011701SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000011702 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000011703 SelectionDAG &DAG = DCI.DAG;
11704 switch (N->getOpcode()) {
11705 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000011706 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011707 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, *this);
Chris Lattneraf723b92008-01-25 05:46:26 +000011708 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +000011709 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000011710 case ISD::ADD:
11711 case ISD::SUB: return OptimizeConditonalInDecrement(N, DAG);
Chris Lattner23a01992010-12-20 01:37:09 +000011712 case X86ISD::ADC: return PerformADCCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000011713 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000011714 case ISD::SHL:
11715 case ISD::SRA:
11716 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Nate Begemanb65c1752010-12-17 22:55:37 +000011717 case ISD::AND: return PerformAndCombine(N, DAG, DCI, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000011718 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000011719 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +000011720 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000011721 case X86ISD::FOR: return PerformFORCombine(N, DAG);
11722 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000011723 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000011724 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Evan Cheng2e489c42009-12-16 00:53:11 +000011725 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG);
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011726 case X86ISD::SETCC: return PerformSETCCCombine(N, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000011727 case X86ISD::SHUFPS: // Handle all target specific shuffles
11728 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +000011729 case X86ISD::PALIGN:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000011730 case X86ISD::PUNPCKHBW:
11731 case X86ISD::PUNPCKHWD:
11732 case X86ISD::PUNPCKHDQ:
11733 case X86ISD::PUNPCKHQDQ:
11734 case X86ISD::UNPCKHPS:
11735 case X86ISD::UNPCKHPD:
11736 case X86ISD::PUNPCKLBW:
11737 case X86ISD::PUNPCKLWD:
11738 case X86ISD::PUNPCKLDQ:
11739 case X86ISD::PUNPCKLQDQ:
11740 case X86ISD::UNPCKLPS:
11741 case X86ISD::UNPCKLPD:
11742 case X86ISD::MOVHLPS:
11743 case X86ISD::MOVLHPS:
11744 case X86ISD::PSHUFD:
11745 case X86ISD::PSHUFHW:
11746 case X86ISD::PSHUFLW:
11747 case X86ISD::MOVSS:
11748 case X86ISD::MOVSD:
Mon P Wanga0fd0d52010-12-19 23:55:53 +000011749 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, DCI);
Evan Cheng206ee9d2006-07-07 08:33:52 +000011750 }
11751
Dan Gohman475871a2008-07-27 21:46:04 +000011752 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000011753}
11754
Evan Chenge5b51ac2010-04-17 06:13:15 +000011755/// isTypeDesirableForOp - Return true if the target has native support for
11756/// the specified value type and it is 'desirable' to use the type for the
11757/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
11758/// instruction encodings are longer and some i16 instructions are slow.
11759bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
11760 if (!isTypeLegal(VT))
11761 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000011762 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000011763 return true;
11764
11765 switch (Opc) {
11766 default:
11767 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000011768 case ISD::LOAD:
11769 case ISD::SIGN_EXTEND:
11770 case ISD::ZERO_EXTEND:
11771 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000011772 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000011773 case ISD::SRL:
11774 case ISD::SUB:
11775 case ISD::ADD:
11776 case ISD::MUL:
11777 case ISD::AND:
11778 case ISD::OR:
11779 case ISD::XOR:
11780 return false;
11781 }
11782}
11783
11784/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000011785/// beneficial for dag combiner to promote the specified node. If true, it
11786/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000011787bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000011788 EVT VT = Op.getValueType();
11789 if (VT != MVT::i16)
11790 return false;
11791
Evan Cheng4c26e932010-04-19 19:29:22 +000011792 bool Promote = false;
11793 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000011794 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000011795 default: break;
11796 case ISD::LOAD: {
11797 LoadSDNode *LD = cast<LoadSDNode>(Op);
11798 // If the non-extending load has a single use and it's not live out, then it
11799 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000011800 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
11801 Op.hasOneUse()*/) {
11802 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
11803 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
11804 // The only case where we'd want to promote LOAD (rather then it being
11805 // promoted as an operand is when it's only use is liveout.
11806 if (UI->getOpcode() != ISD::CopyToReg)
11807 return false;
11808 }
11809 }
Evan Cheng4c26e932010-04-19 19:29:22 +000011810 Promote = true;
11811 break;
11812 }
11813 case ISD::SIGN_EXTEND:
11814 case ISD::ZERO_EXTEND:
11815 case ISD::ANY_EXTEND:
11816 Promote = true;
11817 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000011818 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000011819 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000011820 SDValue N0 = Op.getOperand(0);
11821 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000011822 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000011823 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000011824 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000011825 break;
11826 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000011827 case ISD::ADD:
11828 case ISD::MUL:
11829 case ISD::AND:
11830 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000011831 case ISD::XOR:
11832 Commute = true;
11833 // fallthrough
11834 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000011835 SDValue N0 = Op.getOperand(0);
11836 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000011837 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000011838 return false;
11839 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000011840 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000011841 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000011842 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000011843 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000011844 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000011845 }
11846 }
11847
11848 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000011849 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000011850}
11851
Evan Cheng60c07e12006-07-05 22:17:51 +000011852//===----------------------------------------------------------------------===//
11853// X86 Inline Assembly Support
11854//===----------------------------------------------------------------------===//
11855
Chris Lattnerb8105652009-07-20 17:51:36 +000011856bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
11857 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattnerb8105652009-07-20 17:51:36 +000011858
11859 std::string AsmStr = IA->getAsmString();
11860
11861 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000011862 SmallVector<StringRef, 4> AsmPieces;
Peter Collingbourne98361182010-11-13 19:54:23 +000011863 SplitString(AsmStr, AsmPieces, ";\n");
Chris Lattnerb8105652009-07-20 17:51:36 +000011864
11865 switch (AsmPieces.size()) {
11866 default: return false;
11867 case 1:
11868 AsmStr = AsmPieces[0];
11869 AsmPieces.clear();
11870 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
11871
Evan Cheng55d42002011-01-08 01:24:27 +000011872 // FIXME: this should verify that we are targetting a 486 or better. If not,
11873 // we will turn this bswap into something that will be lowered to logical ops
11874 // instead of emitting the bswap asm. For now, we don't support 486 or lower
11875 // so don't worry about this.
Chris Lattnerb8105652009-07-20 17:51:36 +000011876 // bswap $0
11877 if (AsmPieces.size() == 2 &&
11878 (AsmPieces[0] == "bswap" ||
11879 AsmPieces[0] == "bswapq" ||
11880 AsmPieces[0] == "bswapl") &&
11881 (AsmPieces[1] == "$0" ||
11882 AsmPieces[1] == "${0:q}")) {
11883 // No need to check constraints, nothing other than the equivalent of
11884 // "=r,0" would be valid here.
Evan Cheng55d42002011-01-08 01:24:27 +000011885 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
11886 if (!Ty || Ty->getBitWidth() % 16 != 0)
11887 return false;
11888 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000011889 }
11890 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000011891 if (CI->getType()->isIntegerTy(16) &&
Chris Lattnerb8105652009-07-20 17:51:36 +000011892 AsmPieces.size() == 3 &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000011893 (AsmPieces[0] == "rorw" || AsmPieces[0] == "rolw") &&
Chris Lattnerb8105652009-07-20 17:51:36 +000011894 AsmPieces[1] == "$$8," &&
11895 AsmPieces[2] == "${0:w}" &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000011896 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
11897 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000011898 const std::string &ConstraintsStr = IA->getConstraintString();
11899 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000011900 std::sort(AsmPieces.begin(), AsmPieces.end());
11901 if (AsmPieces.size() == 4 &&
11902 AsmPieces[0] == "~{cc}" &&
11903 AsmPieces[1] == "~{dirflag}" &&
11904 AsmPieces[2] == "~{flags}" &&
11905 AsmPieces[3] == "~{fpsr}") {
Evan Cheng55d42002011-01-08 01:24:27 +000011906 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
11907 if (!Ty || Ty->getBitWidth() % 16 != 0)
11908 return false;
11909 return IntrinsicLowering::LowerToByteSwap(CI);
Dan Gohman0ef701e2010-03-04 19:58:08 +000011910 }
Chris Lattnerb8105652009-07-20 17:51:36 +000011911 }
11912 break;
11913 case 3:
Peter Collingbourne948cf022010-11-13 19:54:30 +000011914 if (CI->getType()->isIntegerTy(32) &&
11915 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
11916 SmallVector<StringRef, 4> Words;
11917 SplitString(AsmPieces[0], Words, " \t,");
11918 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
11919 Words[2] == "${0:w}") {
11920 Words.clear();
11921 SplitString(AsmPieces[1], Words, " \t,");
11922 if (Words.size() == 3 && Words[0] == "rorl" && Words[1] == "$$16" &&
11923 Words[2] == "$0") {
11924 Words.clear();
11925 SplitString(AsmPieces[2], Words, " \t,");
11926 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
11927 Words[2] == "${0:w}") {
11928 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000011929 const std::string &ConstraintsStr = IA->getConstraintString();
11930 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Peter Collingbourne948cf022010-11-13 19:54:30 +000011931 std::sort(AsmPieces.begin(), AsmPieces.end());
11932 if (AsmPieces.size() == 4 &&
11933 AsmPieces[0] == "~{cc}" &&
11934 AsmPieces[1] == "~{dirflag}" &&
11935 AsmPieces[2] == "~{flags}" &&
11936 AsmPieces[3] == "~{fpsr}") {
Evan Cheng55d42002011-01-08 01:24:27 +000011937 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
11938 if (!Ty || Ty->getBitWidth() % 16 != 0)
11939 return false;
11940 return IntrinsicLowering::LowerToByteSwap(CI);
Peter Collingbourne948cf022010-11-13 19:54:30 +000011941 }
11942 }
11943 }
11944 }
11945 }
Evan Cheng55d42002011-01-08 01:24:27 +000011946
11947 if (CI->getType()->isIntegerTy(64)) {
11948 InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
11949 if (Constraints.size() >= 2 &&
11950 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
11951 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
11952 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
11953 SmallVector<StringRef, 4> Words;
11954 SplitString(AsmPieces[0], Words, " \t");
11955 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
Chris Lattnerb8105652009-07-20 17:51:36 +000011956 Words.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000011957 SplitString(AsmPieces[1], Words, " \t");
11958 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
11959 Words.clear();
11960 SplitString(AsmPieces[2], Words, " \t,");
11961 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
11962 Words[2] == "%edx") {
11963 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
11964 if (!Ty || Ty->getBitWidth() % 16 != 0)
11965 return false;
11966 return IntrinsicLowering::LowerToByteSwap(CI);
11967 }
Chris Lattnerb8105652009-07-20 17:51:36 +000011968 }
11969 }
11970 }
11971 }
11972 break;
11973 }
11974 return false;
11975}
11976
11977
11978
Chris Lattnerf4dff842006-07-11 02:54:03 +000011979/// getConstraintType - Given a constraint letter, return the type of
11980/// constraint it is for this target.
11981X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000011982X86TargetLowering::getConstraintType(const std::string &Constraint) const {
11983 if (Constraint.size() == 1) {
11984 switch (Constraint[0]) {
Chris Lattner4234f572007-03-25 02:14:49 +000011985 case 'R':
Chris Lattner4234f572007-03-25 02:14:49 +000011986 case 'q':
11987 case 'Q':
John Thompson44ab89e2010-10-29 17:29:13 +000011988 case 'f':
11989 case 't':
11990 case 'u':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000011991 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +000011992 case 'x':
Chris Lattner4234f572007-03-25 02:14:49 +000011993 case 'Y':
11994 return C_RegisterClass;
John Thompson44ab89e2010-10-29 17:29:13 +000011995 case 'a':
11996 case 'b':
11997 case 'c':
11998 case 'd':
11999 case 'S':
12000 case 'D':
12001 case 'A':
12002 return C_Register;
12003 case 'I':
12004 case 'J':
12005 case 'K':
12006 case 'L':
12007 case 'M':
12008 case 'N':
12009 case 'G':
12010 case 'C':
Dale Johannesen78e3e522009-02-12 20:58:09 +000012011 case 'e':
12012 case 'Z':
12013 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000012014 default:
12015 break;
12016 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000012017 }
Chris Lattner4234f572007-03-25 02:14:49 +000012018 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000012019}
12020
John Thompson44ab89e2010-10-29 17:29:13 +000012021/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +000012022/// This object must already have been set up with the operand type
12023/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +000012024TargetLowering::ConstraintWeight
12025 X86TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +000012026 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +000012027 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012028 Value *CallOperandVal = info.CallOperandVal;
12029 // If we don't have a value, we can't do a match,
12030 // but allow it at the lowest weight.
12031 if (CallOperandVal == NULL)
John Thompson44ab89e2010-10-29 17:29:13 +000012032 return CW_Default;
12033 const Type *type = CallOperandVal->getType();
John Thompsoneac6e1d2010-09-13 18:15:37 +000012034 // Look at the constraint type.
12035 switch (*constraint) {
12036 default:
John Thompson44ab89e2010-10-29 17:29:13 +000012037 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
12038 case 'R':
12039 case 'q':
12040 case 'Q':
12041 case 'a':
12042 case 'b':
12043 case 'c':
12044 case 'd':
12045 case 'S':
12046 case 'D':
12047 case 'A':
12048 if (CallOperandVal->getType()->isIntegerTy())
12049 weight = CW_SpecificReg;
12050 break;
12051 case 'f':
12052 case 't':
12053 case 'u':
12054 if (type->isFloatingPointTy())
12055 weight = CW_SpecificReg;
12056 break;
12057 case 'y':
Chris Lattner2a786eb2010-12-19 20:19:20 +000012058 if (type->isX86_MMXTy() && Subtarget->hasMMX())
John Thompson44ab89e2010-10-29 17:29:13 +000012059 weight = CW_SpecificReg;
12060 break;
12061 case 'x':
12062 case 'Y':
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012063 if ((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasXMM())
John Thompson44ab89e2010-10-29 17:29:13 +000012064 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012065 break;
12066 case 'I':
12067 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
12068 if (C->getZExtValue() <= 31)
John Thompson44ab89e2010-10-29 17:29:13 +000012069 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012070 }
12071 break;
John Thompson44ab89e2010-10-29 17:29:13 +000012072 case 'J':
12073 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12074 if (C->getZExtValue() <= 63)
12075 weight = CW_Constant;
12076 }
12077 break;
12078 case 'K':
12079 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12080 if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f))
12081 weight = CW_Constant;
12082 }
12083 break;
12084 case 'L':
12085 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12086 if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff))
12087 weight = CW_Constant;
12088 }
12089 break;
12090 case 'M':
12091 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12092 if (C->getZExtValue() <= 3)
12093 weight = CW_Constant;
12094 }
12095 break;
12096 case 'N':
12097 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12098 if (C->getZExtValue() <= 0xff)
12099 weight = CW_Constant;
12100 }
12101 break;
12102 case 'G':
12103 case 'C':
12104 if (dyn_cast<ConstantFP>(CallOperandVal)) {
12105 weight = CW_Constant;
12106 }
12107 break;
12108 case 'e':
12109 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12110 if ((C->getSExtValue() >= -0x80000000LL) &&
12111 (C->getSExtValue() <= 0x7fffffffLL))
12112 weight = CW_Constant;
12113 }
12114 break;
12115 case 'Z':
12116 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12117 if (C->getZExtValue() <= 0xffffffff)
12118 weight = CW_Constant;
12119 }
12120 break;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012121 }
12122 return weight;
12123}
12124
Dale Johannesenba2a0b92008-01-29 02:21:21 +000012125/// LowerXConstraint - try to replace an X constraint, which matches anything,
12126/// with another that has more specific requirements based on the type of the
12127/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000012128const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000012129LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000012130 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
12131 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000012132 if (ConstraintVT.isFloatingPoint()) {
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012133 if (Subtarget->hasXMMInt())
Chris Lattner5e764232008-04-26 23:02:14 +000012134 return "Y";
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012135 if (Subtarget->hasXMM())
Chris Lattner5e764232008-04-26 23:02:14 +000012136 return "x";
12137 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012138
Chris Lattner5e764232008-04-26 23:02:14 +000012139 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000012140}
12141
Chris Lattner48884cd2007-08-25 00:47:38 +000012142/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
12143/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000012144void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattner48884cd2007-08-25 00:47:38 +000012145 char Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000012146 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000012147 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000012148 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000012149
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012150 switch (Constraint) {
12151 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000012152 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000012153 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000012154 if (C->getZExtValue() <= 31) {
12155 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000012156 break;
12157 }
Devang Patel84f7fd22007-03-17 00:13:28 +000012158 }
Chris Lattner48884cd2007-08-25 00:47:38 +000012159 return;
Evan Cheng364091e2008-09-22 23:57:37 +000012160 case 'J':
12161 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000012162 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000012163 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
12164 break;
12165 }
12166 }
12167 return;
12168 case 'K':
12169 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000012170 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +000012171 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
12172 break;
12173 }
12174 }
12175 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000012176 case 'N':
12177 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000012178 if (C->getZExtValue() <= 255) {
12179 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000012180 break;
12181 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000012182 }
Chris Lattner48884cd2007-08-25 00:47:38 +000012183 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000012184 case 'e': {
12185 // 32-bit signed value
12186 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000012187 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
12188 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000012189 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000012190 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000012191 break;
12192 }
12193 // FIXME gcc accepts some relocatable values here too, but only in certain
12194 // memory models; it's complicated.
12195 }
12196 return;
12197 }
12198 case 'Z': {
12199 // 32-bit unsigned value
12200 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000012201 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
12202 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000012203 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
12204 break;
12205 }
12206 }
12207 // FIXME gcc accepts some relocatable values here too, but only in certain
12208 // memory models; it's complicated.
12209 return;
12210 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000012211 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012212 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000012213 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000012214 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000012215 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000012216 break;
12217 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012218
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000012219 // In any sort of PIC mode addresses need to be computed at runtime by
12220 // adding in a register or some sort of table lookup. These can't
12221 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000012222 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000012223 return;
12224
Chris Lattnerdc43a882007-05-03 16:52:29 +000012225 // If we are in non-pic codegen mode, we allow the address of a global (with
12226 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000012227 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000012228 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000012229
Chris Lattner49921962009-05-08 18:23:14 +000012230 // Match either (GA), (GA+C), (GA+C1+C2), etc.
12231 while (1) {
12232 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
12233 Offset += GA->getOffset();
12234 break;
12235 } else if (Op.getOpcode() == ISD::ADD) {
12236 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
12237 Offset += C->getZExtValue();
12238 Op = Op.getOperand(0);
12239 continue;
12240 }
12241 } else if (Op.getOpcode() == ISD::SUB) {
12242 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
12243 Offset += -C->getZExtValue();
12244 Op = Op.getOperand(0);
12245 continue;
12246 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000012247 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000012248
Chris Lattner49921962009-05-08 18:23:14 +000012249 // Otherwise, this isn't something we can handle, reject it.
12250 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000012251 }
Eric Christopherfd179292009-08-27 18:07:15 +000012252
Dan Gohman46510a72010-04-15 01:51:59 +000012253 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000012254 // If we require an extra load to get this address, as in PIC mode, we
12255 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000012256 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
12257 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000012258 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000012259
Devang Patel0d881da2010-07-06 22:08:15 +000012260 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
12261 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000012262 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012263 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000012264 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012265
Gabor Greifba36cb52008-08-28 21:40:38 +000012266 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000012267 Ops.push_back(Result);
12268 return;
12269 }
Dale Johannesen1784d162010-06-25 21:55:36 +000012270 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012271}
12272
Chris Lattner259e97c2006-01-31 19:43:35 +000012273std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +000012274getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000012275 EVT VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +000012276 if (Constraint.size() == 1) {
12277 // FIXME: not handling fp-stack yet!
Chris Lattner259e97c2006-01-31 19:43:35 +000012278 switch (Constraint[0]) { // GCC X86 Constraint Letters
Chris Lattnerf4dff842006-07-11 02:54:03 +000012279 default: break; // Unknown constraint letter
Evan Cheng47e9fab2009-07-17 22:13:25 +000012280 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
12281 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000012282 if (VT == MVT::i32)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012283 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX,
12284 X86::ESI, X86::EDI, X86::R8D, X86::R9D,
12285 X86::R10D,X86::R11D,X86::R12D,
12286 X86::R13D,X86::R14D,X86::R15D,
12287 X86::EBP, X86::ESP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012288 else if (VT == MVT::i16)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012289 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX,
12290 X86::SI, X86::DI, X86::R8W,X86::R9W,
12291 X86::R10W,X86::R11W,X86::R12W,
12292 X86::R13W,X86::R14W,X86::R15W,
12293 X86::BP, X86::SP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012294 else if (VT == MVT::i8)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012295 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL,
12296 X86::SIL, X86::DIL, X86::R8B,X86::R9B,
12297 X86::R10B,X86::R11B,X86::R12B,
12298 X86::R13B,X86::R14B,X86::R15B,
12299 X86::BPL, X86::SPL, 0);
12300
Owen Anderson825b72b2009-08-11 20:47:22 +000012301 else if (VT == MVT::i64)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012302 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX,
12303 X86::RSI, X86::RDI, X86::R8, X86::R9,
12304 X86::R10, X86::R11, X86::R12,
12305 X86::R13, X86::R14, X86::R15,
12306 X86::RBP, X86::RSP, 0);
12307
12308 break;
12309 }
Eric Christopherfd179292009-08-27 18:07:15 +000012310 // 32-bit fallthrough
Chris Lattner259e97c2006-01-31 19:43:35 +000012311 case 'Q': // Q_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +000012312 if (VT == MVT::i32)
Chris Lattner80a7ecc2006-05-06 00:29:37 +000012313 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012314 else if (VT == MVT::i16)
Chris Lattner80a7ecc2006-05-06 00:29:37 +000012315 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012316 else if (VT == MVT::i8)
Evan Cheng12914382007-08-13 23:27:11 +000012317 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012318 else if (VT == MVT::i64)
Chris Lattner03e6c702007-11-04 06:51:12 +000012319 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
12320 break;
Chris Lattner259e97c2006-01-31 19:43:35 +000012321 }
12322 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012323
Chris Lattner1efa40f2006-02-22 00:56:39 +000012324 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +000012325}
Chris Lattnerf76d1802006-07-31 23:26:50 +000012326
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012327std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000012328X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000012329 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000012330 // First, see if this is a constraint that directly corresponds to an LLVM
12331 // register class.
12332 if (Constraint.size() == 1) {
12333 // GCC Constraint Letters
12334 switch (Constraint[0]) {
12335 default: break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000012336 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000012337 case 'l': // INDEX_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +000012338 if (VT == MVT::i8)
Chris Lattner0f65cad2007-04-09 05:49:22 +000012339 return std::make_pair(0U, X86::GR8RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012340 if (VT == MVT::i16)
Chris Lattner1fa71982008-10-17 18:15:05 +000012341 return std::make_pair(0U, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012342 if (VT == MVT::i32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +000012343 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +000012344 return std::make_pair(0U, X86::GR64RegisterClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000012345 case 'R': // LEGACY_REGS
12346 if (VT == MVT::i8)
12347 return std::make_pair(0U, X86::GR8_NOREXRegisterClass);
12348 if (VT == MVT::i16)
12349 return std::make_pair(0U, X86::GR16_NOREXRegisterClass);
12350 if (VT == MVT::i32 || !Subtarget->is64Bit())
12351 return std::make_pair(0U, X86::GR32_NOREXRegisterClass);
12352 return std::make_pair(0U, X86::GR64_NOREXRegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000012353 case 'f': // FP Stack registers.
12354 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
12355 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000012356 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000012357 return std::make_pair(0U, X86::RFP32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012358 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000012359 return std::make_pair(0U, X86::RFP64RegisterClass);
12360 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000012361 case 'y': // MMX_REGS if MMX allowed.
12362 if (!Subtarget->hasMMX()) break;
12363 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000012364 case 'Y': // SSE_REGS if SSE2 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012365 if (!Subtarget->hasXMMInt()) break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000012366 // FALL THROUGH.
12367 case 'x': // SSE_REGS if SSE1 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012368 if (!Subtarget->hasXMM()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000012369
Owen Anderson825b72b2009-08-11 20:47:22 +000012370 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000012371 default: break;
12372 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000012373 case MVT::f32:
12374 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +000012375 return std::make_pair(0U, X86::FR32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012376 case MVT::f64:
12377 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +000012378 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000012379 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000012380 case MVT::v16i8:
12381 case MVT::v8i16:
12382 case MVT::v4i32:
12383 case MVT::v2i64:
12384 case MVT::v4f32:
12385 case MVT::v2f64:
Chris Lattner0f65cad2007-04-09 05:49:22 +000012386 return std::make_pair(0U, X86::VR128RegisterClass);
12387 }
Chris Lattnerad043e82007-04-09 05:11:28 +000012388 break;
12389 }
12390 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012391
Chris Lattnerf76d1802006-07-31 23:26:50 +000012392 // Use the default implementation in TargetLowering to convert the register
12393 // constraint into a member of a register class.
12394 std::pair<unsigned, const TargetRegisterClass*> Res;
12395 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000012396
12397 // Not found as a standard register?
12398 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000012399 // Map st(0) -> st(7) -> ST0
12400 if (Constraint.size() == 7 && Constraint[0] == '{' &&
12401 tolower(Constraint[1]) == 's' &&
12402 tolower(Constraint[2]) == 't' &&
12403 Constraint[3] == '(' &&
12404 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
12405 Constraint[5] == ')' &&
12406 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000012407
Chris Lattner56d77c72009-09-13 22:41:48 +000012408 Res.first = X86::ST0+Constraint[4]-'0';
12409 Res.second = X86::RFP80RegisterClass;
12410 return Res;
12411 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000012412
Chris Lattner56d77c72009-09-13 22:41:48 +000012413 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000012414 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000012415 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +000012416 Res.second = X86::RFP80RegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000012417 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000012418 }
Chris Lattner56d77c72009-09-13 22:41:48 +000012419
12420 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000012421 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000012422 Res.first = X86::EFLAGS;
12423 Res.second = X86::CCRRegisterClass;
12424 return Res;
12425 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000012426
Dale Johannesen330169f2008-11-13 21:52:36 +000012427 // 'A' means EAX + EDX.
12428 if (Constraint == "A") {
12429 Res.first = X86::EAX;
Dan Gohman68a31c22009-07-30 17:02:08 +000012430 Res.second = X86::GR32_ADRegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000012431 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000012432 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000012433 return Res;
12434 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012435
Chris Lattnerf76d1802006-07-31 23:26:50 +000012436 // Otherwise, check to see if this is a register class of the wrong value
12437 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
12438 // turn into {ax},{dx}.
12439 if (Res.second->hasType(VT))
12440 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012441
Chris Lattnerf76d1802006-07-31 23:26:50 +000012442 // All of the single-register GCC register classes map their values onto
12443 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
12444 // really want an 8-bit or 32-bit register, map to the appropriate register
12445 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +000012446 if (Res.second == X86::GR16RegisterClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000012447 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000012448 unsigned DestReg = 0;
12449 switch (Res.first) {
12450 default: break;
12451 case X86::AX: DestReg = X86::AL; break;
12452 case X86::DX: DestReg = X86::DL; break;
12453 case X86::CX: DestReg = X86::CL; break;
12454 case X86::BX: DestReg = X86::BL; break;
12455 }
12456 if (DestReg) {
12457 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000012458 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000012459 }
Owen Anderson825b72b2009-08-11 20:47:22 +000012460 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000012461 unsigned DestReg = 0;
12462 switch (Res.first) {
12463 default: break;
12464 case X86::AX: DestReg = X86::EAX; break;
12465 case X86::DX: DestReg = X86::EDX; break;
12466 case X86::CX: DestReg = X86::ECX; break;
12467 case X86::BX: DestReg = X86::EBX; break;
12468 case X86::SI: DestReg = X86::ESI; break;
12469 case X86::DI: DestReg = X86::EDI; break;
12470 case X86::BP: DestReg = X86::EBP; break;
12471 case X86::SP: DestReg = X86::ESP; break;
12472 }
12473 if (DestReg) {
12474 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000012475 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000012476 }
Owen Anderson825b72b2009-08-11 20:47:22 +000012477 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000012478 unsigned DestReg = 0;
12479 switch (Res.first) {
12480 default: break;
12481 case X86::AX: DestReg = X86::RAX; break;
12482 case X86::DX: DestReg = X86::RDX; break;
12483 case X86::CX: DestReg = X86::RCX; break;
12484 case X86::BX: DestReg = X86::RBX; break;
12485 case X86::SI: DestReg = X86::RSI; break;
12486 case X86::DI: DestReg = X86::RDI; break;
12487 case X86::BP: DestReg = X86::RBP; break;
12488 case X86::SP: DestReg = X86::RSP; break;
12489 }
12490 if (DestReg) {
12491 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000012492 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000012493 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000012494 }
Chris Lattner6ba50a92008-08-26 06:19:02 +000012495 } else if (Res.second == X86::FR32RegisterClass ||
12496 Res.second == X86::FR64RegisterClass ||
12497 Res.second == X86::VR128RegisterClass) {
12498 // Handle references to XMM physical registers that got mapped into the
12499 // wrong class. This can happen with constraints like {xmm0} where the
12500 // target independent register mapper will just pick the first match it can
12501 // find, ignoring the required type.
Owen Anderson825b72b2009-08-11 20:47:22 +000012502 if (VT == MVT::f32)
Chris Lattner6ba50a92008-08-26 06:19:02 +000012503 Res.second = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +000012504 else if (VT == MVT::f64)
Chris Lattner6ba50a92008-08-26 06:19:02 +000012505 Res.second = X86::FR64RegisterClass;
12506 else if (X86::VR128RegisterClass->hasType(VT))
12507 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000012508 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012509
Chris Lattnerf76d1802006-07-31 23:26:50 +000012510 return Res;
12511}