blob: 69295f847ce3d43557516b94961c4c967645847a [file] [log] [blame]
Arnold Schwaighofer92226dd2007-10-12 21:53:12 +00001//===-- X86ISelLowering.cpp - X86 DAG Lowering Implementation -------------===//
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that X86 uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
Evan Chengb1712452010-01-27 06:25:16 +000015#define DEBUG_TYPE "x86-isel"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000016#include "X86.h"
Evan Cheng0cc39452006-01-16 21:21:29 +000017#include "X86InstrBuilder.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000018#include "X86ISelLowering.h"
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000019#include "X86ShuffleDecode.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000020#include "X86TargetMachine.h"
Chris Lattner8c6ed052009-09-16 01:46:41 +000021#include "X86TargetObjectFile.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000022#include "llvm/CallingConv.h"
Evan Cheng223547a2006-01-31 22:28:30 +000023#include "llvm/Constants.h"
Evan Cheng347d5f72006-04-28 21:29:37 +000024#include "llvm/DerivedTypes.h"
Chris Lattnerb903bed2009-06-26 21:20:29 +000025#include "llvm/GlobalAlias.h"
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000026#include "llvm/GlobalVariable.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000027#include "llvm/Function.h"
Chris Lattnerb8105652009-07-20 17:51:36 +000028#include "llvm/Instructions.h"
Evan Cheng6be2c582006-04-05 23:38:46 +000029#include "llvm/Intrinsics.h"
Owen Andersona90b3dc2009-07-15 21:51:10 +000030#include "llvm/LLVMContext.h"
Evan Cheng55d42002011-01-08 01:24:27 +000031#include "llvm/CodeGen/IntrinsicLowering.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000032#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng4a460802006-01-11 00:33:36 +000033#include "llvm/CodeGen/MachineFunction.h"
34#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner5e1df8d2010-01-25 23:38:14 +000035#include "llvm/CodeGen/MachineJumpTableInfo.h"
Evan Chenga844bde2008-02-02 04:07:54 +000036#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000037#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman69de1932008-02-06 22:27:42 +000038#include "llvm/CodeGen/PseudoSourceValue.h"
Chris Lattner589c6f62010-01-26 06:28:43 +000039#include "llvm/MC/MCAsmInfo.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000040#include "llvm/MC/MCContext.h"
Daniel Dunbar4e815f82010-03-15 23:51:06 +000041#include "llvm/MC/MCExpr.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000042#include "llvm/MC/MCSymbol.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000043#include "llvm/ADT/BitVector.h"
Evan Cheng14b32e12007-12-11 01:46:18 +000044#include "llvm/ADT/SmallSet.h"
Evan Chengb1712452010-01-27 06:25:16 +000045#include "llvm/ADT/Statistic.h"
Chris Lattner1a60aa72006-10-31 19:42:44 +000046#include "llvm/ADT/StringExtras.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000047#include "llvm/ADT/VectorExtras.h"
Mon P Wang3c81d352008-11-23 04:37:22 +000048#include "llvm/Support/CommandLine.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000049#include "llvm/Support/Debug.h"
Bill Wendlingec041eb2010-03-12 19:20:40 +000050#include "llvm/Support/Dwarf.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000051#include "llvm/Support/ErrorHandling.h"
52#include "llvm/Support/MathExtras.h"
Torok Edwindac237e2009-07-08 20:53:28 +000053#include "llvm/Support/raw_ostream.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000054using namespace llvm;
Bill Wendlingec041eb2010-03-12 19:20:40 +000055using namespace dwarf;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000056
Evan Chengb1712452010-01-27 06:25:16 +000057STATISTIC(NumTailCalls, "Number of tail calls");
58
Evan Cheng10e86422008-04-25 19:11:04 +000059// Forward declarations.
Owen Andersone50ed302009-08-10 22:56:29 +000060static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +000061 SDValue V2);
Evan Cheng10e86422008-04-25 19:11:04 +000062
Chris Lattnerf0144122009-07-28 03:13:23 +000063static TargetLoweringObjectFile *createTLOF(X86TargetMachine &TM) {
Evan Cheng2bffee22011-02-01 01:14:13 +000064 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
65 bool is64Bit = Subtarget->is64Bit();
NAKAMURA Takumi27635382011-02-05 15:10:54 +000066
Evan Cheng2bffee22011-02-01 01:14:13 +000067 if (Subtarget->isTargetEnvMacho()) {
Chris Lattnere019ec12010-12-19 20:07:10 +000068 if (is64Bit)
69 return new X8664_MachoTargetObjectFile();
Anton Korobeynikov293d5922010-02-21 20:28:15 +000070 return new TargetLoweringObjectFileMachO();
Michael J. Spencerec38de22010-10-10 22:04:20 +000071 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000072
Evan Cheng2bffee22011-02-01 01:14:13 +000073 if (Subtarget->isTargetELF()) {
Chris Lattnere019ec12010-12-19 20:07:10 +000074 if (is64Bit)
75 return new X8664_ELFTargetObjectFile(TM);
76 return new X8632_ELFTargetObjectFile(TM);
77 }
Evan Cheng2bffee22011-02-01 01:14:13 +000078 if (Subtarget->isTargetCOFF() && !Subtarget->isTargetEnvMacho())
Chris Lattnere019ec12010-12-19 20:07:10 +000079 return new TargetLoweringObjectFileCOFF();
Eric Christopher62f35a22010-07-05 19:26:33 +000080 llvm_unreachable("unknown subtarget type");
Chris Lattnerf0144122009-07-28 03:13:23 +000081}
82
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +000083X86TargetLowering::X86TargetLowering(X86TargetMachine &TM)
Chris Lattnerf0144122009-07-28 03:13:23 +000084 : TargetLowering(TM, createTLOF(TM)) {
Evan Cheng559806f2006-01-27 08:10:46 +000085 Subtarget = &TM.getSubtarget<X86Subtarget>();
Nate Begeman2ea8ee72010-12-10 00:26:57 +000086 X86ScalarSSEf64 = Subtarget->hasXMMInt();
87 X86ScalarSSEf32 = Subtarget->hasXMM();
Evan Cheng25ab6902006-09-08 06:48:29 +000088 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000089
Anton Korobeynikov2365f512007-07-14 14:06:15 +000090 RegInfo = TM.getRegisterInfo();
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000091 TD = getTargetData();
Anton Korobeynikov2365f512007-07-14 14:06:15 +000092
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000093 // Set up the TargetLowering object.
Chris Lattnera34b3cf2010-12-19 20:03:11 +000094 static MVT IntVTs[] = { MVT::i8, MVT::i16, MVT::i32, MVT::i64 };
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000095
96 // X86 is weird, it always uses i8 for shift amounts and setcc results.
Owen Anderson825b72b2009-08-11 20:47:22 +000097 setShiftAmountType(MVT::i8);
Duncan Sands03228082008-11-23 15:47:28 +000098 setBooleanContents(ZeroOrOneBooleanContent);
Evan Cheng211ffa12010-05-19 20:19:50 +000099 setSchedulingPreference(Sched::RegPressure);
Evan Cheng25ab6902006-09-08 06:48:29 +0000100 setStackPointerRegisterToSaveRestore(X86StackPtr);
Evan Cheng714554d2006-03-16 21:47:42 +0000101
Michael J. Spencer92bf38c2010-10-10 23:11:06 +0000102 if (Subtarget->isTargetWindows() && !Subtarget->isTargetCygMing()) {
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000103 // Setup Windows compiler runtime calls.
104 setLibcallName(RTLIB::SDIV_I64, "_alldiv");
Michael J. Spencer335b8062010-10-11 05:29:15 +0000105 setLibcallName(RTLIB::UDIV_I64, "_aulldiv");
106 setLibcallName(RTLIB::FPTOUINT_F64_I64, "_ftol2");
Michael J. Spencer94f7eeb2010-10-19 07:32:52 +0000107 setLibcallName(RTLIB::FPTOUINT_F32_I64, "_ftol2");
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000108 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::X86_StdCall);
Michael J. Spencer335b8062010-10-11 05:29:15 +0000109 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::X86_StdCall);
Michael J. Spencer6dad10e2010-10-27 18:52:38 +0000110 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I64, CallingConv::C);
111 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I64, CallingConv::C);
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000112 }
113
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000114 if (Subtarget->isTargetDarwin()) {
Evan Chengdf57fa02006-03-17 20:31:41 +0000115 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000116 setUseUnderscoreSetJmp(false);
117 setUseUnderscoreLongJmp(false);
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000118 } else if (Subtarget->isTargetMingw()) {
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000119 // MS runtime is weird: it exports _setjmp, but longjmp!
120 setUseUnderscoreSetJmp(true);
121 setUseUnderscoreLongJmp(false);
122 } else {
123 setUseUnderscoreSetJmp(true);
124 setUseUnderscoreLongJmp(true);
125 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000126
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000127 // Set up the register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000128 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
Dan Gohman71edb242010-04-30 18:30:26 +0000129 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000130 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
Evan Cheng25ab6902006-09-08 06:48:29 +0000131 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000132 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000133
Owen Anderson825b72b2009-08-11 20:47:22 +0000134 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Evan Chengc5484282006-10-04 00:56:09 +0000135
Scott Michelfdc40a02009-02-17 22:15:04 +0000136 // We don't accept any truncstore of integer registers.
Owen Anderson825b72b2009-08-11 20:47:22 +0000137 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000138 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000139 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000140 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000141 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
142 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
Evan Cheng7f042682008-10-15 02:05:31 +0000143
144 // SETOEQ and SETUNE require checking two conditions.
Owen Anderson825b72b2009-08-11 20:47:22 +0000145 setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand);
146 setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand);
147 setCondCodeAction(ISD::SETOEQ, MVT::f80, Expand);
148 setCondCodeAction(ISD::SETUNE, MVT::f32, Expand);
149 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
150 setCondCodeAction(ISD::SETUNE, MVT::f80, Expand);
Chris Lattnerddf89562008-01-17 19:59:44 +0000151
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000152 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
153 // operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000154 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
155 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
156 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +0000157
Evan Cheng25ab6902006-09-08 06:48:29 +0000158 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000159 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
160 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
Eli Friedman948e95a2009-05-23 09:59:16 +0000161 } else if (!UseSoftFloat) {
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000162 // We have an algorithm for SSE2->double, and we turn this into a
163 // 64-bit FILD followed by conditional FADD for other targets.
164 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Eli Friedman948e95a2009-05-23 09:59:16 +0000165 // We have an algorithm for SSE2, and we turn this into a 64-bit
166 // FILD for other targets.
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000167 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000168 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000169
170 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
171 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000172 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
173 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000174
Devang Patel6a784892009-06-05 18:48:29 +0000175 if (!UseSoftFloat) {
Bill Wendling105be5a2009-03-13 08:41:47 +0000176 // SSE has no i16 to fp conversion, only i32
177 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000178 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000179 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000180 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000181 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000182 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
183 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000184 }
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000185 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000186 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
187 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Promote);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000188 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000189
Dale Johannesen73328d12007-09-19 23:55:34 +0000190 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
191 // are Legal, f80 is custom lowered.
Owen Anderson825b72b2009-08-11 20:47:22 +0000192 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
193 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Evan Cheng6dab0532006-01-30 08:02:57 +0000194
Evan Cheng02568ff2006-01-30 22:13:22 +0000195 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
196 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000197 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
198 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
Evan Cheng02568ff2006-01-30 22:13:22 +0000199
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000200 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000201 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000202 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000203 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000204 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000205 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
206 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000207 }
208
209 // Handle FP_TO_UINT by promoting the destination to a larger signed
210 // conversion.
Owen Anderson825b72b2009-08-11 20:47:22 +0000211 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
212 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
213 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000214
Evan Cheng25ab6902006-09-08 06:48:29 +0000215 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000216 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
217 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Eli Friedman948e95a2009-05-23 09:59:16 +0000218 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000219 if (X86ScalarSSEf32 && !Subtarget->hasSSE3())
Evan Cheng25ab6902006-09-08 06:48:29 +0000220 // Expand FP_TO_UINT into a select.
221 // FIXME: We would like to use a Custom expander here eventually to do
222 // the optimal thing for SSE vs. the default expansion in the legalizer.
Owen Anderson825b72b2009-08-11 20:47:22 +0000223 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000224 else
Eli Friedman948e95a2009-05-23 09:59:16 +0000225 // With SSE3 we can use fisttpll to convert to a signed i64; without
226 // SSE, we're stuck with a fistpll.
Owen Anderson825b72b2009-08-11 20:47:22 +0000227 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000228 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000229
Chris Lattner399610a2006-12-05 18:22:22 +0000230 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Michael J. Spencerec38de22010-10-10 22:04:20 +0000231 if (!X86ScalarSSEf64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000232 setOperationAction(ISD::BITCAST , MVT::f32 , Expand);
233 setOperationAction(ISD::BITCAST , MVT::i32 , Expand);
Dale Johannesene39859a2010-05-21 18:40:15 +0000234 if (Subtarget->is64Bit()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000235 setOperationAction(ISD::BITCAST , MVT::f64 , Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000236 // Without SSE, i64->f64 goes through memory.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000237 setOperationAction(ISD::BITCAST , MVT::i64 , Expand);
Dale Johannesen7d07b482010-05-21 00:52:33 +0000238 }
Chris Lattnerf3597a12006-12-05 18:45:06 +0000239 }
Chris Lattner21f66852005-12-23 05:15:23 +0000240
Dan Gohmanb00ee212008-02-18 19:34:53 +0000241 // Scalar integer divide and remainder are lowered to use operations that
242 // produce two results, to match the available instructions. This exposes
243 // the two-result form to trivial CSE, which is able to combine x/y and x%y
244 // into a single instruction.
245 //
246 // Scalar integer multiply-high is also lowered to use two-result
247 // operations, to match the available instructions. However, plain multiply
248 // (low) operations are left as Legal, as there are single-result
249 // instructions for this in x86. Using the two-result multiply instructions
250 // when both high and low results are needed must be arranged by dagcombine.
Chris Lattnere019ec12010-12-19 20:07:10 +0000251 for (unsigned i = 0, e = 4; i != e; ++i) {
252 MVT VT = IntVTs[i];
253 setOperationAction(ISD::MULHS, VT, Expand);
254 setOperationAction(ISD::MULHU, VT, Expand);
255 setOperationAction(ISD::SDIV, VT, Expand);
256 setOperationAction(ISD::UDIV, VT, Expand);
257 setOperationAction(ISD::SREM, VT, Expand);
258 setOperationAction(ISD::UREM, VT, Expand);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000259
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000260 // Add/Sub overflow ops with MVT::Glues are lowered to EFLAGS dependences.
Chris Lattnerd8ff7ec2010-12-20 01:03:27 +0000261 setOperationAction(ISD::ADDC, VT, Custom);
262 setOperationAction(ISD::ADDE, VT, Custom);
263 setOperationAction(ISD::SUBC, VT, Custom);
264 setOperationAction(ISD::SUBE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000265 }
Dan Gohmana37c9f72007-09-25 18:23:27 +0000266
Owen Anderson825b72b2009-08-11 20:47:22 +0000267 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
268 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
269 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
270 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000271 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000272 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
273 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
274 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
275 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
276 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
277 setOperationAction(ISD::FREM , MVT::f32 , Expand);
278 setOperationAction(ISD::FREM , MVT::f64 , Expand);
279 setOperationAction(ISD::FREM , MVT::f80 , Expand);
280 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000281
Owen Anderson825b72b2009-08-11 20:47:22 +0000282 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
283 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000284 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
285 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000286 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
287 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000288 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000289 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
290 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000291 }
292
Benjamin Kramer1292c222010-12-04 20:32:23 +0000293 if (Subtarget->hasPOPCNT()) {
294 setOperationAction(ISD::CTPOP , MVT::i8 , Promote);
295 } else {
296 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
297 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
298 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
299 if (Subtarget->is64Bit())
300 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
301 }
302
Owen Anderson825b72b2009-08-11 20:47:22 +0000303 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
304 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000305
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000306 // These should be promoted to a larger select which is supported.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000307 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000308 // X86 wants to expand cmov itself.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000309 setOperationAction(ISD::SELECT , MVT::i8 , Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000310 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000311 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
312 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
313 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
314 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
315 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000316 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000317 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
318 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
319 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
320 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000321 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000322 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
323 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000324 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000325 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000326
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000327 // Darwin ABI issue.
Owen Anderson825b72b2009-08-11 20:47:22 +0000328 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
329 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
330 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
331 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000332 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000333 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
334 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000335 setOperationAction(ISD::BlockAddress , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000336 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000337 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
338 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
339 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
340 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000341 setOperationAction(ISD::BlockAddress , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000342 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000343 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Owen Anderson825b72b2009-08-11 20:47:22 +0000344 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
345 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
346 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000347 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000348 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
349 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
350 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000351 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000352
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000353 if (Subtarget->hasXMM())
Owen Anderson825b72b2009-08-11 20:47:22 +0000354 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Cheng27b7db52008-03-08 00:58:38 +0000355
Eric Christopher9a9d2752010-07-22 02:48:34 +0000356 // We may not have a libcall for MEMBARRIER so we should lower this.
357 setOperationAction(ISD::MEMBARRIER , MVT::Other, Custom);
Michael J. Spencerec38de22010-10-10 22:04:20 +0000358
Jim Grosbachf1ab49e2010-06-23 16:25:07 +0000359 // On X86 and X86-64, atomic operations are lowered to locked instructions.
360 // Locked instructions, in turn, have implicit fence semantics (all memory
361 // operations are flushed before issuing the locked instruction, and they
362 // are not buffered), so we can fold away the common pattern of
363 // fence-atomic-fence.
364 setShouldFoldAtomicFences(true);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000365
Mon P Wang63307c32008-05-05 19:05:59 +0000366 // Expand certain atomics
Chris Lattnere019ec12010-12-19 20:07:10 +0000367 for (unsigned i = 0, e = 4; i != e; ++i) {
368 MVT VT = IntVTs[i];
369 setOperationAction(ISD::ATOMIC_CMP_SWAP, VT, Custom);
370 setOperationAction(ISD::ATOMIC_LOAD_SUB, VT, Custom);
371 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000372
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000373 if (!Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000374 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
375 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
376 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
377 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
378 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
379 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
380 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000381 }
382
Evan Cheng3c992d22006-03-07 02:02:57 +0000383 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000384 if (!Subtarget->isTargetDarwin() &&
385 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000386 !Subtarget->isTargetCygMing()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000387 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Dan Gohman44066042008-07-01 00:05:16 +0000388 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000389
Owen Anderson825b72b2009-08-11 20:47:22 +0000390 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
391 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
392 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
393 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000394 if (Subtarget->is64Bit()) {
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000395 setExceptionPointerRegister(X86::RAX);
396 setExceptionSelectorRegister(X86::RDX);
397 } else {
398 setExceptionPointerRegister(X86::EAX);
399 setExceptionSelectorRegister(X86::EDX);
400 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000401 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
402 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000403
Owen Anderson825b72b2009-08-11 20:47:22 +0000404 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000405
Owen Anderson825b72b2009-08-11 20:47:22 +0000406 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000407
Nate Begemanacc398c2006-01-25 18:21:52 +0000408 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson825b72b2009-08-11 20:47:22 +0000409 setOperationAction(ISD::VASTART , MVT::Other, Custom);
410 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000411 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000412 setOperationAction(ISD::VAARG , MVT::Other, Custom);
413 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000414 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000415 setOperationAction(ISD::VAARG , MVT::Other, Expand);
416 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000417 }
Evan Chengae642192007-03-02 23:16:35 +0000418
Owen Anderson825b72b2009-08-11 20:47:22 +0000419 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
420 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000421 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000422 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Michael J. Spencere9c253e2010-10-21 01:41:01 +0000423 if (Subtarget->isTargetCygMing() || Subtarget->isTargetWindows())
Owen Anderson825b72b2009-08-11 20:47:22 +0000424 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +0000425 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000426 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000427
Evan Chengc7ce29b2009-02-13 22:36:38 +0000428 if (!UseSoftFloat && X86ScalarSSEf64) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000429 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000430 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000431 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
432 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000433
Evan Cheng223547a2006-01-31 22:28:30 +0000434 // Use ANDPD to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000435 setOperationAction(ISD::FABS , MVT::f64, Custom);
436 setOperationAction(ISD::FABS , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000437
438 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000439 setOperationAction(ISD::FNEG , MVT::f64, Custom);
440 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000441
Evan Cheng68c47cb2007-01-05 07:55:56 +0000442 // Use ANDPD and ORPD to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000443 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
444 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000445
Evan Chengd25e9e82006-02-02 00:28:23 +0000446 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000447 setOperationAction(ISD::FSIN , MVT::f64, Expand);
448 setOperationAction(ISD::FCOS , MVT::f64, Expand);
449 setOperationAction(ISD::FSIN , MVT::f32, Expand);
450 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000451
Chris Lattnera54aa942006-01-29 06:26:08 +0000452 // Expand FP immediates into loads from the stack, except for the special
453 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000454 addLegalFPImmediate(APFloat(+0.0)); // xorpd
455 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Evan Chengc7ce29b2009-02-13 22:36:38 +0000456 } else if (!UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000457 // Use SSE for f32, x87 for f64.
458 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000459 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
460 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000461
462 // Use ANDPS to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000463 setOperationAction(ISD::FABS , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000464
465 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000466 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000467
Owen Anderson825b72b2009-08-11 20:47:22 +0000468 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000469
470 // Use ANDPS and ORPS to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000471 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
472 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000473
474 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000475 setOperationAction(ISD::FSIN , MVT::f32, Expand);
476 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000477
Nate Begemane1795842008-02-14 08:57:00 +0000478 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000479 addLegalFPImmediate(APFloat(+0.0f)); // xorps
480 addLegalFPImmediate(APFloat(+0.0)); // FLD0
481 addLegalFPImmediate(APFloat(+1.0)); // FLD1
482 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
483 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
484
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000485 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000486 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
487 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000488 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000489 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000490 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000491 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000492 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
493 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000494
Owen Anderson825b72b2009-08-11 20:47:22 +0000495 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
496 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
497 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
498 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000499
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000500 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000501 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
502 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000503 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000504 addLegalFPImmediate(APFloat(+0.0)); // FLD0
505 addLegalFPImmediate(APFloat(+1.0)); // FLD1
506 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
507 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000508 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
509 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
510 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
511 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000512 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000513
Dale Johannesen59a58732007-08-05 18:49:15 +0000514 // Long double always uses X87.
Evan Cheng92722532009-03-26 23:06:32 +0000515 if (!UseSoftFloat) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000516 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
517 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
518 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000519 {
Benjamin Kramer98383962010-12-04 14:22:24 +0000520 APFloat TmpFlt = APFloat::getZero(APFloat::x87DoubleExtended);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000521 addLegalFPImmediate(TmpFlt); // FLD0
522 TmpFlt.changeSign();
523 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
Benjamin Kramer98383962010-12-04 14:22:24 +0000524
525 bool ignored;
Evan Chengc7ce29b2009-02-13 22:36:38 +0000526 APFloat TmpFlt2(+1.0);
527 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
528 &ignored);
529 addLegalFPImmediate(TmpFlt2); // FLD1
530 TmpFlt2.changeSign();
531 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
532 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000533
Evan Chengc7ce29b2009-02-13 22:36:38 +0000534 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000535 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
536 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000537 }
Dale Johannesen2f429012007-09-26 21:10:55 +0000538 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000539
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000540 // Always use a library call for pow.
Owen Anderson825b72b2009-08-11 20:47:22 +0000541 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
542 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
543 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000544
Owen Anderson825b72b2009-08-11 20:47:22 +0000545 setOperationAction(ISD::FLOG, MVT::f80, Expand);
546 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
547 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
548 setOperationAction(ISD::FEXP, MVT::f80, Expand);
549 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000550
Mon P Wangf007a8b2008-11-06 05:31:54 +0000551 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000552 // (for widening) or expand (for scalarization). Then we will selectively
553 // turn on ones that can be effectively codegen'd.
Owen Anderson825b72b2009-08-11 20:47:22 +0000554 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
555 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
556 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
557 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
558 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
559 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
560 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
561 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
562 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
563 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
564 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
565 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
566 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
567 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
568 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
569 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
570 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000571 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
David Greenecfe33c42011-01-26 19:13:22 +0000572 setOperationAction(ISD::EXTRACT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
573 setOperationAction(ISD::INSERT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000574 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
575 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
576 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
577 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
578 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
579 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
580 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
581 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
582 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
583 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
584 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
585 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
586 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
587 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
588 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
589 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
590 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
591 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
592 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
593 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
594 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
595 setOperationAction(ISD::VSETCC, (MVT::SimpleValueType)VT, Expand);
596 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
597 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
598 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
599 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
600 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
601 setOperationAction(ISD::FP_TO_UINT, (MVT::SimpleValueType)VT, Expand);
602 setOperationAction(ISD::FP_TO_SINT, (MVT::SimpleValueType)VT, Expand);
603 setOperationAction(ISD::UINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
604 setOperationAction(ISD::SINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
Dan Gohman87862e72009-12-11 21:31:27 +0000605 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,Expand);
Dan Gohman2e141d72009-12-14 23:40:38 +0000606 setOperationAction(ISD::TRUNCATE, (MVT::SimpleValueType)VT, Expand);
607 setOperationAction(ISD::SIGN_EXTEND, (MVT::SimpleValueType)VT, Expand);
608 setOperationAction(ISD::ZERO_EXTEND, (MVT::SimpleValueType)VT, Expand);
609 setOperationAction(ISD::ANY_EXTEND, (MVT::SimpleValueType)VT, Expand);
610 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
611 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
612 setTruncStoreAction((MVT::SimpleValueType)VT,
613 (MVT::SimpleValueType)InnerVT, Expand);
614 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
615 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
616 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000617 }
618
Evan Chengc7ce29b2009-02-13 22:36:38 +0000619 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
620 // with -msoft-float, disable use of MMX as well.
Chris Lattner2a786eb2010-12-19 20:19:20 +0000621 if (!UseSoftFloat && Subtarget->hasMMX()) {
Dale Johannesene93d99c2010-10-20 21:32:10 +0000622 addRegisterClass(MVT::x86mmx, X86::VR64RegisterClass);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000623 // No operations on x86mmx supported, everything uses intrinsics.
Evan Cheng470a6ad2006-02-22 02:26:30 +0000624 }
625
Dale Johannesen0488fb62010-09-30 23:57:10 +0000626 // MMX-sized vectors (other than x86mmx) are expected to be expanded
627 // into smaller operations.
628 setOperationAction(ISD::MULHS, MVT::v8i8, Expand);
629 setOperationAction(ISD::MULHS, MVT::v4i16, Expand);
630 setOperationAction(ISD::MULHS, MVT::v2i32, Expand);
631 setOperationAction(ISD::MULHS, MVT::v1i64, Expand);
632 setOperationAction(ISD::AND, MVT::v8i8, Expand);
633 setOperationAction(ISD::AND, MVT::v4i16, Expand);
634 setOperationAction(ISD::AND, MVT::v2i32, Expand);
635 setOperationAction(ISD::AND, MVT::v1i64, Expand);
636 setOperationAction(ISD::OR, MVT::v8i8, Expand);
637 setOperationAction(ISD::OR, MVT::v4i16, Expand);
638 setOperationAction(ISD::OR, MVT::v2i32, Expand);
639 setOperationAction(ISD::OR, MVT::v1i64, Expand);
640 setOperationAction(ISD::XOR, MVT::v8i8, Expand);
641 setOperationAction(ISD::XOR, MVT::v4i16, Expand);
642 setOperationAction(ISD::XOR, MVT::v2i32, Expand);
643 setOperationAction(ISD::XOR, MVT::v1i64, Expand);
644 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Expand);
645 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Expand);
646 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Expand);
647 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Expand);
648 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v1i64, Expand);
649 setOperationAction(ISD::SELECT, MVT::v8i8, Expand);
650 setOperationAction(ISD::SELECT, MVT::v4i16, Expand);
651 setOperationAction(ISD::SELECT, MVT::v2i32, Expand);
652 setOperationAction(ISD::SELECT, MVT::v1i64, Expand);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000653 setOperationAction(ISD::BITCAST, MVT::v8i8, Expand);
654 setOperationAction(ISD::BITCAST, MVT::v4i16, Expand);
655 setOperationAction(ISD::BITCAST, MVT::v2i32, Expand);
656 setOperationAction(ISD::BITCAST, MVT::v1i64, Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000657
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000658 if (!UseSoftFloat && Subtarget->hasXMM()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000659 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000660
Owen Anderson825b72b2009-08-11 20:47:22 +0000661 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
662 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
663 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
664 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
665 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
666 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
667 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
668 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
669 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
670 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
671 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
672 setOperationAction(ISD::VSETCC, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000673 }
674
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000675 if (!UseSoftFloat && Subtarget->hasXMMInt()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000676 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000677
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000678 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
679 // registers cannot be used even for integer operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000680 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
681 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
682 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
683 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000684
Owen Anderson825b72b2009-08-11 20:47:22 +0000685 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
686 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
687 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
688 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
689 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
690 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
691 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
692 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
693 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
694 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
695 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
696 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
697 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
698 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
699 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
700 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000701
Owen Anderson825b72b2009-08-11 20:47:22 +0000702 setOperationAction(ISD::VSETCC, MVT::v2f64, Custom);
703 setOperationAction(ISD::VSETCC, MVT::v16i8, Custom);
704 setOperationAction(ISD::VSETCC, MVT::v8i16, Custom);
705 setOperationAction(ISD::VSETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000706
Owen Anderson825b72b2009-08-11 20:47:22 +0000707 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
708 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
709 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
710 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
711 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000712
Mon P Wangeb38ebf2010-01-24 00:05:03 +0000713 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2f64, Custom);
714 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2i64, Custom);
715 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i8, Custom);
716 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i16, Custom);
717 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i32, Custom);
718
Evan Cheng2c3ae372006-04-12 21:21:57 +0000719 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Owen Anderson825b72b2009-08-11 20:47:22 +0000720 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
721 EVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000722 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000723 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000724 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000725 // Do not attempt to custom lower non-128-bit vectors
726 if (!VT.is128BitVector())
727 continue;
Owen Anderson825b72b2009-08-11 20:47:22 +0000728 setOperationAction(ISD::BUILD_VECTOR,
729 VT.getSimpleVT().SimpleTy, Custom);
730 setOperationAction(ISD::VECTOR_SHUFFLE,
731 VT.getSimpleVT().SimpleTy, Custom);
732 setOperationAction(ISD::EXTRACT_VECTOR_ELT,
733 VT.getSimpleVT().SimpleTy, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000734 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000735
Owen Anderson825b72b2009-08-11 20:47:22 +0000736 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
737 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
738 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
739 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
740 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
741 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000742
Nate Begemancdd1eec2008-02-12 22:51:28 +0000743 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000744 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
745 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000746 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000747
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000748 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Owen Anderson825b72b2009-08-11 20:47:22 +0000749 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; i++) {
750 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
Owen Andersone50ed302009-08-10 22:56:29 +0000751 EVT VT = SVT;
David Greene9b9838d2009-06-29 16:47:10 +0000752
753 // Do not attempt to promote non-128-bit vectors
Chris Lattner32b4b5a2010-07-05 05:53:14 +0000754 if (!VT.is128BitVector())
David Greene9b9838d2009-06-29 16:47:10 +0000755 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +0000756
Owen Andersond6662ad2009-08-10 20:46:15 +0000757 setOperationAction(ISD::AND, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000758 AddPromotedToType (ISD::AND, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000759 setOperationAction(ISD::OR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000760 AddPromotedToType (ISD::OR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000761 setOperationAction(ISD::XOR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000762 AddPromotedToType (ISD::XOR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000763 setOperationAction(ISD::LOAD, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000764 AddPromotedToType (ISD::LOAD, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000765 setOperationAction(ISD::SELECT, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000766 AddPromotedToType (ISD::SELECT, SVT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000767 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000768
Owen Anderson825b72b2009-08-11 20:47:22 +0000769 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000770
Evan Cheng2c3ae372006-04-12 21:21:57 +0000771 // Custom lower v2i64 and v2f64 selects.
Owen Anderson825b72b2009-08-11 20:47:22 +0000772 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
773 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
774 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
775 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000776
Owen Anderson825b72b2009-08-11 20:47:22 +0000777 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
778 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000779 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000780
Nate Begeman14d12ca2008-02-11 04:19:36 +0000781 if (Subtarget->hasSSE41()) {
Dale Johannesen54feef22010-05-27 20:12:41 +0000782 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
783 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
784 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
785 setOperationAction(ISD::FRINT, MVT::f32, Legal);
786 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
787 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
788 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
789 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
790 setOperationAction(ISD::FRINT, MVT::f64, Legal);
791 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
792
Nate Begeman14d12ca2008-02-11 04:19:36 +0000793 // FIXME: Do we need to handle scalar-to-vector here?
Owen Anderson825b72b2009-08-11 20:47:22 +0000794 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000795
Nate Begemanbdcb5af2010-07-27 22:37:06 +0000796 // Can turn SHL into an integer multiply.
797 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
Nate Begeman51409212010-07-28 00:21:48 +0000798 setOperationAction(ISD::SHL, MVT::v16i8, Custom);
Nate Begemanbdcb5af2010-07-27 22:37:06 +0000799
Nate Begeman14d12ca2008-02-11 04:19:36 +0000800 // i8 and i16 vectors are custom , because the source register and source
801 // source memory operand types are not the same width. f32 vectors are
802 // custom since the immediate controlling the insert encodes additional
803 // information.
Owen Anderson825b72b2009-08-11 20:47:22 +0000804 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
805 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
806 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
807 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000808
Owen Anderson825b72b2009-08-11 20:47:22 +0000809 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
810 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
811 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
812 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000813
814 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000815 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
816 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000817 }
818 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000819
Chris Lattnera34b3cf2010-12-19 20:03:11 +0000820 if (Subtarget->hasSSE42())
Owen Anderson825b72b2009-08-11 20:47:22 +0000821 setOperationAction(ISD::VSETCC, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000822
David Greene9b9838d2009-06-29 16:47:10 +0000823 if (!UseSoftFloat && Subtarget->hasAVX()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000824 addRegisterClass(MVT::v8f32, X86::VR256RegisterClass);
825 addRegisterClass(MVT::v4f64, X86::VR256RegisterClass);
826 addRegisterClass(MVT::v8i32, X86::VR256RegisterClass);
827 addRegisterClass(MVT::v4i64, X86::VR256RegisterClass);
Bruno Cardoso Lopes405f11b2010-08-10 01:43:16 +0000828 addRegisterClass(MVT::v32i8, X86::VR256RegisterClass);
David Greened94c1012009-06-29 22:50:51 +0000829
Owen Anderson825b72b2009-08-11 20:47:22 +0000830 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
831 setOperationAction(ISD::LOAD, MVT::v8i32, Legal);
832 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
833 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
David Greene54d8eba2011-01-27 22:38:56 +0000834
Owen Anderson825b72b2009-08-11 20:47:22 +0000835 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
836 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
837 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
838 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
839 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
840 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000841
Owen Anderson825b72b2009-08-11 20:47:22 +0000842 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
843 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
844 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
845 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
846 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
847 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000848
David Greene54d8eba2011-01-27 22:38:56 +0000849 // Custom lower build_vector, vector_shuffle, scalar_to_vector,
850 // insert_vector_elt extract_subvector and extract_vector_elt for
851 // 256-bit types.
852 for (unsigned i = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
853 i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE;
854 ++i) {
855 MVT::SimpleValueType VT = (MVT::SimpleValueType)i;
856 // Do not attempt to custom lower non-256-bit vectors
857 if (!isPowerOf2_32(MVT(VT).getVectorNumElements())
858 || (MVT(VT).getSizeInBits() < 256))
David Greene9b9838d2009-06-29 16:47:10 +0000859 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000860 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
861 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
David Greene54d8eba2011-01-27 22:38:56 +0000862 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000863 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
David Greene54d8eba2011-01-27 22:38:56 +0000864 setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000865 }
David Greene54d8eba2011-01-27 22:38:56 +0000866 // Custom-lower insert_subvector and extract_subvector based on
867 // the result type.
868 for (unsigned i = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
869 i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE;
870 ++i) {
871 MVT::SimpleValueType VT = (MVT::SimpleValueType)i;
872 // Do not attempt to custom lower non-256-bit vectors
873 if (!isPowerOf2_32(MVT(VT).getVectorNumElements()))
David Greene9b9838d2009-06-29 16:47:10 +0000874 continue;
David Greene54d8eba2011-01-27 22:38:56 +0000875
876 if (MVT(VT).getSizeInBits() == 128) {
877 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000878 }
David Greene54d8eba2011-01-27 22:38:56 +0000879 else if (MVT(VT).getSizeInBits() == 256) {
880 setOperationAction(ISD::INSERT_SUBVECTOR, VT, Custom);
881 }
David Greene9b9838d2009-06-29 16:47:10 +0000882 }
883
David Greene54d8eba2011-01-27 22:38:56 +0000884 // Promote v32i8, v16i16, v8i32 select, and, or, xor to v4i64.
885 // Don't promote loads because we need them for VPERM vector index versions.
886
887 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
888 VT != (unsigned)MVT::LAST_VECTOR_VALUETYPE;
889 VT++) {
890 if (!isPowerOf2_32(MVT((MVT::SimpleValueType)VT).getVectorNumElements())
891 || (MVT((MVT::SimpleValueType)VT).getSizeInBits() < 256))
892 continue;
893 setOperationAction(ISD::AND, (MVT::SimpleValueType)VT, Promote);
894 AddPromotedToType (ISD::AND, (MVT::SimpleValueType)VT, MVT::v4i64);
895 setOperationAction(ISD::OR, (MVT::SimpleValueType)VT, Promote);
896 AddPromotedToType (ISD::OR, (MVT::SimpleValueType)VT, MVT::v4i64);
897 setOperationAction(ISD::XOR, (MVT::SimpleValueType)VT, Promote);
898 AddPromotedToType (ISD::XOR, (MVT::SimpleValueType)VT, MVT::v4i64);
899 //setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Promote);
900 //AddPromotedToType (ISD::LOAD, (MVT::SimpleValueType)VT, MVT::v4i64);
901 setOperationAction(ISD::SELECT, (MVT::SimpleValueType)VT, Promote);
902 AddPromotedToType (ISD::SELECT, (MVT::SimpleValueType)VT, MVT::v4i64);
903 }
David Greene9b9838d2009-06-29 16:47:10 +0000904 }
905
Evan Cheng6be2c582006-04-05 23:38:46 +0000906 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000907 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +0000908
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000909
Eli Friedman962f5492010-06-02 19:35:46 +0000910 // Only custom-lower 64-bit SADDO and friends on 64-bit because we don't
911 // handle type legalization for these operations here.
Dan Gohman71c62a22010-06-02 19:13:40 +0000912 //
Eli Friedman962f5492010-06-02 19:35:46 +0000913 // FIXME: We really should do custom legalization for addition and
914 // subtraction on x86-32 once PR3203 is fixed. We really can't do much better
915 // than generic legalization for 64-bit multiplication-with-overflow, though.
Chris Lattnera34b3cf2010-12-19 20:03:11 +0000916 for (unsigned i = 0, e = 3+Subtarget->is64Bit(); i != e; ++i) {
917 // Add/Sub/Mul with overflow operations are custom lowered.
918 MVT VT = IntVTs[i];
919 setOperationAction(ISD::SADDO, VT, Custom);
920 setOperationAction(ISD::UADDO, VT, Custom);
921 setOperationAction(ISD::SSUBO, VT, Custom);
922 setOperationAction(ISD::USUBO, VT, Custom);
923 setOperationAction(ISD::SMULO, VT, Custom);
924 setOperationAction(ISD::UMULO, VT, Custom);
Eli Friedmana993f0a2010-06-02 00:27:18 +0000925 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000926
Chris Lattnera34b3cf2010-12-19 20:03:11 +0000927 // There are no 8-bit 3-address imul/mul instructions
928 setOperationAction(ISD::SMULO, MVT::i8, Expand);
929 setOperationAction(ISD::UMULO, MVT::i8, Expand);
Bill Wendling41ea7e72008-11-24 19:21:46 +0000930
Evan Chengd54f2d52009-03-31 19:38:51 +0000931 if (!Subtarget->is64Bit()) {
932 // These libcalls are not available in 32-bit.
933 setLibcallName(RTLIB::SHL_I128, 0);
934 setLibcallName(RTLIB::SRL_I128, 0);
935 setLibcallName(RTLIB::SRA_I128, 0);
936 }
937
Evan Cheng206ee9d2006-07-07 08:33:52 +0000938 // We have target-specific dag combine patterns for the following nodes:
939 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Dan Gohman1bbf72b2010-03-15 23:23:03 +0000940 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Evan Chengd880b972008-05-09 21:53:03 +0000941 setTargetDAGCombine(ISD::BUILD_VECTOR);
Chris Lattner83e6c992006-10-04 06:57:07 +0000942 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +0000943 setTargetDAGCombine(ISD::SHL);
944 setTargetDAGCombine(ISD::SRA);
945 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +0000946 setTargetDAGCombine(ISD::OR);
Nate Begemanb65c1752010-12-17 22:55:37 +0000947 setTargetDAGCombine(ISD::AND);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +0000948 setTargetDAGCombine(ISD::ADD);
949 setTargetDAGCombine(ISD::SUB);
Chris Lattner149a4e52008-02-22 02:09:43 +0000950 setTargetDAGCombine(ISD::STORE);
Evan Cheng2e489c42009-12-16 00:53:11 +0000951 setTargetDAGCombine(ISD::ZERO_EXTEND);
Evan Cheng0b0cd912009-03-28 05:57:29 +0000952 if (Subtarget->is64Bit())
953 setTargetDAGCombine(ISD::MUL);
Evan Cheng206ee9d2006-07-07 08:33:52 +0000954
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000955 computeRegisterProperties();
956
Evan Cheng05219282011-01-06 06:52:41 +0000957 // On Darwin, -Os means optimize for size without hurting performance,
958 // do not reduce the limit.
Dan Gohman87060f52008-06-30 21:00:56 +0000959 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +0000960 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 16 : 8;
Evan Cheng255f20f2010-04-01 06:04:33 +0000961 maxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +0000962 maxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
963 maxStoresPerMemmove = 8; // For @llvm.memmove -> sequence of stores
964 maxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Evan Chengfb8075d2008-02-28 00:43:03 +0000965 setPrefLoopAlignment(16);
Evan Cheng6ebf7bc2009-05-13 21:42:09 +0000966 benefitFromCodePlacementOpt = true;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000967}
968
Scott Michel5b8f82e2008-03-10 15:42:14 +0000969
Owen Anderson825b72b2009-08-11 20:47:22 +0000970MVT::SimpleValueType X86TargetLowering::getSetCCResultType(EVT VT) const {
971 return MVT::i8;
Scott Michel5b8f82e2008-03-10 15:42:14 +0000972}
973
974
Evan Cheng29286502008-01-23 23:17:41 +0000975/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
976/// the desired ByVal argument alignment.
977static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
978 if (MaxAlign == 16)
979 return;
980 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
981 if (VTy->getBitWidth() == 128)
982 MaxAlign = 16;
Evan Cheng29286502008-01-23 23:17:41 +0000983 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
984 unsigned EltAlign = 0;
985 getMaxByValAlign(ATy->getElementType(), EltAlign);
986 if (EltAlign > MaxAlign)
987 MaxAlign = EltAlign;
988 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
989 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
990 unsigned EltAlign = 0;
991 getMaxByValAlign(STy->getElementType(i), EltAlign);
992 if (EltAlign > MaxAlign)
993 MaxAlign = EltAlign;
994 if (MaxAlign == 16)
995 break;
996 }
997 }
998 return;
999}
1000
1001/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1002/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001003/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1004/// are at 4-byte boundaries.
Evan Cheng29286502008-01-23 23:17:41 +00001005unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001006 if (Subtarget->is64Bit()) {
1007 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001008 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001009 if (TyAlign > 8)
1010 return TyAlign;
1011 return 8;
1012 }
1013
Evan Cheng29286502008-01-23 23:17:41 +00001014 unsigned Align = 4;
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001015 if (Subtarget->hasXMM())
Dale Johannesen0c191872008-02-08 19:48:20 +00001016 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001017 return Align;
1018}
Chris Lattner2b02a442007-02-25 08:29:00 +00001019
Evan Chengf0df0312008-05-15 08:39:06 +00001020/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengc3b0c342010-04-08 07:37:57 +00001021/// and store operations as a result of memset, memcpy, and memmove
1022/// lowering. If DstAlign is zero that means it's safe to destination
1023/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1024/// means there isn't a need to check it against alignment requirement,
1025/// probably because the source does not need to be loaded. If
1026/// 'NonScalarIntSafe' is true, that means it's safe to return a
1027/// non-scalar-integer type, e.g. empty string source, constant, or loaded
1028/// from memory. 'MemcpyStrSrc' indicates whether the memcpy source is
1029/// constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001030/// It returns EVT::Other if the type should be determined using generic
1031/// target-independent logic.
Owen Andersone50ed302009-08-10 22:56:29 +00001032EVT
Evan Cheng255f20f2010-04-01 06:04:33 +00001033X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1034 unsigned DstAlign, unsigned SrcAlign,
Evan Chengf28f8bc2010-04-02 19:36:14 +00001035 bool NonScalarIntSafe,
Evan Chengc3b0c342010-04-08 07:37:57 +00001036 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00001037 MachineFunction &MF) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001038 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
1039 // linux. This is because the stack realignment code can't handle certain
1040 // cases like PR2962. This should be removed when PR2962 is fixed.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001041 const Function *F = MF.getFunction();
Evan Chenga5e13622011-01-07 19:35:30 +00001042 if (NonScalarIntSafe &&
1043 !F->hasFnAttr(Attribute::NoImplicitFloat)) {
Evan Cheng255f20f2010-04-01 06:04:33 +00001044 if (Size >= 16 &&
Evan Chenga5e13622011-01-07 19:35:30 +00001045 (Subtarget->isUnalignedMemAccessFast() ||
1046 ((DstAlign == 0 || DstAlign >= 16) &&
1047 (SrcAlign == 0 || SrcAlign >= 16))) &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001048 Subtarget->getStackAlignment() >= 16) {
1049 if (Subtarget->hasSSE2())
1050 return MVT::v4i32;
Evan Chengf28f8bc2010-04-02 19:36:14 +00001051 if (Subtarget->hasSSE1())
Evan Cheng255f20f2010-04-01 06:04:33 +00001052 return MVT::v4f32;
Evan Chengc3b0c342010-04-08 07:37:57 +00001053 } else if (!MemcpyStrSrc && Size >= 8 &&
Evan Cheng3ea97552010-04-01 20:27:45 +00001054 !Subtarget->is64Bit() &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001055 Subtarget->getStackAlignment() >= 8 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001056 Subtarget->hasXMMInt()) {
Evan Chengc3b0c342010-04-08 07:37:57 +00001057 // Do not use f64 to lower memcpy if source is string constant. It's
1058 // better to use i32 to avoid the loads.
Evan Cheng255f20f2010-04-01 06:04:33 +00001059 return MVT::f64;
Evan Chengc3b0c342010-04-08 07:37:57 +00001060 }
Chris Lattner4002a1b2008-10-28 05:49:35 +00001061 }
Evan Chengf0df0312008-05-15 08:39:06 +00001062 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001063 return MVT::i64;
1064 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001065}
1066
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001067/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1068/// current function. The returned value is a member of the
1069/// MachineJumpTableInfo::JTEntryKind enum.
1070unsigned X86TargetLowering::getJumpTableEncoding() const {
1071 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1072 // symbol.
1073 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1074 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001075 return MachineJumpTableInfo::EK_Custom32;
Michael J. Spencerec38de22010-10-10 22:04:20 +00001076
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001077 // Otherwise, use the normal jump table encoding heuristics.
1078 return TargetLowering::getJumpTableEncoding();
1079}
1080
Chris Lattnerc64daab2010-01-26 05:02:42 +00001081const MCExpr *
1082X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1083 const MachineBasicBlock *MBB,
1084 unsigned uid,MCContext &Ctx) const{
1085 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1086 Subtarget->isPICStyleGOT());
1087 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1088 // entries.
Daniel Dunbar4e815f82010-03-15 23:51:06 +00001089 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1090 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001091}
1092
Evan Chengcc415862007-11-09 01:32:10 +00001093/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1094/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001095SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001096 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001097 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001098 // This doesn't have DebugLoc associated with it, but is not really the
1099 // same as a Register.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001100 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001101 return Table;
1102}
1103
Chris Lattner589c6f62010-01-26 06:28:43 +00001104/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1105/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1106/// MCExpr.
1107const MCExpr *X86TargetLowering::
1108getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1109 MCContext &Ctx) const {
1110 // X86-64 uses RIP relative addressing based on the jump table label.
1111 if (Subtarget->isPICStyleRIPRel())
1112 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1113
1114 // Otherwise, the reference is relative to the PIC base.
Chris Lattner142b5312010-11-14 22:48:15 +00001115 return MCSymbolRefExpr::Create(MF->getPICBaseSymbol(), Ctx);
Chris Lattner589c6f62010-01-26 06:28:43 +00001116}
1117
Bill Wendlingb4202b82009-07-01 18:50:55 +00001118/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +00001119unsigned X86TargetLowering::getFunctionAlignment(const Function *F) const {
Dan Gohman25103a22009-08-18 00:20:06 +00001120 return F->hasFnAttr(Attribute::OptimizeForSize) ? 0 : 4;
Bill Wendling20c568f2009-06-30 22:38:32 +00001121}
1122
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001123// FIXME: Why this routine is here? Move to RegInfo!
Evan Chengdee81012010-07-26 21:50:05 +00001124std::pair<const TargetRegisterClass*, uint8_t>
1125X86TargetLowering::findRepresentativeClass(EVT VT) const{
1126 const TargetRegisterClass *RRC = 0;
1127 uint8_t Cost = 1;
1128 switch (VT.getSimpleVT().SimpleTy) {
1129 default:
1130 return TargetLowering::findRepresentativeClass(VT);
1131 case MVT::i8: case MVT::i16: case MVT::i32: case MVT::i64:
1132 RRC = (Subtarget->is64Bit()
1133 ? X86::GR64RegisterClass : X86::GR32RegisterClass);
1134 break;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001135 case MVT::x86mmx:
Evan Chengdee81012010-07-26 21:50:05 +00001136 RRC = X86::VR64RegisterClass;
1137 break;
1138 case MVT::f32: case MVT::f64:
1139 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1140 case MVT::v4f32: case MVT::v2f64:
1141 case MVT::v32i8: case MVT::v8i32: case MVT::v4i64: case MVT::v8f32:
1142 case MVT::v4f64:
1143 RRC = X86::VR128RegisterClass;
1144 break;
1145 }
1146 return std::make_pair(RRC, Cost);
1147}
1148
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001149// FIXME: Why this routine is here? Move to RegInfo!
Evan Cheng70017e42010-07-24 00:39:05 +00001150unsigned
1151X86TargetLowering::getRegPressureLimit(const TargetRegisterClass *RC,
1152 MachineFunction &MF) const {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001153 const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
Anton Korobeynikovd0c38172010-11-18 21:19:35 +00001154
1155 unsigned FPDiff = TFI->hasFP(MF) ? 1 : 0;
Evan Cheng70017e42010-07-24 00:39:05 +00001156 switch (RC->getID()) {
1157 default:
1158 return 0;
1159 case X86::GR32RegClassID:
1160 return 4 - FPDiff;
1161 case X86::GR64RegClassID:
1162 return 8 - FPDiff;
1163 case X86::VR128RegClassID:
1164 return Subtarget->is64Bit() ? 10 : 4;
1165 case X86::VR64RegClassID:
1166 return 4;
1167 }
1168}
1169
Eric Christopherf7a0c7b2010-07-06 05:18:56 +00001170bool X86TargetLowering::getStackCookieLocation(unsigned &AddressSpace,
1171 unsigned &Offset) const {
1172 if (!Subtarget->isTargetLinux())
1173 return false;
1174
1175 if (Subtarget->is64Bit()) {
1176 // %fs:0x28, unless we're using a Kernel code model, in which case it's %gs:
1177 Offset = 0x28;
1178 if (getTargetMachine().getCodeModel() == CodeModel::Kernel)
1179 AddressSpace = 256;
1180 else
1181 AddressSpace = 257;
1182 } else {
1183 // %gs:0x14 on i386
1184 Offset = 0x14;
1185 AddressSpace = 256;
1186 }
1187 return true;
1188}
1189
1190
Chris Lattner2b02a442007-02-25 08:29:00 +00001191//===----------------------------------------------------------------------===//
1192// Return Value Calling Convention Implementation
1193//===----------------------------------------------------------------------===//
1194
Chris Lattner59ed56b2007-02-28 04:55:35 +00001195#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001196
Michael J. Spencerec38de22010-10-10 22:04:20 +00001197bool
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001198X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv, bool isVarArg,
Dan Gohman84023e02010-07-10 09:00:22 +00001199 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001200 LLVMContext &Context) const {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001201 SmallVector<CCValAssign, 16> RVLocs;
1202 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001203 RVLocs, Context);
Dan Gohman84023e02010-07-10 09:00:22 +00001204 return CCInfo.CheckReturn(Outs, RetCC_X86);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001205}
1206
Dan Gohman98ca4f22009-08-05 01:29:28 +00001207SDValue
1208X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001209 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001210 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001211 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001212 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001213 MachineFunction &MF = DAG.getMachineFunction();
1214 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001215
Chris Lattner9774c912007-02-27 05:28:59 +00001216 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001217 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1218 RVLocs, *DAG.getContext());
1219 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001220
Evan Chengdcea1632010-02-04 02:40:39 +00001221 // Add the regs to the liveout set for the function.
1222 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1223 for (unsigned i = 0; i != RVLocs.size(); ++i)
1224 if (RVLocs[i].isRegLoc() && !MRI.isLiveOut(RVLocs[i].getLocReg()))
1225 MRI.addLiveOut(RVLocs[i].getLocReg());
Scott Michelfdc40a02009-02-17 22:15:04 +00001226
Dan Gohman475871a2008-07-27 21:46:04 +00001227 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001228
Dan Gohman475871a2008-07-27 21:46:04 +00001229 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001230 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1231 // Operand #1 = Bytes To Pop
Dan Gohman1e93df62010-04-17 14:41:14 +00001232 RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1233 MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001234
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001235 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001236 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1237 CCValAssign &VA = RVLocs[i];
1238 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohmanc9403652010-07-07 15:54:55 +00001239 SDValue ValToCopy = OutVals[i];
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001240 EVT ValVT = ValToCopy.getValueType();
1241
Dale Johannesenc4510512010-09-24 19:05:48 +00001242 // If this is x86-64, and we disabled SSE, we can't return FP values,
1243 // or SSE or MMX vectors.
1244 if ((ValVT == MVT::f32 || ValVT == MVT::f64 ||
1245 VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001246 (Subtarget->is64Bit() && !Subtarget->hasXMM())) {
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001247 report_fatal_error("SSE register return with SSE disabled");
1248 }
1249 // Likewise we can't return F64 values with SSE1 only. gcc does so, but
1250 // llvm-gcc has never done it right and no one has noticed, so this
1251 // should be OK for now.
1252 if (ValVT == MVT::f64 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001253 (Subtarget->is64Bit() && !Subtarget->hasXMMInt()))
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001254 report_fatal_error("SSE2 register return with SSE2 disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001255
Chris Lattner447ff682008-03-11 03:23:40 +00001256 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1257 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001258 if (VA.getLocReg() == X86::ST0 ||
1259 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001260 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1261 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001262 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001263 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001264 RetOps.push_back(ValToCopy);
1265 // Don't emit a copytoreg.
1266 continue;
1267 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001268
Evan Cheng242b38b2009-02-23 09:03:22 +00001269 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1270 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001271 if (Subtarget->is64Bit()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001272 if (ValVT == MVT::x86mmx) {
Chris Lattner97a2a562010-08-26 05:24:29 +00001273 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001274 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ValToCopy);
Eric Christopher90eb4022010-07-22 00:26:08 +00001275 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
1276 ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001277 // If we don't have SSE2 available, convert to v4f32 so the generated
1278 // register is legal.
1279 if (!Subtarget->hasSSE2())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001280 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32,ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001281 }
Evan Cheng242b38b2009-02-23 09:03:22 +00001282 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001283 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00001284
Dale Johannesendd64c412009-02-04 00:33:20 +00001285 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001286 Flag = Chain.getValue(1);
1287 }
Dan Gohman61a92132008-04-21 23:59:07 +00001288
1289 // The x86-64 ABI for returning structs by value requires that we copy
1290 // the sret argument into %rax for the return. We saved the argument into
1291 // a virtual register in the entry block, so now we copy the value out
1292 // and into %rax.
1293 if (Subtarget->is64Bit() &&
1294 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1295 MachineFunction &MF = DAG.getMachineFunction();
1296 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1297 unsigned Reg = FuncInfo->getSRetReturnReg();
Michael J. Spencerec38de22010-10-10 22:04:20 +00001298 assert(Reg &&
Zhongxing Xuc2798a12010-05-26 08:10:02 +00001299 "SRetReturnReg should have been set in LowerFormalArguments().");
Dale Johannesendd64c412009-02-04 00:33:20 +00001300 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001301
Dale Johannesendd64c412009-02-04 00:33:20 +00001302 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001303 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001304
1305 // RAX now acts like a return value.
Evan Chengdcea1632010-02-04 02:40:39 +00001306 MRI.addLiveOut(X86::RAX);
Dan Gohman61a92132008-04-21 23:59:07 +00001307 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001308
Chris Lattner447ff682008-03-11 03:23:40 +00001309 RetOps[0] = Chain; // Update chain.
1310
1311 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001312 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001313 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001314
1315 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001316 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001317}
1318
Evan Cheng3d2125c2010-11-30 23:55:39 +00001319bool X86TargetLowering::isUsedByReturnOnly(SDNode *N) const {
1320 if (N->getNumValues() != 1)
1321 return false;
1322 if (!N->hasNUsesOfValue(1, 0))
1323 return false;
1324
1325 SDNode *Copy = *N->use_begin();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001326 if (Copy->getOpcode() != ISD::CopyToReg &&
1327 Copy->getOpcode() != ISD::FP_EXTEND)
Evan Cheng3d2125c2010-11-30 23:55:39 +00001328 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001329
1330 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001331 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001332 UI != UE; ++UI) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001333 if (UI->getOpcode() != X86ISD::RET_FLAG)
1334 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001335 HasRet = true;
1336 }
Evan Cheng3d2125c2010-11-30 23:55:39 +00001337
Evan Cheng1bf891a2010-12-01 22:59:46 +00001338 return HasRet;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001339}
1340
Dan Gohman98ca4f22009-08-05 01:29:28 +00001341/// LowerCallResult - Lower the result values of a call into the
1342/// appropriate copies out of appropriate physical registers.
1343///
1344SDValue
1345X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001346 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001347 const SmallVectorImpl<ISD::InputArg> &Ins,
1348 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001349 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001350
Chris Lattnere32bbf62007-02-28 07:09:55 +00001351 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001352 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001353 bool Is64Bit = Subtarget->is64Bit();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001354 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +00001355 RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001356 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001357
Chris Lattner3085e152007-02-25 08:59:22 +00001358 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001359 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001360 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001361 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001362
Torok Edwin3f142c32009-02-01 18:15:56 +00001363 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001364 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001365 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasXMM())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001366 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001367 }
1368
Evan Cheng79fb3b42009-02-20 20:43:02 +00001369 SDValue Val;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001370
1371 // If this is a call to a function that returns an fp value on the floating
1372 // point stack, we must guarantee the the value is popped from the stack, so
1373 // a CopyFromReg is not good enough - the copy instruction may be eliminated
1374 // if the return value is not used. We use the FpGET_ST0 instructions
1375 // instead.
1376 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1) {
1377 // If we prefer to use the value in xmm registers, copy it out as f80 and
1378 // use a truncate to move it from fp stack reg to xmm reg.
1379 if (isScalarFPTypeInSSEReg(VA.getValVT())) CopyVT = MVT::f80;
1380 bool isST0 = VA.getLocReg() == X86::ST0;
1381 unsigned Opc = 0;
1382 if (CopyVT == MVT::f32) Opc = isST0 ? X86::FpGET_ST0_32:X86::FpGET_ST1_32;
1383 if (CopyVT == MVT::f64) Opc = isST0 ? X86::FpGET_ST0_64:X86::FpGET_ST1_64;
1384 if (CopyVT == MVT::f80) Opc = isST0 ? X86::FpGET_ST0_80:X86::FpGET_ST1_80;
1385 SDValue Ops[] = { Chain, InFlag };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001386 Chain = SDValue(DAG.getMachineNode(Opc, dl, CopyVT, MVT::Other, MVT::Glue,
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001387 Ops, 2), 1);
1388 Val = Chain.getValue(0);
1389
1390 // Round the f80 to the right size, which also moves it to the appropriate
1391 // xmm register.
1392 if (CopyVT != VA.getValVT())
1393 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
1394 // This truncation won't change the value.
1395 DAG.getIntPtrConstant(1));
1396 } else if (Is64Bit && CopyVT.isVector() && CopyVT.getSizeInBits() == 64) {
Evan Cheng242b38b2009-02-23 09:03:22 +00001397 // For x86-64, MMX values are returned in XMM0 / XMM1 except for v1i64.
1398 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
1399 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001400 MVT::v2i64, InFlag).getValue(1);
Evan Cheng242b38b2009-02-23 09:03:22 +00001401 Val = Chain.getValue(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00001402 Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64,
1403 Val, DAG.getConstant(0, MVT::i64));
Evan Cheng242b38b2009-02-23 09:03:22 +00001404 } else {
1405 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001406 MVT::i64, InFlag).getValue(1);
Evan Cheng242b38b2009-02-23 09:03:22 +00001407 Val = Chain.getValue(0);
1408 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001409 Val = DAG.getNode(ISD::BITCAST, dl, CopyVT, Val);
Evan Cheng79fb3b42009-02-20 20:43:02 +00001410 } else {
1411 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1412 CopyVT, InFlag).getValue(1);
1413 Val = Chain.getValue(0);
1414 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001415 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001416 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001417 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001418
Dan Gohman98ca4f22009-08-05 01:29:28 +00001419 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001420}
1421
1422
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001423//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001424// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001425//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001426// StdCall calling convention seems to be standard for many Windows' API
1427// routines and around. It differs from C calling convention just a little:
1428// callee should clean up the stack, not caller. Symbols should be also
1429// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001430// For info on fast calling convention see Fast Calling Convention (tail call)
1431// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001432
Dan Gohman98ca4f22009-08-05 01:29:28 +00001433/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001434/// semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001435static bool CallIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
1436 if (Outs.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001437 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001438
Dan Gohman98ca4f22009-08-05 01:29:28 +00001439 return Outs[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001440}
1441
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001442/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001443/// return semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001444static bool
1445ArgsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
1446 if (Ins.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001447 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001448
Dan Gohman98ca4f22009-08-05 01:29:28 +00001449 return Ins[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001450}
1451
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001452/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1453/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001454/// the specific parameter attribute. The copy will be passed as a byval
1455/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001456static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001457CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001458 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1459 DebugLoc dl) {
Chris Lattnere72f2022010-09-21 05:40:29 +00001460 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Michael J. Spencerec38de22010-10-10 22:04:20 +00001461
Dale Johannesendd64c412009-02-04 00:33:20 +00001462 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Mon P Wang20adc9d2010-04-04 03:10:48 +00001463 /*isVolatile*/false, /*AlwaysInline=*/true,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001464 MachinePointerInfo(), MachinePointerInfo());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001465}
1466
Chris Lattner29689432010-03-11 00:22:57 +00001467/// IsTailCallConvention - Return true if the calling convention is one that
1468/// supports tail call optimization.
1469static bool IsTailCallConvention(CallingConv::ID CC) {
1470 return (CC == CallingConv::Fast || CC == CallingConv::GHC);
1471}
1472
Evan Cheng0c439eb2010-01-27 00:07:07 +00001473/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1474/// a tailcall target by changing its ABI.
1475static bool FuncIsMadeTailCallSafe(CallingConv::ID CC) {
Chris Lattner29689432010-03-11 00:22:57 +00001476 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001477}
1478
Dan Gohman98ca4f22009-08-05 01:29:28 +00001479SDValue
1480X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001481 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001482 const SmallVectorImpl<ISD::InputArg> &Ins,
1483 DebugLoc dl, SelectionDAG &DAG,
1484 const CCValAssign &VA,
1485 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00001486 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00001487 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001488 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Evan Cheng0c439eb2010-01-27 00:07:07 +00001489 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001490 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001491 EVT ValVT;
1492
1493 // If value is passed by pointer we have address passed instead of the value
1494 // itself.
1495 if (VA.getLocInfo() == CCValAssign::Indirect)
1496 ValVT = VA.getLocVT();
1497 else
1498 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001499
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001500 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001501 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001502 // In case of tail call optimization mark all arguments mutable. Since they
1503 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001504 if (Flags.isByVal()) {
1505 int FI = MFI->CreateFixedObject(Flags.getByValSize(),
Evan Chenged2ae132010-07-03 00:40:23 +00001506 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001507 return DAG.getFrameIndex(FI, getPointerTy());
1508 } else {
1509 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00001510 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001511 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1512 return DAG.getLoad(ValVT, dl, Chain, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001513 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00001514 false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001515 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001516}
1517
Dan Gohman475871a2008-07-27 21:46:04 +00001518SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001519X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001520 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001521 bool isVarArg,
1522 const SmallVectorImpl<ISD::InputArg> &Ins,
1523 DebugLoc dl,
1524 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001525 SmallVectorImpl<SDValue> &InVals)
1526 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00001527 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001528 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001529
Gordon Henriksen86737662008-01-05 16:56:59 +00001530 const Function* Fn = MF.getFunction();
1531 if (Fn->hasExternalLinkage() &&
1532 Subtarget->isTargetCygMing() &&
1533 Fn->getName() == "main")
1534 FuncInfo->setForceFramePointer(true);
1535
Evan Cheng1bc78042006-04-26 01:20:17 +00001536 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001537 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001538 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001539
Chris Lattner29689432010-03-11 00:22:57 +00001540 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1541 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001542
Chris Lattner638402b2007-02-28 07:00:42 +00001543 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001544 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001545 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1546 ArgLocs, *DAG.getContext());
Duncan Sands45907662010-10-31 13:21:44 +00001547 CCInfo.AnalyzeFormalArguments(Ins, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001548
Chris Lattnerf39f7712007-02-28 05:46:49 +00001549 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001550 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001551 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1552 CCValAssign &VA = ArgLocs[i];
1553 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1554 // places.
1555 assert(VA.getValNo() != LastVal &&
1556 "Don't support value assigned to multiple locs yet");
1557 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001558
Chris Lattnerf39f7712007-02-28 05:46:49 +00001559 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001560 EVT RegVT = VA.getLocVT();
Devang Patel8a84e442009-01-05 17:31:22 +00001561 TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001562 if (RegVT == MVT::i32)
Chris Lattnerf39f7712007-02-28 05:46:49 +00001563 RC = X86::GR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001564 else if (Is64Bit && RegVT == MVT::i64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001565 RC = X86::GR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001566 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001567 RC = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001568 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001569 RC = X86::FR64RegisterClass;
Bruno Cardoso Lopesac098352010-08-05 23:35:51 +00001570 else if (RegVT.isVector() && RegVT.getSizeInBits() == 256)
1571 RC = X86::VR256RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001572 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001573 RC = X86::VR128RegisterClass;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001574 else if (RegVT == MVT::x86mmx)
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001575 RC = X86::VR64RegisterClass;
1576 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001577 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001578
Devang Patele9a7ea62011-01-31 21:38:14 +00001579 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC, dl);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001580 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001581
Chris Lattnerf39f7712007-02-28 05:46:49 +00001582 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1583 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1584 // right size.
1585 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001586 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001587 DAG.getValueType(VA.getValVT()));
1588 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001589 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001590 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001591 else if (VA.getLocInfo() == CCValAssign::BCvt)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001592 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001593
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001594 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001595 // Handle MMX values passed in XMM regs.
1596 if (RegVT.isVector()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001597 ArgValue = DAG.getNode(X86ISD::MOVDQ2Q, dl, VA.getValVT(),
1598 ArgValue);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001599 } else
1600 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001601 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001602 } else {
1603 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001604 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001605 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001606
1607 // If value is passed via pointer - do a load.
1608 if (VA.getLocInfo() == CCValAssign::Indirect)
Chris Lattner51abfe42010-09-21 06:02:19 +00001609 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue,
1610 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001611
Dan Gohman98ca4f22009-08-05 01:29:28 +00001612 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001613 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001614
Dan Gohman61a92132008-04-21 23:59:07 +00001615 // The x86-64 ABI for returning structs by value requires that we copy
1616 // the sret argument into %rax for the return. Save the argument into
1617 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001618 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001619 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1620 unsigned Reg = FuncInfo->getSRetReturnReg();
1621 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001622 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001623 FuncInfo->setSRetReturnReg(Reg);
1624 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001625 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001626 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001627 }
1628
Chris Lattnerf39f7712007-02-28 05:46:49 +00001629 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00001630 // Align stack specially for tail calls.
1631 if (FuncIsMadeTailCallSafe(CallConv))
Gordon Henriksenae636f82008-01-03 16:47:34 +00001632 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001633
Evan Cheng1bc78042006-04-26 01:20:17 +00001634 // If the function takes variable number of arguments, make a frame index for
1635 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001636 if (isVarArg) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001637 if (!IsWin64 && (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
1638 CallConv != CallingConv::X86_ThisCall))) {
Jakob Stoklund Olesenb2eeed72010-07-29 17:42:27 +00001639 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,true));
Gordon Henriksen86737662008-01-05 16:56:59 +00001640 }
1641 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001642 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1643
1644 // FIXME: We should really autogenerate these arrays
1645 static const unsigned GPR64ArgRegsWin64[] = {
1646 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001647 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001648 static const unsigned GPR64ArgRegs64Bit[] = {
1649 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1650 };
1651 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001652 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1653 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1654 };
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001655 const unsigned *GPR64ArgRegs;
1656 unsigned NumXMMRegs = 0;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001657
1658 if (IsWin64) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001659 // The XMM registers which might contain var arg parameters are shadowed
1660 // in their paired GPR. So we only need to save the GPR to their home
1661 // slots.
1662 TotalNumIntRegs = 4;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001663 GPR64ArgRegs = GPR64ArgRegsWin64;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001664 } else {
1665 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1666 GPR64ArgRegs = GPR64ArgRegs64Bit;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001667
1668 NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs64Bit, TotalNumXMMRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001669 }
1670 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1671 TotalNumIntRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001672
Devang Patel578efa92009-06-05 21:57:13 +00001673 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001674 assert(!(NumXMMRegs && !Subtarget->hasXMM()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001675 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001676 assert(!(NumXMMRegs && UseSoftFloat && NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00001677 "SSE register cannot be used when SSE is disabled!");
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001678 if (UseSoftFloat || NoImplicitFloatOps || !Subtarget->hasXMM())
Torok Edwin3f142c32009-02-01 18:15:56 +00001679 // Kernel mode asks for SSE to be disabled, so don't push them
1680 // on the stack.
1681 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001682
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001683 if (IsWin64) {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001684 const TargetFrameLowering &TFI = *getTargetMachine().getFrameLowering();
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001685 // Get to the caller-allocated home save location. Add 8 to account
1686 // for the return address.
1687 int HomeOffset = TFI.getOffsetOfLocalArea() + 8;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001688 FuncInfo->setRegSaveFrameIndex(
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001689 MFI->CreateFixedObject(1, NumIntRegs * 8 + HomeOffset, false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001690 FuncInfo->setVarArgsFrameIndex(FuncInfo->getRegSaveFrameIndex());
1691 } else {
1692 // For X86-64, if there are vararg parameters that are passed via
1693 // registers, then we must store them to their spots on the stack so they
1694 // may be loaded by deferencing the result of va_next.
1695 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
1696 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
1697 FuncInfo->setRegSaveFrameIndex(
1698 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
Dan Gohman1e93df62010-04-17 14:41:14 +00001699 false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001700 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001701
Gordon Henriksen86737662008-01-05 16:56:59 +00001702 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001703 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00001704 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
1705 getPointerTy());
1706 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001707 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00001708 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
1709 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00001710 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
Devang Patele9a7ea62011-01-31 21:38:14 +00001711 X86::GR64RegisterClass, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00001712 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00001713 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001714 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001715 MachinePointerInfo::getFixedStack(
1716 FuncInfo->getRegSaveFrameIndex(), Offset),
1717 false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001718 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001719 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001720 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001721
Dan Gohmanface41a2009-08-16 21:24:25 +00001722 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
1723 // Now store the XMM (fp + vector) parameter registers.
1724 SmallVector<SDValue, 11> SaveXMMOps;
1725 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001726
Devang Patele9a7ea62011-01-31 21:38:14 +00001727 unsigned AL = MF.addLiveIn(X86::AL, X86::GR8RegisterClass, dl);
Dan Gohmanface41a2009-08-16 21:24:25 +00001728 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
1729 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001730
Dan Gohman1e93df62010-04-17 14:41:14 +00001731 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1732 FuncInfo->getRegSaveFrameIndex()));
1733 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1734 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00001735
Dan Gohmanface41a2009-08-16 21:24:25 +00001736 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001737 unsigned VReg = MF.addLiveIn(XMMArgRegs64Bit[NumXMMRegs],
Devang Patele9a7ea62011-01-31 21:38:14 +00001738 X86::VR128RegisterClass, dl);
Dan Gohmanface41a2009-08-16 21:24:25 +00001739 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
1740 SaveXMMOps.push_back(Val);
1741 }
1742 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
1743 MVT::Other,
1744 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00001745 }
Dan Gohmanface41a2009-08-16 21:24:25 +00001746
1747 if (!MemOps.empty())
1748 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1749 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001750 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001751 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001752
Gordon Henriksen86737662008-01-05 16:56:59 +00001753 // Some CCs need callee pop.
Dan Gohman4d3d6e12010-05-27 18:43:40 +00001754 if (Subtarget->IsCalleePop(isVarArg, CallConv)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001755 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001756 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00001757 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001758 // If this is an sret function, the return should pop the hidden pointer.
Chris Lattner29689432010-03-11 00:22:57 +00001759 if (!Is64Bit && !IsTailCallConvention(CallConv) && ArgsAreStructReturn(Ins))
Dan Gohman1e93df62010-04-17 14:41:14 +00001760 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001761 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001762
Gordon Henriksen86737662008-01-05 16:56:59 +00001763 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001764 // RegSaveFrameIndex is X86-64 only.
1765 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Anton Korobeynikovded05e32010-05-16 09:08:45 +00001766 if (CallConv == CallingConv::X86_FastCall ||
1767 CallConv == CallingConv::X86_ThisCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00001768 // fastcc functions can't have varargs.
1769 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00001770 }
Evan Cheng25caf632006-05-23 21:06:34 +00001771
Dan Gohman98ca4f22009-08-05 01:29:28 +00001772 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001773}
1774
Dan Gohman475871a2008-07-27 21:46:04 +00001775SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001776X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
1777 SDValue StackPtr, SDValue Arg,
1778 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00001779 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001780 ISD::ArgFlagsTy Flags) const {
Anton Korobeynikovc7c62bb2010-09-02 22:31:32 +00001781 const unsigned FirstStackArgOffset = (Subtarget->isTargetWin64() ? 32 : 0);
1782 unsigned LocMemOffset = FirstStackArgOffset + VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00001783 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00001784 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001785 if (Flags.isByVal())
Dale Johannesendd64c412009-02-04 00:33:20 +00001786 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001787
1788 return DAG.getStore(Chain, dl, Arg, PtrOff,
1789 MachinePointerInfo::getStack(LocMemOffset),
David Greene67c9d422010-02-15 16:53:33 +00001790 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00001791}
1792
Bill Wendling64e87322009-01-16 19:25:27 +00001793/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001794/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00001795SDValue
1796X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00001797 SDValue &OutRetAddr, SDValue Chain,
1798 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00001799 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001800 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00001801 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001802 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00001803
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001804 // Load the "old" Return address.
Chris Lattner51abfe42010-09-21 06:02:19 +00001805 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, MachinePointerInfo(),
1806 false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001807 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001808}
1809
1810/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1811/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00001812static SDValue
1813EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00001814 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00001815 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001816 // Store the return address to the appropriate stack slot.
1817 if (!FPDiff) return Chain;
1818 // Calculate the new stack slot for the return address.
1819 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00001820 int NewReturnAddrFI =
Evan Chenged2ae132010-07-03 00:40:23 +00001821 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false);
Owen Anderson825b72b2009-08-11 20:47:22 +00001822 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00001823 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001824 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00001825 MachinePointerInfo::getFixedStack(NewReturnAddrFI),
David Greene67c9d422010-02-15 16:53:33 +00001826 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001827 return Chain;
1828}
1829
Dan Gohman98ca4f22009-08-05 01:29:28 +00001830SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001831X86TargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001832 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001833 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001834 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001835 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001836 const SmallVectorImpl<ISD::InputArg> &Ins,
1837 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001838 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001839 MachineFunction &MF = DAG.getMachineFunction();
1840 bool Is64Bit = Subtarget->is64Bit();
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00001841 bool IsWin64 = Subtarget->isTargetWin64();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001842 bool IsStructRet = CallIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00001843 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001844
Evan Cheng5f941932010-02-05 02:21:12 +00001845 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00001846 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00001847 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1848 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001849 Outs, OutVals, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00001850
1851 // Sibcalls are automatically detected tailcalls which do not require
1852 // ABI changes.
Dan Gohman1797ed52010-02-08 20:27:50 +00001853 if (!GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00001854 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00001855
1856 if (isTailCall)
1857 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00001858 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00001859
Chris Lattner29689432010-03-11 00:22:57 +00001860 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1861 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001862
Chris Lattner638402b2007-02-28 07:00:42 +00001863 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00001864 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001865 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1866 ArgLocs, *DAG.getContext());
Duncan Sands45907662010-10-31 13:21:44 +00001867 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001868
Chris Lattner423c5f42007-02-28 05:31:48 +00001869 // Get a count of how many bytes are to be pushed on the stack.
1870 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00001871 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00001872 // This is a sibcall. The memory operands are available in caller's
1873 // own caller's stack.
1874 NumBytes = 0;
Chris Lattner29689432010-03-11 00:22:57 +00001875 else if (GuaranteedTailCallOpt && IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00001876 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001877
Gordon Henriksen86737662008-01-05 16:56:59 +00001878 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00001879 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001880 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00001881 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00001882 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1883 FPDiff = NumBytesCallerPushed - NumBytes;
1884
1885 // Set the delta of movement of the returnaddr stackslot.
1886 // But only set if delta is greater than previous delta.
1887 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1888 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1889 }
1890
Evan Chengf22f9b32010-02-06 03:28:46 +00001891 if (!IsSibcall)
1892 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001893
Dan Gohman475871a2008-07-27 21:46:04 +00001894 SDValue RetAddrFrIdx;
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001895 // Load return adress for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00001896 if (isTailCall && FPDiff)
1897 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
1898 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00001899
Dan Gohman475871a2008-07-27 21:46:04 +00001900 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
1901 SmallVector<SDValue, 8> MemOpChains;
1902 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00001903
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001904 // Walk the register/memloc assignments, inserting copies/loads. In the case
1905 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00001906 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1907 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001908 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00001909 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00001910 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00001911 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00001912
Chris Lattner423c5f42007-02-28 05:31:48 +00001913 // Promote the value if needed.
1914 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001915 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00001916 case CCValAssign::Full: break;
1917 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001918 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001919 break;
1920 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001921 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001922 break;
1923 case CCValAssign::AExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001924 if (RegVT.isVector() && RegVT.getSizeInBits() == 128) {
1925 // Special case: passing MMX values in XMM registers.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001926 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00001927 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
1928 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001929 } else
1930 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
1931 break;
1932 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001933 Arg = DAG.getNode(ISD::BITCAST, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001934 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001935 case CCValAssign::Indirect: {
1936 // Store the argument.
1937 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00001938 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001939 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00001940 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00001941 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001942 Arg = SpillSlot;
1943 break;
1944 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00001945 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001946
Chris Lattner423c5f42007-02-28 05:31:48 +00001947 if (VA.isRegLoc()) {
1948 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00001949 if (isVarArg && IsWin64) {
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00001950 // Win64 ABI requires argument XMM reg to be copied to the corresponding
1951 // shadow reg if callee is a varargs function.
1952 unsigned ShadowReg = 0;
1953 switch (VA.getLocReg()) {
1954 case X86::XMM0: ShadowReg = X86::RCX; break;
1955 case X86::XMM1: ShadowReg = X86::RDX; break;
1956 case X86::XMM2: ShadowReg = X86::R8; break;
1957 case X86::XMM3: ShadowReg = X86::R9; break;
1958 }
1959 if (ShadowReg)
1960 RegsToPass.push_back(std::make_pair(ShadowReg, Arg));
1961 }
Evan Chengf22f9b32010-02-06 03:28:46 +00001962 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00001963 assert(VA.isMemLoc());
1964 if (StackPtr.getNode() == 0)
1965 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
1966 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1967 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001968 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001969 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001970
Evan Cheng32fe1032006-05-25 00:59:30 +00001971 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001972 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001973 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001974
Evan Cheng347d5f72006-04-28 21:29:37 +00001975 // Build a sequence of copy-to-reg nodes chained together with token chain
1976 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001977 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001978 // Tail call byval lowering might overwrite argument registers so in case of
1979 // tail call optimization the copies to registers are lowered later.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001980 if (!isTailCall)
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001981 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001982 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00001983 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001984 InFlag = Chain.getValue(1);
1985 }
Gordon Henriksen86737662008-01-05 16:56:59 +00001986
Chris Lattner88e1fd52009-07-09 04:24:46 +00001987 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00001988 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1989 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001990 if (!isTailCall) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00001991 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
1992 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001993 DebugLoc(), getPointerTy()),
Chris Lattnerb133a0a2009-07-09 02:55:47 +00001994 InFlag);
1995 InFlag = Chain.getValue(1);
1996 } else {
1997 // If we are tail calling and generating PIC/GOT style code load the
1998 // address of the callee into ECX. The value in ecx is used as target of
1999 // the tail jump. This is done to circumvent the ebx/callee-saved problem
2000 // for tail calls on PIC/GOT architectures. Normally we would just put the
2001 // address of GOT into ebx and then call target@PLT. But for tail calls
2002 // ebx would be restored (since ebx is callee saved) before jumping to the
2003 // target@PLT.
2004
2005 // Note: The actual moving to ECX is done further down.
2006 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
2007 if (G && !G->getGlobal()->hasHiddenVisibility() &&
2008 !G->getGlobal()->hasProtectedVisibility())
2009 Callee = LowerGlobalAddress(Callee, DAG);
2010 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00002011 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002012 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002013 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002014
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002015 if (Is64Bit && isVarArg && !IsWin64) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002016 // From AMD64 ABI document:
2017 // For calls that may call functions that use varargs or stdargs
2018 // (prototype-less calls or calls to functions containing ellipsis (...) in
2019 // the declaration) %al is used as hidden argument to specify the number
2020 // of SSE registers used. The contents of %al do not need to match exactly
2021 // the number of registers, but must be an ubound on the number of SSE
2022 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002023
Gordon Henriksen86737662008-01-05 16:56:59 +00002024 // Count the number of XMM registers allocated.
2025 static const unsigned XMMArgRegs[] = {
2026 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2027 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2028 };
2029 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00002030 assert((Subtarget->hasXMM() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00002031 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00002032
Dale Johannesendd64c412009-02-04 00:33:20 +00002033 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Owen Anderson825b72b2009-08-11 20:47:22 +00002034 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002035 InFlag = Chain.getValue(1);
2036 }
2037
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002038
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002039 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002040 if (isTailCall) {
2041 // Force all the incoming stack arguments to be loaded from the stack
2042 // before any new outgoing arguments are stored to the stack, because the
2043 // outgoing stack slots may alias the incoming argument stack slots, and
2044 // the alias isn't otherwise explicit. This is slightly more conservative
2045 // than necessary, because it means that each store effectively depends
2046 // on every argument instead of just those arguments it would clobber.
2047 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2048
Dan Gohman475871a2008-07-27 21:46:04 +00002049 SmallVector<SDValue, 8> MemOpChains2;
2050 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002051 int FI = 0;
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002052 // Do not flag preceeding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00002053 InFlag = SDValue();
Dan Gohman1797ed52010-02-08 20:27:50 +00002054 if (GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002055 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2056 CCValAssign &VA = ArgLocs[i];
2057 if (VA.isRegLoc())
2058 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002059 assert(VA.isMemLoc());
Dan Gohmanc9403652010-07-07 15:54:55 +00002060 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002061 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002062 // Create frame index.
2063 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002064 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002065 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002066 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002067
Duncan Sands276dcbd2008-03-21 09:14:45 +00002068 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002069 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002070 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002071 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00002072 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00002073 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002074 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002075
Dan Gohman98ca4f22009-08-05 01:29:28 +00002076 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2077 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002078 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002079 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002080 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002081 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002082 DAG.getStore(ArgChain, dl, Arg, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002083 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002084 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002085 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002086 }
2087 }
2088
2089 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002090 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002091 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002092
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002093 // Copy arguments to their registers.
2094 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002095 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002096 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002097 InFlag = Chain.getValue(1);
2098 }
Dan Gohman475871a2008-07-27 21:46:04 +00002099 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002100
Gordon Henriksen86737662008-01-05 16:56:59 +00002101 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002102 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00002103 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002104 }
2105
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002106 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2107 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2108 // In the 64-bit large code model, we have to make all calls
2109 // through a register, since the call instruction's 32-bit
2110 // pc-relative offset may not be large enough to hold the whole
2111 // address.
2112 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002113 // If the callee is a GlobalAddress node (quite common, every direct call
2114 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2115 // it.
2116
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002117 // We should use extra load for direct calls to dllimported functions in
2118 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002119 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002120 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002121 unsigned char OpFlags = 0;
Eric Christopherfd179292009-08-27 18:07:15 +00002122
Chris Lattner48a7d022009-07-09 05:02:21 +00002123 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2124 // external symbols most go through the PLT in PIC mode. If the symbol
2125 // has hidden or protected visibility, or if it is static or local, then
2126 // we don't need to use the PLT - we can directly call it.
2127 if (Subtarget->isTargetELF() &&
2128 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002129 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002130 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002131 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner80945782010-09-27 06:34:01 +00002132 (GV->isDeclaration() || GV->isWeakForLinker()) &&
2133 Subtarget->getDarwinVers() < 9) {
Chris Lattner74e726e2009-07-09 05:27:35 +00002134 // PC-relative references to external symbols should go through $stub,
2135 // unless we're building with the leopard linker or later, which
2136 // automatically synthesizes these stubs.
2137 OpFlags = X86II::MO_DARWIN_STUB;
2138 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002139
Devang Patel0d881da2010-07-06 22:08:15 +00002140 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002141 G->getOffset(), OpFlags);
2142 }
Bill Wendling056292f2008-09-16 21:48:12 +00002143 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002144 unsigned char OpFlags = 0;
2145
Evan Cheng1bf891a2010-12-01 22:59:46 +00002146 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to
2147 // external symbols should go through the PLT.
2148 if (Subtarget->isTargetELF() &&
2149 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
2150 OpFlags = X86II::MO_PLT;
2151 } else if (Subtarget->isPICStyleStubAny() &&
2152 Subtarget->getDarwinVers() < 9) {
2153 // PC-relative references to external symbols should go through $stub,
2154 // unless we're building with the leopard linker or later, which
2155 // automatically synthesizes these stubs.
2156 OpFlags = X86II::MO_DARWIN_STUB;
Chris Lattner74e726e2009-07-09 05:27:35 +00002157 }
Eric Christopherfd179292009-08-27 18:07:15 +00002158
Chris Lattner48a7d022009-07-09 05:02:21 +00002159 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2160 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002161 }
2162
Chris Lattnerd96d0722007-02-25 06:40:16 +00002163 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002164 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002165 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002166
Evan Chengf22f9b32010-02-06 03:28:46 +00002167 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002168 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2169 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002170 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002171 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002172
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002173 Ops.push_back(Chain);
2174 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002175
Dan Gohman98ca4f22009-08-05 01:29:28 +00002176 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002177 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002178
Gordon Henriksen86737662008-01-05 16:56:59 +00002179 // Add argument registers to the end of the list so that they are known live
2180 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002181 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2182 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2183 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002184
Evan Cheng586ccac2008-03-18 23:36:35 +00002185 // Add an implicit use GOT pointer in EBX.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002186 if (!isTailCall && Subtarget->isPICStyleGOT())
Evan Cheng586ccac2008-03-18 23:36:35 +00002187 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
2188
Anton Korobeynikov3a1e54a2010-08-17 21:06:07 +00002189 // Add an implicit use of AL for non-Windows x86 64-bit vararg functions.
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002190 if (Is64Bit && isVarArg && !IsWin64)
Owen Anderson825b72b2009-08-11 20:47:22 +00002191 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
Evan Cheng586ccac2008-03-18 23:36:35 +00002192
Gabor Greifba36cb52008-08-28 21:40:38 +00002193 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002194 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002195
Dan Gohman98ca4f22009-08-05 01:29:28 +00002196 if (isTailCall) {
Dale Johannesen88004c22010-06-05 00:30:45 +00002197 // We used to do:
2198 //// If this is the first return lowered for this function, add the regs
2199 //// to the liveout set for the function.
2200 // This isn't right, although it's probably harmless on x86; liveouts
2201 // should be computed from returns not tail calls. Consider a void
2202 // function making a tail call to a function returning int.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002203 return DAG.getNode(X86ISD::TC_RETURN, dl,
2204 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002205 }
2206
Dale Johannesenace16102009-02-03 19:33:06 +00002207 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002208 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002209
Chris Lattner2d297092006-05-23 18:50:38 +00002210 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002211 unsigned NumBytesForCalleeToPush;
Dan Gohman4d3d6e12010-05-27 18:43:40 +00002212 if (Subtarget->IsCalleePop(isVarArg, CallConv))
Gordon Henriksen86737662008-01-05 16:56:59 +00002213 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Chris Lattner29689432010-03-11 00:22:57 +00002214 else if (!Is64Bit && !IsTailCallConvention(CallConv) && IsStructRet)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002215 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002216 // pops the hidden struct pointer, so we have to push it back.
2217 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002218 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002219 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002220 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002221
Gordon Henriksenae636f82008-01-03 16:47:34 +00002222 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002223 if (!IsSibcall) {
2224 Chain = DAG.getCALLSEQ_END(Chain,
2225 DAG.getIntPtrConstant(NumBytes, true),
2226 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2227 true),
2228 InFlag);
2229 InFlag = Chain.getValue(1);
2230 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002231
Chris Lattner3085e152007-02-25 08:59:22 +00002232 // Handle result values, copying them out of physregs into vregs that we
2233 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002234 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2235 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002236}
2237
Evan Cheng25ab6902006-09-08 06:48:29 +00002238
2239//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002240// Fast Calling Convention (tail call) implementation
2241//===----------------------------------------------------------------------===//
2242
2243// Like std call, callee cleans arguments, convention except that ECX is
2244// reserved for storing the tail called function address. Only 2 registers are
2245// free for argument passing (inreg). Tail call optimization is performed
2246// provided:
2247// * tailcallopt is enabled
2248// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002249// On X86_64 architecture with GOT-style position independent code only local
2250// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002251// To keep the stack aligned according to platform abi the function
2252// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2253// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002254// If a tail called function callee has more arguments than the caller the
2255// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002256// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002257// original REtADDR, but before the saved framepointer or the spilled registers
2258// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2259// stack layout:
2260// arg1
2261// arg2
2262// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002263// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002264// move area ]
2265// (possible EBP)
2266// ESI
2267// EDI
2268// local1 ..
2269
2270/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2271/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002272unsigned
2273X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2274 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002275 MachineFunction &MF = DAG.getMachineFunction();
2276 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002277 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002278 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002279 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002280 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002281 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002282 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2283 // Number smaller than 12 so just add the difference.
2284 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2285 } else {
2286 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002287 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002288 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002289 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002290 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002291}
2292
Evan Cheng5f941932010-02-05 02:21:12 +00002293/// MatchingStackOffset - Return true if the given stack call argument is
2294/// already available in the same position (relatively) of the caller's
2295/// incoming argument stack.
2296static
2297bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2298 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2299 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002300 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2301 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002302 if (Arg.getOpcode() == ISD::CopyFromReg) {
2303 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00002304 if (!TargetRegisterInfo::isVirtualRegister(VR))
Evan Cheng5f941932010-02-05 02:21:12 +00002305 return false;
2306 MachineInstr *Def = MRI->getVRegDef(VR);
2307 if (!Def)
2308 return false;
2309 if (!Flags.isByVal()) {
2310 if (!TII->isLoadFromStackSlot(Def, FI))
2311 return false;
2312 } else {
2313 unsigned Opcode = Def->getOpcode();
2314 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2315 Def->getOperand(1).isFI()) {
2316 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002317 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002318 } else
2319 return false;
2320 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002321 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2322 if (Flags.isByVal())
2323 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002324 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002325 // define @foo(%struct.X* %A) {
2326 // tail call @bar(%struct.X* byval %A)
2327 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002328 return false;
2329 SDValue Ptr = Ld->getBasePtr();
2330 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2331 if (!FINode)
2332 return false;
2333 FI = FINode->getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002334 } else
2335 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002336
Evan Cheng4cae1332010-03-05 08:38:04 +00002337 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002338 if (!MFI->isFixedObjectIndex(FI))
2339 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002340 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002341}
2342
Dan Gohman98ca4f22009-08-05 01:29:28 +00002343/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2344/// for tail call optimization. Targets which want to do tail call
2345/// optimization should implement this function.
2346bool
2347X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002348 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002349 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002350 bool isCalleeStructRet,
2351 bool isCallerStructRet,
Evan Chengb1712452010-01-27 06:25:16 +00002352 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002353 const SmallVectorImpl<SDValue> &OutVals,
Evan Chengb1712452010-01-27 06:25:16 +00002354 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002355 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002356 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002357 CalleeCC != CallingConv::C)
2358 return false;
2359
Evan Cheng7096ae42010-01-29 06:45:59 +00002360 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002361 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002362 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Cheng13617962010-04-30 01:12:32 +00002363 CallingConv::ID CallerCC = CallerF->getCallingConv();
2364 bool CCMatch = CallerCC == CalleeCC;
2365
Dan Gohman1797ed52010-02-08 20:27:50 +00002366 if (GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002367 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002368 return true;
2369 return false;
2370 }
2371
Dale Johannesen2f05cc02010-05-28 23:24:28 +00002372 // Look for obvious safe cases to perform tail call optimization that do not
2373 // require ABI changes. This is what gcc calls sibcall.
Evan Chengb2c92902010-02-02 02:22:50 +00002374
Evan Cheng2c12cb42010-03-26 16:26:03 +00002375 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2376 // emit a special epilogue.
2377 if (RegInfo->needsStackRealignment(MF))
2378 return false;
2379
Eric Christopher90eb4022010-07-22 00:26:08 +00002380 // Do not sibcall optimize vararg calls unless the call site is not passing
2381 // any arguments.
Evan Cheng3c262ee2010-03-26 02:13:13 +00002382 if (isVarArg && !Outs.empty())
Evan Cheng843bd692010-01-31 06:44:49 +00002383 return false;
2384
Evan Chenga375d472010-03-15 18:54:48 +00002385 // Also avoid sibcall optimization if either caller or callee uses struct
2386 // return semantics.
2387 if (isCalleeStructRet || isCallerStructRet)
2388 return false;
2389
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002390 // If the call result is in ST0 / ST1, it needs to be popped off the x87 stack.
2391 // Therefore if it's not used by the call it is not safe to optimize this into
2392 // a sibcall.
2393 bool Unused = false;
2394 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2395 if (!Ins[i].Used) {
2396 Unused = true;
2397 break;
2398 }
2399 }
2400 if (Unused) {
2401 SmallVector<CCValAssign, 16> RVLocs;
2402 CCState CCInfo(CalleeCC, false, getTargetMachine(),
2403 RVLocs, *DAG.getContext());
2404 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002405 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002406 CCValAssign &VA = RVLocs[i];
2407 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2408 return false;
2409 }
2410 }
2411
Evan Cheng13617962010-04-30 01:12:32 +00002412 // If the calling conventions do not match, then we'd better make sure the
2413 // results are returned in the same way as what the caller expects.
2414 if (!CCMatch) {
2415 SmallVector<CCValAssign, 16> RVLocs1;
2416 CCState CCInfo1(CalleeCC, false, getTargetMachine(),
2417 RVLocs1, *DAG.getContext());
2418 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2419
2420 SmallVector<CCValAssign, 16> RVLocs2;
2421 CCState CCInfo2(CallerCC, false, getTargetMachine(),
2422 RVLocs2, *DAG.getContext());
2423 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2424
2425 if (RVLocs1.size() != RVLocs2.size())
2426 return false;
2427 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2428 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2429 return false;
2430 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2431 return false;
2432 if (RVLocs1[i].isRegLoc()) {
2433 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2434 return false;
2435 } else {
2436 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2437 return false;
2438 }
2439 }
2440 }
2441
Evan Chenga6bff982010-01-30 01:22:00 +00002442 // If the callee takes no arguments then go on to check the results of the
2443 // call.
2444 if (!Outs.empty()) {
2445 // Check if stack adjustment is needed. For now, do not do this if any
2446 // argument is passed on the stack.
2447 SmallVector<CCValAssign, 16> ArgLocs;
2448 CCState CCInfo(CalleeCC, isVarArg, getTargetMachine(),
2449 ArgLocs, *DAG.getContext());
Duncan Sands45907662010-10-31 13:21:44 +00002450 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Evan Chengb2c92902010-02-02 02:22:50 +00002451 if (CCInfo.getNextStackOffset()) {
2452 MachineFunction &MF = DAG.getMachineFunction();
2453 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2454 return false;
Evan Chengb2c92902010-02-02 02:22:50 +00002455
2456 // Check if the arguments are already laid out in the right way as
2457 // the caller's fixed stack objects.
2458 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002459 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2460 const X86InstrInfo *TII =
2461 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002462 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2463 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00002464 SDValue Arg = OutVals[i];
Evan Chengb2c92902010-02-02 02:22:50 +00002465 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002466 if (VA.getLocInfo() == CCValAssign::Indirect)
2467 return false;
2468 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002469 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2470 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002471 return false;
2472 }
2473 }
2474 }
Evan Cheng9c044672010-05-29 01:35:22 +00002475
2476 // If the tailcall address may be in a register, then make sure it's
2477 // possible to register allocate for it. In 32-bit, the call address can
2478 // only target EAX, EDX, or ECX since the tail call must be scheduled after
Evan Chengdedd9742010-07-14 06:44:01 +00002479 // callee-saved registers are restored. These happen to be the same
2480 // registers used to pass 'inreg' arguments so watch out for those.
2481 if (!Subtarget->is64Bit() &&
2482 !isa<GlobalAddressSDNode>(Callee) &&
Evan Cheng9c044672010-05-29 01:35:22 +00002483 !isa<ExternalSymbolSDNode>(Callee)) {
Evan Cheng9c044672010-05-29 01:35:22 +00002484 unsigned NumInRegs = 0;
2485 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2486 CCValAssign &VA = ArgLocs[i];
Evan Chengdedd9742010-07-14 06:44:01 +00002487 if (!VA.isRegLoc())
2488 continue;
2489 unsigned Reg = VA.getLocReg();
2490 switch (Reg) {
2491 default: break;
2492 case X86::EAX: case X86::EDX: case X86::ECX:
2493 if (++NumInRegs == 3)
Evan Cheng9c044672010-05-29 01:35:22 +00002494 return false;
Evan Chengdedd9742010-07-14 06:44:01 +00002495 break;
Evan Cheng9c044672010-05-29 01:35:22 +00002496 }
2497 }
2498 }
Evan Chenga6bff982010-01-30 01:22:00 +00002499 }
Evan Chengb1712452010-01-27 06:25:16 +00002500
Dale Johannesend155d7e2010-10-25 22:17:05 +00002501 // An stdcall caller is expected to clean up its arguments; the callee
Dale Johannesen0e034562010-11-12 00:43:18 +00002502 // isn't going to do that.
Dale Johannesend155d7e2010-10-25 22:17:05 +00002503 if (!CCMatch && CallerCC==CallingConv::X86_StdCall)
2504 return false;
2505
Evan Cheng86809cc2010-02-03 03:28:02 +00002506 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002507}
2508
Dan Gohman3df24e62008-09-03 23:12:08 +00002509FastISel *
Dan Gohmana4160c32010-07-07 16:29:44 +00002510X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
2511 return X86::createFastISel(funcInfo);
Dan Gohmand9f3c482008-08-19 21:32:53 +00002512}
2513
2514
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002515//===----------------------------------------------------------------------===//
2516// Other Lowering Hooks
2517//===----------------------------------------------------------------------===//
2518
Bruno Cardoso Lopese654b562010-09-01 00:51:36 +00002519static bool MayFoldLoad(SDValue Op) {
2520 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
2521}
2522
2523static bool MayFoldIntoStore(SDValue Op) {
2524 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
2525}
2526
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002527static bool isTargetShuffle(unsigned Opcode) {
2528 switch(Opcode) {
2529 default: return false;
2530 case X86ISD::PSHUFD:
2531 case X86ISD::PSHUFHW:
2532 case X86ISD::PSHUFLW:
2533 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002534 case X86ISD::PALIGN:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002535 case X86ISD::SHUFPS:
2536 case X86ISD::MOVLHPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002537 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002538 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002539 case X86ISD::MOVLPS:
2540 case X86ISD::MOVLPD:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002541 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002542 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002543 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002544 case X86ISD::MOVSS:
2545 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002546 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002547 case X86ISD::UNPCKLPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002548 case X86ISD::PUNPCKLWD:
2549 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002550 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002551 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002552 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002553 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002554 case X86ISD::PUNPCKHWD:
2555 case X86ISD::PUNPCKHBW:
2556 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002557 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002558 return true;
2559 }
2560 return false;
2561}
2562
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002563static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002564 SDValue V1, SelectionDAG &DAG) {
2565 switch(Opc) {
2566 default: llvm_unreachable("Unknown x86 shuffle node");
2567 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002568 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002569 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002570 return DAG.getNode(Opc, dl, VT, V1);
2571 }
2572
2573 return SDValue();
2574}
2575
2576static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00002577 SDValue V1, unsigned TargetMask, SelectionDAG &DAG) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002578 switch(Opc) {
2579 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002580 case X86ISD::PSHUFD:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002581 case X86ISD::PSHUFHW:
2582 case X86ISD::PSHUFLW:
2583 return DAG.getNode(Opc, dl, VT, V1, DAG.getConstant(TargetMask, MVT::i8));
2584 }
2585
2586 return SDValue();
2587}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002588
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002589static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2590 SDValue V1, SDValue V2, unsigned TargetMask, SelectionDAG &DAG) {
2591 switch(Opc) {
2592 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002593 case X86ISD::PALIGN:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002594 case X86ISD::SHUFPD:
2595 case X86ISD::SHUFPS:
2596 return DAG.getNode(Opc, dl, VT, V1, V2,
2597 DAG.getConstant(TargetMask, MVT::i8));
2598 }
2599 return SDValue();
2600}
2601
2602static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2603 SDValue V1, SDValue V2, SelectionDAG &DAG) {
2604 switch(Opc) {
2605 default: llvm_unreachable("Unknown x86 shuffle node");
2606 case X86ISD::MOVLHPS:
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00002607 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002608 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002609 case X86ISD::MOVLPS:
2610 case X86ISD::MOVLPD:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002611 case X86ISD::MOVSS:
2612 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002613 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002614 case X86ISD::UNPCKLPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002615 case X86ISD::PUNPCKLWD:
2616 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002617 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002618 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002619 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002620 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002621 case X86ISD::PUNPCKHWD:
2622 case X86ISD::PUNPCKHBW:
2623 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002624 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002625 return DAG.getNode(Opc, dl, VT, V1, V2);
2626 }
2627 return SDValue();
2628}
2629
Dan Gohmand858e902010-04-17 15:26:15 +00002630SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002631 MachineFunction &MF = DAG.getMachineFunction();
2632 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2633 int ReturnAddrIndex = FuncInfo->getRAIndex();
2634
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002635 if (ReturnAddrIndex == 0) {
2636 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002637 uint64_t SlotSize = TD->getPointerSize();
David Greene3f2bf852009-11-12 20:49:22 +00002638 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00002639 false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002640 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002641 }
2642
Evan Cheng25ab6902006-09-08 06:48:29 +00002643 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002644}
2645
2646
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002647bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
2648 bool hasSymbolicDisplacement) {
2649 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00002650 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002651 return false;
2652
2653 // If we don't have a symbolic displacement - we don't have any extra
2654 // restrictions.
2655 if (!hasSymbolicDisplacement)
2656 return true;
2657
2658 // FIXME: Some tweaks might be needed for medium code model.
2659 if (M != CodeModel::Small && M != CodeModel::Kernel)
2660 return false;
2661
2662 // For small code model we assume that latest object is 16MB before end of 31
2663 // bits boundary. We may also accept pretty large negative constants knowing
2664 // that all objects are in the positive half of address space.
2665 if (M == CodeModel::Small && Offset < 16*1024*1024)
2666 return true;
2667
2668 // For kernel code model we know that all object resist in the negative half
2669 // of 32bits address space. We may not accept negative offsets, since they may
2670 // be just off and we may accept pretty large positive ones.
2671 if (M == CodeModel::Kernel && Offset > 0)
2672 return true;
2673
2674 return false;
2675}
2676
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002677/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
2678/// specific condition code, returning the condition code and the LHS/RHS of the
2679/// comparison to make.
2680static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2681 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00002682 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002683 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2684 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2685 // X > -1 -> X == 0, jump !sign.
2686 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002687 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002688 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2689 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002690 return X86::COND_S;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002691 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00002692 // X < 1 -> X <= 0
2693 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002694 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002695 }
Chris Lattnerf9570512006-09-13 03:22:10 +00002696 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002697
Evan Chengd9558e02006-01-06 00:43:03 +00002698 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002699 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002700 case ISD::SETEQ: return X86::COND_E;
2701 case ISD::SETGT: return X86::COND_G;
2702 case ISD::SETGE: return X86::COND_GE;
2703 case ISD::SETLT: return X86::COND_L;
2704 case ISD::SETLE: return X86::COND_LE;
2705 case ISD::SETNE: return X86::COND_NE;
2706 case ISD::SETULT: return X86::COND_B;
2707 case ISD::SETUGT: return X86::COND_A;
2708 case ISD::SETULE: return X86::COND_BE;
2709 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00002710 }
Chris Lattner4c78e022008-12-23 23:42:27 +00002711 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002712
Chris Lattner4c78e022008-12-23 23:42:27 +00002713 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00002714
Chris Lattner4c78e022008-12-23 23:42:27 +00002715 // If LHS is a foldable load, but RHS is not, flip the condition.
Rafael Espindolaf297c932011-02-03 03:58:05 +00002716 if (ISD::isNON_EXTLoad(LHS.getNode()) &&
2717 !ISD::isNON_EXTLoad(RHS.getNode())) {
Chris Lattner4c78e022008-12-23 23:42:27 +00002718 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
2719 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00002720 }
2721
Chris Lattner4c78e022008-12-23 23:42:27 +00002722 switch (SetCCOpcode) {
2723 default: break;
2724 case ISD::SETOLT:
2725 case ISD::SETOLE:
2726 case ISD::SETUGT:
2727 case ISD::SETUGE:
2728 std::swap(LHS, RHS);
2729 break;
2730 }
2731
2732 // On a floating point condition, the flags are set as follows:
2733 // ZF PF CF op
2734 // 0 | 0 | 0 | X > Y
2735 // 0 | 0 | 1 | X < Y
2736 // 1 | 0 | 0 | X == Y
2737 // 1 | 1 | 1 | unordered
2738 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002739 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00002740 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002741 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00002742 case ISD::SETOLT: // flipped
2743 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002744 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00002745 case ISD::SETOLE: // flipped
2746 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002747 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002748 case ISD::SETUGT: // flipped
2749 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002750 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00002751 case ISD::SETUGE: // flipped
2752 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002753 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002754 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002755 case ISD::SETNE: return X86::COND_NE;
2756 case ISD::SETUO: return X86::COND_P;
2757 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00002758 case ISD::SETOEQ:
2759 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00002760 }
Evan Chengd9558e02006-01-06 00:43:03 +00002761}
2762
Evan Cheng4a460802006-01-11 00:33:36 +00002763/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2764/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00002765/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00002766static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00002767 switch (X86CC) {
2768 default:
2769 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00002770 case X86::COND_B:
2771 case X86::COND_BE:
2772 case X86::COND_E:
2773 case X86::COND_P:
2774 case X86::COND_A:
2775 case X86::COND_AE:
2776 case X86::COND_NE:
2777 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00002778 return true;
2779 }
2780}
2781
Evan Chengeb2f9692009-10-27 19:56:55 +00002782/// isFPImmLegal - Returns true if the target can instruction select the
2783/// specified FP immediate natively. If false, the legalizer will
2784/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00002785bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00002786 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
2787 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
2788 return true;
2789 }
2790 return false;
2791}
2792
Nate Begeman9008ca62009-04-27 18:41:29 +00002793/// isUndefOrInRange - Return true if Val is undef or if its value falls within
2794/// the specified range (L, H].
2795static bool isUndefOrInRange(int Val, int Low, int Hi) {
2796 return (Val < 0) || (Val >= Low && Val < Hi);
2797}
2798
2799/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
2800/// specified value.
2801static bool isUndefOrEqual(int Val, int CmpVal) {
2802 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00002803 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00002804 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00002805}
2806
Nate Begeman9008ca62009-04-27 18:41:29 +00002807/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
2808/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
2809/// the second operand.
Owen Andersone50ed302009-08-10 22:56:29 +00002810static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00002811 if (VT == MVT::v4f32 || VT == MVT::v4i32 )
Nate Begeman9008ca62009-04-27 18:41:29 +00002812 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002813 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00002814 return (Mask[0] < 2 && Mask[1] < 2);
2815 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002816}
2817
Nate Begeman9008ca62009-04-27 18:41:29 +00002818bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002819 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002820 N->getMask(M);
2821 return ::isPSHUFDMask(M, N->getValueType(0));
2822}
Evan Cheng0188ecb2006-03-22 18:59:22 +00002823
Nate Begeman9008ca62009-04-27 18:41:29 +00002824/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
2825/// is suitable for input to PSHUFHW.
Owen Andersone50ed302009-08-10 22:56:29 +00002826static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002827 if (VT != MVT::v8i16)
Evan Cheng0188ecb2006-03-22 18:59:22 +00002828 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002829
Nate Begeman9008ca62009-04-27 18:41:29 +00002830 // Lower quadword copied in order or undef.
2831 for (int i = 0; i != 4; ++i)
2832 if (Mask[i] >= 0 && Mask[i] != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00002833 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002834
Evan Cheng506d3df2006-03-29 23:07:14 +00002835 // Upper quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002836 for (int i = 4; i != 8; ++i)
2837 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Evan Cheng506d3df2006-03-29 23:07:14 +00002838 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002839
Evan Cheng506d3df2006-03-29 23:07:14 +00002840 return true;
2841}
2842
Nate Begeman9008ca62009-04-27 18:41:29 +00002843bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002844 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002845 N->getMask(M);
2846 return ::isPSHUFHWMask(M, N->getValueType(0));
2847}
Evan Cheng506d3df2006-03-29 23:07:14 +00002848
Nate Begeman9008ca62009-04-27 18:41:29 +00002849/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
2850/// is suitable for input to PSHUFLW.
Owen Andersone50ed302009-08-10 22:56:29 +00002851static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002852 if (VT != MVT::v8i16)
Evan Cheng506d3df2006-03-29 23:07:14 +00002853 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002854
Rafael Espindola15684b22009-04-24 12:40:33 +00002855 // Upper quadword copied in order.
Nate Begeman9008ca62009-04-27 18:41:29 +00002856 for (int i = 4; i != 8; ++i)
2857 if (Mask[i] >= 0 && Mask[i] != i)
Rafael Espindola15684b22009-04-24 12:40:33 +00002858 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002859
Rafael Espindola15684b22009-04-24 12:40:33 +00002860 // Lower quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002861 for (int i = 0; i != 4; ++i)
2862 if (Mask[i] >= 4)
Rafael Espindola15684b22009-04-24 12:40:33 +00002863 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002864
Rafael Espindola15684b22009-04-24 12:40:33 +00002865 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002866}
2867
Nate Begeman9008ca62009-04-27 18:41:29 +00002868bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002869 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002870 N->getMask(M);
2871 return ::isPSHUFLWMask(M, N->getValueType(0));
2872}
2873
Nate Begemana09008b2009-10-19 02:17:23 +00002874/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
2875/// is suitable for input to PALIGNR.
2876static bool isPALIGNRMask(const SmallVectorImpl<int> &Mask, EVT VT,
2877 bool hasSSSE3) {
2878 int i, e = VT.getVectorNumElements();
Michael J. Spencerec38de22010-10-10 22:04:20 +00002879
Nate Begemana09008b2009-10-19 02:17:23 +00002880 // Do not handle v2i64 / v2f64 shuffles with palignr.
2881 if (e < 4 || !hasSSSE3)
2882 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00002883
Nate Begemana09008b2009-10-19 02:17:23 +00002884 for (i = 0; i != e; ++i)
2885 if (Mask[i] >= 0)
2886 break;
Michael J. Spencerec38de22010-10-10 22:04:20 +00002887
Nate Begemana09008b2009-10-19 02:17:23 +00002888 // All undef, not a palignr.
2889 if (i == e)
2890 return false;
2891
2892 // Determine if it's ok to perform a palignr with only the LHS, since we
2893 // don't have access to the actual shuffle elements to see if RHS is undef.
2894 bool Unary = Mask[i] < (int)e;
2895 bool NeedsUnary = false;
2896
2897 int s = Mask[i] - i;
Michael J. Spencerec38de22010-10-10 22:04:20 +00002898
Nate Begemana09008b2009-10-19 02:17:23 +00002899 // Check the rest of the elements to see if they are consecutive.
2900 for (++i; i != e; ++i) {
2901 int m = Mask[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00002902 if (m < 0)
Nate Begemana09008b2009-10-19 02:17:23 +00002903 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00002904
Nate Begemana09008b2009-10-19 02:17:23 +00002905 Unary = Unary && (m < (int)e);
2906 NeedsUnary = NeedsUnary || (m < s);
2907
2908 if (NeedsUnary && !Unary)
2909 return false;
2910 if (Unary && m != ((s+i) & (e-1)))
2911 return false;
2912 if (!Unary && m != (s+i))
2913 return false;
2914 }
2915 return true;
2916}
2917
2918bool X86::isPALIGNRMask(ShuffleVectorSDNode *N) {
2919 SmallVector<int, 8> M;
2920 N->getMask(M);
2921 return ::isPALIGNRMask(M, N->getValueType(0), true);
2922}
2923
Evan Cheng14aed5e2006-03-24 01:18:28 +00002924/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2925/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Owen Andersone50ed302009-08-10 22:56:29 +00002926static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002927 int NumElems = VT.getVectorNumElements();
2928 if (NumElems != 2 && NumElems != 4)
2929 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002930
Nate Begeman9008ca62009-04-27 18:41:29 +00002931 int Half = NumElems / 2;
2932 for (int i = 0; i < Half; ++i)
2933 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002934 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002935 for (int i = Half; i < NumElems; ++i)
2936 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002937 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002938
Evan Cheng14aed5e2006-03-24 01:18:28 +00002939 return true;
2940}
2941
Nate Begeman9008ca62009-04-27 18:41:29 +00002942bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
2943 SmallVector<int, 8> M;
2944 N->getMask(M);
2945 return ::isSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002946}
2947
Evan Cheng213d2cf2007-05-17 18:45:50 +00002948/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00002949/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2950/// half elements to come from vector 1 (which would equal the dest.) and
2951/// the upper half to come from vector 2.
Owen Andersone50ed302009-08-10 22:56:29 +00002952static bool isCommutedSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002953 int NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00002954
2955 if (NumElems != 2 && NumElems != 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00002956 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002957
Nate Begeman9008ca62009-04-27 18:41:29 +00002958 int Half = NumElems / 2;
2959 for (int i = 0; i < Half; ++i)
2960 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002961 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002962 for (int i = Half; i < NumElems; ++i)
2963 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002964 return false;
2965 return true;
2966}
2967
Nate Begeman9008ca62009-04-27 18:41:29 +00002968static bool isCommutedSHUFP(ShuffleVectorSDNode *N) {
2969 SmallVector<int, 8> M;
2970 N->getMask(M);
2971 return isCommutedSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002972}
2973
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002974/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2975/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00002976bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
2977 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002978 return false;
2979
Evan Cheng2064a2b2006-03-28 06:50:32 +00002980 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman9008ca62009-04-27 18:41:29 +00002981 return isUndefOrEqual(N->getMaskElt(0), 6) &&
2982 isUndefOrEqual(N->getMaskElt(1), 7) &&
2983 isUndefOrEqual(N->getMaskElt(2), 2) &&
2984 isUndefOrEqual(N->getMaskElt(3), 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00002985}
2986
Nate Begeman0b10b912009-11-07 23:17:15 +00002987/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2988/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2989/// <2, 3, 2, 3>
2990bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
2991 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Michael J. Spencerec38de22010-10-10 22:04:20 +00002992
Nate Begeman0b10b912009-11-07 23:17:15 +00002993 if (NumElems != 4)
2994 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00002995
Nate Begeman0b10b912009-11-07 23:17:15 +00002996 return isUndefOrEqual(N->getMaskElt(0), 2) &&
2997 isUndefOrEqual(N->getMaskElt(1), 3) &&
2998 isUndefOrEqual(N->getMaskElt(2), 2) &&
2999 isUndefOrEqual(N->getMaskElt(3), 3);
3000}
3001
Evan Cheng5ced1d82006-04-06 23:23:56 +00003002/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
3003/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman9008ca62009-04-27 18:41:29 +00003004bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
3005 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003006
Evan Cheng5ced1d82006-04-06 23:23:56 +00003007 if (NumElems != 2 && NumElems != 4)
3008 return false;
3009
Evan Chengc5cdff22006-04-07 21:53:05 +00003010 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003011 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003012 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003013
Evan Chengc5cdff22006-04-07 21:53:05 +00003014 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003015 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003016 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003017
3018 return true;
3019}
3020
Nate Begeman0b10b912009-11-07 23:17:15 +00003021/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
3022/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
3023bool X86::isMOVLHPSMask(ShuffleVectorSDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003024 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003025
Evan Cheng5ced1d82006-04-06 23:23:56 +00003026 if (NumElems != 2 && NumElems != 4)
3027 return false;
3028
Evan Chengc5cdff22006-04-07 21:53:05 +00003029 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003030 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003031 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003032
Nate Begeman9008ca62009-04-27 18:41:29 +00003033 for (unsigned i = 0; i < NumElems/2; ++i)
3034 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003035 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003036
3037 return true;
3038}
3039
Evan Cheng0038e592006-03-28 00:39:58 +00003040/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
3041/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Owen Andersone50ed302009-08-10 22:56:29 +00003042static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003043 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003044 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003045 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng0038e592006-03-28 00:39:58 +00003046 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003047
Nate Begeman9008ca62009-04-27 18:41:29 +00003048 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
3049 int BitI = Mask[i];
3050 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00003051 if (!isUndefOrEqual(BitI, j))
3052 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00003053 if (V2IsSplat) {
Mon P Wang7bcaefa2009-02-04 01:16:59 +00003054 if (!isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003055 return false;
3056 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00003057 if (!isUndefOrEqual(BitI1, j + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003058 return false;
3059 }
Evan Cheng0038e592006-03-28 00:39:58 +00003060 }
Evan Cheng0038e592006-03-28 00:39:58 +00003061 return true;
3062}
3063
Nate Begeman9008ca62009-04-27 18:41:29 +00003064bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3065 SmallVector<int, 8> M;
3066 N->getMask(M);
3067 return ::isUNPCKLMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003068}
3069
Evan Cheng4fcb9222006-03-28 02:43:26 +00003070/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3071/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Eric Christopherfd179292009-08-27 18:07:15 +00003072static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003073 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003074 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003075 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng4fcb9222006-03-28 02:43:26 +00003076 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003077
Nate Begeman9008ca62009-04-27 18:41:29 +00003078 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
3079 int BitI = Mask[i];
3080 int BitI1 = Mask[i+1];
Chris Lattner5a88b832007-02-25 07:10:00 +00003081 if (!isUndefOrEqual(BitI, j + NumElts/2))
Evan Chengc5cdff22006-04-07 21:53:05 +00003082 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00003083 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00003084 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003085 return false;
3086 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00003087 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003088 return false;
3089 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003090 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003091 return true;
3092}
3093
Nate Begeman9008ca62009-04-27 18:41:29 +00003094bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3095 SmallVector<int, 8> M;
3096 N->getMask(M);
3097 return ::isUNPCKHMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003098}
3099
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003100/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3101/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3102/// <0, 0, 1, 1>
Owen Andersone50ed302009-08-10 22:56:29 +00003103static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003104 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003105 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003106 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003107
Nate Begeman9008ca62009-04-27 18:41:29 +00003108 for (int i = 0, j = 0; i != NumElems; i += 2, ++j) {
3109 int BitI = Mask[i];
3110 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00003111 if (!isUndefOrEqual(BitI, j))
3112 return false;
3113 if (!isUndefOrEqual(BitI1, j))
3114 return false;
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003115 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003116 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003117}
3118
Nate Begeman9008ca62009-04-27 18:41:29 +00003119bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
3120 SmallVector<int, 8> M;
3121 N->getMask(M);
3122 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
3123}
3124
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003125/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3126/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3127/// <2, 2, 3, 3>
Owen Andersone50ed302009-08-10 22:56:29 +00003128static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003129 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003130 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
3131 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003132
Nate Begeman9008ca62009-04-27 18:41:29 +00003133 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
3134 int BitI = Mask[i];
3135 int BitI1 = Mask[i+1];
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003136 if (!isUndefOrEqual(BitI, j))
3137 return false;
3138 if (!isUndefOrEqual(BitI1, j))
3139 return false;
3140 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003141 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003142}
3143
Nate Begeman9008ca62009-04-27 18:41:29 +00003144bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
3145 SmallVector<int, 8> M;
3146 N->getMask(M);
3147 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
3148}
3149
Evan Cheng017dcc62006-04-21 01:05:10 +00003150/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3151/// specifies a shuffle of elements that is suitable for input to MOVSS,
3152/// MOVSD, and MOVD, i.e. setting the lowest element.
Owen Andersone50ed302009-08-10 22:56:29 +00003153static bool isMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003154 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003155 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003156
3157 int NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003158
Nate Begeman9008ca62009-04-27 18:41:29 +00003159 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003160 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003161
Nate Begeman9008ca62009-04-27 18:41:29 +00003162 for (int i = 1; i < NumElts; ++i)
3163 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003164 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003165
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003166 return true;
3167}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003168
Nate Begeman9008ca62009-04-27 18:41:29 +00003169bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
3170 SmallVector<int, 8> M;
3171 N->getMask(M);
3172 return ::isMOVLMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003173}
3174
Evan Cheng017dcc62006-04-21 01:05:10 +00003175/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
3176/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003177/// element of vector 2 and the other elements to come from vector 1 in order.
Owen Andersone50ed302009-08-10 22:56:29 +00003178static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003179 bool V2IsSplat = false, bool V2IsUndef = false) {
3180 int NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003181 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003182 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003183
Nate Begeman9008ca62009-04-27 18:41:29 +00003184 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003185 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003186
Nate Begeman9008ca62009-04-27 18:41:29 +00003187 for (int i = 1; i < NumOps; ++i)
3188 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3189 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3190 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003191 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003192
Evan Cheng39623da2006-04-20 08:58:49 +00003193 return true;
3194}
3195
Nate Begeman9008ca62009-04-27 18:41:29 +00003196static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00003197 bool V2IsUndef = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003198 SmallVector<int, 8> M;
3199 N->getMask(M);
3200 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00003201}
3202
Evan Chengd9539472006-04-14 21:59:03 +00003203/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3204/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003205bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N) {
3206 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003207 return false;
3208
3209 // Expect 1, 1, 3, 3
Rafael Espindola15684b22009-04-24 12:40:33 +00003210 for (unsigned i = 0; i < 2; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003211 int Elt = N->getMaskElt(i);
3212 if (Elt >= 0 && Elt != 1)
3213 return false;
Rafael Espindola15684b22009-04-24 12:40:33 +00003214 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003215
3216 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003217 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003218 int Elt = N->getMaskElt(i);
3219 if (Elt >= 0 && Elt != 3)
3220 return false;
3221 if (Elt == 3)
3222 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003223 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003224 // Don't use movshdup if it can be done with a shufps.
Nate Begeman9008ca62009-04-27 18:41:29 +00003225 // FIXME: verify that matching u, u, 3, 3 is what we want.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003226 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003227}
3228
3229/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3230/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003231bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N) {
3232 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003233 return false;
3234
3235 // Expect 0, 0, 2, 2
Nate Begeman9008ca62009-04-27 18:41:29 +00003236 for (unsigned i = 0; i < 2; ++i)
3237 if (N->getMaskElt(i) > 0)
3238 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003239
3240 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003241 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003242 int Elt = N->getMaskElt(i);
3243 if (Elt >= 0 && Elt != 2)
3244 return false;
3245 if (Elt == 2)
3246 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003247 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003248 // Don't use movsldup if it can be done with a shufps.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003249 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003250}
3251
Evan Cheng0b457f02008-09-25 20:50:48 +00003252/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3253/// specifies a shuffle of elements that is suitable for input to MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003254bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
3255 int e = N->getValueType(0).getVectorNumElements() / 2;
Eric Christopherfd179292009-08-27 18:07:15 +00003256
Nate Begeman9008ca62009-04-27 18:41:29 +00003257 for (int i = 0; i < e; ++i)
3258 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003259 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003260 for (int i = 0; i < e; ++i)
3261 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003262 return false;
3263 return true;
3264}
3265
David Greenec38a03e2011-02-03 15:50:00 +00003266/// isVEXTRACTF128Index - Return true if the specified
3267/// EXTRACT_SUBVECTOR operand specifies a vector extract that is
3268/// suitable for input to VEXTRACTF128.
3269bool X86::isVEXTRACTF128Index(SDNode *N) {
3270 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
3271 return false;
3272
3273 // The index should be aligned on a 128-bit boundary.
3274 uint64_t Index =
3275 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
3276
3277 unsigned VL = N->getValueType(0).getVectorNumElements();
3278 unsigned VBits = N->getValueType(0).getSizeInBits();
3279 unsigned ElSize = VBits / VL;
3280 bool Result = (Index * ElSize) % 128 == 0;
3281
3282 return Result;
3283}
3284
David Greeneccacdc12011-02-04 16:08:29 +00003285/// isVINSERTF128Index - Return true if the specified INSERT_SUBVECTOR
3286/// operand specifies a subvector insert that is suitable for input to
3287/// VINSERTF128.
3288bool X86::isVINSERTF128Index(SDNode *N) {
3289 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
3290 return false;
3291
3292 // The index should be aligned on a 128-bit boundary.
3293 uint64_t Index =
3294 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
3295
3296 unsigned VL = N->getValueType(0).getVectorNumElements();
3297 unsigned VBits = N->getValueType(0).getSizeInBits();
3298 unsigned ElSize = VBits / VL;
3299 bool Result = (Index * ElSize) % 128 == 0;
3300
3301 return Result;
3302}
3303
Evan Cheng63d33002006-03-22 08:01:21 +00003304/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003305/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Evan Cheng63d33002006-03-22 08:01:21 +00003306unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003307 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3308 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
3309
Evan Chengb9df0ca2006-03-22 02:53:00 +00003310 unsigned Shift = (NumOperands == 4) ? 2 : 1;
3311 unsigned Mask = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00003312 for (int i = 0; i < NumOperands; ++i) {
3313 int Val = SVOp->getMaskElt(NumOperands-i-1);
3314 if (Val < 0) Val = 0;
Evan Cheng14aed5e2006-03-24 01:18:28 +00003315 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00003316 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00003317 if (i != NumOperands - 1)
3318 Mask <<= Shift;
3319 }
Evan Cheng63d33002006-03-22 08:01:21 +00003320 return Mask;
3321}
3322
Evan Cheng506d3df2006-03-29 23:07:14 +00003323/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003324/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003325unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003326 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003327 unsigned Mask = 0;
3328 // 8 nodes, but we only care about the last 4.
3329 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003330 int Val = SVOp->getMaskElt(i);
3331 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00003332 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00003333 if (i != 4)
3334 Mask <<= 2;
3335 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003336 return Mask;
3337}
3338
3339/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003340/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003341unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003342 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003343 unsigned Mask = 0;
3344 // 8 nodes, but we only care about the first 4.
3345 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003346 int Val = SVOp->getMaskElt(i);
3347 if (Val >= 0)
3348 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00003349 if (i != 0)
3350 Mask <<= 2;
3351 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003352 return Mask;
3353}
3354
Nate Begemana09008b2009-10-19 02:17:23 +00003355/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
3356/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
3357unsigned X86::getShufflePALIGNRImmediate(SDNode *N) {
3358 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3359 EVT VVT = N->getValueType(0);
3360 unsigned EltSize = VVT.getVectorElementType().getSizeInBits() >> 3;
3361 int Val = 0;
3362
3363 unsigned i, e;
3364 for (i = 0, e = VVT.getVectorNumElements(); i != e; ++i) {
3365 Val = SVOp->getMaskElt(i);
3366 if (Val >= 0)
3367 break;
3368 }
3369 return (Val - i) * EltSize;
3370}
3371
David Greenec38a03e2011-02-03 15:50:00 +00003372/// getExtractVEXTRACTF128Immediate - Return the appropriate immediate
3373/// to extract the specified EXTRACT_SUBVECTOR index with VEXTRACTF128
3374/// instructions.
3375unsigned X86::getExtractVEXTRACTF128Immediate(SDNode *N) {
3376 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
3377 llvm_unreachable("Illegal extract subvector for VEXTRACTF128");
3378
3379 uint64_t Index =
3380 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
3381
3382 EVT VecVT = N->getOperand(0).getValueType();
3383 EVT ElVT = VecVT.getVectorElementType();
3384
3385 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
3386
3387 return Index / NumElemsPerChunk;
3388}
3389
David Greeneccacdc12011-02-04 16:08:29 +00003390/// getInsertVINSERTF128Immediate - Return the appropriate immediate
3391/// to insert at the specified INSERT_SUBVECTOR index with VINSERTF128
3392/// instructions.
3393unsigned X86::getInsertVINSERTF128Immediate(SDNode *N) {
3394 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
3395 llvm_unreachable("Illegal insert subvector for VINSERTF128");
3396
3397 uint64_t Index =
NAKAMURA Takumi27635382011-02-05 15:10:54 +00003398 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
David Greeneccacdc12011-02-04 16:08:29 +00003399
3400 EVT VecVT = N->getValueType(0);
3401 EVT ElVT = VecVT.getVectorElementType();
3402
3403 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
3404
3405 return Index / NumElemsPerChunk;
3406}
3407
Evan Cheng37b73872009-07-30 08:33:02 +00003408/// isZeroNode - Returns true if Elt is a constant zero or a floating point
3409/// constant +0.0.
3410bool X86::isZeroNode(SDValue Elt) {
3411 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmane368b462010-06-18 14:22:04 +00003412 cast<ConstantSDNode>(Elt)->isNullValue()) ||
Evan Cheng37b73872009-07-30 08:33:02 +00003413 (isa<ConstantFPSDNode>(Elt) &&
3414 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
3415}
3416
Nate Begeman9008ca62009-04-27 18:41:29 +00003417/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
3418/// their permute mask.
3419static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
3420 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003421 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003422 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00003423 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00003424
Nate Begeman5a5ca152009-04-29 05:20:52 +00003425 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003426 int idx = SVOp->getMaskElt(i);
3427 if (idx < 0)
3428 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003429 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003430 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003431 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003432 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003433 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003434 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
3435 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003436}
3437
Evan Cheng779ccea2007-12-07 21:30:01 +00003438/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3439/// the two vector operands have swapped position.
Owen Andersone50ed302009-08-10 22:56:29 +00003440static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00003441 unsigned NumElems = VT.getVectorNumElements();
3442 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003443 int idx = Mask[i];
3444 if (idx < 0)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003445 continue;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003446 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003447 Mask[i] = idx + NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003448 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003449 Mask[i] = idx - NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003450 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003451}
3452
Evan Cheng533a0aa2006-04-19 20:35:22 +00003453/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
3454/// match movhlps. The lower half elements should come from upper half of
3455/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003456/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00003457static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
3458 if (Op->getValueType(0).getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00003459 return false;
3460 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003461 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003462 return false;
3463 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003464 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003465 return false;
3466 return true;
3467}
3468
Evan Cheng5ced1d82006-04-06 23:23:56 +00003469/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00003470/// is promoted to a vector. It also returns the LoadSDNode by reference if
3471/// required.
3472static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00003473 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
3474 return false;
3475 N = N->getOperand(0).getNode();
3476 if (!ISD::isNON_EXTLoad(N))
3477 return false;
3478 if (LD)
3479 *LD = cast<LoadSDNode>(N);
3480 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003481}
3482
Evan Cheng533a0aa2006-04-19 20:35:22 +00003483/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
3484/// match movlp{s|d}. The lower half elements should come from lower half of
3485/// V1 (and in order), and the upper half elements should come from the upper
3486/// half of V2 (and in order). And since V1 will become the source of the
3487/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003488static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
3489 ShuffleVectorSDNode *Op) {
Evan Cheng466685d2006-10-09 20:57:25 +00003490 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003491 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00003492 // Is V2 is a vector load, don't do this transformation. We will try to use
3493 // load folding shufps op.
3494 if (ISD::isNON_EXTLoad(V2))
3495 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003496
Nate Begeman5a5ca152009-04-29 05:20:52 +00003497 unsigned NumElems = Op->getValueType(0).getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003498
Evan Cheng533a0aa2006-04-19 20:35:22 +00003499 if (NumElems != 2 && NumElems != 4)
3500 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003501 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003502 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003503 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003504 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003505 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003506 return false;
3507 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003508}
3509
Evan Cheng39623da2006-04-20 08:58:49 +00003510/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
3511/// all the same.
3512static bool isSplatVector(SDNode *N) {
3513 if (N->getOpcode() != ISD::BUILD_VECTOR)
3514 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003515
Dan Gohman475871a2008-07-27 21:46:04 +00003516 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00003517 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
3518 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003519 return false;
3520 return true;
3521}
3522
Evan Cheng213d2cf2007-05-17 18:45:50 +00003523/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00003524/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00003525/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00003526static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00003527 SDValue V1 = N->getOperand(0);
3528 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003529 unsigned NumElems = N->getValueType(0).getVectorNumElements();
3530 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003531 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003532 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003533 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00003534 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
3535 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003536 if (Opc != ISD::BUILD_VECTOR ||
3537 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00003538 return false;
3539 } else if (Idx >= 0) {
3540 unsigned Opc = V1.getOpcode();
3541 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
3542 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003543 if (Opc != ISD::BUILD_VECTOR ||
3544 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00003545 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00003546 }
3547 }
3548 return true;
3549}
3550
3551/// getZeroVector - Returns a vector of specified type with all zero elements.
3552///
Owen Andersone50ed302009-08-10 22:56:29 +00003553static SDValue getZeroVector(EVT VT, bool HasSSE2, SelectionDAG &DAG,
Dale Johannesenace16102009-02-03 19:33:06 +00003554 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003555 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003556
Dale Johannesen0488fb62010-09-30 23:57:10 +00003557 // Always build SSE zero vectors as <4 x i32> bitcasted
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003558 // to their dest type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00003559 SDValue Vec;
Dale Johannesen0488fb62010-09-30 23:57:10 +00003560 if (VT.getSizeInBits() == 128) { // SSE
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003561 if (HasSSE2) { // SSE2
3562 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3563 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
3564 } else { // SSE1
3565 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
3566 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
3567 }
3568 } else if (VT.getSizeInBits() == 256) { // AVX
3569 // 256-bit logic and arithmetic instructions in AVX are
3570 // all floating-point, no support for integer ops. Default
3571 // to emitting fp zeroed vectors then.
Owen Anderson825b72b2009-08-11 20:47:22 +00003572 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003573 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
3574 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops, 8);
Evan Chengf0df0312008-05-15 08:39:06 +00003575 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003576 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00003577}
3578
Chris Lattner8a594482007-11-25 00:24:49 +00003579/// getOnesVector - Returns a vector of specified type with all bits set.
3580///
Owen Andersone50ed302009-08-10 22:56:29 +00003581static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003582 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003583
Chris Lattner8a594482007-11-25 00:24:49 +00003584 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
3585 // type. This ensures they get CSE'd.
Owen Anderson825b72b2009-08-11 20:47:22 +00003586 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00003587 SDValue Vec;
Dale Johannesen0488fb62010-09-30 23:57:10 +00003588 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003589 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00003590}
3591
3592
Evan Cheng39623da2006-04-20 08:58:49 +00003593/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
3594/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00003595static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003596 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003597 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003598
Evan Cheng39623da2006-04-20 08:58:49 +00003599 bool Changed = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003600 SmallVector<int, 8> MaskVec;
3601 SVOp->getMask(MaskVec);
Eric Christopherfd179292009-08-27 18:07:15 +00003602
Nate Begeman5a5ca152009-04-29 05:20:52 +00003603 for (unsigned i = 0; i != NumElems; ++i) {
3604 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003605 MaskVec[i] = NumElems;
3606 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00003607 }
Evan Cheng39623da2006-04-20 08:58:49 +00003608 }
Evan Cheng39623da2006-04-20 08:58:49 +00003609 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00003610 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
3611 SVOp->getOperand(1), &MaskVec[0]);
3612 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00003613}
3614
Evan Cheng017dcc62006-04-21 01:05:10 +00003615/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
3616/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00003617static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003618 SDValue V2) {
3619 unsigned NumElems = VT.getVectorNumElements();
3620 SmallVector<int, 8> Mask;
3621 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00003622 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003623 Mask.push_back(i);
3624 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00003625}
3626
Nate Begeman9008ca62009-04-27 18:41:29 +00003627/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003628static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003629 SDValue V2) {
3630 unsigned NumElems = VT.getVectorNumElements();
3631 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00003632 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003633 Mask.push_back(i);
3634 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00003635 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003636 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00003637}
3638
Nate Begeman9008ca62009-04-27 18:41:29 +00003639/// getUnpackhMask - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003640static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003641 SDValue V2) {
3642 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00003643 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00003644 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00003645 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003646 Mask.push_back(i + Half);
3647 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00003648 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003649 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00003650}
3651
Bruno Cardoso Lopesbb0a9482010-08-13 17:50:47 +00003652/// PromoteSplat - Promote a splat of v4i32, v8i16 or v16i8 to v4f32.
3653static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003654 EVT PVT = MVT::v4f32;
Owen Andersone50ed302009-08-10 22:56:29 +00003655 EVT VT = SV->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00003656 DebugLoc dl = SV->getDebugLoc();
3657 SDValue V1 = SV->getOperand(0);
3658 int NumElems = VT.getVectorNumElements();
3659 int EltNo = SV->getSplatIndex();
Rafael Espindola15684b22009-04-24 12:40:33 +00003660
Nate Begeman9008ca62009-04-27 18:41:29 +00003661 // unpack elements to the correct location
3662 while (NumElems > 4) {
3663 if (EltNo < NumElems/2) {
3664 V1 = getUnpackl(DAG, dl, VT, V1, V1);
3665 } else {
3666 V1 = getUnpackh(DAG, dl, VT, V1, V1);
3667 EltNo -= NumElems/2;
3668 }
3669 NumElems >>= 1;
3670 }
Eric Christopherfd179292009-08-27 18:07:15 +00003671
Nate Begeman9008ca62009-04-27 18:41:29 +00003672 // Perform the splat.
3673 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003674 V1 = DAG.getNode(ISD::BITCAST, dl, PVT, V1);
Nate Begeman9008ca62009-04-27 18:41:29 +00003675 V1 = DAG.getVectorShuffle(PVT, dl, V1, DAG.getUNDEF(PVT), &SplatMask[0]);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003676 return DAG.getNode(ISD::BITCAST, dl, VT, V1);
Evan Chengc575ca22006-04-17 20:43:08 +00003677}
3678
Evan Chengba05f722006-04-21 23:03:30 +00003679/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00003680/// vector of zero or undef vector. This produces a shuffle where the low
3681/// element of V2 is swizzled into the zero/undef vector, landing at element
3682/// 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 +00003683static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Chengf0df0312008-05-15 08:39:06 +00003684 bool isZero, bool HasSSE2,
3685 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003686 EVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00003687 SDValue V1 = isZero
Nate Begeman9008ca62009-04-27 18:41:29 +00003688 ? getZeroVector(VT, HasSSE2, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
3689 unsigned NumElems = VT.getVectorNumElements();
3690 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00003691 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003692 // If this is the insertion idx, put the low elt of V2 here.
3693 MaskVec.push_back(i == Idx ? NumElems : i);
3694 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00003695}
3696
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003697/// getShuffleScalarElt - Returns the scalar element that will make up the ith
3698/// element of the result of the vector shuffle.
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003699SDValue getShuffleScalarElt(SDNode *N, int Index, SelectionDAG &DAG,
3700 unsigned Depth) {
3701 if (Depth == 6)
3702 return SDValue(); // Limit search depth.
3703
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003704 SDValue V = SDValue(N, 0);
3705 EVT VT = V.getValueType();
3706 unsigned Opcode = V.getOpcode();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003707
3708 // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
3709 if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
3710 Index = SV->getMaskElt(Index);
3711
3712 if (Index < 0)
3713 return DAG.getUNDEF(VT.getVectorElementType());
3714
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003715 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003716 SDValue NewV = (Index < NumElems) ? SV->getOperand(0) : SV->getOperand(1);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003717 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG, Depth+1);
Evan Chengf26ffe92008-05-29 08:22:04 +00003718 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003719
3720 // Recurse into target specific vector shuffles to find scalars.
3721 if (isTargetShuffle(Opcode)) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003722 int NumElems = VT.getVectorNumElements();
3723 SmallVector<unsigned, 16> ShuffleMask;
3724 SDValue ImmN;
3725
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003726 switch(Opcode) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003727 case X86ISD::SHUFPS:
3728 case X86ISD::SHUFPD:
3729 ImmN = N->getOperand(N->getNumOperands()-1);
3730 DecodeSHUFPSMask(NumElems,
3731 cast<ConstantSDNode>(ImmN)->getZExtValue(),
3732 ShuffleMask);
3733 break;
3734 case X86ISD::PUNPCKHBW:
3735 case X86ISD::PUNPCKHWD:
3736 case X86ISD::PUNPCKHDQ:
3737 case X86ISD::PUNPCKHQDQ:
3738 DecodePUNPCKHMask(NumElems, ShuffleMask);
3739 break;
3740 case X86ISD::UNPCKHPS:
3741 case X86ISD::UNPCKHPD:
3742 DecodeUNPCKHPMask(NumElems, ShuffleMask);
3743 break;
3744 case X86ISD::PUNPCKLBW:
3745 case X86ISD::PUNPCKLWD:
3746 case X86ISD::PUNPCKLDQ:
3747 case X86ISD::PUNPCKLQDQ:
3748 DecodePUNPCKLMask(NumElems, ShuffleMask);
3749 break;
3750 case X86ISD::UNPCKLPS:
3751 case X86ISD::UNPCKLPD:
3752 DecodeUNPCKLPMask(NumElems, ShuffleMask);
3753 break;
3754 case X86ISD::MOVHLPS:
3755 DecodeMOVHLPSMask(NumElems, ShuffleMask);
3756 break;
3757 case X86ISD::MOVLHPS:
3758 DecodeMOVLHPSMask(NumElems, ShuffleMask);
3759 break;
3760 case X86ISD::PSHUFD:
3761 ImmN = N->getOperand(N->getNumOperands()-1);
3762 DecodePSHUFMask(NumElems,
3763 cast<ConstantSDNode>(ImmN)->getZExtValue(),
3764 ShuffleMask);
3765 break;
3766 case X86ISD::PSHUFHW:
3767 ImmN = N->getOperand(N->getNumOperands()-1);
3768 DecodePSHUFHWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
3769 ShuffleMask);
3770 break;
3771 case X86ISD::PSHUFLW:
3772 ImmN = N->getOperand(N->getNumOperands()-1);
3773 DecodePSHUFLWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
3774 ShuffleMask);
3775 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003776 case X86ISD::MOVSS:
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00003777 case X86ISD::MOVSD: {
3778 // The index 0 always comes from the first element of the second source,
3779 // this is why MOVSS and MOVSD are used in the first place. The other
3780 // elements come from the other positions of the first source vector.
3781 unsigned OpNum = (Index == 0) ? 1 : 0;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003782 return getShuffleScalarElt(V.getOperand(OpNum).getNode(), Index, DAG,
3783 Depth+1);
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00003784 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003785 default:
3786 assert("not implemented for target shuffle node");
3787 return SDValue();
3788 }
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003789
3790 Index = ShuffleMask[Index];
3791 if (Index < 0)
3792 return DAG.getUNDEF(VT.getVectorElementType());
3793
3794 SDValue NewV = (Index < NumElems) ? N->getOperand(0) : N->getOperand(1);
3795 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG,
3796 Depth+1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003797 }
3798
3799 // Actual nodes that may contain scalar elements
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003800 if (Opcode == ISD::BITCAST) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003801 V = V.getOperand(0);
3802 EVT SrcVT = V.getValueType();
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003803 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003804
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003805 if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003806 return SDValue();
3807 }
3808
3809 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
3810 return (Index == 0) ? V.getOperand(0)
3811 : DAG.getUNDEF(VT.getVectorElementType());
3812
3813 if (V.getOpcode() == ISD::BUILD_VECTOR)
3814 return V.getOperand(Index);
3815
3816 return SDValue();
3817}
3818
3819/// getNumOfConsecutiveZeros - Return the number of elements of a vector
3820/// shuffle operation which come from a consecutively from a zero. The
3821/// search can start in two diferent directions, from left or right.
3822static
3823unsigned getNumOfConsecutiveZeros(SDNode *N, int NumElems,
3824 bool ZerosFromLeft, SelectionDAG &DAG) {
3825 int i = 0;
3826
3827 while (i < NumElems) {
3828 unsigned Index = ZerosFromLeft ? i : NumElems-i-1;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003829 SDValue Elt = getShuffleScalarElt(N, Index, DAG, 0);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003830 if (!(Elt.getNode() &&
3831 (Elt.getOpcode() == ISD::UNDEF || X86::isZeroNode(Elt))))
3832 break;
3833 ++i;
3834 }
3835
3836 return i;
3837}
3838
3839/// isShuffleMaskConsecutive - Check if the shuffle mask indicies from MaskI to
3840/// MaskE correspond consecutively to elements from one of the vector operands,
3841/// starting from its index OpIdx. Also tell OpNum which source vector operand.
3842static
3843bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp, int MaskI, int MaskE,
3844 int OpIdx, int NumElems, unsigned &OpNum) {
3845 bool SeenV1 = false;
3846 bool SeenV2 = false;
3847
3848 for (int i = MaskI; i <= MaskE; ++i, ++OpIdx) {
3849 int Idx = SVOp->getMaskElt(i);
3850 // Ignore undef indicies
3851 if (Idx < 0)
3852 continue;
3853
3854 if (Idx < NumElems)
3855 SeenV1 = true;
3856 else
3857 SeenV2 = true;
3858
3859 // Only accept consecutive elements from the same vector
3860 if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
3861 return false;
3862 }
3863
3864 OpNum = SeenV1 ? 0 : 1;
3865 return true;
3866}
3867
3868/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
3869/// logical left shift of a vector.
3870static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
3871 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
3872 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
3873 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
3874 false /* check zeros from right */, DAG);
3875 unsigned OpSrc;
3876
3877 if (!NumZeros)
3878 return false;
3879
3880 // Considering the elements in the mask that are not consecutive zeros,
3881 // check if they consecutively come from only one of the source vectors.
3882 //
3883 // V1 = {X, A, B, C} 0
3884 // \ \ \ /
3885 // vector_shuffle V1, V2 <1, 2, 3, X>
3886 //
3887 if (!isShuffleMaskConsecutive(SVOp,
3888 0, // Mask Start Index
3889 NumElems-NumZeros-1, // Mask End Index
3890 NumZeros, // Where to start looking in the src vector
3891 NumElems, // Number of elements in vector
3892 OpSrc)) // Which source operand ?
3893 return false;
3894
3895 isLeft = false;
3896 ShAmt = NumZeros;
3897 ShVal = SVOp->getOperand(OpSrc);
3898 return true;
3899}
3900
3901/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
3902/// logical left shift of a vector.
3903static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
3904 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
3905 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
3906 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
3907 true /* check zeros from left */, DAG);
3908 unsigned OpSrc;
3909
3910 if (!NumZeros)
3911 return false;
3912
3913 // Considering the elements in the mask that are not consecutive zeros,
3914 // check if they consecutively come from only one of the source vectors.
3915 //
3916 // 0 { A, B, X, X } = V2
3917 // / \ / /
3918 // vector_shuffle V1, V2 <X, X, 4, 5>
3919 //
3920 if (!isShuffleMaskConsecutive(SVOp,
3921 NumZeros, // Mask Start Index
3922 NumElems-1, // Mask End Index
3923 0, // Where to start looking in the src vector
3924 NumElems, // Number of elements in vector
3925 OpSrc)) // Which source operand ?
3926 return false;
3927
3928 isLeft = true;
3929 ShAmt = NumZeros;
3930 ShVal = SVOp->getOperand(OpSrc);
3931 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00003932}
3933
3934/// isVectorShift - Returns true if the shuffle can be implemented as a
3935/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003936static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00003937 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003938 if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
3939 isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
3940 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00003941
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003942 return false;
Evan Chengf26ffe92008-05-29 08:22:04 +00003943}
3944
Evan Chengc78d3b42006-04-24 18:01:45 +00003945/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
3946///
Dan Gohman475871a2008-07-27 21:46:04 +00003947static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00003948 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00003949 SelectionDAG &DAG,
3950 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003951 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00003952 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00003953
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003954 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003955 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003956 bool First = true;
3957 for (unsigned i = 0; i < 16; ++i) {
3958 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
3959 if (ThisIsNonZero && First) {
3960 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003961 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00003962 else
Owen Anderson825b72b2009-08-11 20:47:22 +00003963 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00003964 First = false;
3965 }
3966
3967 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00003968 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003969 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
3970 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003971 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003972 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00003973 }
3974 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003975 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
3976 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
3977 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00003978 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003979 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00003980 } else
3981 ThisElt = LastElt;
3982
Gabor Greifba36cb52008-08-28 21:40:38 +00003983 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00003984 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00003985 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00003986 }
3987 }
3988
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003989 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00003990}
3991
Bill Wendlinga348c562007-03-22 18:42:45 +00003992/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00003993///
Dan Gohman475871a2008-07-27 21:46:04 +00003994static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00003995 unsigned NumNonZero, unsigned NumZero,
3996 SelectionDAG &DAG,
3997 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003998 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00003999 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004000
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004001 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004002 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004003 bool First = true;
4004 for (unsigned i = 0; i < 8; ++i) {
4005 bool isNonZero = (NonZeros & (1 << i)) != 0;
4006 if (isNonZero) {
4007 if (First) {
4008 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004009 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004010 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004011 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004012 First = false;
4013 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004014 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004015 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00004016 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00004017 }
4018 }
4019
4020 return V;
4021}
4022
Evan Chengf26ffe92008-05-29 08:22:04 +00004023/// getVShift - Return a vector logical shift node.
4024///
Owen Andersone50ed302009-08-10 22:56:29 +00004025static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00004026 unsigned NumBits, SelectionDAG &DAG,
4027 const TargetLowering &TLI, DebugLoc dl) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004028 EVT ShVT = MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00004029 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004030 SrcOp = DAG.getNode(ISD::BITCAST, dl, ShVT, SrcOp);
4031 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004032 DAG.getNode(Opc, dl, ShVT, SrcOp,
Gabor Greif327ef032008-08-28 23:19:51 +00004033 DAG.getConstant(NumBits, TLI.getShiftAmountTy())));
Evan Chengf26ffe92008-05-29 08:22:04 +00004034}
4035
Dan Gohman475871a2008-07-27 21:46:04 +00004036SDValue
Evan Chengc3630942009-12-09 21:00:30 +00004037X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00004038 SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00004039
Evan Chengc3630942009-12-09 21:00:30 +00004040 // Check if the scalar load can be widened into a vector load. And if
4041 // the address is "base + cst" see if the cst can be "absorbed" into
4042 // the shuffle mask.
4043 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
4044 SDValue Ptr = LD->getBasePtr();
4045 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
4046 return SDValue();
4047 EVT PVT = LD->getValueType(0);
4048 if (PVT != MVT::i32 && PVT != MVT::f32)
4049 return SDValue();
4050
4051 int FI = -1;
4052 int64_t Offset = 0;
4053 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
4054 FI = FINode->getIndex();
4055 Offset = 0;
4056 } else if (Ptr.getOpcode() == ISD::ADD &&
4057 isa<ConstantSDNode>(Ptr.getOperand(1)) &&
4058 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4059 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4060 Offset = Ptr.getConstantOperandVal(1);
4061 Ptr = Ptr.getOperand(0);
4062 } else {
4063 return SDValue();
4064 }
4065
4066 SDValue Chain = LD->getChain();
4067 // Make sure the stack object alignment is at least 16.
4068 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
4069 if (DAG.InferPtrAlignment(Ptr) < 16) {
4070 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00004071 // Can't change the alignment. FIXME: It's possible to compute
4072 // the exact stack offset and reference FI + adjust offset instead.
4073 // If someone *really* cares about this. That's the way to implement it.
4074 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004075 } else {
4076 MFI->setObjectAlignment(FI, 16);
4077 }
4078 }
4079
4080 // (Offset % 16) must be multiple of 4. Then address is then
4081 // Ptr + (Offset & ~15).
4082 if (Offset < 0)
4083 return SDValue();
4084 if ((Offset % 16) & 3)
4085 return SDValue();
4086 int64_t StartOffset = Offset & ~15;
4087 if (StartOffset)
4088 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
4089 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
4090
4091 int EltNo = (Offset - StartOffset) >> 2;
4092 int Mask[4] = { EltNo, EltNo, EltNo, EltNo };
4093 EVT VT = (PVT == MVT::i32) ? MVT::v4i32 : MVT::v4f32;
Chris Lattner51abfe42010-09-21 06:02:19 +00004094 SDValue V1 = DAG.getLoad(VT, dl, Chain, Ptr,
4095 LD->getPointerInfo().getWithOffset(StartOffset),
David Greene67c9d422010-02-15 16:53:33 +00004096 false, false, 0);
Evan Chengc3630942009-12-09 21:00:30 +00004097 // Canonicalize it to a v4i32 shuffle.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004098 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, V1);
4099 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Chengc3630942009-12-09 21:00:30 +00004100 DAG.getVectorShuffle(MVT::v4i32, dl, V1,
Chris Lattner51abfe42010-09-21 06:02:19 +00004101 DAG.getUNDEF(MVT::v4i32),&Mask[0]));
Evan Chengc3630942009-12-09 21:00:30 +00004102 }
4103
4104 return SDValue();
4105}
4106
Michael J. Spencerec38de22010-10-10 22:04:20 +00004107/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
4108/// vector of type 'VT', see if the elements can be replaced by a single large
Nate Begeman1449f292010-03-24 22:19:06 +00004109/// load which has the same value as a build_vector whose operands are 'elts'.
4110///
4111/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
Michael J. Spencerec38de22010-10-10 22:04:20 +00004112///
Nate Begeman1449f292010-03-24 22:19:06 +00004113/// FIXME: we'd also like to handle the case where the last elements are zero
4114/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
4115/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004116static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
Chris Lattner88641552010-09-22 00:34:38 +00004117 DebugLoc &DL, SelectionDAG &DAG) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00004118 EVT EltVT = VT.getVectorElementType();
4119 unsigned NumElems = Elts.size();
Michael J. Spencerec38de22010-10-10 22:04:20 +00004120
Nate Begemanfdea31a2010-03-24 20:49:50 +00004121 LoadSDNode *LDBase = NULL;
4122 unsigned LastLoadedElt = -1U;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004123
Nate Begeman1449f292010-03-24 22:19:06 +00004124 // For each element in the initializer, see if we've found a load or an undef.
Michael J. Spencerec38de22010-10-10 22:04:20 +00004125 // If we don't find an initial load element, or later load elements are
Nate Begeman1449f292010-03-24 22:19:06 +00004126 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004127 for (unsigned i = 0; i < NumElems; ++i) {
4128 SDValue Elt = Elts[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00004129
Nate Begemanfdea31a2010-03-24 20:49:50 +00004130 if (!Elt.getNode() ||
4131 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
4132 return SDValue();
4133 if (!LDBase) {
4134 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
4135 return SDValue();
4136 LDBase = cast<LoadSDNode>(Elt.getNode());
4137 LastLoadedElt = i;
4138 continue;
4139 }
4140 if (Elt.getOpcode() == ISD::UNDEF)
4141 continue;
4142
4143 LoadSDNode *LD = cast<LoadSDNode>(Elt);
4144 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
4145 return SDValue();
4146 LastLoadedElt = i;
4147 }
Nate Begeman1449f292010-03-24 22:19:06 +00004148
4149 // If we have found an entire vector of loads and undefs, then return a large
4150 // load of the entire vector width starting at the base pointer. If we found
4151 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004152 if (LastLoadedElt == NumElems - 1) {
4153 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
Chris Lattner88641552010-09-22 00:34:38 +00004154 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004155 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004156 LDBase->isVolatile(), LDBase->isNonTemporal(), 0);
Chris Lattner88641552010-09-22 00:34:38 +00004157 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004158 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004159 LDBase->isVolatile(), LDBase->isNonTemporal(),
4160 LDBase->getAlignment());
4161 } else if (NumElems == 4 && LastLoadedElt == 1) {
4162 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
4163 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
Chris Lattner88641552010-09-22 00:34:38 +00004164 SDValue ResNode = DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys,
4165 Ops, 2, MVT::i32,
4166 LDBase->getMemOperand());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004167 return DAG.getNode(ISD::BITCAST, DL, VT, ResNode);
Nate Begemanfdea31a2010-03-24 20:49:50 +00004168 }
4169 return SDValue();
4170}
4171
Evan Chengc3630942009-12-09 21:00:30 +00004172SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004173X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004174 DebugLoc dl = Op.getDebugLoc();
Chris Lattner6e80e442010-08-28 17:15:43 +00004175 // All zero's are handled with pxor in SSE2 and above, xorps in SSE1.
4176 // All one's are handled with pcmpeqd. In AVX, zero's are handled with
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004177 // vpxor in 128-bit and xor{pd,ps} in 256-bit, but no 256 version of pcmpeqd
4178 // is present, so AllOnes is ignored.
4179 if (ISD::isBuildVectorAllZeros(Op.getNode()) ||
4180 (Op.getValueType().getSizeInBits() != 256 &&
4181 ISD::isBuildVectorAllOnes(Op.getNode()))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004182 // Canonicalize this to <4 x i32> (SSE) to
Chris Lattner8a594482007-11-25 00:24:49 +00004183 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
4184 // eliminated on x86-32 hosts.
Dale Johannesen0488fb62010-09-30 23:57:10 +00004185 if (Op.getValueType() == MVT::v4i32)
Chris Lattner8a594482007-11-25 00:24:49 +00004186 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004187
Gabor Greifba36cb52008-08-28 21:40:38 +00004188 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00004189 return getOnesVector(Op.getValueType(), DAG, dl);
4190 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00004191 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004192
Owen Andersone50ed302009-08-10 22:56:29 +00004193 EVT VT = Op.getValueType();
4194 EVT ExtVT = VT.getVectorElementType();
4195 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004196
4197 unsigned NumElems = Op.getNumOperands();
4198 unsigned NumZero = 0;
4199 unsigned NumNonZero = 0;
4200 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004201 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00004202 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004203 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00004204 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00004205 if (Elt.getOpcode() == ISD::UNDEF)
4206 continue;
4207 Values.insert(Elt);
4208 if (Elt.getOpcode() != ISD::Constant &&
4209 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004210 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00004211 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00004212 NumZero++;
4213 else {
4214 NonZeros |= (1 << i);
4215 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004216 }
4217 }
4218
Chris Lattner97a2a562010-08-26 05:24:29 +00004219 // All undef vector. Return an UNDEF. All zero vectors were handled above.
4220 if (NumNonZero == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00004221 return DAG.getUNDEF(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004222
Chris Lattner67f453a2008-03-09 05:42:06 +00004223 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00004224 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004225 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00004226 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00004227
Chris Lattner62098042008-03-09 01:05:04 +00004228 // If this is an insertion of an i64 value on x86-32, and if the top bits of
4229 // the value are obviously zero, truncate the value to i32 and do the
4230 // insertion that way. Only do this if the value is non-constant or if the
4231 // value is a constant being inserted into element 0. It is cheaper to do
4232 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00004233 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00004234 (!IsAllConstants || Idx == 0)) {
4235 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004236 // Handle SSE only.
4237 assert(VT == MVT::v2i64 && "Expected an SSE value type!");
4238 EVT VecVT = MVT::v4i32;
4239 unsigned VecElts = 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00004240
Chris Lattner62098042008-03-09 01:05:04 +00004241 // Truncate the value (which may itself be a constant) to i32, and
4242 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00004243 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00004244 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00004245 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
4246 Subtarget->hasSSE2(), DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00004247
Chris Lattner62098042008-03-09 01:05:04 +00004248 // Now we have our 32-bit value zero extended in the low element of
4249 // a vector. If Idx != 0, swizzle it into place.
4250 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004251 SmallVector<int, 4> Mask;
4252 Mask.push_back(Idx);
4253 for (unsigned i = 1; i != VecElts; ++i)
4254 Mask.push_back(i);
4255 Item = DAG.getVectorShuffle(VecVT, dl, Item,
Eric Christopherfd179292009-08-27 18:07:15 +00004256 DAG.getUNDEF(Item.getValueType()),
Nate Begeman9008ca62009-04-27 18:41:29 +00004257 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00004258 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004259 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00004260 }
4261 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004262
Chris Lattner19f79692008-03-08 22:59:52 +00004263 // If we have a constant or non-constant insertion into the low element of
4264 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
4265 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00004266 // depending on what the source datatype is.
4267 if (Idx == 0) {
4268 if (NumZero == 0) {
4269 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Owen Anderson825b72b2009-08-11 20:47:22 +00004270 } else if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
4271 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Eli Friedman10415532009-06-06 06:05:10 +00004272 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
4273 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
4274 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget->hasSSE2(),
4275 DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00004276 } else if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
4277 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
Dale Johannesen0488fb62010-09-30 23:57:10 +00004278 assert(VT.getSizeInBits() == 128 && "Expected an SSE value type!");
4279 EVT MiddleVT = MVT::v4i32;
Eli Friedman10415532009-06-06 06:05:10 +00004280 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MiddleVT, Item);
4281 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
4282 Subtarget->hasSSE2(), DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004283 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Eli Friedman10415532009-06-06 06:05:10 +00004284 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004285 }
Evan Chengf26ffe92008-05-29 08:22:04 +00004286
4287 // Is it a vector logical left shift?
4288 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00004289 X86::isZeroNode(Op.getOperand(0)) &&
4290 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004291 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00004292 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00004293 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004294 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00004295 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004296 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004297
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004298 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00004299 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004300
Chris Lattner19f79692008-03-08 22:59:52 +00004301 // Otherwise, if this is a vector with i32 or f32 elements, and the element
4302 // is a non-constant being inserted into an element other than the low one,
4303 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
4304 // movd/movss) to move this into the low element, then shuffle it into
4305 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004306 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00004307 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00004308
Evan Cheng0db9fe62006-04-25 20:13:52 +00004309 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00004310 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
4311 Subtarget->hasSSE2(), DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00004312 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004313 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00004314 MaskVec.push_back(i == Idx ? 0 : 1);
4315 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004316 }
4317 }
4318
Chris Lattner67f453a2008-03-09 05:42:06 +00004319 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00004320 if (Values.size() == 1) {
4321 if (EVTBits == 32) {
4322 // Instead of a shuffle like this:
4323 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
4324 // Check if it's possible to issue this instead.
4325 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
4326 unsigned Idx = CountTrailingZeros_32(NonZeros);
4327 SDValue Item = Op.getOperand(Idx);
4328 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
4329 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
4330 }
Dan Gohman475871a2008-07-27 21:46:04 +00004331 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004332 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004333
Dan Gohmana3941172007-07-24 22:55:08 +00004334 // A vector full of immediates; various special cases are already
4335 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004336 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00004337 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00004338
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00004339 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00004340 if (EVTBits == 64) {
4341 if (NumNonZero == 1) {
4342 // One half is zero or undef.
4343 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00004344 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00004345 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00004346 return getShuffleVectorZeroOrUndef(V2, Idx, true,
4347 Subtarget->hasSSE2(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00004348 }
Dan Gohman475871a2008-07-27 21:46:04 +00004349 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00004350 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004351
4352 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00004353 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00004354 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00004355 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004356 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004357 }
4358
Bill Wendling826f36f2007-03-28 00:57:11 +00004359 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00004360 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Chris Lattner97a2a562010-08-26 05:24:29 +00004361 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004362 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004363 }
4364
4365 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00004366 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00004367 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004368 if (NumElems == 4 && NumZero > 0) {
4369 for (unsigned i = 0; i < 4; ++i) {
4370 bool isZero = !(NonZeros & (1 << i));
4371 if (isZero)
Dale Johannesenace16102009-02-03 19:33:06 +00004372 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004373 else
Dale Johannesenace16102009-02-03 19:33:06 +00004374 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004375 }
4376
4377 for (unsigned i = 0; i < 2; ++i) {
4378 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
4379 default: break;
4380 case 0:
4381 V[i] = V[i*2]; // Must be a zero vector.
4382 break;
4383 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00004384 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004385 break;
4386 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00004387 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004388 break;
4389 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00004390 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004391 break;
4392 }
4393 }
4394
Nate Begeman9008ca62009-04-27 18:41:29 +00004395 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004396 bool Reverse = (NonZeros & 0x3) == 2;
4397 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004398 MaskVec.push_back(Reverse ? 1-i : i);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004399 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
4400 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004401 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
4402 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004403 }
4404
Nate Begemanfdea31a2010-03-24 20:49:50 +00004405 if (Values.size() > 1 && VT.getSizeInBits() == 128) {
4406 // Check for a build vector of consecutive loads.
4407 for (unsigned i = 0; i < NumElems; ++i)
4408 V[i] = Op.getOperand(i);
Michael J. Spencerec38de22010-10-10 22:04:20 +00004409
Nate Begemanfdea31a2010-03-24 20:49:50 +00004410 // Check for elements which are consecutive loads.
4411 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
4412 if (LD.getNode())
4413 return LD;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004414
4415 // For SSE 4.1, use insertps to put the high elements into the low element.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004416 if (getSubtarget()->hasSSE41()) {
Chris Lattner24faf612010-08-28 17:59:08 +00004417 SDValue Result;
4418 if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
4419 Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
4420 else
4421 Result = DAG.getUNDEF(VT);
Michael J. Spencerec38de22010-10-10 22:04:20 +00004422
Chris Lattner24faf612010-08-28 17:59:08 +00004423 for (unsigned i = 1; i < NumElems; ++i) {
4424 if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
4425 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
Nate Begeman9008ca62009-04-27 18:41:29 +00004426 Op.getOperand(i), DAG.getIntPtrConstant(i));
Chris Lattner24faf612010-08-28 17:59:08 +00004427 }
4428 return Result;
Nate Begeman9008ca62009-04-27 18:41:29 +00004429 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00004430
Chris Lattner6e80e442010-08-28 17:15:43 +00004431 // Otherwise, expand into a number of unpckl*, start by extending each of
4432 // our (non-undef) elements to the full vector width with the element in the
4433 // bottom slot of the vector (which generates no code for SSE).
4434 for (unsigned i = 0; i < NumElems; ++i) {
4435 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
4436 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
4437 else
4438 V[i] = DAG.getUNDEF(VT);
4439 }
4440
4441 // Next, we iteratively mix elements, e.g. for v4f32:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004442 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
4443 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
4444 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Chris Lattner6e80e442010-08-28 17:15:43 +00004445 unsigned EltStride = NumElems >> 1;
4446 while (EltStride != 0) {
Chris Lattner3ddcc432010-08-28 17:28:30 +00004447 for (unsigned i = 0; i < EltStride; ++i) {
4448 // If V[i+EltStride] is undef and this is the first round of mixing,
4449 // then it is safe to just drop this shuffle: V[i] is already in the
4450 // right place, the one element (since it's the first round) being
4451 // inserted as undef can be dropped. This isn't safe for successive
4452 // rounds because they will permute elements within both vectors.
4453 if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
4454 EltStride == NumElems/2)
4455 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004456
Chris Lattner6e80e442010-08-28 17:15:43 +00004457 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
Chris Lattner3ddcc432010-08-28 17:28:30 +00004458 }
Chris Lattner6e80e442010-08-28 17:15:43 +00004459 EltStride >>= 1;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004460 }
4461 return V[0];
4462 }
Dan Gohman475871a2008-07-27 21:46:04 +00004463 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004464}
4465
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004466SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004467X86TargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const {
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004468 // We support concatenate two MMX registers and place them in a MMX
4469 // register. This is better than doing a stack convert.
4470 DebugLoc dl = Op.getDebugLoc();
4471 EVT ResVT = Op.getValueType();
4472 assert(Op.getNumOperands() == 2);
4473 assert(ResVT == MVT::v2i64 || ResVT == MVT::v4i32 ||
4474 ResVT == MVT::v8i16 || ResVT == MVT::v16i8);
4475 int Mask[2];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004476 SDValue InVec = DAG.getNode(ISD::BITCAST,dl, MVT::v1i64, Op.getOperand(0));
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004477 SDValue VecOp = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
4478 InVec = Op.getOperand(1);
4479 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4480 unsigned NumElts = ResVT.getVectorNumElements();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004481 VecOp = DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004482 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, ResVT, VecOp,
4483 InVec.getOperand(0), DAG.getIntPtrConstant(NumElts/2+1));
4484 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004485 InVec = DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, InVec);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004486 SDValue VecOp2 = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
4487 Mask[0] = 0; Mask[1] = 2;
4488 VecOp = DAG.getVectorShuffle(MVT::v2i64, dl, VecOp, VecOp2, Mask);
4489 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004490 return DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004491}
4492
Nate Begemanb9a47b82009-02-23 08:49:38 +00004493// v8i16 shuffles - Prefer shuffles in the following order:
4494// 1. [all] pshuflw, pshufhw, optional move
4495// 2. [ssse3] 1 x pshufb
4496// 3. [ssse3] 2 x pshufb + 1 x por
4497// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004498SDValue
4499X86TargetLowering::LowerVECTOR_SHUFFLEv8i16(SDValue Op,
4500 SelectionDAG &DAG) const {
4501 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00004502 SDValue V1 = SVOp->getOperand(0);
4503 SDValue V2 = SVOp->getOperand(1);
4504 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00004505 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00004506
Nate Begemanb9a47b82009-02-23 08:49:38 +00004507 // Determine if more than 1 of the words in each of the low and high quadwords
4508 // of the result come from the same quadword of one of the two inputs. Undef
4509 // mask values count as coming from any quadword, for better codegen.
4510 SmallVector<unsigned, 4> LoQuad(4);
4511 SmallVector<unsigned, 4> HiQuad(4);
4512 BitVector InputQuads(4);
4513 for (unsigned i = 0; i < 8; ++i) {
4514 SmallVectorImpl<unsigned> &Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00004515 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004516 MaskVals.push_back(EltIdx);
4517 if (EltIdx < 0) {
4518 ++Quad[0];
4519 ++Quad[1];
4520 ++Quad[2];
4521 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00004522 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004523 }
4524 ++Quad[EltIdx / 4];
4525 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00004526 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004527
Nate Begemanb9a47b82009-02-23 08:49:38 +00004528 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00004529 unsigned MaxQuad = 1;
4530 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004531 if (LoQuad[i] > MaxQuad) {
4532 BestLoQuad = i;
4533 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00004534 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004535 }
4536
Nate Begemanb9a47b82009-02-23 08:49:38 +00004537 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00004538 MaxQuad = 1;
4539 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004540 if (HiQuad[i] > MaxQuad) {
4541 BestHiQuad = i;
4542 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00004543 }
4544 }
4545
Nate Begemanb9a47b82009-02-23 08:49:38 +00004546 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00004547 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00004548 // single pshufb instruction is necessary. If There are more than 2 input
4549 // quads, disable the next transformation since it does not help SSSE3.
4550 bool V1Used = InputQuads[0] || InputQuads[1];
4551 bool V2Used = InputQuads[2] || InputQuads[3];
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004552 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004553 if (InputQuads.count() == 2 && V1Used && V2Used) {
4554 BestLoQuad = InputQuads.find_first();
4555 BestHiQuad = InputQuads.find_next(BestLoQuad);
4556 }
4557 if (InputQuads.count() > 2) {
4558 BestLoQuad = -1;
4559 BestHiQuad = -1;
4560 }
4561 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004562
Nate Begemanb9a47b82009-02-23 08:49:38 +00004563 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
4564 // the shuffle mask. If a quad is scored as -1, that means that it contains
4565 // words from all 4 input quadwords.
4566 SDValue NewV;
4567 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004568 SmallVector<int, 8> MaskV;
4569 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
4570 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
Eric Christopherfd179292009-08-27 18:07:15 +00004571 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004572 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V1),
4573 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V2), &MaskV[0]);
4574 NewV = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00004575
Nate Begemanb9a47b82009-02-23 08:49:38 +00004576 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
4577 // source words for the shuffle, to aid later transformations.
4578 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00004579 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00004580 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004581 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00004582 if (idx != (int)i)
4583 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004584 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00004585 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004586 AllWordsInNewV = false;
4587 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00004588 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004589
Nate Begemanb9a47b82009-02-23 08:49:38 +00004590 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
4591 if (AllWordsInNewV) {
4592 for (int i = 0; i != 8; ++i) {
4593 int idx = MaskVals[i];
4594 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00004595 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004596 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004597 if ((idx != i) && idx < 4)
4598 pshufhw = false;
4599 if ((idx != i) && idx > 3)
4600 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00004601 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00004602 V1 = NewV;
4603 V2Used = false;
4604 BestLoQuad = 0;
4605 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004606 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004607
Nate Begemanb9a47b82009-02-23 08:49:38 +00004608 // If we've eliminated the use of V2, and the new mask is a pshuflw or
4609 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00004610 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00004611 unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
4612 unsigned TargetMask = 0;
4613 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00004614 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00004615 TargetMask = pshufhw ? X86::getShufflePSHUFHWImmediate(NewV.getNode()):
4616 X86::getShufflePSHUFLWImmediate(NewV.getNode());
4617 V1 = NewV.getOperand(0);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00004618 return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
Evan Cheng14b32e12007-12-11 01:46:18 +00004619 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004620 }
Eric Christopherfd179292009-08-27 18:07:15 +00004621
Nate Begemanb9a47b82009-02-23 08:49:38 +00004622 // If we have SSSE3, and all words of the result are from 1 input vector,
4623 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
4624 // is present, fall back to case 4.
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004625 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004626 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00004627
Nate Begemanb9a47b82009-02-23 08:49:38 +00004628 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00004629 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00004630 // mask, and elements that come from V1 in the V2 mask, so that the two
4631 // results can be OR'd together.
4632 bool TwoInputs = V1Used && V2Used;
4633 for (unsigned i = 0; i != 8; ++i) {
4634 int EltIdx = MaskVals[i] * 2;
4635 if (TwoInputs && (EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004636 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4637 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004638 continue;
4639 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004640 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
4641 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004642 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004643 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004644 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00004645 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004646 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004647 if (!TwoInputs)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004648 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004649
Nate Begemanb9a47b82009-02-23 08:49:38 +00004650 // Calculate the shuffle mask for the second input, shuffle it, and
4651 // OR it with the first shuffled input.
4652 pshufbMask.clear();
4653 for (unsigned i = 0; i != 8; ++i) {
4654 int EltIdx = MaskVals[i] * 2;
4655 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004656 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4657 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004658 continue;
4659 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004660 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
4661 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004662 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004663 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00004664 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00004665 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004666 MVT::v16i8, &pshufbMask[0], 16));
4667 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004668 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004669 }
4670
4671 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
4672 // and update MaskVals with new element order.
4673 BitVector InOrder(8);
4674 if (BestLoQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004675 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004676 for (int i = 0; i != 4; ++i) {
4677 int idx = MaskVals[i];
4678 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004679 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004680 InOrder.set(i);
4681 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004682 MaskV.push_back(idx & 3);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004683 InOrder.set(i);
4684 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004685 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004686 }
4687 }
4688 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004689 MaskV.push_back(i);
Owen Anderson825b72b2009-08-11 20:47:22 +00004690 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00004691 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00004692
4693 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3())
4694 NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
4695 NewV.getOperand(0),
4696 X86::getShufflePSHUFLWImmediate(NewV.getNode()),
4697 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004698 }
Eric Christopherfd179292009-08-27 18:07:15 +00004699
Nate Begemanb9a47b82009-02-23 08:49:38 +00004700 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
4701 // and update MaskVals with the new element order.
4702 if (BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004703 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004704 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004705 MaskV.push_back(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004706 for (unsigned i = 4; i != 8; ++i) {
4707 int idx = MaskVals[i];
4708 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004709 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004710 InOrder.set(i);
4711 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004712 MaskV.push_back((idx & 3) + 4);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004713 InOrder.set(i);
4714 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004715 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004716 }
4717 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004718 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00004719 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00004720
4721 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3())
4722 NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
4723 NewV.getOperand(0),
4724 X86::getShufflePSHUFHWImmediate(NewV.getNode()),
4725 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004726 }
Eric Christopherfd179292009-08-27 18:07:15 +00004727
Nate Begemanb9a47b82009-02-23 08:49:38 +00004728 // In case BestHi & BestLo were both -1, which means each quadword has a word
4729 // from each of the four input quadwords, calculate the InOrder bitvector now
4730 // before falling through to the insert/extract cleanup.
4731 if (BestLoQuad == -1 && BestHiQuad == -1) {
4732 NewV = V1;
4733 for (int i = 0; i != 8; ++i)
4734 if (MaskVals[i] < 0 || MaskVals[i] == i)
4735 InOrder.set(i);
4736 }
Eric Christopherfd179292009-08-27 18:07:15 +00004737
Nate Begemanb9a47b82009-02-23 08:49:38 +00004738 // The other elements are put in the right place using pextrw and pinsrw.
4739 for (unsigned i = 0; i != 8; ++i) {
4740 if (InOrder[i])
4741 continue;
4742 int EltIdx = MaskVals[i];
4743 if (EltIdx < 0)
4744 continue;
4745 SDValue ExtOp = (EltIdx < 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00004746 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004747 DAG.getIntPtrConstant(EltIdx))
Owen Anderson825b72b2009-08-11 20:47:22 +00004748 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004749 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00004750 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004751 DAG.getIntPtrConstant(i));
4752 }
4753 return NewV;
4754}
4755
4756// v16i8 shuffles - Prefer shuffles in the following order:
4757// 1. [ssse3] 1 x pshufb
4758// 2. [ssse3] 2 x pshufb + 1 x por
4759// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
4760static
Nate Begeman9008ca62009-04-27 18:41:29 +00004761SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00004762 SelectionDAG &DAG,
4763 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004764 SDValue V1 = SVOp->getOperand(0);
4765 SDValue V2 = SVOp->getOperand(1);
4766 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00004767 SmallVector<int, 16> MaskVals;
Nate Begeman9008ca62009-04-27 18:41:29 +00004768 SVOp->getMask(MaskVals);
Eric Christopherfd179292009-08-27 18:07:15 +00004769
Nate Begemanb9a47b82009-02-23 08:49:38 +00004770 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00004771 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00004772 // present, fall back to case 3.
4773 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
4774 bool V1Only = true;
4775 bool V2Only = true;
4776 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004777 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00004778 if (EltIdx < 0)
4779 continue;
4780 if (EltIdx < 16)
4781 V2Only = false;
4782 else
4783 V1Only = false;
4784 }
Eric Christopherfd179292009-08-27 18:07:15 +00004785
Nate Begemanb9a47b82009-02-23 08:49:38 +00004786 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
4787 if (TLI.getSubtarget()->hasSSSE3()) {
4788 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00004789
Nate Begemanb9a47b82009-02-23 08:49:38 +00004790 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00004791 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00004792 //
4793 // Otherwise, we have elements from both input vectors, and must zero out
4794 // elements that come from V2 in the first mask, and V1 in the second mask
4795 // so that we can OR them together.
4796 bool TwoInputs = !(V1Only || V2Only);
4797 for (unsigned i = 0; i != 16; ++i) {
4798 int EltIdx = MaskVals[i];
4799 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004800 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004801 continue;
4802 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004803 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004804 }
4805 // If all the elements are from V2, assign it to V1 and return after
4806 // building the first pshufb.
4807 if (V2Only)
4808 V1 = V2;
Owen Anderson825b72b2009-08-11 20:47:22 +00004809 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00004810 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004811 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004812 if (!TwoInputs)
4813 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00004814
Nate Begemanb9a47b82009-02-23 08:49:38 +00004815 // Calculate the shuffle mask for the second input, shuffle it, and
4816 // OR it with the first shuffled input.
4817 pshufbMask.clear();
4818 for (unsigned i = 0; i != 16; ++i) {
4819 int EltIdx = MaskVals[i];
4820 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004821 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004822 continue;
4823 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004824 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004825 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004826 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
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));
4829 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004830 }
Eric Christopherfd179292009-08-27 18:07:15 +00004831
Nate Begemanb9a47b82009-02-23 08:49:38 +00004832 // No SSSE3 - Calculate in place words and then fix all out of place words
4833 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
4834 // the 16 different words that comprise the two doublequadword input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004835 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
4836 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004837 SDValue NewV = V2Only ? V2 : V1;
4838 for (int i = 0; i != 8; ++i) {
4839 int Elt0 = MaskVals[i*2];
4840 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00004841
Nate Begemanb9a47b82009-02-23 08:49:38 +00004842 // This word of the result is all undef, skip it.
4843 if (Elt0 < 0 && Elt1 < 0)
4844 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004845
Nate Begemanb9a47b82009-02-23 08:49:38 +00004846 // This word of the result is already in the correct place, skip it.
4847 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
4848 continue;
4849 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
4850 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004851
Nate Begemanb9a47b82009-02-23 08:49:38 +00004852 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
4853 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
4854 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00004855
4856 // If Elt0 and Elt1 are defined, are consecutive, and can be load
4857 // using a single extract together, load it and store it.
4858 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004859 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00004860 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00004861 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00004862 DAG.getIntPtrConstant(i));
4863 continue;
4864 }
4865
Nate Begemanb9a47b82009-02-23 08:49:38 +00004866 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00004867 // source byte is not also odd, shift the extracted word left 8 bits
4868 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00004869 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004870 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004871 DAG.getIntPtrConstant(Elt1 / 2));
4872 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004873 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004874 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00004875 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004876 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
4877 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004878 }
4879 // If Elt0 is defined, extract it from the appropriate source. If the
4880 // source byte is not also even, shift the extracted word right 8 bits. If
4881 // Elt1 was also defined, OR the extracted values together before
4882 // inserting them in the result.
4883 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004884 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004885 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
4886 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004887 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004888 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00004889 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004890 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
4891 DAG.getConstant(0x00FF, MVT::i16));
4892 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00004893 : InsElt0;
4894 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004895 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004896 DAG.getIntPtrConstant(i));
4897 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004898 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00004899}
4900
Evan Cheng7a831ce2007-12-15 03:00:47 +00004901/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00004902/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00004903/// done when every pair / quad of shuffle mask elements point to elements in
4904/// the right sequence. e.g.
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00004905/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
Evan Cheng14b32e12007-12-11 01:46:18 +00004906static
Nate Begeman9008ca62009-04-27 18:41:29 +00004907SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00004908 SelectionDAG &DAG, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00004909 EVT VT = SVOp->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00004910 SDValue V1 = SVOp->getOperand(0);
4911 SDValue V2 = SVOp->getOperand(1);
4912 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00004913 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00004914 EVT NewVT;
Owen Anderson825b72b2009-08-11 20:47:22 +00004915 switch (VT.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004916 default: assert(false && "Unexpected!");
Owen Anderson825b72b2009-08-11 20:47:22 +00004917 case MVT::v4f32: NewVT = MVT::v2f64; break;
4918 case MVT::v4i32: NewVT = MVT::v2i64; break;
4919 case MVT::v8i16: NewVT = MVT::v4i32; break;
4920 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00004921 }
4922
Nate Begeman9008ca62009-04-27 18:41:29 +00004923 int Scale = NumElems / NewWidth;
4924 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00004925 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004926 int StartIdx = -1;
4927 for (int j = 0; j < Scale; ++j) {
4928 int EltIdx = SVOp->getMaskElt(i+j);
4929 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00004930 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00004931 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00004932 StartIdx = EltIdx - (EltIdx % Scale);
4933 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00004934 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00004935 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004936 if (StartIdx == -1)
4937 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00004938 else
Nate Begeman9008ca62009-04-27 18:41:29 +00004939 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004940 }
4941
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004942 V1 = DAG.getNode(ISD::BITCAST, dl, NewVT, V1);
4943 V2 = DAG.getNode(ISD::BITCAST, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00004944 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004945}
4946
Evan Chengd880b972008-05-09 21:53:03 +00004947/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00004948///
Owen Andersone50ed302009-08-10 22:56:29 +00004949static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00004950 SDValue SrcOp, SelectionDAG &DAG,
4951 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004952 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004953 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00004954 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00004955 LD = dyn_cast<LoadSDNode>(SrcOp);
4956 if (!LD) {
4957 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
4958 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00004959 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Duncan Sandscdfad362010-11-03 12:17:33 +00004960 if ((ExtVT != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00004961 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004962 SrcOp.getOperand(0).getOpcode() == ISD::BITCAST &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00004963 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004964 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00004965 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004966 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004967 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
4968 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
4969 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00004970 SrcOp.getOperand(0)
4971 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00004972 }
4973 }
4974 }
4975
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004976 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004977 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004978 DAG.getNode(ISD::BITCAST, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00004979 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00004980}
4981
Evan Chengace3c172008-07-22 21:13:36 +00004982/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
4983/// shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00004984static SDValue
Nate Begeman9008ca62009-04-27 18:41:29 +00004985LowerVECTOR_SHUFFLE_4wide(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
4986 SDValue V1 = SVOp->getOperand(0);
4987 SDValue V2 = SVOp->getOperand(1);
4988 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00004989 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00004990
Evan Chengace3c172008-07-22 21:13:36 +00004991 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00004992 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00004993 SmallVector<int, 8> Mask1(4U, -1);
4994 SmallVector<int, 8> PermMask;
4995 SVOp->getMask(PermMask);
4996
Evan Chengace3c172008-07-22 21:13:36 +00004997 unsigned NumHi = 0;
4998 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00004999 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005000 int Idx = PermMask[i];
5001 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00005002 Locs[i] = std::make_pair(-1, -1);
5003 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005004 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
5005 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00005006 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00005007 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005008 NumLo++;
5009 } else {
5010 Locs[i] = std::make_pair(1, NumHi);
5011 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00005012 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005013 NumHi++;
5014 }
5015 }
5016 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005017
Evan Chengace3c172008-07-22 21:13:36 +00005018 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005019 // If no more than two elements come from either vector. This can be
5020 // implemented with two shuffles. First shuffle gather the elements.
5021 // The second shuffle, which takes the first shuffle as both of its
5022 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00005023 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005024
Nate Begeman9008ca62009-04-27 18:41:29 +00005025 SmallVector<int, 8> Mask2(4U, -1);
Eric Christopherfd179292009-08-27 18:07:15 +00005026
Evan Chengace3c172008-07-22 21:13:36 +00005027 for (unsigned i = 0; i != 4; ++i) {
5028 if (Locs[i].first == -1)
5029 continue;
5030 else {
5031 unsigned Idx = (i < 2) ? 0 : 4;
5032 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00005033 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005034 }
5035 }
5036
Nate Begeman9008ca62009-04-27 18:41:29 +00005037 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005038 } else if (NumLo == 3 || NumHi == 3) {
5039 // Otherwise, we must have three elements from one vector, call it X, and
5040 // one element from the other, call it Y. First, use a shufps to build an
5041 // intermediate vector with the one element from Y and the element from X
5042 // that will be in the same half in the final destination (the indexes don't
5043 // matter). Then, use a shufps to build the final vector, taking the half
5044 // containing the element from Y from the intermediate, and the other half
5045 // from X.
5046 if (NumHi == 3) {
5047 // Normalize it so the 3 elements come from V1.
Nate Begeman9008ca62009-04-27 18:41:29 +00005048 CommuteVectorShuffleMask(PermMask, VT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005049 std::swap(V1, V2);
5050 }
5051
5052 // Find the element from V2.
5053 unsigned HiIndex;
5054 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005055 int Val = PermMask[HiIndex];
5056 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005057 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005058 if (Val >= 4)
5059 break;
5060 }
5061
Nate Begeman9008ca62009-04-27 18:41:29 +00005062 Mask1[0] = PermMask[HiIndex];
5063 Mask1[1] = -1;
5064 Mask1[2] = PermMask[HiIndex^1];
5065 Mask1[3] = -1;
5066 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005067
5068 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005069 Mask1[0] = PermMask[0];
5070 Mask1[1] = PermMask[1];
5071 Mask1[2] = HiIndex & 1 ? 6 : 4;
5072 Mask1[3] = HiIndex & 1 ? 4 : 6;
5073 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005074 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005075 Mask1[0] = HiIndex & 1 ? 2 : 0;
5076 Mask1[1] = HiIndex & 1 ? 0 : 2;
5077 Mask1[2] = PermMask[2];
5078 Mask1[3] = PermMask[3];
5079 if (Mask1[2] >= 0)
5080 Mask1[2] += 4;
5081 if (Mask1[3] >= 0)
5082 Mask1[3] += 4;
5083 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005084 }
Evan Chengace3c172008-07-22 21:13:36 +00005085 }
5086
5087 // Break it into (shuffle shuffle_hi, shuffle_lo).
5088 Locs.clear();
Nate Begeman9008ca62009-04-27 18:41:29 +00005089 SmallVector<int,8> LoMask(4U, -1);
5090 SmallVector<int,8> HiMask(4U, -1);
5091
5092 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00005093 unsigned MaskIdx = 0;
5094 unsigned LoIdx = 0;
5095 unsigned HiIdx = 2;
5096 for (unsigned i = 0; i != 4; ++i) {
5097 if (i == 2) {
5098 MaskPtr = &HiMask;
5099 MaskIdx = 1;
5100 LoIdx = 0;
5101 HiIdx = 2;
5102 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005103 int Idx = PermMask[i];
5104 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00005105 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00005106 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00005107 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00005108 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005109 LoIdx++;
5110 } else {
5111 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00005112 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005113 HiIdx++;
5114 }
5115 }
5116
Nate Begeman9008ca62009-04-27 18:41:29 +00005117 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
5118 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
5119 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00005120 for (unsigned i = 0; i != 4; ++i) {
5121 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005122 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00005123 } else {
5124 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00005125 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00005126 }
5127 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005128 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00005129}
5130
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005131static bool MayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005132 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005133 V = V.getOperand(0);
5134 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
5135 V = V.getOperand(0);
5136 if (MayFoldLoad(V))
5137 return true;
5138 return false;
5139}
5140
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005141// FIXME: the version above should always be used. Since there's
5142// a bug where several vector shuffles can't be folded because the
5143// DAG is not updated during lowering and a node claims to have two
5144// uses while it only has one, use this version, and let isel match
5145// another instruction if the load really happens to have more than
5146// one use. Remove this version after this bug get fixed.
Evan Cheng835580f2010-10-07 20:50:20 +00005147// rdar://8434668, PR8156
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005148static bool RelaxedMayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005149 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005150 V = V.getOperand(0);
5151 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
5152 V = V.getOperand(0);
5153 if (ISD::isNormalLoad(V.getNode()))
5154 return true;
5155 return false;
5156}
5157
5158/// CanFoldShuffleIntoVExtract - Check if the current shuffle is used by
5159/// a vector extract, and if both can be later optimized into a single load.
5160/// This is done in visitEXTRACT_VECTOR_ELT and the conditions are checked
5161/// here because otherwise a target specific shuffle node is going to be
5162/// emitted for this shuffle, and the optimization not done.
5163/// FIXME: This is probably not the best approach, but fix the problem
5164/// until the right path is decided.
5165static
5166bool CanXFormVExtractWithShuffleIntoLoad(SDValue V, SelectionDAG &DAG,
5167 const TargetLowering &TLI) {
5168 EVT VT = V.getValueType();
5169 ShuffleVectorSDNode *SVOp = dyn_cast<ShuffleVectorSDNode>(V);
5170
5171 // Be sure that the vector shuffle is present in a pattern like this:
5172 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), c) -> (f32 load $addr)
5173 if (!V.hasOneUse())
5174 return false;
5175
5176 SDNode *N = *V.getNode()->use_begin();
5177 if (N->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
5178 return false;
5179
5180 SDValue EltNo = N->getOperand(1);
5181 if (!isa<ConstantSDNode>(EltNo))
5182 return false;
5183
5184 // If the bit convert changed the number of elements, it is unsafe
5185 // to examine the mask.
5186 bool HasShuffleIntoBitcast = false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005187 if (V.getOpcode() == ISD::BITCAST) {
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005188 EVT SrcVT = V.getOperand(0).getValueType();
5189 if (SrcVT.getVectorNumElements() != VT.getVectorNumElements())
5190 return false;
5191 V = V.getOperand(0);
5192 HasShuffleIntoBitcast = true;
5193 }
5194
5195 // Select the input vector, guarding against out of range extract vector.
5196 unsigned NumElems = VT.getVectorNumElements();
5197 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
5198 int Idx = (Elt > NumElems) ? -1 : SVOp->getMaskElt(Elt);
5199 V = (Idx < (int)NumElems) ? V.getOperand(0) : V.getOperand(1);
5200
5201 // Skip one more bit_convert if necessary
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005202 if (V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005203 V = V.getOperand(0);
5204
5205 if (ISD::isNormalLoad(V.getNode())) {
5206 // Is the original load suitable?
5207 LoadSDNode *LN0 = cast<LoadSDNode>(V);
5208
5209 // FIXME: avoid the multi-use bug that is preventing lots of
5210 // of foldings to be detected, this is still wrong of course, but
5211 // give the temporary desired behavior, and if it happens that
5212 // the load has real more uses, during isel it will not fold, and
5213 // will generate poor code.
5214 if (!LN0 || LN0->isVolatile()) // || !LN0->hasOneUse()
5215 return false;
5216
5217 if (!HasShuffleIntoBitcast)
5218 return true;
5219
5220 // If there's a bitcast before the shuffle, check if the load type and
5221 // alignment is valid.
5222 unsigned Align = LN0->getAlignment();
5223 unsigned NewAlign =
5224 TLI.getTargetData()->getABITypeAlignment(
5225 VT.getTypeForEVT(*DAG.getContext()));
5226
5227 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
5228 return false;
5229 }
5230
5231 return true;
5232}
5233
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005234static
Evan Cheng835580f2010-10-07 20:50:20 +00005235SDValue getMOVDDup(SDValue &Op, DebugLoc &dl, SDValue V1, SelectionDAG &DAG) {
5236 EVT VT = Op.getValueType();
5237
5238 // Canonizalize to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005239 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1);
5240 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng835580f2010-10-07 20:50:20 +00005241 getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64,
5242 V1, DAG));
5243}
5244
5245static
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005246SDValue getMOVLowToHigh(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG,
5247 bool HasSSE2) {
5248 SDValue V1 = Op.getOperand(0);
5249 SDValue V2 = Op.getOperand(1);
5250 EVT VT = Op.getValueType();
5251
5252 assert(VT != MVT::v2i64 && "unsupported shuffle type");
5253
5254 if (HasSSE2 && VT == MVT::v2f64)
5255 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
5256
5257 // v4f32 or v4i32
5258 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V2, DAG);
5259}
5260
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00005261static
5262SDValue getMOVHighToLow(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG) {
5263 SDValue V1 = Op.getOperand(0);
5264 SDValue V2 = Op.getOperand(1);
5265 EVT VT = Op.getValueType();
5266
5267 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
5268 "unsupported shuffle type");
5269
5270 if (V2.getOpcode() == ISD::UNDEF)
5271 V2 = V1;
5272
5273 // v4i32 or v4f32
5274 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
5275}
5276
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005277static
5278SDValue getMOVLP(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG, bool HasSSE2) {
5279 SDValue V1 = Op.getOperand(0);
5280 SDValue V2 = Op.getOperand(1);
5281 EVT VT = Op.getValueType();
5282 unsigned NumElems = VT.getVectorNumElements();
5283
5284 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
5285 // operand of these instructions is only memory, so check if there's a
5286 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
5287 // same masks.
5288 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005289
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00005290 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005291 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005292 CanFoldLoad = true;
5293
5294 // When V1 is a load, it can be folded later into a store in isel, example:
5295 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
5296 // turns into:
5297 // (MOVLPSmr addr:$src1, VR128:$src2)
5298 // So, recognize this potential and also use MOVLPS or MOVLPD
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005299 if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005300 CanFoldLoad = true;
5301
5302 if (CanFoldLoad) {
5303 if (HasSSE2 && NumElems == 2)
5304 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
5305
5306 if (NumElems == 4)
5307 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
5308 }
5309
5310 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5311 // movl and movlp will both match v2i64, but v2i64 is never matched by
5312 // movl earlier because we make it strict to avoid messing with the movlp load
5313 // folding logic (see the code above getMOVLP call). Match it here then,
5314 // this is horrible, but will stay like this until we move all shuffle
5315 // matching to x86 specific nodes. Note that for the 1st condition all
5316 // types are matched with movsd.
5317 if ((HasSSE2 && NumElems == 2) || !X86::isMOVLMask(SVOp))
5318 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
5319 else if (HasSSE2)
5320 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
5321
5322
5323 assert(VT != MVT::v4i32 && "unsupported shuffle type");
5324
5325 // Invert the operand order and use SHUFPS to match it.
5326 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V2, V1,
5327 X86::getShuffleSHUFImmediate(SVOp), DAG);
5328}
5329
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005330static inline unsigned getUNPCKLOpcode(EVT VT) {
5331 switch(VT.getSimpleVT().SimpleTy) {
5332 case MVT::v4i32: return X86ISD::PUNPCKLDQ;
5333 case MVT::v2i64: return X86ISD::PUNPCKLQDQ;
5334 case MVT::v4f32: return X86ISD::UNPCKLPS;
5335 case MVT::v2f64: return X86ISD::UNPCKLPD;
5336 case MVT::v16i8: return X86ISD::PUNPCKLBW;
5337 case MVT::v8i16: return X86ISD::PUNPCKLWD;
5338 default:
5339 llvm_unreachable("Unknow type for unpckl");
5340 }
5341 return 0;
5342}
5343
5344static inline unsigned getUNPCKHOpcode(EVT VT) {
5345 switch(VT.getSimpleVT().SimpleTy) {
5346 case MVT::v4i32: return X86ISD::PUNPCKHDQ;
5347 case MVT::v2i64: return X86ISD::PUNPCKHQDQ;
5348 case MVT::v4f32: return X86ISD::UNPCKHPS;
5349 case MVT::v2f64: return X86ISD::UNPCKHPD;
5350 case MVT::v16i8: return X86ISD::PUNPCKHBW;
5351 case MVT::v8i16: return X86ISD::PUNPCKHWD;
5352 default:
5353 llvm_unreachable("Unknow type for unpckh");
5354 }
5355 return 0;
5356}
5357
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005358static
5359SDValue NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG,
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005360 const TargetLowering &TLI,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005361 const X86Subtarget *Subtarget) {
5362 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5363 EVT VT = Op.getValueType();
5364 DebugLoc dl = Op.getDebugLoc();
5365 SDValue V1 = Op.getOperand(0);
5366 SDValue V2 = Op.getOperand(1);
5367
5368 if (isZeroShuffle(SVOp))
5369 return getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
5370
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005371 // Handle splat operations
5372 if (SVOp->isSplat()) {
5373 // Special case, this is the only place now where it's
5374 // allowed to return a vector_shuffle operation without
5375 // using a target specific node, because *hopefully* it
5376 // will be optimized away by the dag combiner.
5377 if (VT.getVectorNumElements() <= 4 &&
5378 CanXFormVExtractWithShuffleIntoLoad(Op, DAG, TLI))
5379 return Op;
5380
5381 // Handle splats by matching through known masks
5382 if (VT.getVectorNumElements() <= 4)
5383 return SDValue();
5384
Evan Cheng835580f2010-10-07 20:50:20 +00005385 // Canonicalize all of the remaining to v4f32.
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005386 return PromoteSplat(SVOp, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005387 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005388
5389 // If the shuffle can be profitably rewritten as a narrower shuffle, then
5390 // do it!
5391 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
5392 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5393 if (NewOp.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005394 return DAG.getNode(ISD::BITCAST, dl, VT, NewOp);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005395 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
5396 // FIXME: Figure out a cleaner way to do this.
5397 // Try to make use of movq to zero out the top part.
5398 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
5399 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5400 if (NewOp.getNode()) {
5401 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
5402 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
5403 DAG, Subtarget, dl);
5404 }
5405 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
5406 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5407 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
5408 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
5409 DAG, Subtarget, dl);
5410 }
5411 }
5412 return SDValue();
5413}
5414
Dan Gohman475871a2008-07-27 21:46:04 +00005415SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005416X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00005417 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00005418 SDValue V1 = Op.getOperand(0);
5419 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00005420 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005421 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00005422 unsigned NumElems = VT.getVectorNumElements();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005423 bool isMMX = VT.getSizeInBits() == 64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005424 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
5425 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00005426 bool V1IsSplat = false;
5427 bool V2IsSplat = false;
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005428 bool HasSSE2 = Subtarget->hasSSE2() || Subtarget->hasAVX();
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00005429 bool HasSSE3 = Subtarget->hasSSE3() || Subtarget->hasAVX();
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00005430 bool HasSSSE3 = Subtarget->hasSSSE3() || Subtarget->hasAVX();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005431 MachineFunction &MF = DAG.getMachineFunction();
5432 bool OptForSize = MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005433
Dale Johannesen0488fb62010-09-30 23:57:10 +00005434 // Shuffle operations on MMX not supported.
5435 if (isMMX)
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00005436 return Op;
5437
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005438 // Vector shuffle lowering takes 3 steps:
5439 //
5440 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
5441 // narrowing and commutation of operands should be handled.
5442 // 2) Matching of shuffles with known shuffle masks to x86 target specific
5443 // shuffle nodes.
5444 // 3) Rewriting of unmatched masks into new generic shuffle operations,
5445 // so the shuffle can be broken into other shuffles and the legalizer can
5446 // try the lowering again.
5447 //
5448 // The general ideia is that no vector_shuffle operation should be left to
5449 // be matched during isel, all of them must be converted to a target specific
5450 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00005451
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005452 // Normalize the input vectors. Here splats, zeroed vectors, profitable
5453 // narrowing and commutation of operands should be handled. The actual code
5454 // doesn't include all of those, work in progress...
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005455 SDValue NewOp = NormalizeVectorShuffle(Op, DAG, *this, Subtarget);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005456 if (NewOp.getNode())
5457 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00005458
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005459 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
5460 // unpckh_undef). Only use pshufd if speed is more important than size.
5461 if (OptForSize && X86::isUNPCKL_v_undef_Mask(SVOp))
5462 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5463 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V1, DAG);
5464 if (OptForSize && X86::isUNPCKH_v_undef_Mask(SVOp))
5465 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5466 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00005467
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005468 if (X86::isMOVDDUPMask(SVOp) && HasSSE3 && V2IsUndef &&
Dale Johannesen0488fb62010-09-30 23:57:10 +00005469 RelaxedMayFoldVectorLoad(V1))
Evan Cheng835580f2010-10-07 20:50:20 +00005470 return getMOVDDup(Op, dl, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005471
Dale Johannesen0488fb62010-09-30 23:57:10 +00005472 if (X86::isMOVHLPS_v_undef_Mask(SVOp))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005473 return getMOVHighToLow(Op, dl, DAG);
5474
5475 // Use to match splats
5476 if (HasSSE2 && X86::isUNPCKHMask(SVOp) && V2IsUndef &&
5477 (VT == MVT::v2f64 || VT == MVT::v2i64))
5478 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
5479
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005480 if (X86::isPSHUFDMask(SVOp)) {
5481 // The actual implementation will match the mask in the if above and then
5482 // during isel it can match several different instructions, not only pshufd
5483 // as its name says, sad but true, emulate the behavior for now...
5484 if (X86::isMOVDDUPMask(SVOp) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
5485 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
5486
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005487 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
5488
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005489 if (HasSSE2 && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005490 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
5491
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005492 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005493 return getTargetShuffleNode(X86ISD::SHUFPD, dl, VT, V1, V1,
5494 TargetMask, DAG);
5495
5496 if (VT == MVT::v4f32)
5497 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V1, V1,
5498 TargetMask, DAG);
5499 }
Eric Christopherfd179292009-08-27 18:07:15 +00005500
Evan Chengf26ffe92008-05-29 08:22:04 +00005501 // Check if this can be converted into a logical shift.
5502 bool isLeft = false;
5503 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00005504 SDValue ShVal;
Nate Begeman9008ca62009-04-27 18:41:29 +00005505 bool isShift = getSubtarget()->hasSSE2() &&
Evan Chengc3630942009-12-09 21:00:30 +00005506 isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00005507 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00005508 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00005509 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00005510 EVT EltVT = VT.getVectorElementType();
5511 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00005512 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005513 }
Eric Christopherfd179292009-08-27 18:07:15 +00005514
Nate Begeman9008ca62009-04-27 18:41:29 +00005515 if (X86::isMOVLMask(SVOp)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005516 if (V1IsUndef)
5517 return V2;
Gabor Greifba36cb52008-08-28 21:40:38 +00005518 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00005519 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Dale Johannesen0488fb62010-09-30 23:57:10 +00005520 if (!X86::isMOVLPMask(SVOp)) {
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005521 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00005522 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
5523
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005524 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00005525 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
5526 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00005527 }
Eric Christopherfd179292009-08-27 18:07:15 +00005528
Nate Begeman9008ca62009-04-27 18:41:29 +00005529 // FIXME: fold these into legal mask.
Dale Johannesen0488fb62010-09-30 23:57:10 +00005530 if (X86::isMOVLHPSMask(SVOp) && !X86::isUNPCKLMask(SVOp))
5531 return getMOVLowToHigh(Op, dl, DAG, HasSSE2);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005532
Dale Johannesen0488fb62010-09-30 23:57:10 +00005533 if (X86::isMOVHLPSMask(SVOp))
5534 return getMOVHighToLow(Op, dl, DAG);
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00005535
Dale Johannesen0488fb62010-09-30 23:57:10 +00005536 if (X86::isMOVSHDUPMask(SVOp) && HasSSE3 && V2IsUndef && NumElems == 4)
5537 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00005538
Dale Johannesen0488fb62010-09-30 23:57:10 +00005539 if (X86::isMOVSLDUPMask(SVOp) && HasSSE3 && V2IsUndef && NumElems == 4)
5540 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00005541
Dale Johannesen0488fb62010-09-30 23:57:10 +00005542 if (X86::isMOVLPMask(SVOp))
5543 return getMOVLP(Op, dl, DAG, HasSSE2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005544
Nate Begeman9008ca62009-04-27 18:41:29 +00005545 if (ShouldXformToMOVHLPS(SVOp) ||
5546 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
5547 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005548
Evan Chengf26ffe92008-05-29 08:22:04 +00005549 if (isShift) {
5550 // No better options. Use a vshl / vsrl.
Dan Gohman8a55ce42009-09-23 21:02:20 +00005551 EVT EltVT = VT.getVectorElementType();
5552 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00005553 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005554 }
Eric Christopherfd179292009-08-27 18:07:15 +00005555
Evan Cheng9eca5e82006-10-25 21:49:50 +00005556 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00005557 // FIXME: This should also accept a bitcast of a splat? Be careful, not
5558 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00005559 V1IsSplat = isSplatVector(V1.getNode());
5560 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00005561
Chris Lattner8a594482007-11-25 00:24:49 +00005562 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00005563 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005564 Op = CommuteVectorShuffle(SVOp, DAG);
5565 SVOp = cast<ShuffleVectorSDNode>(Op);
5566 V1 = SVOp->getOperand(0);
5567 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00005568 std::swap(V1IsSplat, V2IsSplat);
5569 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00005570 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00005571 }
5572
Nate Begeman9008ca62009-04-27 18:41:29 +00005573 if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) {
5574 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00005575 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00005576 return V1;
5577 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
5578 // the instruction selector will not match, so get a canonical MOVL with
5579 // swapped operands to undo the commute.
5580 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00005581 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005582
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005583 if (X86::isUNPCKLMask(SVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005584 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005585
5586 if (X86::isUNPCKHMask(SVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005587 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V2, DAG);
Evan Chenge1113032006-10-04 18:33:38 +00005588
Evan Cheng9bbbb982006-10-25 20:48:19 +00005589 if (V2IsSplat) {
5590 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005591 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00005592 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00005593 SDValue NewMask = NormalizeMask(SVOp, DAG);
5594 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
5595 if (NSVOp != SVOp) {
5596 if (X86::isUNPCKLMask(NSVOp, true)) {
5597 return NewMask;
5598 } else if (X86::isUNPCKHMask(NSVOp, true)) {
5599 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005600 }
5601 }
5602 }
5603
Evan Cheng9eca5e82006-10-25 21:49:50 +00005604 if (Commuted) {
5605 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00005606 // FIXME: this seems wrong.
5607 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
5608 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005609
5610 if (X86::isUNPCKLMask(NewSVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005611 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V2, V1, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005612
5613 if (X86::isUNPCKHMask(NewSVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005614 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V2, V1, DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00005615 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005616
Nate Begeman9008ca62009-04-27 18:41:29 +00005617 // Normalize the node to match x86 shuffle ops if needed
Dale Johannesen0488fb62010-09-30 23:57:10 +00005618 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp))
Nate Begeman9008ca62009-04-27 18:41:29 +00005619 return CommuteVectorShuffle(SVOp, DAG);
5620
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00005621 // The checks below are all present in isShuffleMaskLegal, but they are
5622 // inlined here right now to enable us to directly emit target specific
5623 // nodes, and remove one by one until they don't return Op anymore.
5624 SmallVector<int, 16> M;
5625 SVOp->getMask(M);
5626
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00005627 if (isPALIGNRMask(M, VT, HasSSSE3))
5628 return getTargetShuffleNode(X86ISD::PALIGN, dl, VT, V1, V2,
5629 X86::getShufflePALIGNRImmediate(SVOp),
5630 DAG);
5631
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00005632 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
5633 SVOp->getSplatIndex() == 0 && V2IsUndef) {
5634 if (VT == MVT::v2f64)
5635 return getTargetShuffleNode(X86ISD::UNPCKLPD, dl, VT, V1, V1, DAG);
5636 if (VT == MVT::v2i64)
5637 return getTargetShuffleNode(X86ISD::PUNPCKLQDQ, dl, VT, V1, V1, DAG);
5638 }
5639
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00005640 if (isPSHUFHWMask(M, VT))
5641 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
5642 X86::getShufflePSHUFHWImmediate(SVOp),
5643 DAG);
5644
5645 if (isPSHUFLWMask(M, VT))
5646 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
5647 X86::getShufflePSHUFLWImmediate(SVOp),
5648 DAG);
5649
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00005650 if (isSHUFPMask(M, VT)) {
5651 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
5652 if (VT == MVT::v4f32 || VT == MVT::v4i32)
5653 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V1, V2,
5654 TargetMask, DAG);
5655 if (VT == MVT::v2f64 || VT == MVT::v2i64)
5656 return getTargetShuffleNode(X86ISD::SHUFPD, dl, VT, V1, V2,
5657 TargetMask, DAG);
5658 }
5659
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005660 if (X86::isUNPCKL_v_undef_Mask(SVOp))
5661 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5662 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V1, DAG);
5663 if (X86::isUNPCKH_v_undef_Mask(SVOp))
5664 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5665 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
5666
Evan Cheng14b32e12007-12-11 01:46:18 +00005667 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
Owen Anderson825b72b2009-08-11 20:47:22 +00005668 if (VT == MVT::v8i16) {
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005669 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00005670 if (NewOp.getNode())
Evan Cheng14b32e12007-12-11 01:46:18 +00005671 return NewOp;
5672 }
5673
Owen Anderson825b72b2009-08-11 20:47:22 +00005674 if (VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005675 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005676 if (NewOp.getNode())
5677 return NewOp;
5678 }
Eric Christopherfd179292009-08-27 18:07:15 +00005679
Dale Johannesen0488fb62010-09-30 23:57:10 +00005680 // Handle all 4 wide cases with a number of shuffles.
5681 if (NumElems == 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00005682 return LowerVECTOR_SHUFFLE_4wide(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005683
Dan Gohman475871a2008-07-27 21:46:04 +00005684 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005685}
5686
Dan Gohman475871a2008-07-27 21:46:04 +00005687SDValue
5688X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00005689 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005690 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005691 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005692 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005693 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005694 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00005695 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005696 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00005697 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005698 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00005699 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
5700 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
5701 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005702 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
5703 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005704 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005705 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00005706 Op.getOperand(0)),
5707 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00005708 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005709 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00005710 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005711 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00005712 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Owen Anderson825b72b2009-08-11 20:47:22 +00005713 } else if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00005714 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
5715 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00005716 // result has a single use which is a store or a bitcast to i32. And in
5717 // the case of a store, it's not worth it if the index is a constant 0,
5718 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00005719 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00005720 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00005721 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00005722 if ((User->getOpcode() != ISD::STORE ||
5723 (isa<ConstantSDNode>(Op.getOperand(1)) &&
5724 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005725 (User->getOpcode() != ISD::BITCAST ||
Owen Anderson825b72b2009-08-11 20:47:22 +00005726 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00005727 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00005728 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005729 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00005730 Op.getOperand(0)),
5731 Op.getOperand(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005732 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Extract);
Owen Anderson825b72b2009-08-11 20:47:22 +00005733 } else if (VT == MVT::i32) {
Mon P Wangf0fcdd82009-01-15 21:10:20 +00005734 // ExtractPS works with constant index.
5735 if (isa<ConstantSDNode>(Op.getOperand(1)))
5736 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00005737 }
Dan Gohman475871a2008-07-27 21:46:04 +00005738 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00005739}
5740
5741
Dan Gohman475871a2008-07-27 21:46:04 +00005742SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005743X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
5744 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005745 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00005746 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005747
Evan Cheng62a3f152008-03-24 21:52:23 +00005748 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005749 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00005750 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00005751 return Res;
5752 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00005753
Owen Andersone50ed302009-08-10 22:56:29 +00005754 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005755 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005756 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005757 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00005758 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005759 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00005760 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005761 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
5762 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005763 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005764 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00005765 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005766 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00005767 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00005768 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00005769 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00005770 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00005771 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00005772 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005773 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005774 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005775 if (Idx == 0)
5776 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00005777
Evan Cheng0db9fe62006-04-25 20:13:52 +00005778 // SHUFPS the element to the lowest double word, then movss.
Nate Begeman9008ca62009-04-27 18:41:29 +00005779 int Mask[4] = { Idx, -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00005780 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00005781 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00005782 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00005783 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00005784 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00005785 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00005786 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
5787 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
5788 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005789 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005790 if (Idx == 0)
5791 return Op;
5792
5793 // UNPCKHPD the element to the lowest double word, then movsd.
5794 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
5795 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00005796 int Mask[2] = { 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));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005802 }
5803
Dan Gohman475871a2008-07-27 21:46:04 +00005804 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005805}
5806
Dan Gohman475871a2008-07-27 21:46:04 +00005807SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005808X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op,
5809 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005810 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00005811 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005812 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00005813
Dan Gohman475871a2008-07-27 21:46:04 +00005814 SDValue N0 = Op.getOperand(0);
5815 SDValue N1 = Op.getOperand(1);
5816 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00005817
Dan Gohman8a55ce42009-09-23 21:02:20 +00005818 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00005819 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00005820 unsigned Opc;
5821 if (VT == MVT::v8i16)
5822 Opc = X86ISD::PINSRW;
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00005823 else if (VT == MVT::v16i8)
5824 Opc = X86ISD::PINSRB;
5825 else
5826 Opc = X86ISD::PINSRB;
5827
Nate Begeman14d12ca2008-02-11 04:19:36 +00005828 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
5829 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00005830 if (N1.getValueType() != MVT::i32)
5831 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
5832 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005833 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00005834 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00005835 } else if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00005836 // Bits [7:6] of the constant are the source select. This will always be
5837 // zero here. The DAG Combiner may combine an extract_elt index into these
5838 // bits. For example (insert (extract, 3), 2) could be matched by putting
5839 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00005840 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00005841 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00005842 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00005843 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005844 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00005845 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00005846 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00005847 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00005848 } else if (EltVT == MVT::i32 && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00005849 // PINSR* works with constant index.
5850 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00005851 }
Dan Gohman475871a2008-07-27 21:46:04 +00005852 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00005853}
5854
Dan Gohman475871a2008-07-27 21:46:04 +00005855SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005856X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005857 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00005858 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00005859
5860 if (Subtarget->hasSSE41())
5861 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
5862
Dan Gohman8a55ce42009-09-23 21:02:20 +00005863 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00005864 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00005865
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005866 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00005867 SDValue N0 = Op.getOperand(0);
5868 SDValue N1 = Op.getOperand(1);
5869 SDValue N2 = Op.getOperand(2);
Evan Cheng794405e2007-12-12 07:55:34 +00005870
Dan Gohman8a55ce42009-09-23 21:02:20 +00005871 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00005872 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
5873 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00005874 if (N1.getValueType() != MVT::i32)
5875 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
5876 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005877 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesen0488fb62010-09-30 23:57:10 +00005878 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005879 }
Dan Gohman475871a2008-07-27 21:46:04 +00005880 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005881}
5882
Dan Gohman475871a2008-07-27 21:46:04 +00005883SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005884X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005885 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00005886
Chris Lattnerf172ecd2010-07-04 23:07:25 +00005887 if (Op.getValueType() == MVT::v1i64 &&
5888 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00005889 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00005890
Owen Anderson825b72b2009-08-11 20:47:22 +00005891 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Dale Johannesen0488fb62010-09-30 23:57:10 +00005892 assert(Op.getValueType().getSimpleVT().getSizeInBits() == 128 &&
5893 "Expected an SSE type!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005894 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(),
Dale Johannesen0488fb62010-09-30 23:57:10 +00005895 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005896}
5897
David Greene91585092011-01-26 15:38:49 +00005898// Lower a node with an EXTRACT_SUBVECTOR opcode. This may result in
5899// a simple subregister reference or explicit instructions to grab
5900// upper bits of a vector.
5901SDValue
5902X86TargetLowering::LowerEXTRACT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
5903 if (Subtarget->hasAVX()) {
5904 // TODO
5905 }
5906 return SDValue();
5907}
5908
David Greenecfe33c42011-01-26 19:13:22 +00005909// Lower a node with an INSERT_SUBVECTOR opcode. This may result in a
5910// simple superregister reference or explicit instructions to insert
5911// the upper bits of a vector.
5912SDValue
5913X86TargetLowering::LowerINSERT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
5914 if (Subtarget->hasAVX()) {
5915 DebugLoc dl = Op.getNode()->getDebugLoc();
5916 SDValue Vec = Op.getNode()->getOperand(0);
5917 SDValue SubVec = Op.getNode()->getOperand(1);
5918 SDValue Idx = Op.getNode()->getOperand(2);
5919
5920 if (Op.getNode()->getValueType(0).getSizeInBits() == 256
5921 && SubVec.getNode()->getValueType(0).getSizeInBits() == 128) {
5922 // TODO
5923 }
5924 }
5925 return SDValue();
5926}
5927
Bill Wendling056292f2008-09-16 21:48:12 +00005928// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
5929// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
5930// one of the above mentioned nodes. It has to be wrapped because otherwise
5931// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
5932// be used to form addressing mode. These wrapped nodes will be selected
5933// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00005934SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005935X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005936 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00005937
Chris Lattner41621a22009-06-26 19:22:52 +00005938 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
5939 // global base reg.
5940 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00005941 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005942 CodeModel::Model M = getTargetMachine().getCodeModel();
5943
Chris Lattner4f066492009-07-11 20:29:19 +00005944 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005945 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00005946 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00005947 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005948 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00005949 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005950 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00005951
Evan Cheng1606e8e2009-03-13 07:51:59 +00005952 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00005953 CP->getAlignment(),
5954 CP->getOffset(), OpFlag);
5955 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00005956 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00005957 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00005958 if (OpFlag) {
5959 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00005960 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00005961 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00005962 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005963 }
5964
5965 return Result;
5966}
5967
Dan Gohmand858e902010-04-17 15:26:15 +00005968SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00005969 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00005970
Chris Lattner18c59872009-06-27 04:16:01 +00005971 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
5972 // global base reg.
5973 unsigned char OpFlag = 0;
5974 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005975 CodeModel::Model M = getTargetMachine().getCodeModel();
5976
Chris Lattner4f066492009-07-11 20:29:19 +00005977 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005978 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00005979 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00005980 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005981 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00005982 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005983 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00005984
Chris Lattner18c59872009-06-27 04:16:01 +00005985 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
5986 OpFlag);
5987 DebugLoc DL = JT->getDebugLoc();
5988 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00005989
Chris Lattner18c59872009-06-27 04:16:01 +00005990 // With PIC, the address is actually $g + Offset.
Chris Lattner1e61e692010-11-15 02:46:57 +00005991 if (OpFlag)
Chris Lattner18c59872009-06-27 04:16:01 +00005992 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
5993 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00005994 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00005995 Result);
Eric Christopherfd179292009-08-27 18:07:15 +00005996
Chris Lattner18c59872009-06-27 04:16:01 +00005997 return Result;
5998}
5999
6000SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006001X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00006002 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00006003
Chris Lattner18c59872009-06-27 04:16:01 +00006004 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6005 // global base reg.
6006 unsigned char OpFlag = 0;
6007 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006008 CodeModel::Model M = getTargetMachine().getCodeModel();
6009
Chris Lattner4f066492009-07-11 20:29:19 +00006010 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006011 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00006012 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00006013 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006014 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00006015 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006016 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00006017
Chris Lattner18c59872009-06-27 04:16:01 +00006018 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00006019
Chris Lattner18c59872009-06-27 04:16:01 +00006020 DebugLoc DL = Op.getDebugLoc();
6021 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00006022
6023
Chris Lattner18c59872009-06-27 04:16:01 +00006024 // With PIC, the address is actually $g + Offset.
6025 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00006026 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00006027 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
6028 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006029 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00006030 Result);
6031 }
Eric Christopherfd179292009-08-27 18:07:15 +00006032
Chris Lattner18c59872009-06-27 04:16:01 +00006033 return Result;
6034}
6035
Dan Gohman475871a2008-07-27 21:46:04 +00006036SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006037X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00006038 // Create the TargetBlockAddressAddress node.
6039 unsigned char OpFlags =
6040 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00006041 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00006042 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Dan Gohman29cbade2009-11-20 23:18:13 +00006043 DebugLoc dl = Op.getDebugLoc();
6044 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(),
6045 /*isTarget=*/true, OpFlags);
6046
Dan Gohmanf705adb2009-10-30 01:28:02 +00006047 if (Subtarget->isPICStyleRIPRel() &&
6048 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00006049 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
6050 else
6051 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00006052
Dan Gohman29cbade2009-11-20 23:18:13 +00006053 // With PIC, the address is actually $g + Offset.
6054 if (isGlobalRelativeToPICBase(OpFlags)) {
6055 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
6056 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
6057 Result);
6058 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00006059
6060 return Result;
6061}
6062
6063SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00006064X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00006065 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00006066 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00006067 // Create the TargetGlobalAddress node, folding in the constant
6068 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00006069 unsigned char OpFlags =
6070 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006071 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00006072 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006073 if (OpFlags == X86II::MO_NO_FLAG &&
6074 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00006075 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00006076 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00006077 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00006078 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00006079 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00006080 }
Eric Christopherfd179292009-08-27 18:07:15 +00006081
Chris Lattner4f066492009-07-11 20:29:19 +00006082 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006083 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00006084 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
6085 else
6086 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00006087
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006088 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00006089 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00006090 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
6091 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006092 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006093 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006094
Chris Lattner36c25012009-07-10 07:34:39 +00006095 // For globals that require a load from a stub to get the address, emit the
6096 // load.
6097 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00006098 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00006099 MachinePointerInfo::getGOT(), false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006100
Dan Gohman6520e202008-10-18 02:06:02 +00006101 // If there was a non-zero offset that we didn't fold, create an explicit
6102 // addition for it.
6103 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00006104 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00006105 DAG.getConstant(Offset, getPointerTy()));
6106
Evan Cheng0db9fe62006-04-25 20:13:52 +00006107 return Result;
6108}
6109
Evan Chengda43bcf2008-09-24 00:05:32 +00006110SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006111X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00006112 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00006113 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006114 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00006115}
6116
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006117static SDValue
6118GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00006119 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Chris Lattnerb903bed2009-06-26 21:20:29 +00006120 unsigned char OperandFlags) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006121 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006122 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006123 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00006124 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006125 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00006126 GA->getOffset(),
6127 OperandFlags);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006128 if (InFlag) {
6129 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00006130 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006131 } else {
6132 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00006133 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006134 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006135
6136 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00006137 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006138
Rafael Espindola15f1b662009-04-24 12:59:40 +00006139 SDValue Flag = Chain.getValue(1);
6140 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006141}
6142
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006143// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00006144static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006145LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006146 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00006147 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00006148 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
6149 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006150 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006151 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006152 InFlag = Chain.getValue(1);
6153
Chris Lattnerb903bed2009-06-26 21:20:29 +00006154 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006155}
6156
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006157// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00006158static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006159LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006160 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00006161 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
6162 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006163}
6164
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006165// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
6166// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00006167static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006168 const EVT PtrVT, TLSModel::Model model,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00006169 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00006170 DebugLoc dl = GA->getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00006171
Chris Lattnerf93b90c2010-09-22 04:39:11 +00006172 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
6173 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
6174 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00006175
Michael J. Spencerec38de22010-10-10 22:04:20 +00006176 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Chris Lattnerf93b90c2010-09-22 04:39:11 +00006177 DAG.getIntPtrConstant(0),
6178 MachinePointerInfo(Ptr), false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00006179
Chris Lattnerb903bed2009-06-26 21:20:29 +00006180 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00006181 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
6182 // initialexec.
6183 unsigned WrapperKind = X86ISD::Wrapper;
6184 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00006185 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Chris Lattner18c59872009-06-27 04:16:01 +00006186 } else if (is64Bit) {
6187 assert(model == TLSModel::InitialExec);
6188 OperandFlags = X86II::MO_GOTTPOFF;
6189 WrapperKind = X86ISD::WrapperRIP;
6190 } else {
6191 assert(model == TLSModel::InitialExec);
6192 OperandFlags = X86II::MO_INDNTPOFF;
Chris Lattnerb903bed2009-06-26 21:20:29 +00006193 }
Eric Christopherfd179292009-08-27 18:07:15 +00006194
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006195 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
6196 // exec)
Michael J. Spencerec38de22010-10-10 22:04:20 +00006197 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Devang Patel0d881da2010-07-06 22:08:15 +00006198 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00006199 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00006200 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00006201
Rafael Espindola9a580232009-02-27 13:37:18 +00006202 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00006203 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00006204 MachinePointerInfo::getGOT(), false, false, 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00006205
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006206 // The address of the thread local variable is the add of the thread
6207 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00006208 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006209}
6210
Dan Gohman475871a2008-07-27 21:46:04 +00006211SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006212X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00006213
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006214 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00006215 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00006216
Eric Christopher30ef0e52010-06-03 04:07:48 +00006217 if (Subtarget->isTargetELF()) {
6218 // TODO: implement the "local dynamic" model
6219 // TODO: implement the "initial exec"model for pic executables
Michael J. Spencerec38de22010-10-10 22:04:20 +00006220
Eric Christopher30ef0e52010-06-03 04:07:48 +00006221 // If GV is an alias then use the aliasee for determining
6222 // thread-localness.
6223 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
6224 GV = GA->resolveAliasedGlobal(false);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006225
6226 TLSModel::Model model
Eric Christopher30ef0e52010-06-03 04:07:48 +00006227 = getTLSModel(GV, getTargetMachine().getRelocationModel());
Michael J. Spencerec38de22010-10-10 22:04:20 +00006228
Eric Christopher30ef0e52010-06-03 04:07:48 +00006229 switch (model) {
6230 case TLSModel::GeneralDynamic:
6231 case TLSModel::LocalDynamic: // not implemented
6232 if (Subtarget->is64Bit())
6233 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
6234 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Michael J. Spencerec38de22010-10-10 22:04:20 +00006235
Eric Christopher30ef0e52010-06-03 04:07:48 +00006236 case TLSModel::InitialExec:
6237 case TLSModel::LocalExec:
6238 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
6239 Subtarget->is64Bit());
6240 }
6241 } else if (Subtarget->isTargetDarwin()) {
6242 // Darwin only has one model of TLS. Lower to that.
6243 unsigned char OpFlag = 0;
6244 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
6245 X86ISD::WrapperRIP : X86ISD::Wrapper;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006246
Eric Christopher30ef0e52010-06-03 04:07:48 +00006247 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6248 // global base reg.
6249 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
6250 !Subtarget->is64Bit();
6251 if (PIC32)
6252 OpFlag = X86II::MO_TLVP_PIC_BASE;
6253 else
6254 OpFlag = X86II::MO_TLVP;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006255 DebugLoc DL = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00006256 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopherd8c05362010-12-09 06:25:53 +00006257 GA->getValueType(0),
Eric Christopher30ef0e52010-06-03 04:07:48 +00006258 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00006259 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006260
Eric Christopher30ef0e52010-06-03 04:07:48 +00006261 // With PIC32, the address is actually $g + Offset.
6262 if (PIC32)
6263 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
6264 DAG.getNode(X86ISD::GlobalBaseReg,
6265 DebugLoc(), getPointerTy()),
6266 Offset);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006267
Eric Christopher30ef0e52010-06-03 04:07:48 +00006268 // Lowering the machine isd will make sure everything is in the right
6269 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00006270 SDValue Chain = DAG.getEntryNode();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006271 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Eric Christopherd8c05362010-12-09 06:25:53 +00006272 SDValue Args[] = { Chain, Offset };
6273 Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006274
Eric Christopher30ef0e52010-06-03 04:07:48 +00006275 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
6276 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
6277 MFI->setAdjustsStack(true);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00006278
Eric Christopher30ef0e52010-06-03 04:07:48 +00006279 // And our return value (tls address) is in the standard call return value
6280 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00006281 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
6282 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy());
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006283 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00006284
Eric Christopher30ef0e52010-06-03 04:07:48 +00006285 assert(false &&
6286 "TLS not implemented for this target.");
Eric Christopherfd179292009-08-27 18:07:15 +00006287
Torok Edwinc23197a2009-07-14 16:55:14 +00006288 llvm_unreachable("Unreachable");
Chris Lattner5867de12009-04-01 22:14:45 +00006289 return SDValue();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006290}
6291
Evan Cheng0db9fe62006-04-25 20:13:52 +00006292
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006293/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michelfdc40a02009-02-17 22:15:04 +00006294/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00006295SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman4c1fa612008-03-03 22:22:09 +00006296 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00006297 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006298 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006299 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006300 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00006301 SDValue ShOpLo = Op.getOperand(0);
6302 SDValue ShOpHi = Op.getOperand(1);
6303 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00006304 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00006305 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00006306 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00006307
Dan Gohman475871a2008-07-27 21:46:04 +00006308 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006309 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00006310 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
6311 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006312 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00006313 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
6314 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006315 }
Evan Chenge3413162006-01-09 18:33:28 +00006316
Owen Anderson825b72b2009-08-11 20:47:22 +00006317 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
6318 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00006319 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00006320 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00006321
Dan Gohman475871a2008-07-27 21:46:04 +00006322 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00006323 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00006324 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
6325 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00006326
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006327 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00006328 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
6329 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006330 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00006331 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
6332 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006333 }
6334
Dan Gohman475871a2008-07-27 21:46:04 +00006335 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00006336 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006337}
Evan Chenga3195e82006-01-12 22:54:21 +00006338
Dan Gohmand858e902010-04-17 15:26:15 +00006339SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
6340 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006341 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00006342
Dale Johannesen0488fb62010-09-30 23:57:10 +00006343 if (SrcVT.isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00006344 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00006345
Owen Anderson825b72b2009-08-11 20:47:22 +00006346 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00006347 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00006348
Eli Friedman36df4992009-05-27 00:47:34 +00006349 // These are really Legal; return the operand so the caller accepts it as
6350 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00006351 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00006352 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00006353 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00006354 Subtarget->is64Bit()) {
6355 return Op;
6356 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006357
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006358 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006359 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006360 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00006361 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006362 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00006363 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00006364 StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006365 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006366 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00006367 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
6368}
Evan Cheng0db9fe62006-04-25 20:13:52 +00006369
Owen Andersone50ed302009-08-10 22:56:29 +00006370SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Michael J. Spencerec38de22010-10-10 22:04:20 +00006371 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00006372 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006373 // Build the FILD
Chris Lattner492a43e2010-09-22 01:28:21 +00006374 DebugLoc DL = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00006375 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00006376 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006377 if (useSSE)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006378 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Glue);
Chris Lattner5a88b832007-02-25 07:10:00 +00006379 else
Owen Anderson825b72b2009-08-11 20:47:22 +00006380 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006381
Chris Lattner492a43e2010-09-22 01:28:21 +00006382 unsigned ByteSize = SrcVT.getSizeInBits()/8;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006383
Chris Lattner492a43e2010-09-22 01:28:21 +00006384 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
6385 MachineMemOperand *MMO =
6386 DAG.getMachineFunction()
6387 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6388 MachineMemOperand::MOLoad, ByteSize, ByteSize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006389
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006390 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Chris Lattner492a43e2010-09-22 01:28:21 +00006391 SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
6392 X86ISD::FILD, DL,
6393 Tys, Ops, array_lengthof(Ops),
6394 SrcVT, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006395
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006396 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006397 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00006398 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006399
6400 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
6401 // shouldn't be necessary except that RFP cannot be live across
6402 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006403 MachineFunction &MF = DAG.getMachineFunction();
Bob Wilsoneafca4e2010-09-22 17:35:14 +00006404 unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
6405 int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006406 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00006407 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006408 SDValue Ops[] = {
6409 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
6410 };
Chris Lattner492a43e2010-09-22 01:28:21 +00006411 MachineMemOperand *MMO =
6412 DAG.getMachineFunction()
6413 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
Bob Wilsoneafca4e2010-09-22 17:35:14 +00006414 MachineMemOperand::MOStore, SSFISize, SSFISize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006415
Chris Lattner492a43e2010-09-22 01:28:21 +00006416 Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
6417 Ops, array_lengthof(Ops),
6418 Op.getValueType(), MMO);
6419 Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006420 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006421 false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006422 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006423
Evan Cheng0db9fe62006-04-25 20:13:52 +00006424 return Result;
6425}
6426
Bill Wendling8b8a6362009-01-17 03:56:04 +00006427// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00006428SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
6429 SelectionDAG &DAG) const {
Bill Wendling8b8a6362009-01-17 03:56:04 +00006430 // This algorithm is not obvious. Here it is in C code, more or less:
6431 /*
6432 double uint64_to_double( uint32_t hi, uint32_t lo ) {
6433 static const __m128i exp = { 0x4330000045300000ULL, 0 };
6434 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00006435
Bill Wendling8b8a6362009-01-17 03:56:04 +00006436 // Copy ints to xmm registers.
6437 __m128i xh = _mm_cvtsi32_si128( hi );
6438 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00006439
Bill Wendling8b8a6362009-01-17 03:56:04 +00006440 // Combine into low half of a single xmm register.
6441 __m128i x = _mm_unpacklo_epi32( xh, xl );
6442 __m128d d;
6443 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00006444
Bill Wendling8b8a6362009-01-17 03:56:04 +00006445 // Merge in appropriate exponents to give the integer bits the right
6446 // magnitude.
6447 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00006448
Bill Wendling8b8a6362009-01-17 03:56:04 +00006449 // Subtract away the biases to deal with the IEEE-754 double precision
6450 // implicit 1.
6451 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00006452
Bill Wendling8b8a6362009-01-17 03:56:04 +00006453 // All conversions up to here are exact. The correctly rounded result is
6454 // calculated using the current rounding mode using the following
6455 // horizontal add.
6456 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
6457 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
6458 // store doesn't really need to be here (except
6459 // maybe to zero the other double)
6460 return sd;
6461 }
6462 */
Dale Johannesen040225f2008-10-21 23:07:49 +00006463
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006464 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00006465 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00006466
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006467 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00006468 std::vector<Constant*> CV0;
Owen Andersoneed707b2009-07-24 23:12:02 +00006469 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x45300000)));
6470 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x43300000)));
6471 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
6472 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
Owen Andersonaf7ec972009-07-28 21:19:26 +00006473 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006474 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006475
Bill Wendling8b8a6362009-01-17 03:56:04 +00006476 std::vector<Constant*> CV1;
Owen Andersona90b3dc2009-07-15 21:51:10 +00006477 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006478 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
Owen Andersona90b3dc2009-07-15 21:51:10 +00006479 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006480 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Owen Andersonaf7ec972009-07-28 21:19:26 +00006481 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006482 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006483
Owen Anderson825b72b2009-08-11 20:47:22 +00006484 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6485 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00006486 Op.getOperand(0),
6487 DAG.getIntPtrConstant(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006488 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6489 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00006490 Op.getOperand(0),
6491 DAG.getIntPtrConstant(0)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006492 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
6493 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Chris Lattnere8639032010-09-21 06:22:23 +00006494 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006495 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00006496 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006497 SDValue XR2F = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Unpck2);
Owen Anderson825b72b2009-08-11 20:47:22 +00006498 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00006499 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006500 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00006501 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006502
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006503 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman9008ca62009-04-27 18:41:29 +00006504 int ShufMask[2] = { 1, -1 };
Owen Anderson825b72b2009-08-11 20:47:22 +00006505 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
6506 DAG.getUNDEF(MVT::v2f64), ShufMask);
6507 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
6508 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006509 DAG.getIntPtrConstant(0));
6510}
6511
Bill Wendling8b8a6362009-01-17 03:56:04 +00006512// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00006513SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
6514 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006515 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00006516 // FP constant to bias correct the final result.
6517 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00006518 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006519
6520 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00006521 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6522 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Bill Wendling8b8a6362009-01-17 03:56:04 +00006523 Op.getOperand(0),
6524 DAG.getIntPtrConstant(0)));
6525
Owen Anderson825b72b2009-08-11 20:47:22 +00006526 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006527 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00006528 DAG.getIntPtrConstant(0));
6529
6530 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00006531 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006532 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00006533 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006534 MVT::v2f64, Load)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006535 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00006536 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006537 MVT::v2f64, Bias)));
6538 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006539 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00006540 DAG.getIntPtrConstant(0));
6541
6542 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00006543 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006544
6545 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00006546 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00006547
Owen Anderson825b72b2009-08-11 20:47:22 +00006548 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006549 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00006550 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00006551 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006552 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00006553 }
6554
6555 // Handle final rounding.
6556 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00006557}
6558
Dan Gohmand858e902010-04-17 15:26:15 +00006559SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
6560 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00006561 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006562 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00006563
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006564 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00006565 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
6566 // the optimization here.
6567 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00006568 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00006569
Owen Andersone50ed302009-08-10 22:56:29 +00006570 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006571 EVT DstVT = Op.getValueType();
6572 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00006573 return LowerUINT_TO_FP_i64(Op, DAG);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006574 else if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00006575 return LowerUINT_TO_FP_i32(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00006576
6577 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00006578 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006579 if (SrcVT == MVT::i32) {
6580 SDValue WordOff = DAG.getConstant(4, getPointerTy());
6581 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
6582 getPointerTy(), StackSlot, WordOff);
6583 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006584 StackSlot, MachinePointerInfo(),
6585 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006586 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006587 OffsetSlot, MachinePointerInfo(),
6588 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006589 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
6590 return Fild;
6591 }
6592
6593 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
6594 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006595 StackSlot, MachinePointerInfo(),
6596 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006597 // For i64 source, we need to add the appropriate power of 2 if the input
6598 // was negative. This is the same as the optimization in
6599 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
6600 // we must be careful to do the computation in x87 extended precision, not
6601 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00006602 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
6603 MachineMemOperand *MMO =
6604 DAG.getMachineFunction()
6605 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6606 MachineMemOperand::MOLoad, 8, 8);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006607
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006608 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
6609 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Chris Lattner492a43e2010-09-22 01:28:21 +00006610 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, 3,
6611 MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006612
6613 APInt FF(32, 0x5F800000ULL);
6614
6615 // Check whether the sign bit is set.
6616 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
6617 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
6618 ISD::SETLT);
6619
6620 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
6621 SDValue FudgePtr = DAG.getConstantPool(
6622 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
6623 getPointerTy());
6624
6625 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
6626 SDValue Zero = DAG.getIntPtrConstant(0);
6627 SDValue Four = DAG.getIntPtrConstant(4);
6628 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
6629 Zero, Four);
6630 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
6631
6632 // Load the value out, extending it from f32 to f80.
6633 // FIXME: Avoid the extend by constructing the right constant pool?
Evan Chengbcc80172010-07-07 22:15:37 +00006634 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, MVT::f80, dl, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00006635 FudgePtr, MachinePointerInfo::getConstantPool(),
6636 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006637 // Extend everything to 80 bits to force it to be done on x87.
6638 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
6639 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00006640}
6641
Dan Gohman475871a2008-07-27 21:46:04 +00006642std::pair<SDValue,SDValue> X86TargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00006643FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) const {
Chris Lattner07290932010-09-22 01:05:16 +00006644 DebugLoc DL = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00006645
Owen Andersone50ed302009-08-10 22:56:29 +00006646 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00006647
6648 if (!IsSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006649 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
6650 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00006651 }
6652
Owen Anderson825b72b2009-08-11 20:47:22 +00006653 assert(DstTy.getSimpleVT() <= MVT::i64 &&
6654 DstTy.getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00006655 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00006656
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006657 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00006658 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00006659 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00006660 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00006661 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00006662 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00006663 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00006664 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006665
Evan Cheng87c89352007-10-15 20:11:21 +00006666 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
6667 // stack slot.
6668 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00006669 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00006670 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006671 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00006672
Michael J. Spencerec38de22010-10-10 22:04:20 +00006673
6674
Evan Cheng0db9fe62006-04-25 20:13:52 +00006675 unsigned Opc;
Owen Anderson825b72b2009-08-11 20:47:22 +00006676 switch (DstTy.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00006677 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
Owen Anderson825b72b2009-08-11 20:47:22 +00006678 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
6679 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
6680 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006681 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006682
Dan Gohman475871a2008-07-27 21:46:04 +00006683 SDValue Chain = DAG.getEntryNode();
6684 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00006685 EVT TheVT = Op.getOperand(0).getValueType();
6686 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006687 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00006688 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006689 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006690 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00006691 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00006692 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00006693 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00006694 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00006695
Chris Lattner492a43e2010-09-22 01:28:21 +00006696 MachineMemOperand *MMO =
6697 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6698 MachineMemOperand::MOLoad, MemSize, MemSize);
6699 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, 3,
6700 DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006701 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00006702 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006703 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
6704 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00006705
Chris Lattner07290932010-09-22 01:05:16 +00006706 MachineMemOperand *MMO =
6707 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6708 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006709
Evan Cheng0db9fe62006-04-25 20:13:52 +00006710 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00006711 SDValue Ops[] = { Chain, Value, StackSlot };
Chris Lattner07290932010-09-22 01:05:16 +00006712 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
6713 Ops, 3, DstTy, MMO);
Evan Chengd9558e02006-01-06 00:43:03 +00006714
Chris Lattner27a6c732007-11-24 07:07:01 +00006715 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006716}
6717
Dan Gohmand858e902010-04-17 15:26:15 +00006718SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
6719 SelectionDAG &DAG) const {
Dale Johannesen0488fb62010-09-30 23:57:10 +00006720 if (Op.getValueType().isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00006721 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00006722
Eli Friedman948e95a2009-05-23 09:59:16 +00006723 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00006724 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00006725 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
6726 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00006727
Chris Lattner27a6c732007-11-24 07:07:01 +00006728 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006729 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00006730 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00006731}
6732
Dan Gohmand858e902010-04-17 15:26:15 +00006733SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
6734 SelectionDAG &DAG) const {
Eli Friedman948e95a2009-05-23 09:59:16 +00006735 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
6736 SDValue FIST = Vals.first, StackSlot = Vals.second;
6737 assert(FIST.getNode() && "Unexpected failure");
6738
6739 // Load the result.
6740 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00006741 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00006742}
6743
Dan Gohmand858e902010-04-17 15:26:15 +00006744SDValue X86TargetLowering::LowerFABS(SDValue Op,
6745 SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00006746 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006747 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00006748 EVT VT = Op.getValueType();
6749 EVT EltVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00006750 if (VT.isVector())
6751 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006752 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00006753 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006754 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00006755 CV.push_back(C);
6756 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006757 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006758 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00006759 CV.push_back(C);
6760 CV.push_back(C);
6761 CV.push_back(C);
6762 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006763 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00006764 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006765 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006766 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00006767 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006768 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006769 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006770}
6771
Dan Gohmand858e902010-04-17 15:26:15 +00006772SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00006773 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006774 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00006775 EVT VT = Op.getValueType();
6776 EVT EltVT = VT;
Duncan Sandsda9ad382009-09-06 19:29:07 +00006777 if (VT.isVector())
Duncan Sands83ec4b62008-06-06 12:08:01 +00006778 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006779 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00006780 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006781 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00006782 CV.push_back(C);
6783 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006784 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006785 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00006786 CV.push_back(C);
6787 CV.push_back(C);
6788 CV.push_back(C);
6789 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006790 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00006791 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006792 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006793 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00006794 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006795 false, false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006796 if (VT.isVector()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006797 return DAG.getNode(ISD::BITCAST, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00006798 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006799 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00006800 Op.getOperand(0)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006801 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00006802 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00006803 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00006804 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006805}
6806
Dan Gohmand858e902010-04-17 15:26:15 +00006807SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00006808 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00006809 SDValue Op0 = Op.getOperand(0);
6810 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006811 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00006812 EVT VT = Op.getValueType();
6813 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00006814
6815 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00006816 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006817 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00006818 SrcVT = VT;
6819 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00006820 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00006821 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006822 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00006823 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00006824 }
6825
6826 // At this point the operands and the result should have the same
6827 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00006828
Evan Cheng68c47cb2007-01-05 07:55:56 +00006829 // First get the sign bit of second operand.
6830 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00006831 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006832 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
6833 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00006834 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006835 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
6836 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
6837 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
6838 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00006839 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00006840 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006841 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006842 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00006843 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006844 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006845 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00006846
6847 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00006848 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006849 // Op0 is MVT::f32, Op1 is MVT::f64.
6850 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
6851 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
6852 DAG.getConstant(32, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006853 SignBit = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, SignBit);
Owen Anderson825b72b2009-08-11 20:47:22 +00006854 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00006855 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00006856 }
6857
Evan Cheng73d6cf12007-01-05 21:37:56 +00006858 // Clear first operand sign bit.
6859 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00006860 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006861 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
6862 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00006863 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006864 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
6865 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
6866 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
6867 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00006868 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00006869 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006870 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006871 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00006872 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006873 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006874 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00006875
6876 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00006877 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00006878}
6879
Dan Gohman076aee32009-03-04 19:44:21 +00006880/// Emit nodes that will be selected as "test Op0,Op0", or something
6881/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00006882SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00006883 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00006884 DebugLoc dl = Op.getDebugLoc();
6885
Dan Gohman31125812009-03-07 01:58:32 +00006886 // CF and OF aren't always set the way we want. Determine which
6887 // of these we need.
6888 bool NeedCF = false;
6889 bool NeedOF = false;
6890 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006891 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00006892 case X86::COND_A: case X86::COND_AE:
6893 case X86::COND_B: case X86::COND_BE:
6894 NeedCF = true;
6895 break;
6896 case X86::COND_G: case X86::COND_GE:
6897 case X86::COND_L: case X86::COND_LE:
6898 case X86::COND_O: case X86::COND_NO:
6899 NeedOF = true;
6900 break;
Dan Gohman31125812009-03-07 01:58:32 +00006901 }
6902
Dan Gohman076aee32009-03-04 19:44:21 +00006903 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00006904 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
6905 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006906 if (Op.getResNo() != 0 || NeedOF || NeedCF)
6907 // Emit a CMP with 0, which is the TEST pattern.
6908 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
6909 DAG.getConstant(0, Op.getValueType()));
6910
6911 unsigned Opcode = 0;
6912 unsigned NumOperands = 0;
6913 switch (Op.getNode()->getOpcode()) {
6914 case ISD::ADD:
6915 // Due to an isel shortcoming, be conservative if this add is likely to be
6916 // selected as part of a load-modify-store instruction. When the root node
6917 // in a match is a store, isel doesn't know how to remap non-chain non-flag
6918 // uses of other nodes in the match, such as the ADD in this case. This
6919 // leads to the ADD being left around and reselected, with the result being
6920 // two adds in the output. Alas, even if none our users are stores, that
6921 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
6922 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
6923 // climbing the DAG back to the root, and it doesn't seem to be worth the
6924 // effort.
6925 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Dan Gohman076aee32009-03-04 19:44:21 +00006926 UE = Op.getNode()->use_end(); UI != UE; ++UI)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006927 if (UI->getOpcode() != ISD::CopyToReg && UI->getOpcode() != ISD::SETCC)
6928 goto default_case;
6929
6930 if (ConstantSDNode *C =
6931 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
6932 // An add of one will be selected as an INC.
6933 if (C->getAPIntValue() == 1) {
6934 Opcode = X86ISD::INC;
6935 NumOperands = 1;
6936 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00006937 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006938
6939 // An add of negative one (subtract of one) will be selected as a DEC.
6940 if (C->getAPIntValue().isAllOnesValue()) {
6941 Opcode = X86ISD::DEC;
6942 NumOperands = 1;
6943 break;
6944 }
Dan Gohman076aee32009-03-04 19:44:21 +00006945 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006946
6947 // Otherwise use a regular EFLAGS-setting add.
6948 Opcode = X86ISD::ADD;
6949 NumOperands = 2;
6950 break;
6951 case ISD::AND: {
6952 // If the primary and result isn't used, don't bother using X86ISD::AND,
6953 // because a TEST instruction will be better.
6954 bool NonFlagUse = false;
6955 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
6956 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
6957 SDNode *User = *UI;
6958 unsigned UOpNo = UI.getOperandNo();
6959 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
6960 // Look pass truncate.
6961 UOpNo = User->use_begin().getOperandNo();
6962 User = *User->use_begin();
6963 }
6964
6965 if (User->getOpcode() != ISD::BRCOND &&
6966 User->getOpcode() != ISD::SETCC &&
6967 (User->getOpcode() != ISD::SELECT || UOpNo != 0)) {
6968 NonFlagUse = true;
6969 break;
6970 }
Dan Gohman076aee32009-03-04 19:44:21 +00006971 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006972
6973 if (!NonFlagUse)
6974 break;
6975 }
6976 // FALL THROUGH
6977 case ISD::SUB:
6978 case ISD::OR:
6979 case ISD::XOR:
6980 // Due to the ISEL shortcoming noted above, be conservative if this op is
6981 // likely to be selected as part of a load-modify-store instruction.
6982 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
6983 UE = Op.getNode()->use_end(); UI != UE; ++UI)
6984 if (UI->getOpcode() == ISD::STORE)
6985 goto default_case;
6986
6987 // Otherwise use a regular EFLAGS-setting instruction.
6988 switch (Op.getNode()->getOpcode()) {
6989 default: llvm_unreachable("unexpected operator!");
6990 case ISD::SUB: Opcode = X86ISD::SUB; break;
6991 case ISD::OR: Opcode = X86ISD::OR; break;
6992 case ISD::XOR: Opcode = X86ISD::XOR; break;
6993 case ISD::AND: Opcode = X86ISD::AND; break;
6994 }
6995
6996 NumOperands = 2;
6997 break;
6998 case X86ISD::ADD:
6999 case X86ISD::SUB:
7000 case X86ISD::INC:
7001 case X86ISD::DEC:
7002 case X86ISD::OR:
7003 case X86ISD::XOR:
7004 case X86ISD::AND:
7005 return SDValue(Op.getNode(), 1);
7006 default:
7007 default_case:
7008 break;
Dan Gohman076aee32009-03-04 19:44:21 +00007009 }
7010
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007011 if (Opcode == 0)
7012 // Emit a CMP with 0, which is the TEST pattern.
7013 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
7014 DAG.getConstant(0, Op.getValueType()));
7015
7016 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
7017 SmallVector<SDValue, 4> Ops;
7018 for (unsigned i = 0; i != NumOperands; ++i)
7019 Ops.push_back(Op.getOperand(i));
7020
7021 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
7022 DAG.ReplaceAllUsesWith(Op, New);
7023 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00007024}
7025
7026/// Emit nodes that will be selected as "cmp Op0,Op1", or something
7027/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00007028SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00007029 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00007030 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
7031 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00007032 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00007033
7034 DebugLoc dl = Op0.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00007035 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00007036}
7037
Evan Chengd40d03e2010-01-06 19:38:29 +00007038/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
7039/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00007040SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
7041 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00007042 SDValue Op0 = And.getOperand(0);
7043 SDValue Op1 = And.getOperand(1);
7044 if (Op0.getOpcode() == ISD::TRUNCATE)
7045 Op0 = Op0.getOperand(0);
7046 if (Op1.getOpcode() == ISD::TRUNCATE)
7047 Op1 = Op1.getOperand(0);
7048
Evan Chengd40d03e2010-01-06 19:38:29 +00007049 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00007050 if (Op1.getOpcode() == ISD::SHL)
7051 std::swap(Op0, Op1);
7052 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00007053 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
7054 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00007055 // If we looked past a truncate, check that it's only truncating away
7056 // known zeros.
7057 unsigned BitWidth = Op0.getValueSizeInBits();
7058 unsigned AndBitWidth = And.getValueSizeInBits();
7059 if (BitWidth > AndBitWidth) {
7060 APInt Mask = APInt::getAllOnesValue(BitWidth), Zeros, Ones;
7061 DAG.ComputeMaskedBits(Op0, Mask, Zeros, Ones);
7062 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
7063 return SDValue();
7064 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00007065 LHS = Op1;
7066 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00007067 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00007068 } else if (Op1.getOpcode() == ISD::Constant) {
7069 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
7070 SDValue AndLHS = Op0;
Evan Chengd40d03e2010-01-06 19:38:29 +00007071 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
7072 LHS = AndLHS.getOperand(0);
7073 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00007074 }
Evan Chengd40d03e2010-01-06 19:38:29 +00007075 }
Evan Cheng0488db92007-09-25 01:57:46 +00007076
Evan Chengd40d03e2010-01-06 19:38:29 +00007077 if (LHS.getNode()) {
Evan Chenge5b51ac2010-04-17 06:13:15 +00007078 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00007079 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00007080 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00007081 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00007082 // Also promote i16 to i32 for performance / code size reason.
7083 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00007084 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00007085 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00007086
Evan Chengd40d03e2010-01-06 19:38:29 +00007087 // If the operand types disagree, extend the shift amount to match. Since
7088 // BT ignores high bits (like shifts) we can use anyextend.
7089 if (LHS.getValueType() != RHS.getValueType())
7090 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00007091
Evan Chengd40d03e2010-01-06 19:38:29 +00007092 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
7093 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
7094 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
7095 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00007096 }
7097
Evan Cheng54de3ea2010-01-05 06:52:31 +00007098 return SDValue();
7099}
7100
Dan Gohmand858e902010-04-17 15:26:15 +00007101SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng54de3ea2010-01-05 06:52:31 +00007102 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
7103 SDValue Op0 = Op.getOperand(0);
7104 SDValue Op1 = Op.getOperand(1);
7105 DebugLoc dl = Op.getDebugLoc();
7106 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
7107
7108 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00007109 // Lower (X & (1 << N)) == 0 to BT(X, N).
7110 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
7111 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Chris Lattner481eebc2010-12-19 21:23:48 +00007112 if (Op0.getOpcode() == ISD::AND && Op0.hasOneUse() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00007113 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane368b462010-06-18 14:22:04 +00007114 cast<ConstantSDNode>(Op1)->isNullValue() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00007115 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
7116 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
7117 if (NewSetCC.getNode())
7118 return NewSetCC;
7119 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00007120
Chris Lattner481eebc2010-12-19 21:23:48 +00007121 // Look for X == 0, X == 1, X != 0, or X != 1. We can simplify some forms of
7122 // these.
7123 if (Op1.getOpcode() == ISD::Constant &&
Evan Cheng2c755ba2010-02-27 07:36:59 +00007124 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
7125 cast<ConstantSDNode>(Op1)->isNullValue()) &&
7126 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007127
Chris Lattner481eebc2010-12-19 21:23:48 +00007128 // If the input is a setcc, then reuse the input setcc or use a new one with
7129 // the inverted condition.
7130 if (Op0.getOpcode() == X86ISD::SETCC) {
7131 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
7132 bool Invert = (CC == ISD::SETNE) ^
7133 cast<ConstantSDNode>(Op1)->isNullValue();
7134 if (!Invert) return Op0;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007135
Evan Cheng2c755ba2010-02-27 07:36:59 +00007136 CCode = X86::GetOppositeBranchCondition(CCode);
Chris Lattner481eebc2010-12-19 21:23:48 +00007137 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
7138 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
7139 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00007140 }
7141
Evan Chenge5b51ac2010-04-17 06:13:15 +00007142 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00007143 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00007144 if (X86CC == X86::COND_INVALID)
7145 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00007146
Chris Lattnerc19d1c32010-12-19 22:08:31 +00007147 SDValue EFLAGS = EmitCmp(Op0, Op1, X86CC, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00007148 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattnerc19d1c32010-12-19 22:08:31 +00007149 DAG.getConstant(X86CC, MVT::i8), EFLAGS);
Evan Cheng0488db92007-09-25 01:57:46 +00007150}
7151
Dan Gohmand858e902010-04-17 15:26:15 +00007152SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00007153 SDValue Cond;
7154 SDValue Op0 = Op.getOperand(0);
7155 SDValue Op1 = Op.getOperand(1);
7156 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00007157 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00007158 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
7159 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007160 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00007161
7162 if (isFP) {
7163 unsigned SSECC = 8;
Owen Andersone50ed302009-08-10 22:56:29 +00007164 EVT VT0 = Op0.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00007165 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
7166 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00007167 bool Swap = false;
7168
7169 switch (SetCCOpcode) {
7170 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00007171 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00007172 case ISD::SETEQ: SSECC = 0; break;
Scott Michelfdc40a02009-02-17 22:15:04 +00007173 case ISD::SETOGT:
Nate Begeman30a0de92008-07-17 16:51:19 +00007174 case ISD::SETGT: Swap = true; // Fallthrough
7175 case ISD::SETLT:
7176 case ISD::SETOLT: SSECC = 1; break;
7177 case ISD::SETOGE:
7178 case ISD::SETGE: Swap = true; // Fallthrough
7179 case ISD::SETLE:
7180 case ISD::SETOLE: SSECC = 2; break;
7181 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00007182 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00007183 case ISD::SETNE: SSECC = 4; break;
7184 case ISD::SETULE: Swap = true;
7185 case ISD::SETUGE: SSECC = 5; break;
7186 case ISD::SETULT: Swap = true;
7187 case ISD::SETUGT: SSECC = 6; break;
7188 case ISD::SETO: SSECC = 7; break;
7189 }
7190 if (Swap)
7191 std::swap(Op0, Op1);
7192
Nate Begemanfb8ead02008-07-25 19:05:58 +00007193 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00007194 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00007195 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00007196 SDValue UNORD, EQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00007197 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
7198 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00007199 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00007200 }
7201 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00007202 SDValue ORD, NEQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00007203 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
7204 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00007205 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00007206 }
Torok Edwinc23197a2009-07-14 16:55:14 +00007207 llvm_unreachable("Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00007208 }
7209 // Handle all other FP comparisons here.
Owen Anderson825b72b2009-08-11 20:47:22 +00007210 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00007211 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007212
Nate Begeman30a0de92008-07-17 16:51:19 +00007213 // We are handling one of the integer comparisons here. Since SSE only has
7214 // GT and EQ comparisons for integer, swapping operands and multiple
7215 // operations may be required for some comparisons.
7216 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
7217 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00007218
Owen Anderson825b72b2009-08-11 20:47:22 +00007219 switch (VT.getSimpleVT().SimpleTy) {
Nate Begeman30a0de92008-07-17 16:51:19 +00007220 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007221 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007222 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007223 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
7224 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00007225 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007226
Nate Begeman30a0de92008-07-17 16:51:19 +00007227 switch (SetCCOpcode) {
7228 default: break;
7229 case ISD::SETNE: Invert = true;
7230 case ISD::SETEQ: Opc = EQOpc; break;
7231 case ISD::SETLT: Swap = true;
7232 case ISD::SETGT: Opc = GTOpc; break;
7233 case ISD::SETGE: Swap = true;
7234 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
7235 case ISD::SETULT: Swap = true;
7236 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
7237 case ISD::SETUGE: Swap = true;
7238 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
7239 }
7240 if (Swap)
7241 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007242
Nate Begeman30a0de92008-07-17 16:51:19 +00007243 // Since SSE has no unsigned integer comparisons, we need to flip the sign
7244 // bits of the inputs before performing those operations.
7245 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00007246 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00007247 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
7248 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00007249 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00007250 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
7251 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00007252 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
7253 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00007254 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007255
Dale Johannesenace16102009-02-03 19:33:06 +00007256 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00007257
7258 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00007259 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00007260 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00007261
Nate Begeman30a0de92008-07-17 16:51:19 +00007262 return Result;
7263}
Evan Cheng0488db92007-09-25 01:57:46 +00007264
Evan Cheng370e5342008-12-03 08:38:43 +00007265// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00007266static bool isX86LogicalCmp(SDValue Op) {
7267 unsigned Opc = Op.getNode()->getOpcode();
7268 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
7269 return true;
7270 if (Op.getResNo() == 1 &&
7271 (Opc == X86ISD::ADD ||
7272 Opc == X86ISD::SUB ||
Chris Lattner5b856542010-12-20 00:59:46 +00007273 Opc == X86ISD::ADC ||
7274 Opc == X86ISD::SBB ||
Dan Gohman076aee32009-03-04 19:44:21 +00007275 Opc == X86ISD::SMUL ||
7276 Opc == X86ISD::UMUL ||
7277 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00007278 Opc == X86ISD::DEC ||
7279 Opc == X86ISD::OR ||
7280 Opc == X86ISD::XOR ||
7281 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00007282 return true;
7283
Chris Lattner9637d5b2010-12-05 07:49:54 +00007284 if (Op.getResNo() == 2 && Opc == X86ISD::UMUL)
7285 return true;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007286
Dan Gohman076aee32009-03-04 19:44:21 +00007287 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00007288}
7289
Chris Lattnera2b56002010-12-05 01:23:24 +00007290static bool isZero(SDValue V) {
7291 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
7292 return C && C->isNullValue();
7293}
7294
Chris Lattner96908b12010-12-05 02:00:51 +00007295static bool isAllOnes(SDValue V) {
7296 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
7297 return C && C->isAllOnesValue();
7298}
7299
Dan Gohmand858e902010-04-17 15:26:15 +00007300SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00007301 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00007302 SDValue Cond = Op.getOperand(0);
Chris Lattnera2b56002010-12-05 01:23:24 +00007303 SDValue Op1 = Op.getOperand(1);
7304 SDValue Op2 = Op.getOperand(2);
7305 DebugLoc DL = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00007306 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00007307
Dan Gohman1a492952009-10-20 16:22:37 +00007308 if (Cond.getOpcode() == ISD::SETCC) {
7309 SDValue NewCond = LowerSETCC(Cond, DAG);
7310 if (NewCond.getNode())
7311 Cond = NewCond;
7312 }
Evan Cheng734503b2006-09-11 02:19:56 +00007313
Chris Lattnera2b56002010-12-05 01:23:24 +00007314 // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00007315 // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
Chris Lattnera2b56002010-12-05 01:23:24 +00007316 // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00007317 // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007318 if (Cond.getOpcode() == X86ISD::SETCC &&
Chris Lattner96908b12010-12-05 02:00:51 +00007319 Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
7320 isZero(Cond.getOperand(1).getOperand(1))) {
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007321 SDValue Cmp = Cond.getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007322
Chris Lattnera2b56002010-12-05 01:23:24 +00007323 unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007324
7325 if ((isAllOnes(Op1) || isAllOnes(Op2)) &&
Chris Lattner96908b12010-12-05 02:00:51 +00007326 (CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
7327 SDValue Y = isAllOnes(Op2) ? Op1 : Op2;
Chris Lattnera2b56002010-12-05 01:23:24 +00007328
7329 SDValue CmpOp0 = Cmp.getOperand(0);
7330 Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32,
7331 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007332
Chris Lattner96908b12010-12-05 02:00:51 +00007333 SDValue Res = // Res = 0 or -1.
Chris Lattnera2b56002010-12-05 01:23:24 +00007334 DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
7335 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007336
Chris Lattner96908b12010-12-05 02:00:51 +00007337 if (isAllOnes(Op1) != (CondCode == X86::COND_E))
7338 Res = DAG.getNOT(DL, Res, Res.getValueType());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007339
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007340 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
Chris Lattnera2b56002010-12-05 01:23:24 +00007341 if (N2C == 0 || !N2C->isNullValue())
7342 Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
7343 return Res;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007344 }
7345 }
7346
Chris Lattnera2b56002010-12-05 01:23:24 +00007347 // Look past (and (setcc_carry (cmp ...)), 1).
Evan Chengad9c0a32009-12-15 00:53:42 +00007348 if (Cond.getOpcode() == ISD::AND &&
7349 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
7350 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00007351 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00007352 Cond = Cond.getOperand(0);
7353 }
7354
Evan Cheng3f41d662007-10-08 22:16:29 +00007355 // If condition flag is set by a X86ISD::CMP, then use it as the condition
7356 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00007357 if (Cond.getOpcode() == X86ISD::SETCC ||
7358 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00007359 CC = Cond.getOperand(0);
7360
Dan Gohman475871a2008-07-27 21:46:04 +00007361 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00007362 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00007363 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00007364
Evan Cheng3f41d662007-10-08 22:16:29 +00007365 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007366 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00007367 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00007368 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00007369
Chris Lattnerd1980a52009-03-12 06:52:53 +00007370 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
7371 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00007372 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00007373 addTest = false;
7374 }
7375 }
7376
7377 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007378 // Look pass the truncate.
7379 if (Cond.getOpcode() == ISD::TRUNCATE)
7380 Cond = Cond.getOperand(0);
7381
7382 // We know the result of AND is compared against zero. Try to match
7383 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00007384 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Chris Lattnera2b56002010-12-05 01:23:24 +00007385 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG);
Evan Chengd40d03e2010-01-06 19:38:29 +00007386 if (NewSetCC.getNode()) {
7387 CC = NewSetCC.getOperand(0);
7388 Cond = NewSetCC.getOperand(1);
7389 addTest = false;
7390 }
7391 }
7392 }
7393
7394 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007395 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00007396 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00007397 }
7398
Benjamin Kramere915ff32010-12-22 23:09:28 +00007399 // a < b ? -1 : 0 -> RES = ~setcc_carry
7400 // a < b ? 0 : -1 -> RES = setcc_carry
7401 // a >= b ? -1 : 0 -> RES = setcc_carry
7402 // a >= b ? 0 : -1 -> RES = ~setcc_carry
7403 if (Cond.getOpcode() == X86ISD::CMP) {
7404 unsigned CondCode = cast<ConstantSDNode>(CC)->getZExtValue();
7405
7406 if ((CondCode == X86::COND_AE || CondCode == X86::COND_B) &&
7407 (isAllOnes(Op1) || isAllOnes(Op2)) && (isZero(Op1) || isZero(Op2))) {
7408 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
7409 DAG.getConstant(X86::COND_B, MVT::i8), Cond);
7410 if (isAllOnes(Op1) != (CondCode == X86::COND_B))
7411 return DAG.getNOT(DL, Res, Res.getValueType());
7412 return Res;
7413 }
7414 }
7415
Evan Cheng0488db92007-09-25 01:57:46 +00007416 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
7417 // condition is true.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007418 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007419 SDValue Ops[] = { Op2, Op1, CC, Cond };
Chris Lattnera2b56002010-12-05 01:23:24 +00007420 return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00007421}
7422
Evan Cheng370e5342008-12-03 08:38:43 +00007423// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
7424// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
7425// from the AND / OR.
7426static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
7427 Opc = Op.getOpcode();
7428 if (Opc != ISD::OR && Opc != ISD::AND)
7429 return false;
7430 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
7431 Op.getOperand(0).hasOneUse() &&
7432 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
7433 Op.getOperand(1).hasOneUse());
7434}
7435
Evan Cheng961d6d42009-02-02 08:19:07 +00007436// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
7437// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00007438static bool isXor1OfSetCC(SDValue Op) {
7439 if (Op.getOpcode() != ISD::XOR)
7440 return false;
7441 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
7442 if (N1C && N1C->getAPIntValue() == 1) {
7443 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
7444 Op.getOperand(0).hasOneUse();
7445 }
7446 return false;
7447}
7448
Dan Gohmand858e902010-04-17 15:26:15 +00007449SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00007450 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00007451 SDValue Chain = Op.getOperand(0);
7452 SDValue Cond = Op.getOperand(1);
7453 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007454 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00007455 SDValue CC;
Evan Cheng734503b2006-09-11 02:19:56 +00007456
Dan Gohman1a492952009-10-20 16:22:37 +00007457 if (Cond.getOpcode() == ISD::SETCC) {
7458 SDValue NewCond = LowerSETCC(Cond, DAG);
7459 if (NewCond.getNode())
7460 Cond = NewCond;
7461 }
Chris Lattnere55484e2008-12-25 05:34:37 +00007462#if 0
7463 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00007464 else if (Cond.getOpcode() == X86ISD::ADD ||
7465 Cond.getOpcode() == X86ISD::SUB ||
7466 Cond.getOpcode() == X86ISD::SMUL ||
7467 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00007468 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00007469#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00007470
Evan Chengad9c0a32009-12-15 00:53:42 +00007471 // Look pass (and (setcc_carry (cmp ...)), 1).
7472 if (Cond.getOpcode() == ISD::AND &&
7473 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
7474 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00007475 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00007476 Cond = Cond.getOperand(0);
7477 }
7478
Evan Cheng3f41d662007-10-08 22:16:29 +00007479 // If condition flag is set by a X86ISD::CMP, then use it as the condition
7480 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00007481 if (Cond.getOpcode() == X86ISD::SETCC ||
7482 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00007483 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007484
Dan Gohman475871a2008-07-27 21:46:04 +00007485 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00007486 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00007487 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00007488 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00007489 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00007490 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00007491 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00007492 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00007493 default: break;
7494 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00007495 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00007496 // These can only come from an arithmetic instruction with overflow,
7497 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00007498 Cond = Cond.getNode()->getOperand(1);
7499 addTest = false;
7500 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00007501 }
Evan Cheng0488db92007-09-25 01:57:46 +00007502 }
Evan Cheng370e5342008-12-03 08:38:43 +00007503 } else {
7504 unsigned CondOpc;
7505 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
7506 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00007507 if (CondOpc == ISD::OR) {
7508 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
7509 // two branches instead of an explicit OR instruction with a
7510 // separate test.
7511 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00007512 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00007513 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007514 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00007515 Chain, Dest, CC, Cmp);
7516 CC = Cond.getOperand(1).getOperand(0);
7517 Cond = Cmp;
7518 addTest = false;
7519 }
7520 } else { // ISD::AND
7521 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
7522 // two branches instead of an explicit AND instruction with a
7523 // separate test. However, we only do this if this block doesn't
7524 // have a fall-through edge, because this requires an explicit
7525 // jmp when the condition is false.
7526 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00007527 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00007528 Op.getNode()->hasOneUse()) {
7529 X86::CondCode CCode =
7530 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
7531 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007532 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00007533 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00007534 // Look for an unconditional branch following this conditional branch.
7535 // We need this because we need to reverse the successors in order
7536 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00007537 if (User->getOpcode() == ISD::BR) {
7538 SDValue FalseBB = User->getOperand(1);
7539 SDNode *NewBR =
7540 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00007541 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00007542 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00007543 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00007544
Dale Johannesene4d209d2009-02-03 20:21:25 +00007545 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00007546 Chain, Dest, CC, Cmp);
7547 X86::CondCode CCode =
7548 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
7549 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007550 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00007551 Cond = Cmp;
7552 addTest = false;
7553 }
7554 }
Dan Gohman279c22e2008-10-21 03:29:32 +00007555 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00007556 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
7557 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
7558 // It should be transformed during dag combiner except when the condition
7559 // is set by a arithmetics with overflow node.
7560 X86::CondCode CCode =
7561 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
7562 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007563 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00007564 Cond = Cond.getOperand(0).getOperand(1);
7565 addTest = false;
Dan Gohman279c22e2008-10-21 03:29:32 +00007566 }
Evan Cheng0488db92007-09-25 01:57:46 +00007567 }
7568
7569 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007570 // Look pass the truncate.
7571 if (Cond.getOpcode() == ISD::TRUNCATE)
7572 Cond = Cond.getOperand(0);
7573
7574 // We know the result of AND is compared against zero. Try to match
7575 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00007576 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007577 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
7578 if (NewSetCC.getNode()) {
7579 CC = NewSetCC.getOperand(0);
7580 Cond = NewSetCC.getOperand(1);
7581 addTest = false;
7582 }
7583 }
7584 }
7585
7586 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007587 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00007588 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00007589 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00007590 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00007591 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00007592}
7593
Anton Korobeynikove060b532007-04-17 19:34:00 +00007594
7595// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
7596// Calls to _alloca is needed to probe the stack when allocating more than 4k
7597// bytes in one go. Touching the stack at 4K increments is necessary to ensure
7598// that the guard pages used by the OS virtual memory manager are allocated in
7599// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00007600SDValue
7601X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00007602 SelectionDAG &DAG) const {
Duncan Sands1e1ca0b2010-10-21 16:02:12 +00007603 assert((Subtarget->isTargetCygMing() || Subtarget->isTargetWindows()) &&
Michael J. Spencere9c253e2010-10-21 01:41:01 +00007604 "This should be used only on Windows targets");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007605 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007606
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007607 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00007608 SDValue Chain = Op.getOperand(0);
7609 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007610 // FIXME: Ensure alignment here
7611
Dan Gohman475871a2008-07-27 21:46:04 +00007612 SDValue Flag;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007613
Owen Anderson825b72b2009-08-11 20:47:22 +00007614 EVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007615
Dale Johannesendd64c412009-02-04 00:33:20 +00007616 Chain = DAG.getCopyToReg(Chain, dl, X86::EAX, Size, Flag);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00007617 Flag = Chain.getValue(1);
7618
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007619 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00007620
Michael J. Spencere9c253e2010-10-21 01:41:01 +00007621 Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag);
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00007622 Flag = Chain.getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007623
Dale Johannesendd64c412009-02-04 00:33:20 +00007624 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007625
Dan Gohman475871a2008-07-27 21:46:04 +00007626 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Dale Johannesene4d209d2009-02-03 20:21:25 +00007627 return DAG.getMergeValues(Ops1, 2, dl);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007628}
7629
Dan Gohmand858e902010-04-17 15:26:15 +00007630SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00007631 MachineFunction &MF = DAG.getMachineFunction();
7632 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
7633
Dan Gohman69de1932008-02-06 22:27:42 +00007634 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00007635 DebugLoc DL = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00007636
Anton Korobeynikove7beda12010-10-03 22:52:07 +00007637 if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00007638 // vastart just stores the address of the VarArgsFrameIndex slot into the
7639 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +00007640 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
7641 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00007642 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
7643 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007644 }
7645
7646 // __va_list_tag:
7647 // gp_offset (0 - 6 * 8)
7648 // fp_offset (48 - 48 + 8 * 16)
7649 // overflow_arg_area (point to parameters coming in memory).
7650 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00007651 SmallVector<SDValue, 8> MemOps;
7652 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00007653 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00007654 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00007655 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
7656 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007657 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007658 MemOps.push_back(Store);
7659
7660 // Store fp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00007661 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007662 FIN, DAG.getIntPtrConstant(4));
Chris Lattner8026a9d2010-09-21 17:50:43 +00007663 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00007664 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
7665 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007666 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007667 MemOps.push_back(Store);
7668
7669 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +00007670 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007671 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +00007672 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
7673 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00007674 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
7675 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +00007676 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007677 MemOps.push_back(Store);
7678
7679 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +00007680 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007681 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +00007682 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
7683 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00007684 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
7685 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007686 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +00007687 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00007688 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00007689}
7690
Dan Gohmand858e902010-04-17 15:26:15 +00007691SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman320afb82010-10-12 18:00:49 +00007692 assert(Subtarget->is64Bit() &&
7693 "LowerVAARG only handles 64-bit va_arg!");
7694 assert((Subtarget->isTargetLinux() ||
7695 Subtarget->isTargetDarwin()) &&
7696 "Unhandled target in LowerVAARG");
7697 assert(Op.getNode()->getNumOperands() == 4);
7698 SDValue Chain = Op.getOperand(0);
7699 SDValue SrcPtr = Op.getOperand(1);
7700 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
7701 unsigned Align = Op.getConstantOperandVal(3);
7702 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9018e832008-05-10 01:26:14 +00007703
Dan Gohman320afb82010-10-12 18:00:49 +00007704 EVT ArgVT = Op.getNode()->getValueType(0);
7705 const Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
7706 uint32_t ArgSize = getTargetData()->getTypeAllocSize(ArgTy);
7707 uint8_t ArgMode;
7708
7709 // Decide which area this value should be read from.
7710 // TODO: Implement the AMD64 ABI in its entirety. This simple
7711 // selection mechanism works only for the basic types.
7712 if (ArgVT == MVT::f80) {
7713 llvm_unreachable("va_arg for f80 not yet implemented");
7714 } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) {
7715 ArgMode = 2; // Argument passed in XMM register. Use fp_offset.
7716 } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) {
7717 ArgMode = 1; // Argument passed in GPR64 register(s). Use gp_offset.
7718 } else {
7719 llvm_unreachable("Unhandled argument type in LowerVAARG");
7720 }
7721
7722 if (ArgMode == 2) {
7723 // Sanity Check: Make sure using fp_offset makes sense.
Michael J. Spencer87b86652010-10-19 07:32:42 +00007724 assert(!UseSoftFloat &&
Eric Christopher52b45052010-10-12 19:44:17 +00007725 !(DAG.getMachineFunction()
7726 .getFunction()->hasFnAttr(Attribute::NoImplicitFloat)) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00007727 Subtarget->hasXMM());
Dan Gohman320afb82010-10-12 18:00:49 +00007728 }
7729
7730 // Insert VAARG_64 node into the DAG
7731 // VAARG_64 returns two values: Variable Argument Address, Chain
7732 SmallVector<SDValue, 11> InstOps;
7733 InstOps.push_back(Chain);
7734 InstOps.push_back(SrcPtr);
7735 InstOps.push_back(DAG.getConstant(ArgSize, MVT::i32));
7736 InstOps.push_back(DAG.getConstant(ArgMode, MVT::i8));
7737 InstOps.push_back(DAG.getConstant(Align, MVT::i32));
7738 SDVTList VTs = DAG.getVTList(getPointerTy(), MVT::Other);
7739 SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl,
7740 VTs, &InstOps[0], InstOps.size(),
7741 MVT::i64,
7742 MachinePointerInfo(SV),
7743 /*Align=*/0,
7744 /*Volatile=*/false,
7745 /*ReadMem=*/true,
7746 /*WriteMem=*/true);
7747 Chain = VAARG.getValue(1);
7748
7749 // Load the next argument and return it
7750 return DAG.getLoad(ArgVT, dl,
7751 Chain,
7752 VAARG,
7753 MachinePointerInfo(),
7754 false, false, 0);
Dan Gohman9018e832008-05-10 01:26:14 +00007755}
7756
Dan Gohmand858e902010-04-17 15:26:15 +00007757SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) const {
Evan Chengae642192007-03-02 23:16:35 +00007758 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00007759 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00007760 SDValue Chain = Op.getOperand(0);
7761 SDValue DstPtr = Op.getOperand(1);
7762 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00007763 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
7764 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Chris Lattnere72f2022010-09-21 05:40:29 +00007765 DebugLoc DL = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00007766
Chris Lattnere72f2022010-09-21 05:40:29 +00007767 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +00007768 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Michael J. Spencerec38de22010-10-10 22:04:20 +00007769 false,
Chris Lattnere72f2022010-09-21 05:40:29 +00007770 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +00007771}
7772
Dan Gohman475871a2008-07-27 21:46:04 +00007773SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007774X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007775 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007776 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007777 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00007778 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00007779 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00007780 case Intrinsic::x86_sse_comieq_ss:
7781 case Intrinsic::x86_sse_comilt_ss:
7782 case Intrinsic::x86_sse_comile_ss:
7783 case Intrinsic::x86_sse_comigt_ss:
7784 case Intrinsic::x86_sse_comige_ss:
7785 case Intrinsic::x86_sse_comineq_ss:
7786 case Intrinsic::x86_sse_ucomieq_ss:
7787 case Intrinsic::x86_sse_ucomilt_ss:
7788 case Intrinsic::x86_sse_ucomile_ss:
7789 case Intrinsic::x86_sse_ucomigt_ss:
7790 case Intrinsic::x86_sse_ucomige_ss:
7791 case Intrinsic::x86_sse_ucomineq_ss:
7792 case Intrinsic::x86_sse2_comieq_sd:
7793 case Intrinsic::x86_sse2_comilt_sd:
7794 case Intrinsic::x86_sse2_comile_sd:
7795 case Intrinsic::x86_sse2_comigt_sd:
7796 case Intrinsic::x86_sse2_comige_sd:
7797 case Intrinsic::x86_sse2_comineq_sd:
7798 case Intrinsic::x86_sse2_ucomieq_sd:
7799 case Intrinsic::x86_sse2_ucomilt_sd:
7800 case Intrinsic::x86_sse2_ucomile_sd:
7801 case Intrinsic::x86_sse2_ucomigt_sd:
7802 case Intrinsic::x86_sse2_ucomige_sd:
7803 case Intrinsic::x86_sse2_ucomineq_sd: {
7804 unsigned Opc = 0;
7805 ISD::CondCode CC = ISD::SETCC_INVALID;
7806 switch (IntNo) {
7807 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007808 case Intrinsic::x86_sse_comieq_ss:
7809 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007810 Opc = X86ISD::COMI;
7811 CC = ISD::SETEQ;
7812 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00007813 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007814 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007815 Opc = X86ISD::COMI;
7816 CC = ISD::SETLT;
7817 break;
7818 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007819 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007820 Opc = X86ISD::COMI;
7821 CC = ISD::SETLE;
7822 break;
7823 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007824 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007825 Opc = X86ISD::COMI;
7826 CC = ISD::SETGT;
7827 break;
7828 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007829 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007830 Opc = X86ISD::COMI;
7831 CC = ISD::SETGE;
7832 break;
7833 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007834 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007835 Opc = X86ISD::COMI;
7836 CC = ISD::SETNE;
7837 break;
7838 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007839 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007840 Opc = X86ISD::UCOMI;
7841 CC = ISD::SETEQ;
7842 break;
7843 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007844 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007845 Opc = X86ISD::UCOMI;
7846 CC = ISD::SETLT;
7847 break;
7848 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007849 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007850 Opc = X86ISD::UCOMI;
7851 CC = ISD::SETLE;
7852 break;
7853 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007854 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007855 Opc = X86ISD::UCOMI;
7856 CC = ISD::SETGT;
7857 break;
7858 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007859 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007860 Opc = X86ISD::UCOMI;
7861 CC = ISD::SETGE;
7862 break;
7863 case Intrinsic::x86_sse_ucomineq_ss:
7864 case Intrinsic::x86_sse2_ucomineq_sd:
7865 Opc = X86ISD::UCOMI;
7866 CC = ISD::SETNE;
7867 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00007868 }
Evan Cheng734503b2006-09-11 02:19:56 +00007869
Dan Gohman475871a2008-07-27 21:46:04 +00007870 SDValue LHS = Op.getOperand(1);
7871 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00007872 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00007873 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00007874 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
7875 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
7876 DAG.getConstant(X86CC, MVT::i8), Cond);
7877 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00007878 }
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007879 // ptest and testp intrinsics. The intrinsic these come from are designed to
7880 // return an integer value, not just an instruction so lower it to the ptest
7881 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00007882 case Intrinsic::x86_sse41_ptestz:
7883 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007884 case Intrinsic::x86_sse41_ptestnzc:
7885 case Intrinsic::x86_avx_ptestz_256:
7886 case Intrinsic::x86_avx_ptestc_256:
7887 case Intrinsic::x86_avx_ptestnzc_256:
7888 case Intrinsic::x86_avx_vtestz_ps:
7889 case Intrinsic::x86_avx_vtestc_ps:
7890 case Intrinsic::x86_avx_vtestnzc_ps:
7891 case Intrinsic::x86_avx_vtestz_pd:
7892 case Intrinsic::x86_avx_vtestc_pd:
7893 case Intrinsic::x86_avx_vtestnzc_pd:
7894 case Intrinsic::x86_avx_vtestz_ps_256:
7895 case Intrinsic::x86_avx_vtestc_ps_256:
7896 case Intrinsic::x86_avx_vtestnzc_ps_256:
7897 case Intrinsic::x86_avx_vtestz_pd_256:
7898 case Intrinsic::x86_avx_vtestc_pd_256:
7899 case Intrinsic::x86_avx_vtestnzc_pd_256: {
7900 bool IsTestPacked = false;
Eric Christopher71c67532009-07-29 00:28:05 +00007901 unsigned X86CC = 0;
7902 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00007903 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007904 case Intrinsic::x86_avx_vtestz_ps:
7905 case Intrinsic::x86_avx_vtestz_pd:
7906 case Intrinsic::x86_avx_vtestz_ps_256:
7907 case Intrinsic::x86_avx_vtestz_pd_256:
7908 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00007909 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007910 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +00007911 // ZF = 1
7912 X86CC = X86::COND_E;
7913 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007914 case Intrinsic::x86_avx_vtestc_ps:
7915 case Intrinsic::x86_avx_vtestc_pd:
7916 case Intrinsic::x86_avx_vtestc_ps_256:
7917 case Intrinsic::x86_avx_vtestc_pd_256:
7918 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00007919 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007920 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00007921 // CF = 1
7922 X86CC = X86::COND_B;
7923 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007924 case Intrinsic::x86_avx_vtestnzc_ps:
7925 case Intrinsic::x86_avx_vtestnzc_pd:
7926 case Intrinsic::x86_avx_vtestnzc_ps_256:
7927 case Intrinsic::x86_avx_vtestnzc_pd_256:
7928 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +00007929 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007930 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00007931 // ZF and CF = 0
7932 X86CC = X86::COND_A;
7933 break;
7934 }
Eric Christopherfd179292009-08-27 18:07:15 +00007935
Eric Christopher71c67532009-07-29 00:28:05 +00007936 SDValue LHS = Op.getOperand(1);
7937 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007938 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
7939 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00007940 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
7941 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
7942 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +00007943 }
Evan Cheng5759f972008-05-04 09:15:50 +00007944
7945 // Fix vector shift instructions where the last operand is a non-immediate
7946 // i32 value.
7947 case Intrinsic::x86_sse2_pslli_w:
7948 case Intrinsic::x86_sse2_pslli_d:
7949 case Intrinsic::x86_sse2_pslli_q:
7950 case Intrinsic::x86_sse2_psrli_w:
7951 case Intrinsic::x86_sse2_psrli_d:
7952 case Intrinsic::x86_sse2_psrli_q:
7953 case Intrinsic::x86_sse2_psrai_w:
7954 case Intrinsic::x86_sse2_psrai_d:
7955 case Intrinsic::x86_mmx_pslli_w:
7956 case Intrinsic::x86_mmx_pslli_d:
7957 case Intrinsic::x86_mmx_pslli_q:
7958 case Intrinsic::x86_mmx_psrli_w:
7959 case Intrinsic::x86_mmx_psrli_d:
7960 case Intrinsic::x86_mmx_psrli_q:
7961 case Intrinsic::x86_mmx_psrai_w:
7962 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00007963 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00007964 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00007965 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00007966
7967 unsigned NewIntNo = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00007968 EVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00007969 switch (IntNo) {
7970 case Intrinsic::x86_sse2_pslli_w:
7971 NewIntNo = Intrinsic::x86_sse2_psll_w;
7972 break;
7973 case Intrinsic::x86_sse2_pslli_d:
7974 NewIntNo = Intrinsic::x86_sse2_psll_d;
7975 break;
7976 case Intrinsic::x86_sse2_pslli_q:
7977 NewIntNo = Intrinsic::x86_sse2_psll_q;
7978 break;
7979 case Intrinsic::x86_sse2_psrli_w:
7980 NewIntNo = Intrinsic::x86_sse2_psrl_w;
7981 break;
7982 case Intrinsic::x86_sse2_psrli_d:
7983 NewIntNo = Intrinsic::x86_sse2_psrl_d;
7984 break;
7985 case Intrinsic::x86_sse2_psrli_q:
7986 NewIntNo = Intrinsic::x86_sse2_psrl_q;
7987 break;
7988 case Intrinsic::x86_sse2_psrai_w:
7989 NewIntNo = Intrinsic::x86_sse2_psra_w;
7990 break;
7991 case Intrinsic::x86_sse2_psrai_d:
7992 NewIntNo = Intrinsic::x86_sse2_psra_d;
7993 break;
7994 default: {
Owen Anderson825b72b2009-08-11 20:47:22 +00007995 ShAmtVT = MVT::v2i32;
Evan Cheng5759f972008-05-04 09:15:50 +00007996 switch (IntNo) {
7997 case Intrinsic::x86_mmx_pslli_w:
7998 NewIntNo = Intrinsic::x86_mmx_psll_w;
7999 break;
8000 case Intrinsic::x86_mmx_pslli_d:
8001 NewIntNo = Intrinsic::x86_mmx_psll_d;
8002 break;
8003 case Intrinsic::x86_mmx_pslli_q:
8004 NewIntNo = Intrinsic::x86_mmx_psll_q;
8005 break;
8006 case Intrinsic::x86_mmx_psrli_w:
8007 NewIntNo = Intrinsic::x86_mmx_psrl_w;
8008 break;
8009 case Intrinsic::x86_mmx_psrli_d:
8010 NewIntNo = Intrinsic::x86_mmx_psrl_d;
8011 break;
8012 case Intrinsic::x86_mmx_psrli_q:
8013 NewIntNo = Intrinsic::x86_mmx_psrl_q;
8014 break;
8015 case Intrinsic::x86_mmx_psrai_w:
8016 NewIntNo = Intrinsic::x86_mmx_psra_w;
8017 break;
8018 case Intrinsic::x86_mmx_psrai_d:
8019 NewIntNo = Intrinsic::x86_mmx_psra_d;
8020 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00008021 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng5759f972008-05-04 09:15:50 +00008022 }
8023 break;
8024 }
8025 }
Mon P Wangefa42202009-09-03 19:56:25 +00008026
8027 // The vector shift intrinsics with scalars uses 32b shift amounts but
8028 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
8029 // to be zero.
8030 SDValue ShOps[4];
8031 ShOps[0] = ShAmt;
8032 ShOps[1] = DAG.getConstant(0, MVT::i32);
8033 if (ShAmtVT == MVT::v4i32) {
8034 ShOps[2] = DAG.getUNDEF(MVT::i32);
8035 ShOps[3] = DAG.getUNDEF(MVT::i32);
8036 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 4);
8037 } else {
8038 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 2);
Dale Johannesen0488fb62010-09-30 23:57:10 +00008039// FIXME this must be lowered to get rid of the invalid type.
Mon P Wangefa42202009-09-03 19:56:25 +00008040 }
8041
Owen Andersone50ed302009-08-10 22:56:29 +00008042 EVT VT = Op.getValueType();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008043 ShAmt = DAG.getNode(ISD::BITCAST, dl, VT, ShAmt);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008044 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008045 DAG.getConstant(NewIntNo, MVT::i32),
Evan Cheng5759f972008-05-04 09:15:50 +00008046 Op.getOperand(1), ShAmt);
8047 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00008048 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008049}
Evan Cheng72261582005-12-20 06:22:03 +00008050
Dan Gohmand858e902010-04-17 15:26:15 +00008051SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
8052 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +00008053 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
8054 MFI->setReturnAddressIsTaken(true);
8055
Bill Wendling64e87322009-01-16 19:25:27 +00008056 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008057 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00008058
8059 if (Depth > 0) {
8060 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
8061 SDValue Offset =
8062 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +00008063 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008064 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00008065 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008066 FrameAddr, Offset),
Chris Lattner51abfe42010-09-21 06:02:19 +00008067 MachinePointerInfo(), false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +00008068 }
8069
8070 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00008071 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00008072 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Chris Lattner51abfe42010-09-21 06:02:19 +00008073 RetAddrFI, MachinePointerInfo(), false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00008074}
8075
Dan Gohmand858e902010-04-17 15:26:15 +00008076SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +00008077 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
8078 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00008079
Owen Andersone50ed302009-08-10 22:56:29 +00008080 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008081 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00008082 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
8083 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00008084 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00008085 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +00008086 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
8087 MachinePointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +00008088 false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00008089 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00008090}
8091
Dan Gohman475871a2008-07-27 21:46:04 +00008092SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008093 SelectionDAG &DAG) const {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00008094 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008095}
8096
Dan Gohmand858e902010-04-17 15:26:15 +00008097SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008098 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00008099 SDValue Chain = Op.getOperand(0);
8100 SDValue Offset = Op.getOperand(1);
8101 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008102 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008103
Dan Gohmand8816272010-08-11 18:14:00 +00008104 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
8105 Subtarget->is64Bit() ? X86::RBP : X86::EBP,
8106 getPointerTy());
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008107 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008108
Dan Gohmand8816272010-08-11 18:14:00 +00008109 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Frame,
8110 DAG.getIntPtrConstant(TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008111 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +00008112 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
8113 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00008114 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008115 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008116
Dale Johannesene4d209d2009-02-03 20:21:25 +00008117 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008118 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008119 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008120}
8121
Dan Gohman475871a2008-07-27 21:46:04 +00008122SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008123 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00008124 SDValue Root = Op.getOperand(0);
8125 SDValue Trmp = Op.getOperand(1); // trampoline
8126 SDValue FPtr = Op.getOperand(2); // nested function
8127 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008128 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008129
Dan Gohman69de1932008-02-06 22:27:42 +00008130 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008131
8132 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00008133 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00008134
8135 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +00008136 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
8137 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +00008138
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00008139 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
8140 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00008141
8142 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
8143
8144 // Load the pointer to the nested function into R11.
8145 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00008146 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00008147 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008148 Addr, MachinePointerInfo(TrmpAddr),
8149 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008150
Owen Anderson825b72b2009-08-11 20:47:22 +00008151 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8152 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008153 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
8154 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +00008155 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00008156
8157 // Load the 'nest' parameter value into R10.
8158 // R10 is specified in X86CallingConv.td
8159 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +00008160 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8161 DAG.getConstant(10, MVT::i64));
8162 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008163 Addr, MachinePointerInfo(TrmpAddr, 10),
8164 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008165
Owen Anderson825b72b2009-08-11 20:47:22 +00008166 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8167 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008168 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
8169 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +00008170 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00008171
8172 // Jump to the nested function.
8173 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +00008174 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8175 DAG.getConstant(20, MVT::i64));
8176 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008177 Addr, MachinePointerInfo(TrmpAddr, 20),
8178 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008179
8180 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +00008181 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8182 DAG.getConstant(22, MVT::i64));
8183 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00008184 MachinePointerInfo(TrmpAddr, 22),
8185 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008186
Dan Gohman475871a2008-07-27 21:46:04 +00008187 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00008188 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008189 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008190 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00008191 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00008192 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00008193 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00008194 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008195
8196 switch (CC) {
8197 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00008198 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +00008199 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00008200 case CallingConv::X86_StdCall: {
8201 // Pass 'nest' parameter in ECX.
8202 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00008203 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008204
8205 // Check that ECX wasn't needed by an 'inreg' parameter.
8206 const FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00008207 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008208
Chris Lattner58d74912008-03-12 17:45:29 +00008209 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00008210 unsigned InRegCount = 0;
8211 unsigned Idx = 1;
8212
8213 for (FunctionType::param_iterator I = FTy->param_begin(),
8214 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00008215 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00008216 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00008217 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008218
8219 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +00008220 report_fatal_error("Nest register in use - reduce number of inreg"
8221 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +00008222 }
8223 }
8224 break;
8225 }
8226 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +00008227 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00008228 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00008229 // Pass 'nest' parameter in EAX.
8230 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00008231 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008232 break;
8233 }
8234
Dan Gohman475871a2008-07-27 21:46:04 +00008235 SDValue OutChains[4];
8236 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008237
Owen Anderson825b72b2009-08-11 20:47:22 +00008238 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8239 DAG.getConstant(10, MVT::i32));
8240 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008241
Chris Lattnera62fe662010-02-05 19:20:30 +00008242 // This is storing the opcode for MOV32ri.
8243 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00008244 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00008245 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008246 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008247 Trmp, MachinePointerInfo(TrmpAddr),
8248 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008249
Owen Anderson825b72b2009-08-11 20:47:22 +00008250 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8251 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008252 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
8253 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +00008254 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008255
Chris Lattnera62fe662010-02-05 19:20:30 +00008256 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +00008257 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8258 DAG.getConstant(5, MVT::i32));
8259 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00008260 MachinePointerInfo(TrmpAddr, 5),
8261 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008262
Owen Anderson825b72b2009-08-11 20:47:22 +00008263 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8264 DAG.getConstant(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008265 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
8266 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +00008267 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008268
Dan Gohman475871a2008-07-27 21:46:04 +00008269 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00008270 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008271 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008272 }
8273}
8274
Dan Gohmand858e902010-04-17 15:26:15 +00008275SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
8276 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008277 /*
8278 The rounding mode is in bits 11:10 of FPSR, and has the following
8279 settings:
8280 00 Round to nearest
8281 01 Round to -inf
8282 10 Round to +inf
8283 11 Round to 0
8284
8285 FLT_ROUNDS, on the other hand, expects the following:
8286 -1 Undefined
8287 0 Round to 0
8288 1 Round to nearest
8289 2 Round to +inf
8290 3 Round to -inf
8291
8292 To perform the conversion, we do:
8293 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
8294 */
8295
8296 MachineFunction &MF = DAG.getMachineFunction();
8297 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00008298 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008299 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00008300 EVT VT = Op.getValueType();
Chris Lattner2156b792010-09-22 01:11:26 +00008301 DebugLoc DL = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008302
8303 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +00008304 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +00008305 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008306
Michael J. Spencerec38de22010-10-10 22:04:20 +00008307
Chris Lattner2156b792010-09-22 01:11:26 +00008308 MachineMemOperand *MMO =
8309 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8310 MachineMemOperand::MOStore, 2, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008311
Chris Lattner2156b792010-09-22 01:11:26 +00008312 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
8313 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
8314 DAG.getVTList(MVT::Other),
8315 Ops, 2, MVT::i16, MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008316
8317 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +00008318 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Chris Lattner51abfe42010-09-21 06:02:19 +00008319 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008320
8321 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00008322 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +00008323 DAG.getNode(ISD::SRL, DL, MVT::i16,
8324 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00008325 CWD, DAG.getConstant(0x800, MVT::i16)),
8326 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00008327 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +00008328 DAG.getNode(ISD::SRL, DL, MVT::i16,
8329 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00008330 CWD, DAG.getConstant(0x400, MVT::i16)),
8331 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008332
Dan Gohman475871a2008-07-27 21:46:04 +00008333 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +00008334 DAG.getNode(ISD::AND, DL, MVT::i16,
8335 DAG.getNode(ISD::ADD, DL, MVT::i16,
8336 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +00008337 DAG.getConstant(1, MVT::i16)),
8338 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008339
8340
Duncan Sands83ec4b62008-06-06 12:08:01 +00008341 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +00008342 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008343}
8344
Dan Gohmand858e902010-04-17 15:26:15 +00008345SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008346 EVT VT = Op.getValueType();
8347 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008348 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008349 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00008350
8351 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008352 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00008353 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +00008354 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00008355 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008356 }
Evan Cheng18efe262007-12-14 02:13:44 +00008357
Evan Cheng152804e2007-12-14 08:30:15 +00008358 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008359 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008360 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00008361
8362 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00008363 SDValue Ops[] = {
8364 Op,
8365 DAG.getConstant(NumBits+NumBits-1, OpVT),
8366 DAG.getConstant(X86::COND_E, MVT::i8),
8367 Op.getValue(1)
8368 };
8369 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00008370
8371 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +00008372 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +00008373
Owen Anderson825b72b2009-08-11 20:47:22 +00008374 if (VT == MVT::i8)
8375 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008376 return Op;
8377}
8378
Dan Gohmand858e902010-04-17 15:26:15 +00008379SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008380 EVT VT = Op.getValueType();
8381 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008382 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008383 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00008384
8385 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008386 if (VT == MVT::i8) {
8387 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00008388 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008389 }
Evan Cheng152804e2007-12-14 08:30:15 +00008390
8391 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008392 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008393 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00008394
8395 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00008396 SDValue Ops[] = {
8397 Op,
8398 DAG.getConstant(NumBits, OpVT),
8399 DAG.getConstant(X86::COND_E, MVT::i8),
8400 Op.getValue(1)
8401 };
8402 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00008403
Owen Anderson825b72b2009-08-11 20:47:22 +00008404 if (VT == MVT::i8)
8405 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008406 return Op;
8407}
8408
Dan Gohmand858e902010-04-17 15:26:15 +00008409SDValue X86TargetLowering::LowerMUL_V2I64(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008410 EVT VT = Op.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00008411 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008412 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00008413
Mon P Wangaf9b9522008-12-18 21:42:19 +00008414 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
8415 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
8416 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
8417 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
8418 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
8419 //
8420 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
8421 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
8422 // return AloBlo + AloBhi + AhiBlo;
8423
8424 SDValue A = Op.getOperand(0);
8425 SDValue B = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008426
Dale Johannesene4d209d2009-02-03 20:21:25 +00008427 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008428 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
8429 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008430 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008431 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
8432 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008433 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008434 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008435 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008436 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008437 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008438 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008439 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008440 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008441 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008442 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008443 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
8444 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008445 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008446 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
8447 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008448 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
8449 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +00008450 return Res;
8451}
8452
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008453SDValue X86TargetLowering::LowerSHL(SDValue Op, SelectionDAG &DAG) const {
8454 EVT VT = Op.getValueType();
8455 DebugLoc dl = Op.getDebugLoc();
8456 SDValue R = Op.getOperand(0);
8457
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008458 LLVMContext *Context = DAG.getContext();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008459
Nate Begeman51409212010-07-28 00:21:48 +00008460 assert(Subtarget->hasSSE41() && "Cannot lower SHL without SSE4.1 or later");
8461
8462 if (VT == MVT::v4i32) {
8463 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8464 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
8465 Op.getOperand(1), DAG.getConstant(23, MVT::i32));
8466
8467 ConstantInt *CI = ConstantInt::get(*Context, APInt(32, 0x3f800000U));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008468
Nate Begeman51409212010-07-28 00:21:48 +00008469 std::vector<Constant*> CV(4, CI);
8470 Constant *C = ConstantVector::get(CV);
8471 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8472 SDValue Addend = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008473 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +00008474 false, false, 16);
8475
8476 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Addend);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008477 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op);
Nate Begeman51409212010-07-28 00:21:48 +00008478 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
8479 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
8480 }
8481 if (VT == MVT::v16i8) {
8482 // a = a << 5;
8483 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8484 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
8485 Op.getOperand(1), DAG.getConstant(5, MVT::i32));
8486
8487 ConstantInt *CM1 = ConstantInt::get(*Context, APInt(8, 15));
8488 ConstantInt *CM2 = ConstantInt::get(*Context, APInt(8, 63));
8489
8490 std::vector<Constant*> CVM1(16, CM1);
8491 std::vector<Constant*> CVM2(16, CM2);
8492 Constant *C = ConstantVector::get(CVM1);
8493 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8494 SDValue M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008495 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +00008496 false, false, 16);
8497
8498 // r = pblendv(r, psllw(r & (char16)15, 4), a);
8499 M = DAG.getNode(ISD::AND, dl, VT, R, M);
8500 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8501 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
8502 DAG.getConstant(4, MVT::i32));
Nate Begeman672fb622010-12-20 22:04:24 +00008503 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT, R, M, Op);
Nate Begeman51409212010-07-28 00:21:48 +00008504 // a += a
8505 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008506
Nate Begeman51409212010-07-28 00:21:48 +00008507 C = ConstantVector::get(CVM2);
8508 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8509 M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008510 MachinePointerInfo::getConstantPool(),
Chris Lattner51abfe42010-09-21 06:02:19 +00008511 false, false, 16);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008512
Nate Begeman51409212010-07-28 00:21:48 +00008513 // r = pblendv(r, psllw(r & (char16)63, 2), a);
8514 M = DAG.getNode(ISD::AND, dl, VT, R, M);
8515 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8516 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
8517 DAG.getConstant(2, MVT::i32));
Nate Begeman672fb622010-12-20 22:04:24 +00008518 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT, R, M, Op);
Nate Begeman51409212010-07-28 00:21:48 +00008519 // a += a
8520 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008521
Nate Begeman51409212010-07-28 00:21:48 +00008522 // return pblendv(r, r+r, a);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008523 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT,
Nate Begeman51409212010-07-28 00:21:48 +00008524 R, DAG.getNode(ISD::ADD, dl, VT, R, R), Op);
8525 return R;
8526 }
8527 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008528}
Mon P Wangaf9b9522008-12-18 21:42:19 +00008529
Dan Gohmand858e902010-04-17 15:26:15 +00008530SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
Bill Wendling74c37652008-12-09 22:08:41 +00008531 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
8532 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +00008533 // looks for this combo and may remove the "setcc" instruction if the "setcc"
8534 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +00008535 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +00008536 SDValue LHS = N->getOperand(0);
8537 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +00008538 unsigned BaseOp = 0;
8539 unsigned Cond = 0;
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008540 DebugLoc DL = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +00008541 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008542 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +00008543 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +00008544 // A subtract of one will be selected as a INC. Note that INC doesn't
8545 // set CF, so we can't do this for UADDO.
8546 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
8547 if (C->getAPIntValue() == 1) {
8548 BaseOp = X86ISD::INC;
8549 Cond = X86::COND_O;
8550 break;
8551 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008552 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +00008553 Cond = X86::COND_O;
8554 break;
8555 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008556 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +00008557 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00008558 break;
8559 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +00008560 // A subtract of one will be selected as a DEC. Note that DEC doesn't
8561 // set CF, so we can't do this for USUBO.
8562 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
8563 if (C->getAPIntValue() == 1) {
8564 BaseOp = X86ISD::DEC;
8565 Cond = X86::COND_O;
8566 break;
8567 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008568 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +00008569 Cond = X86::COND_O;
8570 break;
8571 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008572 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +00008573 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00008574 break;
8575 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00008576 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +00008577 Cond = X86::COND_O;
8578 break;
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008579 case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs
8580 SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0),
8581 MVT::i32);
8582 SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008583
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008584 SDValue SetCC =
8585 DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
8586 DAG.getConstant(X86::COND_O, MVT::i32),
8587 SDValue(Sum.getNode(), 2));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008588
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008589 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
8590 return Sum;
8591 }
Bill Wendling74c37652008-12-09 22:08:41 +00008592 }
Bill Wendling3fafd932008-11-26 22:37:40 +00008593
Bill Wendling61edeb52008-12-02 01:06:39 +00008594 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008595 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008596 SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +00008597
Bill Wendling61edeb52008-12-02 01:06:39 +00008598 SDValue SetCC =
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008599 DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1),
8600 DAG.getConstant(Cond, MVT::i32),
8601 SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +00008602
Bill Wendling61edeb52008-12-02 01:06:39 +00008603 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
8604 return Sum;
Bill Wendling41ea7e72008-11-24 19:21:46 +00008605}
8606
Eric Christopher9a9d2752010-07-22 02:48:34 +00008607SDValue X86TargetLowering::LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG) const{
8608 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00008609
Eric Christopherb6729dc2010-08-04 23:03:04 +00008610 if (!Subtarget->hasSSE2()) {
Eric Christopherc0b2a202010-08-14 21:51:50 +00008611 SDValue Chain = Op.getOperand(0);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008612 SDValue Zero = DAG.getConstant(0,
Eric Christopherb6729dc2010-08-04 23:03:04 +00008613 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Eric Christopherc0b2a202010-08-14 21:51:50 +00008614 SDValue Ops[] = {
8615 DAG.getRegister(X86::ESP, MVT::i32), // Base
8616 DAG.getTargetConstant(1, MVT::i8), // Scale
8617 DAG.getRegister(0, MVT::i32), // Index
8618 DAG.getTargetConstant(0, MVT::i32), // Disp
8619 DAG.getRegister(0, MVT::i32), // Segment.
8620 Zero,
8621 Chain
8622 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00008623 SDNode *Res =
Eric Christopherc0b2a202010-08-14 21:51:50 +00008624 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
8625 array_lengthof(Ops));
8626 return SDValue(Res, 0);
Eric Christopherb6729dc2010-08-04 23:03:04 +00008627 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00008628
Eric Christopher9a9d2752010-07-22 02:48:34 +00008629 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
Chris Lattner132929a2010-08-14 17:26:09 +00008630 if (!isDev)
Eric Christopher9a9d2752010-07-22 02:48:34 +00008631 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008632
Chris Lattner132929a2010-08-14 17:26:09 +00008633 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
8634 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
8635 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
8636 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
Michael J. Spencerec38de22010-10-10 22:04:20 +00008637
Chris Lattner132929a2010-08-14 17:26:09 +00008638 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
8639 if (!Op1 && !Op2 && !Op3 && Op4)
8640 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008641
Chris Lattner132929a2010-08-14 17:26:09 +00008642 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
8643 if (Op1 && !Op2 && !Op3 && !Op4)
8644 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008645
8646 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
Chris Lattner132929a2010-08-14 17:26:09 +00008647 // (MFENCE)>;
8648 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
Eric Christopher9a9d2752010-07-22 02:48:34 +00008649}
8650
Dan Gohmand858e902010-04-17 15:26:15 +00008651SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008652 EVT T = Op.getValueType();
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008653 DebugLoc DL = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +00008654 unsigned Reg = 0;
8655 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00008656 switch(T.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00008657 default:
8658 assert(false && "Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +00008659 case MVT::i8: Reg = X86::AL; size = 1; break;
8660 case MVT::i16: Reg = X86::AX; size = 2; break;
8661 case MVT::i32: Reg = X86::EAX; size = 4; break;
8662 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +00008663 assert(Subtarget->is64Bit() && "Node not type legal!");
8664 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +00008665 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00008666 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008667 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +00008668 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +00008669 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +00008670 Op.getOperand(1),
8671 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +00008672 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +00008673 cpIn.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00008674 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008675 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
8676 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
8677 Ops, 5, T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +00008678 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008679 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +00008680 return cpOut;
8681}
8682
Duncan Sands1607f052008-12-01 11:39:25 +00008683SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008684 SelectionDAG &DAG) const {
Duncan Sands1607f052008-12-01 11:39:25 +00008685 assert(Subtarget->is64Bit() && "Result not type legalized?");
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00008686 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +00008687 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008688 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +00008689 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00008690 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
8691 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +00008692 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +00008693 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
8694 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +00008695 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +00008696 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +00008697 rdx.getValue(1)
8698 };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008699 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008700}
8701
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008702SDValue X86TargetLowering::LowerBITCAST(SDValue Op,
Dale Johannesen7d07b482010-05-21 00:52:33 +00008703 SelectionDAG &DAG) const {
8704 EVT SrcVT = Op.getOperand(0).getValueType();
8705 EVT DstVT = Op.getValueType();
Chris Lattner2a786eb2010-12-19 20:19:20 +00008706 assert(Subtarget->is64Bit() && !Subtarget->hasSSE2() &&
8707 Subtarget->hasMMX() && "Unexpected custom BITCAST");
Michael J. Spencerec38de22010-10-10 22:04:20 +00008708 assert((DstVT == MVT::i64 ||
Dale Johannesen7d07b482010-05-21 00:52:33 +00008709 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008710 "Unexpected custom BITCAST");
Dale Johannesen7d07b482010-05-21 00:52:33 +00008711 // i64 <=> MMX conversions are Legal.
8712 if (SrcVT==MVT::i64 && DstVT.isVector())
8713 return Op;
8714 if (DstVT==MVT::i64 && SrcVT.isVector())
8715 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +00008716 // MMX <=> MMX conversions are Legal.
8717 if (SrcVT.isVector() && DstVT.isVector())
8718 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +00008719 // All other conversions need to be expanded.
8720 return SDValue();
8721}
Chris Lattner5b856542010-12-20 00:59:46 +00008722
Dan Gohmand858e902010-04-17 15:26:15 +00008723SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen71d1bf52008-09-29 22:25:26 +00008724 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +00008725 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008726 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008727 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +00008728 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008729 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008730 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +00008731 Node->getOperand(0),
8732 Node->getOperand(1), negOp,
8733 cast<AtomicSDNode>(Node)->getSrcValue(),
8734 cast<AtomicSDNode>(Node)->getAlignment());
Mon P Wang63307c32008-05-05 19:05:59 +00008735}
8736
Chris Lattner5b856542010-12-20 00:59:46 +00008737static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
8738 EVT VT = Op.getNode()->getValueType(0);
8739
8740 // Let legalize expand this if it isn't a legal type yet.
8741 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
8742 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008743
Chris Lattner5b856542010-12-20 00:59:46 +00008744 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008745
Chris Lattner5b856542010-12-20 00:59:46 +00008746 unsigned Opc;
8747 bool ExtraOp = false;
8748 switch (Op.getOpcode()) {
8749 default: assert(0 && "Invalid code");
8750 case ISD::ADDC: Opc = X86ISD::ADD; break;
8751 case ISD::ADDE: Opc = X86ISD::ADC; ExtraOp = true; break;
8752 case ISD::SUBC: Opc = X86ISD::SUB; break;
8753 case ISD::SUBE: Opc = X86ISD::SBB; ExtraOp = true; break;
8754 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008755
Chris Lattner5b856542010-12-20 00:59:46 +00008756 if (!ExtraOp)
8757 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
8758 Op.getOperand(1));
8759 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
8760 Op.getOperand(1), Op.getOperand(2));
8761}
8762
Evan Cheng0db9fe62006-04-25 20:13:52 +00008763/// LowerOperation - Provide custom lowering hooks for some operations.
8764///
Dan Gohmand858e902010-04-17 15:26:15 +00008765SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00008766 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008767 default: llvm_unreachable("Should not custom lower this!");
Eric Christopher9a9d2752010-07-22 02:48:34 +00008768 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op,DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008769 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
8770 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008771 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00008772 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008773 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
8774 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
8775 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
David Greene91585092011-01-26 15:38:49 +00008776 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op, DAG);
David Greenecfe33c42011-01-26 19:13:22 +00008777 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008778 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
8779 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
8780 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00008781 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +00008782 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +00008783 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008784 case ISD::SHL_PARTS:
8785 case ISD::SRA_PARTS:
8786 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
8787 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00008788 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008789 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00008790 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008791 case ISD::FABS: return LowerFABS(Op, DAG);
8792 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008793 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00008794 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman30a0de92008-07-17 16:51:19 +00008795 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00008796 case ISD::SELECT: return LowerSELECT(Op, DAG);
8797 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008798 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008799 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +00008800 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +00008801 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008802 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00008803 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
8804 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008805 case ISD::FRAME_TO_ARGS_OFFSET:
8806 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008807 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008808 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008809 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +00008810 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +00008811 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
8812 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Mon P Wangaf9b9522008-12-18 21:42:19 +00008813 case ISD::MUL: return LowerMUL_V2I64(Op, DAG);
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008814 case ISD::SHL: return LowerSHL(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +00008815 case ISD::SADDO:
8816 case ISD::UADDO:
8817 case ISD::SSUBO:
8818 case ISD::USUBO:
8819 case ISD::SMULO:
8820 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +00008821 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008822 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Chris Lattner5b856542010-12-20 00:59:46 +00008823 case ISD::ADDC:
8824 case ISD::ADDE:
8825 case ISD::SUBC:
8826 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008827 }
Chris Lattner27a6c732007-11-24 07:07:01 +00008828}
8829
Duncan Sands1607f052008-12-01 11:39:25 +00008830void X86TargetLowering::
8831ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00008832 SelectionDAG &DAG, unsigned NewOp) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008833 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008834 DebugLoc dl = Node->getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00008835 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +00008836
8837 SDValue Chain = Node->getOperand(0);
8838 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00008839 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00008840 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00008841 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00008842 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +00008843 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +00008844 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +00008845 SDValue Result =
8846 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
8847 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +00008848 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +00008849 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00008850 Results.push_back(Result.getValue(2));
8851}
8852
Duncan Sands126d9072008-07-04 11:47:58 +00008853/// ReplaceNodeResults - Replace a node with an illegal result type
8854/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00008855void X86TargetLowering::ReplaceNodeResults(SDNode *N,
8856 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00008857 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +00008858 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +00008859 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +00008860 default:
Duncan Sands1607f052008-12-01 11:39:25 +00008861 assert(false && "Do not know how to custom type legalize this operation!");
8862 return;
Chris Lattner5b856542010-12-20 00:59:46 +00008863 case ISD::ADDC:
8864 case ISD::ADDE:
8865 case ISD::SUBC:
8866 case ISD::SUBE:
8867 // We don't want to expand or promote these.
8868 return;
Duncan Sands1607f052008-12-01 11:39:25 +00008869 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +00008870 std::pair<SDValue,SDValue> Vals =
8871 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +00008872 SDValue FIST = Vals.first, StackSlot = Vals.second;
8873 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +00008874 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +00008875 // Return a load from the stack slot.
Chris Lattner51abfe42010-09-21 06:02:19 +00008876 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
8877 MachinePointerInfo(), false, false, 0));
Duncan Sands1607f052008-12-01 11:39:25 +00008878 }
8879 return;
8880 }
8881 case ISD::READCYCLECOUNTER: {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00008882 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +00008883 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008884 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00008885 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +00008886 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00008887 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00008888 eax.getValue(2));
8889 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
8890 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +00008891 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00008892 Results.push_back(edx.getValue(1));
8893 return;
8894 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008895 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +00008896 EVT T = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008897 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
Duncan Sands1607f052008-12-01 11:39:25 +00008898 SDValue cpInL, cpInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00008899 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
8900 DAG.getConstant(0, MVT::i32));
8901 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
8902 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00008903 cpInL = DAG.getCopyToReg(N->getOperand(0), dl, X86::EAX, cpInL, SDValue());
8904 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl, X86::EDX, cpInH,
Duncan Sands1607f052008-12-01 11:39:25 +00008905 cpInL.getValue(1));
8906 SDValue swapInL, swapInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00008907 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
8908 DAG.getConstant(0, MVT::i32));
8909 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
8910 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00008911 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl, X86::EBX, swapInL,
Duncan Sands1607f052008-12-01 11:39:25 +00008912 cpInH.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00008913 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl, X86::ECX, swapInH,
Duncan Sands1607f052008-12-01 11:39:25 +00008914 swapInL.getValue(1));
8915 SDValue Ops[] = { swapInH.getValue(0),
8916 N->getOperand(1),
8917 swapInH.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00008918 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Andrew Trick1a2cf3b2010-10-11 19:02:04 +00008919 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
8920 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG8_DAG, dl, Tys,
8921 Ops, 3, T, MMO);
Dale Johannesendd64c412009-02-04 00:33:20 +00008922 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl, X86::EAX,
Owen Anderson825b72b2009-08-11 20:47:22 +00008923 MVT::i32, Result.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00008924 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl, X86::EDX,
Owen Anderson825b72b2009-08-11 20:47:22 +00008925 MVT::i32, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +00008926 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Owen Anderson825b72b2009-08-11 20:47:22 +00008927 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00008928 Results.push_back(cpOutH.getValue(1));
8929 return;
8930 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008931 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +00008932 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
8933 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008934 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +00008935 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
8936 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008937 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +00008938 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
8939 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008940 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +00008941 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
8942 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008943 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +00008944 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
8945 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008946 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +00008947 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
8948 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008949 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +00008950 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
8951 return;
Chris Lattner27a6c732007-11-24 07:07:01 +00008952 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00008953}
8954
Evan Cheng72261582005-12-20 06:22:03 +00008955const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
8956 switch (Opcode) {
8957 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +00008958 case X86ISD::BSF: return "X86ISD::BSF";
8959 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +00008960 case X86ISD::SHLD: return "X86ISD::SHLD";
8961 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00008962 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +00008963 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +00008964 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +00008965 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +00008966 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00008967 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00008968 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
8969 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
8970 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00008971 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00008972 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +00008973 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +00008974 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +00008975 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +00008976 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +00008977 case X86ISD::COMI: return "X86ISD::COMI";
8978 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +00008979 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +00008980 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Evan Cheng72261582005-12-20 06:22:03 +00008981 case X86ISD::CMOV: return "X86ISD::CMOV";
8982 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00008983 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00008984 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
8985 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +00008986 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00008987 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +00008988 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +00008989 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +00008990 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +00008991 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
8992 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +00008993 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +00008994 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Nate Begemanb65c1752010-12-17 22:55:37 +00008995 case X86ISD::PANDN: return "X86ISD::PANDN";
8996 case X86ISD::PSIGNB: return "X86ISD::PSIGNB";
8997 case X86ISD::PSIGNW: return "X86ISD::PSIGNW";
8998 case X86ISD::PSIGND: return "X86ISD::PSIGND";
Nate Begeman672fb622010-12-20 22:04:24 +00008999 case X86ISD::PBLENDVB: return "X86ISD::PBLENDVB";
Evan Cheng8ca29322006-11-10 21:43:37 +00009000 case X86ISD::FMAX: return "X86ISD::FMAX";
9001 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +00009002 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
9003 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00009004 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +00009005 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009006 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00009007 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009008 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +00009009 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
9010 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009011 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
9012 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
9013 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
9014 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
9015 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
9016 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +00009017 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
9018 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +00009019 case X86ISD::VSHL: return "X86ISD::VSHL";
9020 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +00009021 case X86ISD::CMPPD: return "X86ISD::CMPPD";
9022 case X86ISD::CMPPS: return "X86ISD::CMPPS";
9023 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
9024 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
9025 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
9026 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
9027 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
9028 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
9029 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
9030 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +00009031 case X86ISD::ADD: return "X86ISD::ADD";
9032 case X86ISD::SUB: return "X86ISD::SUB";
Chris Lattner5b856542010-12-20 00:59:46 +00009033 case X86ISD::ADC: return "X86ISD::ADC";
9034 case X86ISD::SBB: return "X86ISD::SBB";
Bill Wendlingd350e022008-12-12 21:15:41 +00009035 case X86ISD::SMUL: return "X86ISD::SMUL";
9036 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +00009037 case X86ISD::INC: return "X86ISD::INC";
9038 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +00009039 case X86ISD::OR: return "X86ISD::OR";
9040 case X86ISD::XOR: return "X86ISD::XOR";
9041 case X86ISD::AND: return "X86ISD::AND";
Evan Cheng73f24c92009-03-30 21:36:47 +00009042 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +00009043 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009044 case X86ISD::TESTP: return "X86ISD::TESTP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009045 case X86ISD::PALIGN: return "X86ISD::PALIGN";
9046 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
9047 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
9048 case X86ISD::PSHUFHW_LD: return "X86ISD::PSHUFHW_LD";
9049 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
9050 case X86ISD::PSHUFLW_LD: return "X86ISD::PSHUFLW_LD";
9051 case X86ISD::SHUFPS: return "X86ISD::SHUFPS";
9052 case X86ISD::SHUFPD: return "X86ISD::SHUFPD";
9053 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009054 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00009055 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009056 case X86ISD::MOVHLPD: return "X86ISD::MOVHLPD";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00009057 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
9058 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009059 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
9060 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
9061 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
9062 case X86ISD::MOVSHDUP_LD: return "X86ISD::MOVSHDUP_LD";
9063 case X86ISD::MOVSLDUP_LD: return "X86ISD::MOVSLDUP_LD";
9064 case X86ISD::MOVSD: return "X86ISD::MOVSD";
9065 case X86ISD::MOVSS: return "X86ISD::MOVSS";
9066 case X86ISD::UNPCKLPS: return "X86ISD::UNPCKLPS";
9067 case X86ISD::UNPCKLPD: return "X86ISD::UNPCKLPD";
9068 case X86ISD::UNPCKHPS: return "X86ISD::UNPCKHPS";
9069 case X86ISD::UNPCKHPD: return "X86ISD::UNPCKHPD";
9070 case X86ISD::PUNPCKLBW: return "X86ISD::PUNPCKLBW";
9071 case X86ISD::PUNPCKLWD: return "X86ISD::PUNPCKLWD";
9072 case X86ISD::PUNPCKLDQ: return "X86ISD::PUNPCKLDQ";
9073 case X86ISD::PUNPCKLQDQ: return "X86ISD::PUNPCKLQDQ";
9074 case X86ISD::PUNPCKHBW: return "X86ISD::PUNPCKHBW";
9075 case X86ISD::PUNPCKHWD: return "X86ISD::PUNPCKHWD";
9076 case X86ISD::PUNPCKHDQ: return "X86ISD::PUNPCKHDQ";
9077 case X86ISD::PUNPCKHQDQ: return "X86ISD::PUNPCKHQDQ";
Dan Gohmand6708ea2009-08-15 01:38:56 +00009078 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Dan Gohman320afb82010-10-12 18:00:49 +00009079 case X86ISD::VAARG_64: return "X86ISD::VAARG_64";
Michael J. Spencere9c253e2010-10-21 01:41:01 +00009080 case X86ISD::WIN_ALLOCA: return "X86ISD::WIN_ALLOCA";
Evan Cheng72261582005-12-20 06:22:03 +00009081 }
9082}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00009083
Chris Lattnerc9addb72007-03-30 23:15:24 +00009084// isLegalAddressingMode - Return true if the addressing mode represented
9085// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +00009086bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerc9addb72007-03-30 23:15:24 +00009087 const Type *Ty) const {
9088 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009089 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +00009090 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +00009091
Chris Lattnerc9addb72007-03-30 23:15:24 +00009092 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009093 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +00009094 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00009095
Chris Lattnerc9addb72007-03-30 23:15:24 +00009096 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +00009097 unsigned GVFlags =
9098 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009099
Chris Lattnerdfed4132009-07-10 07:38:24 +00009100 // If a reference to this global requires an extra load, we can't fold it.
9101 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +00009102 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009103
Chris Lattnerdfed4132009-07-10 07:38:24 +00009104 // If BaseGV requires a register for the PIC base, we cannot also have a
9105 // BaseReg specified.
9106 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +00009107 return false;
Evan Cheng52787842007-08-01 23:46:47 +00009108
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009109 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +00009110 if ((M != CodeModel::Small || R != Reloc::Static) &&
9111 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009112 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +00009113 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009114
Chris Lattnerc9addb72007-03-30 23:15:24 +00009115 switch (AM.Scale) {
9116 case 0:
9117 case 1:
9118 case 2:
9119 case 4:
9120 case 8:
9121 // These scales always work.
9122 break;
9123 case 3:
9124 case 5:
9125 case 9:
9126 // These scales are formed with basereg+scalereg. Only accept if there is
9127 // no basereg yet.
9128 if (AM.HasBaseReg)
9129 return false;
9130 break;
9131 default: // Other stuff never works.
9132 return false;
9133 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009134
Chris Lattnerc9addb72007-03-30 23:15:24 +00009135 return true;
9136}
9137
9138
Evan Cheng2bd122c2007-10-26 01:56:11 +00009139bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00009140 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +00009141 return false;
Evan Chenge127a732007-10-29 07:57:50 +00009142 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
9143 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00009144 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +00009145 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00009146 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +00009147}
9148
Owen Andersone50ed302009-08-10 22:56:29 +00009149bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +00009150 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009151 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009152 unsigned NumBits1 = VT1.getSizeInBits();
9153 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00009154 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009155 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00009156 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009157}
Evan Cheng2bd122c2007-10-26 01:56:11 +00009158
Dan Gohman97121ba2009-04-08 00:15:30 +00009159bool X86TargetLowering::isZExtFree(const Type *Ty1, const Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00009160 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00009161 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00009162}
9163
Owen Andersone50ed302009-08-10 22:56:29 +00009164bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00009165 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00009166 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00009167}
9168
Owen Andersone50ed302009-08-10 22:56:29 +00009169bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +00009170 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +00009171 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +00009172}
9173
Evan Cheng60c07e12006-07-05 22:17:51 +00009174/// isShuffleMaskLegal - Targets can use this to indicate that they only
9175/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
9176/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
9177/// are assumed to be legal.
9178bool
Eric Christopherfd179292009-08-27 18:07:15 +00009179X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +00009180 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +00009181 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +00009182 if (VT.getSizeInBits() == 64)
Eric Christophercff6f852010-04-15 01:40:20 +00009183 return isPALIGNRMask(M, VT, Subtarget->hasSSSE3());
Nate Begeman9008ca62009-04-27 18:41:29 +00009184
Nate Begemana09008b2009-10-19 02:17:23 +00009185 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +00009186 return (VT.getVectorNumElements() == 2 ||
9187 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
9188 isMOVLMask(M, VT) ||
9189 isSHUFPMask(M, VT) ||
9190 isPSHUFDMask(M, VT) ||
9191 isPSHUFHWMask(M, VT) ||
9192 isPSHUFLWMask(M, VT) ||
Nate Begemana09008b2009-10-19 02:17:23 +00009193 isPALIGNRMask(M, VT, Subtarget->hasSSSE3()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +00009194 isUNPCKLMask(M, VT) ||
9195 isUNPCKHMask(M, VT) ||
9196 isUNPCKL_v_undef_Mask(M, VT) ||
9197 isUNPCKH_v_undef_Mask(M, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00009198}
9199
Dan Gohman7d8143f2008-04-09 20:09:42 +00009200bool
Nate Begeman5a5ca152009-04-29 05:20:52 +00009201X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +00009202 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00009203 unsigned NumElts = VT.getVectorNumElements();
9204 // FIXME: This collection of masks seems suspect.
9205 if (NumElts == 2)
9206 return true;
9207 if (NumElts == 4 && VT.getSizeInBits() == 128) {
9208 return (isMOVLMask(Mask, VT) ||
9209 isCommutedMOVLMask(Mask, VT, true) ||
9210 isSHUFPMask(Mask, VT) ||
9211 isCommutedSHUFPMask(Mask, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00009212 }
9213 return false;
9214}
9215
9216//===----------------------------------------------------------------------===//
9217// X86 Scheduler Hooks
9218//===----------------------------------------------------------------------===//
9219
Mon P Wang63307c32008-05-05 19:05:59 +00009220// private utility function
9221MachineBasicBlock *
9222X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
9223 MachineBasicBlock *MBB,
9224 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009225 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009226 unsigned LoadOpc,
9227 unsigned CXchgOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009228 unsigned notOpc,
9229 unsigned EAXreg,
9230 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009231 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00009232 // For the atomic bitwise operator, we generate
9233 // thisMBB:
9234 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00009235 // ld t1 = [bitinstr.addr]
9236 // op t2 = t1, [bitinstr.val]
9237 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00009238 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
9239 // bz newMBB
9240 // fallthrough -->nextMBB
9241 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9242 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009243 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00009244 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009245
Mon P Wang63307c32008-05-05 19:05:59 +00009246 /// First build the CFG
9247 MachineFunction *F = MBB->getParent();
9248 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009249 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9250 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9251 F->insert(MBBIter, newMBB);
9252 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009253
Dan Gohman14152b42010-07-06 20:24:04 +00009254 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9255 nextMBB->splice(nextMBB->begin(), thisMBB,
9256 llvm::next(MachineBasicBlock::iterator(bInstr)),
9257 thisMBB->end());
9258 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009259
Mon P Wang63307c32008-05-05 19:05:59 +00009260 // Update thisMBB to fall through to newMBB
9261 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009262
Mon P Wang63307c32008-05-05 19:05:59 +00009263 // newMBB jumps to itself and fall through to nextMBB
9264 newMBB->addSuccessor(nextMBB);
9265 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009266
Mon P Wang63307c32008-05-05 19:05:59 +00009267 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009268 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009269 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +00009270 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00009271 MachineOperand& destOper = bInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009272 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00009273 int numArgs = bInstr->getNumOperands() - 1;
9274 for (int i=0; i < numArgs; ++i)
9275 argOpers[i] = &bInstr->getOperand(i+1);
9276
9277 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009278 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009279 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00009280
Dale Johannesen140be2d2008-08-19 18:47:28 +00009281 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009282 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00009283 for (int i=0; i <= lastAddrIndx; ++i)
9284 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009285
Dale Johannesen140be2d2008-08-19 18:47:28 +00009286 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009287 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00009288 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009289 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009290 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009291 tt = t1;
9292
Dale Johannesen140be2d2008-08-19 18:47:28 +00009293 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +00009294 assert((argOpers[valArgIndx]->isReg() ||
9295 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00009296 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +00009297 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009298 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00009299 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009300 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009301 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +00009302 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009303
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009304 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +00009305 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009306
Dale Johannesene4d209d2009-02-03 20:21:25 +00009307 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +00009308 for (int i=0; i <= lastAddrIndx; ++i)
9309 (*MIB).addOperand(*argOpers[i]);
9310 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +00009311 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00009312 (*MIB).setMemRefs(bInstr->memoperands_begin(),
9313 bInstr->memoperands_end());
Mon P Wangf5952662008-07-17 04:54:06 +00009314
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009315 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +00009316 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +00009317
Mon P Wang63307c32008-05-05 19:05:59 +00009318 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009319 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00009320
Dan Gohman14152b42010-07-06 20:24:04 +00009321 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00009322 return nextMBB;
9323}
9324
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00009325// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +00009326MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009327X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
9328 MachineBasicBlock *MBB,
9329 unsigned regOpcL,
9330 unsigned regOpcH,
9331 unsigned immOpcL,
9332 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009333 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009334 // For the atomic bitwise operator, we generate
9335 // thisMBB (instructions are in pairs, except cmpxchg8b)
9336 // ld t1,t2 = [bitinstr.addr]
9337 // newMBB:
9338 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
9339 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +00009340 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009341 // mov ECX, EBX <- t5, t6
9342 // mov EAX, EDX <- t1, t2
9343 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
9344 // mov t3, t4 <- EAX, EDX
9345 // bz newMBB
9346 // result in out1, out2
9347 // fallthrough -->nextMBB
9348
9349 const TargetRegisterClass *RC = X86::GR32RegisterClass;
9350 const unsigned LoadOpc = X86::MOV32rm;
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009351 const unsigned NotOpc = X86::NOT32r;
9352 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9353 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
9354 MachineFunction::iterator MBBIter = MBB;
9355 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009356
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009357 /// First build the CFG
9358 MachineFunction *F = MBB->getParent();
9359 MachineBasicBlock *thisMBB = MBB;
9360 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9361 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9362 F->insert(MBBIter, newMBB);
9363 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009364
Dan Gohman14152b42010-07-06 20:24:04 +00009365 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9366 nextMBB->splice(nextMBB->begin(), thisMBB,
9367 llvm::next(MachineBasicBlock::iterator(bInstr)),
9368 thisMBB->end());
9369 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009370
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009371 // Update thisMBB to fall through to newMBB
9372 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009373
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009374 // newMBB jumps to itself and fall through to nextMBB
9375 newMBB->addSuccessor(nextMBB);
9376 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009377
Dale Johannesene4d209d2009-02-03 20:21:25 +00009378 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009379 // Insert instructions into newMBB based on incoming instruction
9380 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009381 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009382 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009383 MachineOperand& dest1Oper = bInstr->getOperand(0);
9384 MachineOperand& dest2Oper = bInstr->getOperand(1);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009385 MachineOperand* argOpers[2 + X86::AddrNumOperands];
9386 for (int i=0; i < 2 + X86::AddrNumOperands; ++i) {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009387 argOpers[i] = &bInstr->getOperand(i+2);
9388
Dan Gohman71ea4e52010-05-14 21:01:44 +00009389 // We use some of the operands multiple times, so conservatively just
9390 // clear any kill flags that might be present.
9391 if (argOpers[i]->isReg() && argOpers[i]->isUse())
9392 argOpers[i]->setIsKill(false);
9393 }
9394
Evan Chengad5b52f2010-01-08 19:14:57 +00009395 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009396 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +00009397
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009398 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009399 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009400 for (int i=0; i <= lastAddrIndx; ++i)
9401 (*MIB).addOperand(*argOpers[i]);
9402 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009403 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +00009404 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +00009405 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009406 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +00009407 MachineOperand newOp3 = *(argOpers[3]);
9408 if (newOp3.isImm())
9409 newOp3.setImm(newOp3.getImm()+4);
9410 else
9411 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009412 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +00009413 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009414
9415 // t3/4 are defined later, at the bottom of the loop
9416 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
9417 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009418 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009419 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009420 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009421 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
9422
Evan Cheng306b4ca2010-01-08 23:41:50 +00009423 // The subsequent operations should be using the destination registers of
9424 //the PHI instructions.
Scott Michelfdc40a02009-02-17 22:15:04 +00009425 if (invSrc) {
Evan Cheng306b4ca2010-01-08 23:41:50 +00009426 t1 = F->getRegInfo().createVirtualRegister(RC);
9427 t2 = F->getRegInfo().createVirtualRegister(RC);
9428 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t1).addReg(dest1Oper.getReg());
9429 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t2).addReg(dest2Oper.getReg());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009430 } else {
Evan Cheng306b4ca2010-01-08 23:41:50 +00009431 t1 = dest1Oper.getReg();
9432 t2 = dest2Oper.getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009433 }
9434
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009435 int valArgIndx = lastAddrIndx + 1;
9436 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +00009437 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009438 "invalid operand");
9439 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
9440 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009441 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009442 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009443 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009444 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +00009445 if (regOpcL != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00009446 MIB.addReg(t1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009447 (*MIB).addOperand(*argOpers[valArgIndx]);
9448 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00009449 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009450 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00009451 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009452 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009453 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009454 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009455 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +00009456 if (regOpcH != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00009457 MIB.addReg(t2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009458 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009459
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009460 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009461 MIB.addReg(t1);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009462 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009463 MIB.addReg(t2);
9464
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009465 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009466 MIB.addReg(t5);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009467 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009468 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +00009469
Dale Johannesene4d209d2009-02-03 20:21:25 +00009470 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009471 for (int i=0; i <= lastAddrIndx; ++i)
9472 (*MIB).addOperand(*argOpers[i]);
9473
9474 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00009475 (*MIB).setMemRefs(bInstr->memoperands_begin(),
9476 bInstr->memoperands_end());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009477
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009478 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009479 MIB.addReg(X86::EAX);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009480 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009481 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +00009482
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009483 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009484 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009485
Dan Gohman14152b42010-07-06 20:24:04 +00009486 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009487 return nextMBB;
9488}
9489
9490// private utility function
9491MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +00009492X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
9493 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009494 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00009495 // For the atomic min/max operator, we generate
9496 // thisMBB:
9497 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00009498 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +00009499 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +00009500 // cmp t1, t2
9501 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +00009502 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00009503 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
9504 // bz newMBB
9505 // fallthrough -->nextMBB
9506 //
9507 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9508 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009509 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00009510 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009511
Mon P Wang63307c32008-05-05 19:05:59 +00009512 /// First build the CFG
9513 MachineFunction *F = MBB->getParent();
9514 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009515 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9516 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9517 F->insert(MBBIter, newMBB);
9518 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009519
Dan Gohman14152b42010-07-06 20:24:04 +00009520 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9521 nextMBB->splice(nextMBB->begin(), thisMBB,
9522 llvm::next(MachineBasicBlock::iterator(mInstr)),
9523 thisMBB->end());
9524 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009525
Mon P Wang63307c32008-05-05 19:05:59 +00009526 // Update thisMBB to fall through to newMBB
9527 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009528
Mon P Wang63307c32008-05-05 19:05:59 +00009529 // newMBB jumps to newMBB and fall through to nextMBB
9530 newMBB->addSuccessor(nextMBB);
9531 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009532
Dale Johannesene4d209d2009-02-03 20:21:25 +00009533 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00009534 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009535 assert(mInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009536 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +00009537 MachineOperand& destOper = mInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009538 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00009539 int numArgs = mInstr->getNumOperands() - 1;
9540 for (int i=0; i < numArgs; ++i)
9541 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009542
Mon P Wang63307c32008-05-05 19:05:59 +00009543 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009544 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009545 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00009546
Mon P Wangab3e7472008-05-05 22:56:23 +00009547 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009548 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00009549 for (int i=0; i <= lastAddrIndx; ++i)
9550 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +00009551
Mon P Wang63307c32008-05-05 19:05:59 +00009552 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +00009553 assert((argOpers[valArgIndx]->isReg() ||
9554 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00009555 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +00009556
9557 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +00009558 if (argOpers[valArgIndx]->isReg())
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009559 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +00009560 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009561 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00009562 (*MIB).addOperand(*argOpers[valArgIndx]);
9563
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009564 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +00009565 MIB.addReg(t1);
9566
Dale Johannesene4d209d2009-02-03 20:21:25 +00009567 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +00009568 MIB.addReg(t1);
9569 MIB.addReg(t2);
9570
9571 // Generate movc
9572 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009573 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +00009574 MIB.addReg(t2);
9575 MIB.addReg(t1);
9576
9577 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +00009578 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +00009579 for (int i=0; i <= lastAddrIndx; ++i)
9580 (*MIB).addOperand(*argOpers[i]);
9581 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +00009582 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00009583 (*MIB).setMemRefs(mInstr->memoperands_begin(),
9584 mInstr->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +00009585
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009586 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +00009587 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +00009588
Mon P Wang63307c32008-05-05 19:05:59 +00009589 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009590 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00009591
Dan Gohman14152b42010-07-06 20:24:04 +00009592 mInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00009593 return nextMBB;
9594}
9595
Eric Christopherf83a5de2009-08-27 18:08:16 +00009596// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00009597// or XMM0_V32I8 in AVX all of this code can be replaced with that
9598// in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +00009599MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +00009600X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +00009601 unsigned numArgs, bool memArg) const {
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00009602 assert((Subtarget->hasSSE42() || Subtarget->hasAVX()) &&
9603 "Target must have SSE4.2 or AVX features enabled");
9604
Eric Christopherb120ab42009-08-18 22:50:32 +00009605 DebugLoc dl = MI->getDebugLoc();
9606 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Eric Christopherb120ab42009-08-18 22:50:32 +00009607 unsigned Opc;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00009608 if (!Subtarget->hasAVX()) {
9609 if (memArg)
9610 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
9611 else
9612 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
9613 } else {
9614 if (memArg)
9615 Opc = numArgs == 3 ? X86::VPCMPISTRM128rm : X86::VPCMPESTRM128rm;
9616 else
9617 Opc = numArgs == 3 ? X86::VPCMPISTRM128rr : X86::VPCMPESTRM128rr;
9618 }
Eric Christopherb120ab42009-08-18 22:50:32 +00009619
Eric Christopher41c902f2010-11-30 08:20:21 +00009620 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Eric Christopherb120ab42009-08-18 22:50:32 +00009621 for (unsigned i = 0; i < numArgs; ++i) {
9622 MachineOperand &Op = MI->getOperand(i+1);
Eric Christopherb120ab42009-08-18 22:50:32 +00009623 if (!(Op.isReg() && Op.isImplicit()))
9624 MIB.addOperand(Op);
9625 }
Eric Christopher41c902f2010-11-30 08:20:21 +00009626 BuildMI(*BB, MI, dl, TII->get(X86::MOVAPSrr), MI->getOperand(0).getReg())
Eric Christopherb120ab42009-08-18 22:50:32 +00009627 .addReg(X86::XMM0);
9628
Dan Gohman14152b42010-07-06 20:24:04 +00009629 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +00009630 return BB;
9631}
9632
9633MachineBasicBlock *
Eric Christopher228232b2010-11-30 07:20:12 +00009634X86TargetLowering::EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +00009635 DebugLoc dl = MI->getDebugLoc();
9636 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009637
Eric Christopher228232b2010-11-30 07:20:12 +00009638 // Address into RAX/EAX, other two args into ECX, EDX.
9639 unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
9640 unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
9641 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg);
9642 for (int i = 0; i < X86::AddrNumOperands; ++i)
Eric Christopher82be2202010-11-30 08:10:28 +00009643 MIB.addOperand(MI->getOperand(i));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009644
Eric Christopher228232b2010-11-30 07:20:12 +00009645 unsigned ValOps = X86::AddrNumOperands;
9646 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
9647 .addReg(MI->getOperand(ValOps).getReg());
9648 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX)
9649 .addReg(MI->getOperand(ValOps+1).getReg());
9650
9651 // The instruction doesn't actually take any operands though.
9652 BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009653
Eric Christopher228232b2010-11-30 07:20:12 +00009654 MI->eraseFromParent(); // The pseudo is gone now.
9655 return BB;
9656}
9657
9658MachineBasicBlock *
9659X86TargetLowering::EmitMwait(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +00009660 DebugLoc dl = MI->getDebugLoc();
9661 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009662
Eric Christopher228232b2010-11-30 07:20:12 +00009663 // First arg in ECX, the second in EAX.
9664 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
9665 .addReg(MI->getOperand(0).getReg());
9666 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EAX)
9667 .addReg(MI->getOperand(1).getReg());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009668
Eric Christopher228232b2010-11-30 07:20:12 +00009669 // The instruction doesn't actually take any operands though.
9670 BuildMI(*BB, MI, dl, TII->get(X86::MWAITrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009671
Eric Christopher228232b2010-11-30 07:20:12 +00009672 MI->eraseFromParent(); // The pseudo is gone now.
9673 return BB;
9674}
9675
9676MachineBasicBlock *
Dan Gohman320afb82010-10-12 18:00:49 +00009677X86TargetLowering::EmitVAARG64WithCustomInserter(
9678 MachineInstr *MI,
9679 MachineBasicBlock *MBB) const {
9680 // Emit va_arg instruction on X86-64.
9681
9682 // Operands to this pseudo-instruction:
9683 // 0 ) Output : destination address (reg)
9684 // 1-5) Input : va_list address (addr, i64mem)
9685 // 6 ) ArgSize : Size (in bytes) of vararg type
9686 // 7 ) ArgMode : 0=overflow only, 1=use gp_offset, 2=use fp_offset
9687 // 8 ) Align : Alignment of type
9688 // 9 ) EFLAGS (implicit-def)
9689
9690 assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
9691 assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands");
9692
9693 unsigned DestReg = MI->getOperand(0).getReg();
9694 MachineOperand &Base = MI->getOperand(1);
9695 MachineOperand &Scale = MI->getOperand(2);
9696 MachineOperand &Index = MI->getOperand(3);
9697 MachineOperand &Disp = MI->getOperand(4);
9698 MachineOperand &Segment = MI->getOperand(5);
9699 unsigned ArgSize = MI->getOperand(6).getImm();
9700 unsigned ArgMode = MI->getOperand(7).getImm();
9701 unsigned Align = MI->getOperand(8).getImm();
9702
9703 // Memory Reference
9704 assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand");
9705 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
9706 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
9707
9708 // Machine Information
9709 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9710 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
9711 const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64);
9712 const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32);
9713 DebugLoc DL = MI->getDebugLoc();
9714
9715 // struct va_list {
9716 // i32 gp_offset
9717 // i32 fp_offset
9718 // i64 overflow_area (address)
9719 // i64 reg_save_area (address)
9720 // }
9721 // sizeof(va_list) = 24
9722 // alignment(va_list) = 8
9723
9724 unsigned TotalNumIntRegs = 6;
9725 unsigned TotalNumXMMRegs = 8;
9726 bool UseGPOffset = (ArgMode == 1);
9727 bool UseFPOffset = (ArgMode == 2);
9728 unsigned MaxOffset = TotalNumIntRegs * 8 +
9729 (UseFPOffset ? TotalNumXMMRegs * 16 : 0);
9730
9731 /* Align ArgSize to a multiple of 8 */
9732 unsigned ArgSizeA8 = (ArgSize + 7) & ~7;
9733 bool NeedsAlign = (Align > 8);
9734
9735 MachineBasicBlock *thisMBB = MBB;
9736 MachineBasicBlock *overflowMBB;
9737 MachineBasicBlock *offsetMBB;
9738 MachineBasicBlock *endMBB;
9739
9740 unsigned OffsetDestReg = 0; // Argument address computed by offsetMBB
9741 unsigned OverflowDestReg = 0; // Argument address computed by overflowMBB
9742 unsigned OffsetReg = 0;
9743
9744 if (!UseGPOffset && !UseFPOffset) {
9745 // If we only pull from the overflow region, we don't create a branch.
9746 // We don't need to alter control flow.
9747 OffsetDestReg = 0; // unused
9748 OverflowDestReg = DestReg;
9749
9750 offsetMBB = NULL;
9751 overflowMBB = thisMBB;
9752 endMBB = thisMBB;
9753 } else {
9754 // First emit code to check if gp_offset (or fp_offset) is below the bound.
9755 // If so, pull the argument from reg_save_area. (branch to offsetMBB)
9756 // If not, pull from overflow_area. (branch to overflowMBB)
9757 //
9758 // thisMBB
9759 // | .
9760 // | .
9761 // offsetMBB overflowMBB
9762 // | .
9763 // | .
9764 // endMBB
9765
9766 // Registers for the PHI in endMBB
9767 OffsetDestReg = MRI.createVirtualRegister(AddrRegClass);
9768 OverflowDestReg = MRI.createVirtualRegister(AddrRegClass);
9769
9770 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
9771 MachineFunction *MF = MBB->getParent();
9772 overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB);
9773 offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB);
9774 endMBB = MF->CreateMachineBasicBlock(LLVM_BB);
9775
9776 MachineFunction::iterator MBBIter = MBB;
9777 ++MBBIter;
9778
9779 // Insert the new basic blocks
9780 MF->insert(MBBIter, offsetMBB);
9781 MF->insert(MBBIter, overflowMBB);
9782 MF->insert(MBBIter, endMBB);
9783
9784 // Transfer the remainder of MBB and its successor edges to endMBB.
9785 endMBB->splice(endMBB->begin(), thisMBB,
9786 llvm::next(MachineBasicBlock::iterator(MI)),
9787 thisMBB->end());
9788 endMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
9789
9790 // Make offsetMBB and overflowMBB successors of thisMBB
9791 thisMBB->addSuccessor(offsetMBB);
9792 thisMBB->addSuccessor(overflowMBB);
9793
9794 // endMBB is a successor of both offsetMBB and overflowMBB
9795 offsetMBB->addSuccessor(endMBB);
9796 overflowMBB->addSuccessor(endMBB);
9797
9798 // Load the offset value into a register
9799 OffsetReg = MRI.createVirtualRegister(OffsetRegClass);
9800 BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg)
9801 .addOperand(Base)
9802 .addOperand(Scale)
9803 .addOperand(Index)
9804 .addDisp(Disp, UseFPOffset ? 4 : 0)
9805 .addOperand(Segment)
9806 .setMemRefs(MMOBegin, MMOEnd);
9807
9808 // Check if there is enough room left to pull this argument.
9809 BuildMI(thisMBB, DL, TII->get(X86::CMP32ri))
9810 .addReg(OffsetReg)
9811 .addImm(MaxOffset + 8 - ArgSizeA8);
9812
9813 // Branch to "overflowMBB" if offset >= max
9814 // Fall through to "offsetMBB" otherwise
9815 BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE)))
9816 .addMBB(overflowMBB);
9817 }
9818
9819 // In offsetMBB, emit code to use the reg_save_area.
9820 if (offsetMBB) {
9821 assert(OffsetReg != 0);
9822
9823 // Read the reg_save_area address.
9824 unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass);
9825 BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg)
9826 .addOperand(Base)
9827 .addOperand(Scale)
9828 .addOperand(Index)
9829 .addDisp(Disp, 16)
9830 .addOperand(Segment)
9831 .setMemRefs(MMOBegin, MMOEnd);
9832
9833 // Zero-extend the offset
9834 unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass);
9835 BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64)
9836 .addImm(0)
9837 .addReg(OffsetReg)
9838 .addImm(X86::sub_32bit);
9839
9840 // Add the offset to the reg_save_area to get the final address.
9841 BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg)
9842 .addReg(OffsetReg64)
9843 .addReg(RegSaveReg);
9844
9845 // Compute the offset for the next argument
9846 unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass);
9847 BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg)
9848 .addReg(OffsetReg)
9849 .addImm(UseFPOffset ? 16 : 8);
9850
9851 // Store it back into the va_list.
9852 BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr))
9853 .addOperand(Base)
9854 .addOperand(Scale)
9855 .addOperand(Index)
9856 .addDisp(Disp, UseFPOffset ? 4 : 0)
9857 .addOperand(Segment)
9858 .addReg(NextOffsetReg)
9859 .setMemRefs(MMOBegin, MMOEnd);
9860
9861 // Jump to endMBB
9862 BuildMI(offsetMBB, DL, TII->get(X86::JMP_4))
9863 .addMBB(endMBB);
9864 }
9865
9866 //
9867 // Emit code to use overflow area
9868 //
9869
9870 // Load the overflow_area address into a register.
9871 unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass);
9872 BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg)
9873 .addOperand(Base)
9874 .addOperand(Scale)
9875 .addOperand(Index)
9876 .addDisp(Disp, 8)
9877 .addOperand(Segment)
9878 .setMemRefs(MMOBegin, MMOEnd);
9879
9880 // If we need to align it, do so. Otherwise, just copy the address
9881 // to OverflowDestReg.
9882 if (NeedsAlign) {
9883 // Align the overflow address
9884 assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2");
9885 unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass);
9886
9887 // aligned_addr = (addr + (align-1)) & ~(align-1)
9888 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg)
9889 .addReg(OverflowAddrReg)
9890 .addImm(Align-1);
9891
9892 BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg)
9893 .addReg(TmpReg)
9894 .addImm(~(uint64_t)(Align-1));
9895 } else {
9896 BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg)
9897 .addReg(OverflowAddrReg);
9898 }
9899
9900 // Compute the next overflow address after this argument.
9901 // (the overflow address should be kept 8-byte aligned)
9902 unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass);
9903 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg)
9904 .addReg(OverflowDestReg)
9905 .addImm(ArgSizeA8);
9906
9907 // Store the new overflow address.
9908 BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr))
9909 .addOperand(Base)
9910 .addOperand(Scale)
9911 .addOperand(Index)
9912 .addDisp(Disp, 8)
9913 .addOperand(Segment)
9914 .addReg(NextAddrReg)
9915 .setMemRefs(MMOBegin, MMOEnd);
9916
9917 // If we branched, emit the PHI to the front of endMBB.
9918 if (offsetMBB) {
9919 BuildMI(*endMBB, endMBB->begin(), DL,
9920 TII->get(X86::PHI), DestReg)
9921 .addReg(OffsetDestReg).addMBB(offsetMBB)
9922 .addReg(OverflowDestReg).addMBB(overflowMBB);
9923 }
9924
9925 // Erase the pseudo instruction
9926 MI->eraseFromParent();
9927
9928 return endMBB;
9929}
9930
9931MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +00009932X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
9933 MachineInstr *MI,
9934 MachineBasicBlock *MBB) const {
9935 // Emit code to save XMM registers to the stack. The ABI says that the
9936 // number of registers to save is given in %al, so it's theoretically
9937 // possible to do an indirect jump trick to avoid saving all of them,
9938 // however this code takes a simpler approach and just executes all
9939 // of the stores if %al is non-zero. It's less code, and it's probably
9940 // easier on the hardware branch predictor, and stores aren't all that
9941 // expensive anyway.
9942
9943 // Create the new basic blocks. One block contains all the XMM stores,
9944 // and one block is the final destination regardless of whether any
9945 // stores were performed.
9946 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
9947 MachineFunction *F = MBB->getParent();
9948 MachineFunction::iterator MBBIter = MBB;
9949 ++MBBIter;
9950 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
9951 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
9952 F->insert(MBBIter, XMMSaveMBB);
9953 F->insert(MBBIter, EndMBB);
9954
Dan Gohman14152b42010-07-06 20:24:04 +00009955 // Transfer the remainder of MBB and its successor edges to EndMBB.
9956 EndMBB->splice(EndMBB->begin(), MBB,
9957 llvm::next(MachineBasicBlock::iterator(MI)),
9958 MBB->end());
9959 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
9960
Dan Gohmand6708ea2009-08-15 01:38:56 +00009961 // The original block will now fall through to the XMM save block.
9962 MBB->addSuccessor(XMMSaveMBB);
9963 // The XMMSaveMBB will fall through to the end block.
9964 XMMSaveMBB->addSuccessor(EndMBB);
9965
9966 // Now add the instructions.
9967 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9968 DebugLoc DL = MI->getDebugLoc();
9969
9970 unsigned CountReg = MI->getOperand(0).getReg();
9971 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
9972 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
9973
9974 if (!Subtarget->isTargetWin64()) {
9975 // If %al is 0, branch around the XMM save block.
9976 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009977 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +00009978 MBB->addSuccessor(EndMBB);
9979 }
9980
9981 // In the XMM save block, save all the XMM argument registers.
9982 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
9983 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +00009984 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +00009985 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +00009986 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +00009987 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +00009988 /*Size=*/16, /*Align=*/16);
Dan Gohmand6708ea2009-08-15 01:38:56 +00009989 BuildMI(XMMSaveMBB, DL, TII->get(X86::MOVAPSmr))
9990 .addFrameIndex(RegSaveFrameIndex)
9991 .addImm(/*Scale=*/1)
9992 .addReg(/*IndexReg=*/0)
9993 .addImm(/*Disp=*/Offset)
9994 .addReg(/*Segment=*/0)
9995 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +00009996 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +00009997 }
9998
Dan Gohman14152b42010-07-06 20:24:04 +00009999 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +000010000
10001 return EndMBB;
10002}
Mon P Wang63307c32008-05-05 19:05:59 +000010003
Evan Cheng60c07e12006-07-05 22:17:51 +000010004MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +000010005X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010006 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +000010007 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10008 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +000010009
Chris Lattner52600972009-09-02 05:57:00 +000010010 // To "insert" a SELECT_CC instruction, we actually have to insert the
10011 // diamond control-flow pattern. The incoming instruction knows the
10012 // destination vreg to set, the condition code register to branch on, the
10013 // true/false values to select between, and a branch opcode to use.
10014 const BasicBlock *LLVM_BB = BB->getBasicBlock();
10015 MachineFunction::iterator It = BB;
10016 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +000010017
Chris Lattner52600972009-09-02 05:57:00 +000010018 // thisMBB:
10019 // ...
10020 // TrueVal = ...
10021 // cmpTY ccX, r1, r2
10022 // bCC copy1MBB
10023 // fallthrough --> copy0MBB
10024 MachineBasicBlock *thisMBB = BB;
10025 MachineFunction *F = BB->getParent();
10026 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
10027 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +000010028 F->insert(It, copy0MBB);
10029 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +000010030
Bill Wendling730c07e2010-06-25 20:48:10 +000010031 // If the EFLAGS register isn't dead in the terminator, then claim that it's
10032 // live into the sink and copy blocks.
10033 const MachineFunction *MF = BB->getParent();
10034 const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo();
10035 BitVector ReservedRegs = TRI->getReservedRegs(*MF);
Bill Wendling730c07e2010-06-25 20:48:10 +000010036
Dan Gohman14152b42010-07-06 20:24:04 +000010037 for (unsigned I = 0, E = MI->getNumOperands(); I != E; ++I) {
10038 const MachineOperand &MO = MI->getOperand(I);
10039 if (!MO.isReg() || !MO.isUse() || MO.isKill()) continue;
Bill Wendling730c07e2010-06-25 20:48:10 +000010040 unsigned Reg = MO.getReg();
10041 if (Reg != X86::EFLAGS) continue;
10042 copy0MBB->addLiveIn(Reg);
10043 sinkMBB->addLiveIn(Reg);
10044 }
10045
Dan Gohman14152b42010-07-06 20:24:04 +000010046 // Transfer the remainder of BB and its successor edges to sinkMBB.
10047 sinkMBB->splice(sinkMBB->begin(), BB,
10048 llvm::next(MachineBasicBlock::iterator(MI)),
10049 BB->end());
10050 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
10051
10052 // Add the true and fallthrough blocks as its successors.
10053 BB->addSuccessor(copy0MBB);
10054 BB->addSuccessor(sinkMBB);
10055
10056 // Create the conditional branch instruction.
10057 unsigned Opc =
10058 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
10059 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
10060
Chris Lattner52600972009-09-02 05:57:00 +000010061 // copy0MBB:
10062 // %FalseValue = ...
10063 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +000010064 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +000010065
Chris Lattner52600972009-09-02 05:57:00 +000010066 // sinkMBB:
10067 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
10068 // ...
Dan Gohman14152b42010-07-06 20:24:04 +000010069 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
10070 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +000010071 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
10072 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
10073
Dan Gohman14152b42010-07-06 20:24:04 +000010074 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +000010075 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +000010076}
10077
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010078MachineBasicBlock *
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010079X86TargetLowering::EmitLoweredWinAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010080 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010081 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10082 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010083
10084 // The lowering is pretty easy: we're just emitting the call to _alloca. The
10085 // non-trivial part is impdef of ESP.
10086 // FIXME: The code should be tweaked as soon as we'll try to do codegen for
10087 // mingw-w64.
10088
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010089 const char *StackProbeSymbol =
10090 Subtarget->isTargetWindows() ? "_chkstk" : "_alloca";
10091
Dan Gohman14152b42010-07-06 20:24:04 +000010092 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010093 .addExternalSymbol(StackProbeSymbol)
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010094 .addReg(X86::EAX, RegState::Implicit)
10095 .addReg(X86::ESP, RegState::Implicit)
10096 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
Anton Korobeynikov9f7f83b2010-08-25 07:50:11 +000010097 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
10098 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010099
Dan Gohman14152b42010-07-06 20:24:04 +000010100 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010101 return BB;
10102}
Chris Lattner52600972009-09-02 05:57:00 +000010103
10104MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +000010105X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
10106 MachineBasicBlock *BB) const {
10107 // This is pretty easy. We're taking the value that we received from
10108 // our load from the relocation, sticking it in either RDI (x86-64)
10109 // or EAX and doing an indirect call. The return value will then
10110 // be in the normal return register.
Michael J. Spencerec38de22010-10-10 22:04:20 +000010111 const X86InstrInfo *TII
Eric Christopher54415362010-06-08 22:04:25 +000010112 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +000010113 DebugLoc DL = MI->getDebugLoc();
10114 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +000010115
10116 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +000010117 assert(MI->getOperand(3).isGlobal() && "This should be a global");
Michael J. Spencerec38de22010-10-10 22:04:20 +000010118
Eric Christopher30ef0e52010-06-03 04:07:48 +000010119 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +000010120 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
10121 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +000010122 .addReg(X86::RIP)
10123 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000010124 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000010125 MI->getOperand(3).getTargetFlags())
10126 .addReg(0);
Eric Christopher722d3152010-09-27 06:01:51 +000010127 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +000010128 addDirectMem(MIB, X86::RDI);
Eric Christopher61025492010-06-15 23:08:42 +000010129 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +000010130 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
10131 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +000010132 .addReg(0)
10133 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000010134 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher61025492010-06-15 23:08:42 +000010135 MI->getOperand(3).getTargetFlags())
10136 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000010137 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000010138 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010139 } else {
Dan Gohman14152b42010-07-06 20:24:04 +000010140 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
10141 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +000010142 .addReg(TII->getGlobalBaseReg(F))
10143 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000010144 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000010145 MI->getOperand(3).getTargetFlags())
10146 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000010147 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000010148 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010149 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000010150
Dan Gohman14152b42010-07-06 20:24:04 +000010151 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +000010152 return BB;
10153}
10154
10155MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +000010156X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010157 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +000010158 switch (MI->getOpcode()) {
10159 default: assert(false && "Unexpected instr type to insert");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000010160 case X86::TAILJMPd64:
10161 case X86::TAILJMPr64:
10162 case X86::TAILJMPm64:
10163 assert(!"TAILJMP64 would not be touched here.");
10164 case X86::TCRETURNdi64:
10165 case X86::TCRETURNri64:
10166 case X86::TCRETURNmi64:
10167 // Defs of TCRETURNxx64 has Win64's callee-saved registers, as subset.
10168 // On AMD64, additional defs should be added before register allocation.
10169 if (!Subtarget->isTargetWin64()) {
10170 MI->addRegisterDefined(X86::RSI);
10171 MI->addRegisterDefined(X86::RDI);
10172 MI->addRegisterDefined(X86::XMM6);
10173 MI->addRegisterDefined(X86::XMM7);
10174 MI->addRegisterDefined(X86::XMM8);
10175 MI->addRegisterDefined(X86::XMM9);
10176 MI->addRegisterDefined(X86::XMM10);
10177 MI->addRegisterDefined(X86::XMM11);
10178 MI->addRegisterDefined(X86::XMM12);
10179 MI->addRegisterDefined(X86::XMM13);
10180 MI->addRegisterDefined(X86::XMM14);
10181 MI->addRegisterDefined(X86::XMM15);
10182 }
10183 return BB;
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010184 case X86::WIN_ALLOCA:
10185 return EmitLoweredWinAlloca(MI, BB);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010186 case X86::TLSCall_32:
10187 case X86::TLSCall_64:
10188 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +000010189 case X86::CMOV_GR8:
Evan Cheng60c07e12006-07-05 22:17:51 +000010190 case X86::CMOV_FR32:
10191 case X86::CMOV_FR64:
10192 case X86::CMOV_V4F32:
10193 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +000010194 case X86::CMOV_V2I64:
Chris Lattner314a1132010-03-14 18:31:44 +000010195 case X86::CMOV_GR16:
10196 case X86::CMOV_GR32:
10197 case X86::CMOV_RFP32:
10198 case X86::CMOV_RFP64:
10199 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010200 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000010201
Dale Johannesen849f2142007-07-03 00:53:03 +000010202 case X86::FP32_TO_INT16_IN_MEM:
10203 case X86::FP32_TO_INT32_IN_MEM:
10204 case X86::FP32_TO_INT64_IN_MEM:
10205 case X86::FP64_TO_INT16_IN_MEM:
10206 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +000010207 case X86::FP64_TO_INT64_IN_MEM:
10208 case X86::FP80_TO_INT16_IN_MEM:
10209 case X86::FP80_TO_INT32_IN_MEM:
10210 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +000010211 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10212 DebugLoc DL = MI->getDebugLoc();
10213
Evan Cheng60c07e12006-07-05 22:17:51 +000010214 // Change the floating point control register to use "round towards zero"
10215 // mode when truncating to an integer value.
10216 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +000010217 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +000010218 addFrameReference(BuildMI(*BB, MI, DL,
10219 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010220
10221 // Load the old value of the high byte of the control word...
10222 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +000010223 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohman14152b42010-07-06 20:24:04 +000010224 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +000010225 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010226
10227 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +000010228 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000010229 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +000010230
10231 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +000010232 addFrameReference(BuildMI(*BB, MI, DL,
10233 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010234
10235 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +000010236 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000010237 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +000010238
10239 // Get the X86 opcode to use.
10240 unsigned Opc;
10241 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000010242 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +000010243 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
10244 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
10245 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
10246 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
10247 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
10248 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +000010249 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
10250 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
10251 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +000010252 }
10253
10254 X86AddressMode AM;
10255 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +000010256 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000010257 AM.BaseType = X86AddressMode::RegBase;
10258 AM.Base.Reg = Op.getReg();
10259 } else {
10260 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +000010261 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +000010262 }
10263 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +000010264 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000010265 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010266 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +000010267 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000010268 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010269 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +000010270 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000010271 AM.GV = Op.getGlobal();
10272 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +000010273 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010274 }
Dan Gohman14152b42010-07-06 20:24:04 +000010275 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010276 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +000010277
10278 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +000010279 addFrameReference(BuildMI(*BB, MI, DL,
10280 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010281
Dan Gohman14152b42010-07-06 20:24:04 +000010282 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +000010283 return BB;
10284 }
Eric Christopherb120ab42009-08-18 22:50:32 +000010285 // String/text processing lowering.
10286 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010287 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000010288 return EmitPCMP(MI, BB, 3, false /* in-mem */);
10289 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010290 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000010291 return EmitPCMP(MI, BB, 3, true /* in-mem */);
10292 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010293 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000010294 return EmitPCMP(MI, BB, 5, false /* in mem */);
10295 case X86::PCMPESTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010296 case X86::VPCMPESTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000010297 return EmitPCMP(MI, BB, 5, true /* in mem */);
10298
Eric Christopher228232b2010-11-30 07:20:12 +000010299 // Thread synchronization.
10300 case X86::MONITOR:
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010301 return EmitMonitor(MI, BB);
Eric Christopher228232b2010-11-30 07:20:12 +000010302 case X86::MWAIT:
10303 return EmitMwait(MI, BB);
10304
Eric Christopherb120ab42009-08-18 22:50:32 +000010305 // Atomic Lowering.
Mon P Wang63307c32008-05-05 19:05:59 +000010306 case X86::ATOMAND32:
10307 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010308 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010309 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010310 X86::NOT32r, X86::EAX,
10311 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000010312 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +000010313 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
10314 X86::OR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010315 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010316 X86::NOT32r, X86::EAX,
10317 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000010318 case X86::ATOMXOR32:
10319 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010320 X86::XOR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010321 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010322 X86::NOT32r, X86::EAX,
10323 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010324 case X86::ATOMNAND32:
10325 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010326 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010327 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010328 X86::NOT32r, X86::EAX,
10329 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +000010330 case X86::ATOMMIN32:
10331 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
10332 case X86::ATOMMAX32:
10333 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
10334 case X86::ATOMUMIN32:
10335 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
10336 case X86::ATOMUMAX32:
10337 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +000010338
10339 case X86::ATOMAND16:
10340 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
10341 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010342 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010343 X86::NOT16r, X86::AX,
10344 X86::GR16RegisterClass);
10345 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +000010346 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010347 X86::OR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010348 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010349 X86::NOT16r, X86::AX,
10350 X86::GR16RegisterClass);
10351 case X86::ATOMXOR16:
10352 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
10353 X86::XOR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010354 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010355 X86::NOT16r, X86::AX,
10356 X86::GR16RegisterClass);
10357 case X86::ATOMNAND16:
10358 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
10359 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010360 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010361 X86::NOT16r, X86::AX,
10362 X86::GR16RegisterClass, true);
10363 case X86::ATOMMIN16:
10364 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
10365 case X86::ATOMMAX16:
10366 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
10367 case X86::ATOMUMIN16:
10368 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
10369 case X86::ATOMUMAX16:
10370 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
10371
10372 case X86::ATOMAND8:
10373 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
10374 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010375 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010376 X86::NOT8r, X86::AL,
10377 X86::GR8RegisterClass);
10378 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +000010379 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010380 X86::OR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010381 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010382 X86::NOT8r, X86::AL,
10383 X86::GR8RegisterClass);
10384 case X86::ATOMXOR8:
10385 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
10386 X86::XOR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010387 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010388 X86::NOT8r, X86::AL,
10389 X86::GR8RegisterClass);
10390 case X86::ATOMNAND8:
10391 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
10392 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010393 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010394 X86::NOT8r, X86::AL,
10395 X86::GR8RegisterClass, true);
10396 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010397 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +000010398 case X86::ATOMAND64:
10399 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010400 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010401 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010402 X86::NOT64r, X86::RAX,
10403 X86::GR64RegisterClass);
10404 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +000010405 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
10406 X86::OR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010407 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010408 X86::NOT64r, X86::RAX,
10409 X86::GR64RegisterClass);
10410 case X86::ATOMXOR64:
10411 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010412 X86::XOR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010413 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010414 X86::NOT64r, X86::RAX,
10415 X86::GR64RegisterClass);
10416 case X86::ATOMNAND64:
10417 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
10418 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010419 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010420 X86::NOT64r, X86::RAX,
10421 X86::GR64RegisterClass, true);
10422 case X86::ATOMMIN64:
10423 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
10424 case X86::ATOMMAX64:
10425 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
10426 case X86::ATOMUMIN64:
10427 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
10428 case X86::ATOMUMAX64:
10429 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010430
10431 // This group does 64-bit operations on a 32-bit host.
10432 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010433 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010434 X86::AND32rr, X86::AND32rr,
10435 X86::AND32ri, X86::AND32ri,
10436 false);
10437 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010438 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010439 X86::OR32rr, X86::OR32rr,
10440 X86::OR32ri, X86::OR32ri,
10441 false);
10442 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010443 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010444 X86::XOR32rr, X86::XOR32rr,
10445 X86::XOR32ri, X86::XOR32ri,
10446 false);
10447 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010448 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010449 X86::AND32rr, X86::AND32rr,
10450 X86::AND32ri, X86::AND32ri,
10451 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010452 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010453 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010454 X86::ADD32rr, X86::ADC32rr,
10455 X86::ADD32ri, X86::ADC32ri,
10456 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010457 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010458 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010459 X86::SUB32rr, X86::SBB32rr,
10460 X86::SUB32ri, X86::SBB32ri,
10461 false);
Dale Johannesen880ae362008-10-03 22:25:52 +000010462 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010463 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +000010464 X86::MOV32rr, X86::MOV32rr,
10465 X86::MOV32ri, X86::MOV32ri,
10466 false);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010467 case X86::VASTART_SAVE_XMM_REGS:
10468 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Dan Gohman320afb82010-10-12 18:00:49 +000010469
10470 case X86::VAARG_64:
10471 return EmitVAARG64WithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000010472 }
10473}
10474
10475//===----------------------------------------------------------------------===//
10476// X86 Optimization Hooks
10477//===----------------------------------------------------------------------===//
10478
Dan Gohman475871a2008-07-27 21:46:04 +000010479void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +000010480 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000010481 APInt &KnownZero,
10482 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +000010483 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +000010484 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010485 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +000010486 assert((Opc >= ISD::BUILTIN_OP_END ||
10487 Opc == ISD::INTRINSIC_WO_CHAIN ||
10488 Opc == ISD::INTRINSIC_W_CHAIN ||
10489 Opc == ISD::INTRINSIC_VOID) &&
10490 "Should use MaskedValueIsZero if you don't know whether Op"
10491 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010492
Dan Gohmanf4f92f52008-02-13 23:07:24 +000010493 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010494 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +000010495 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +000010496 case X86ISD::ADD:
10497 case X86ISD::SUB:
Chris Lattner5b856542010-12-20 00:59:46 +000010498 case X86ISD::ADC:
10499 case X86ISD::SBB:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000010500 case X86ISD::SMUL:
10501 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +000010502 case X86ISD::INC:
10503 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +000010504 case X86ISD::OR:
10505 case X86ISD::XOR:
10506 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000010507 // These nodes' second result is a boolean.
10508 if (Op.getResNo() == 0)
10509 break;
10510 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010511 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000010512 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
10513 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +000010514 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010515 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010516}
Chris Lattner259e97c2006-01-31 19:43:35 +000010517
Owen Andersonbc146b02010-09-21 20:42:50 +000010518unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
10519 unsigned Depth) const {
10520 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
10521 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
10522 return Op.getValueType().getScalarType().getSizeInBits();
Michael J. Spencerec38de22010-10-10 22:04:20 +000010523
Owen Andersonbc146b02010-09-21 20:42:50 +000010524 // Fallback case.
10525 return 1;
10526}
10527
Evan Cheng206ee9d2006-07-07 08:33:52 +000010528/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +000010529/// node is a GlobalAddress + offset.
10530bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +000010531 const GlobalValue* &GA,
10532 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +000010533 if (N->getOpcode() == X86ISD::Wrapper) {
10534 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +000010535 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +000010536 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +000010537 return true;
10538 }
Evan Cheng206ee9d2006-07-07 08:33:52 +000010539 }
Evan Chengad4196b2008-05-12 19:56:52 +000010540 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +000010541}
10542
Evan Cheng206ee9d2006-07-07 08:33:52 +000010543/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
10544/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
10545/// if the load addresses are consecutive, non-overlapping, and in the right
Nate Begemanfdea31a2010-03-24 20:49:50 +000010546/// order.
Dan Gohman475871a2008-07-27 21:46:04 +000010547static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Mon P Wanga0fd0d52010-12-19 23:55:53 +000010548 TargetLowering::DAGCombinerInfo &DCI) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000010549 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000010550 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +000010551
Eli Friedman7a5e5552009-06-07 06:52:44 +000010552 if (VT.getSizeInBits() != 128)
10553 return SDValue();
10554
Mon P Wanga0fd0d52010-12-19 23:55:53 +000010555 // Don't create instructions with illegal types after legalize types has run.
10556 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10557 if (!DCI.isBeforeLegalize() && !TLI.isTypeLegal(VT.getVectorElementType()))
10558 return SDValue();
10559
Nate Begemanfdea31a2010-03-24 20:49:50 +000010560 SmallVector<SDValue, 16> Elts;
10561 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000010562 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +000010563
Nate Begemanfdea31a2010-03-24 20:49:50 +000010564 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000010565}
Evan Chengd880b972008-05-09 21:53:03 +000010566
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +000010567/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
10568/// generation and convert it from being a bunch of shuffles and extracts
10569/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010570static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
10571 const TargetLowering &TLI) {
10572 SDValue InputVector = N->getOperand(0);
10573
10574 // Only operate on vectors of 4 elements, where the alternative shuffling
10575 // gets to be more expensive.
10576 if (InputVector.getValueType() != MVT::v4i32)
10577 return SDValue();
10578
10579 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
10580 // single use which is a sign-extend or zero-extend, and all elements are
10581 // used.
10582 SmallVector<SDNode *, 4> Uses;
10583 unsigned ExtractedElements = 0;
10584 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
10585 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
10586 if (UI.getUse().getResNo() != InputVector.getResNo())
10587 return SDValue();
10588
10589 SDNode *Extract = *UI;
10590 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
10591 return SDValue();
10592
10593 if (Extract->getValueType(0) != MVT::i32)
10594 return SDValue();
10595 if (!Extract->hasOneUse())
10596 return SDValue();
10597 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
10598 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
10599 return SDValue();
10600 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
10601 return SDValue();
10602
10603 // Record which element was extracted.
10604 ExtractedElements |=
10605 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
10606
10607 Uses.push_back(Extract);
10608 }
10609
10610 // If not all the elements were used, this may not be worthwhile.
10611 if (ExtractedElements != 15)
10612 return SDValue();
10613
10614 // Ok, we've now decided to do the transformation.
10615 DebugLoc dl = InputVector.getDebugLoc();
10616
10617 // Store the value to a temporary stack slot.
10618 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000010619 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
10620 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010621
10622 // Replace each use (extract) with a load of the appropriate element.
10623 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
10624 UE = Uses.end(); UI != UE; ++UI) {
10625 SDNode *Extract = *UI;
10626
10627 // Compute the element's address.
10628 SDValue Idx = Extract->getOperand(1);
10629 unsigned EltSize =
10630 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
10631 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
10632 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
10633
Eric Christopher90eb4022010-07-22 00:26:08 +000010634 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, Idx.getValueType(),
Chris Lattner51abfe42010-09-21 06:02:19 +000010635 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010636
10637 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000010638 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000010639 ScalarAddr, MachinePointerInfo(),
10640 false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010641
10642 // Replace the exact with the load.
10643 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
10644 }
10645
10646 // The replacement was made in place; don't return anything.
10647 return SDValue();
10648}
10649
Chris Lattner83e6c992006-10-04 06:57:07 +000010650/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000010651static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner47b4ce82009-03-11 05:48:52 +000010652 const X86Subtarget *Subtarget) {
10653 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +000010654 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000010655 // Get the LHS/RHS of the select.
10656 SDValue LHS = N->getOperand(1);
10657 SDValue RHS = N->getOperand(2);
Eric Christopherfd179292009-08-27 18:07:15 +000010658
Dan Gohman670e5392009-09-21 18:03:22 +000010659 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000010660 // instructions match the semantics of the common C idiom x<y?x:y but not
10661 // x<=y?x:y, because of how they handle negative zero (which can be
10662 // ignored in unsafe-math mode).
Chris Lattner83e6c992006-10-04 06:57:07 +000010663 if (Subtarget->hasSSE2() &&
Owen Anderson825b72b2009-08-11 20:47:22 +000010664 (LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64) &&
Chris Lattner47b4ce82009-03-11 05:48:52 +000010665 Cond.getOpcode() == ISD::SETCC) {
10666 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010667
Chris Lattner47b4ce82009-03-11 05:48:52 +000010668 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000010669 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000010670 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
10671 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000010672 switch (CC) {
10673 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000010674 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000010675 // Converting this to a min would handle NaNs incorrectly, and swapping
10676 // the operands would cause it to handle comparisons between positive
10677 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000010678 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000010679 if (!UnsafeFPMath &&
10680 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
10681 break;
10682 std::swap(LHS, RHS);
10683 }
Dan Gohman670e5392009-09-21 18:03:22 +000010684 Opcode = X86ISD::FMIN;
10685 break;
10686 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000010687 // Converting this to a min would handle comparisons between positive
10688 // and negative zero incorrectly.
10689 if (!UnsafeFPMath &&
10690 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
10691 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010692 Opcode = X86ISD::FMIN;
10693 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000010694 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000010695 // Converting this to a min would handle both negative zeros and NaNs
10696 // incorrectly, but we can swap the operands to fix both.
10697 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000010698 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010699 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000010700 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010701 Opcode = X86ISD::FMIN;
10702 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010703
Dan Gohman670e5392009-09-21 18:03:22 +000010704 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000010705 // Converting this to a max would handle comparisons between positive
10706 // and negative zero incorrectly.
10707 if (!UnsafeFPMath &&
10708 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(LHS))
10709 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010710 Opcode = X86ISD::FMAX;
10711 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000010712 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000010713 // Converting this to a max would handle NaNs incorrectly, and swapping
10714 // the operands would cause it to handle comparisons between positive
10715 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000010716 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000010717 if (!UnsafeFPMath &&
10718 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
10719 break;
10720 std::swap(LHS, RHS);
10721 }
Dan Gohman670e5392009-09-21 18:03:22 +000010722 Opcode = X86ISD::FMAX;
10723 break;
10724 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000010725 // Converting this to a max would handle both negative zeros and NaNs
10726 // incorrectly, but we can swap the operands to fix both.
10727 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000010728 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010729 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010730 case ISD::SETGE:
10731 Opcode = X86ISD::FMAX;
10732 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000010733 }
Dan Gohman670e5392009-09-21 18:03:22 +000010734 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000010735 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
10736 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000010737 switch (CC) {
10738 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000010739 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000010740 // Converting this to a min would handle comparisons between positive
10741 // and negative zero incorrectly, and swapping the operands would
10742 // cause it to handle NaNs incorrectly.
10743 if (!UnsafeFPMath &&
10744 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000010745 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000010746 break;
10747 std::swap(LHS, RHS);
10748 }
Dan Gohman670e5392009-09-21 18:03:22 +000010749 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000010750 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010751 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000010752 // Converting this to a min would handle NaNs incorrectly.
10753 if (!UnsafeFPMath &&
10754 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
10755 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010756 Opcode = X86ISD::FMIN;
10757 break;
10758 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000010759 // Converting this to a min would handle both negative zeros and NaNs
10760 // incorrectly, but we can swap the operands to fix both.
10761 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000010762 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010763 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010764 case ISD::SETGE:
10765 Opcode = X86ISD::FMIN;
10766 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010767
Dan Gohman670e5392009-09-21 18:03:22 +000010768 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000010769 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000010770 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000010771 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010772 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000010773 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010774 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000010775 // Converting this to a max would handle comparisons between positive
10776 // and negative zero incorrectly, and swapping the operands would
10777 // cause it to handle NaNs incorrectly.
10778 if (!UnsafeFPMath &&
10779 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000010780 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000010781 break;
10782 std::swap(LHS, RHS);
10783 }
Dan Gohman670e5392009-09-21 18:03:22 +000010784 Opcode = X86ISD::FMAX;
10785 break;
10786 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000010787 // Converting this to a max would handle both negative zeros and NaNs
10788 // incorrectly, but we can swap the operands to fix both.
10789 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000010790 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010791 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000010792 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010793 Opcode = X86ISD::FMAX;
10794 break;
10795 }
Chris Lattner83e6c992006-10-04 06:57:07 +000010796 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010797
Chris Lattner47b4ce82009-03-11 05:48:52 +000010798 if (Opcode)
10799 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000010800 }
Eric Christopherfd179292009-08-27 18:07:15 +000010801
Chris Lattnerd1980a52009-03-12 06:52:53 +000010802 // If this is a select between two integer constants, try to do some
10803 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000010804 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
10805 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000010806 // Don't do this for crazy integer types.
10807 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
10808 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000010809 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000010810 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000010811
Chris Lattnercee56e72009-03-13 05:53:31 +000010812 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000010813 // Efficiently invertible.
10814 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
10815 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
10816 isa<ConstantSDNode>(Cond.getOperand(1))))) {
10817 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000010818 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000010819 }
Eric Christopherfd179292009-08-27 18:07:15 +000010820
Chris Lattnerd1980a52009-03-12 06:52:53 +000010821 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000010822 if (FalseC->getAPIntValue() == 0 &&
10823 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000010824 if (NeedsCondInvert) // Invert the condition if needed.
10825 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
10826 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000010827
Chris Lattnerd1980a52009-03-12 06:52:53 +000010828 // Zero extend the condition if needed.
10829 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000010830
Chris Lattnercee56e72009-03-13 05:53:31 +000010831 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000010832 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000010833 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000010834 }
Eric Christopherfd179292009-08-27 18:07:15 +000010835
Chris Lattner97a29a52009-03-13 05:22:11 +000010836 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000010837 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +000010838 if (NeedsCondInvert) // Invert the condition if needed.
10839 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
10840 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000010841
Chris Lattner97a29a52009-03-13 05:22:11 +000010842 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000010843 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
10844 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000010845 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000010846 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000010847 }
Eric Christopherfd179292009-08-27 18:07:15 +000010848
Chris Lattnercee56e72009-03-13 05:53:31 +000010849 // Optimize cases that will turn into an LEA instruction. This requires
10850 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000010851 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000010852 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000010853 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000010854
Chris Lattnercee56e72009-03-13 05:53:31 +000010855 bool isFastMultiplier = false;
10856 if (Diff < 10) {
10857 switch ((unsigned char)Diff) {
10858 default: break;
10859 case 1: // result = add base, cond
10860 case 2: // result = lea base( , cond*2)
10861 case 3: // result = lea base(cond, cond*2)
10862 case 4: // result = lea base( , cond*4)
10863 case 5: // result = lea base(cond, cond*4)
10864 case 8: // result = lea base( , cond*8)
10865 case 9: // result = lea base(cond, cond*8)
10866 isFastMultiplier = true;
10867 break;
10868 }
10869 }
Eric Christopherfd179292009-08-27 18:07:15 +000010870
Chris Lattnercee56e72009-03-13 05:53:31 +000010871 if (isFastMultiplier) {
10872 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
10873 if (NeedsCondInvert) // Invert the condition if needed.
10874 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
10875 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000010876
Chris Lattnercee56e72009-03-13 05:53:31 +000010877 // Zero extend the condition if needed.
10878 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
10879 Cond);
10880 // Scale the condition by the difference.
10881 if (Diff != 1)
10882 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
10883 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000010884
Chris Lattnercee56e72009-03-13 05:53:31 +000010885 // Add the base if non-zero.
10886 if (FalseC->getAPIntValue() != 0)
10887 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
10888 SDValue(FalseC, 0));
10889 return Cond;
10890 }
Eric Christopherfd179292009-08-27 18:07:15 +000010891 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000010892 }
10893 }
Eric Christopherfd179292009-08-27 18:07:15 +000010894
Dan Gohman475871a2008-07-27 21:46:04 +000010895 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000010896}
10897
Chris Lattnerd1980a52009-03-12 06:52:53 +000010898/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
10899static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
10900 TargetLowering::DAGCombinerInfo &DCI) {
10901 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +000010902
Chris Lattnerd1980a52009-03-12 06:52:53 +000010903 // If the flag operand isn't dead, don't touch this CMOV.
10904 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
10905 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +000010906
Chris Lattnerd1980a52009-03-12 06:52:53 +000010907 // If this is a select between two integer constants, try to do some
10908 // optimizations. Note that the operands are ordered the opposite of SELECT
10909 // operands.
10910 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
10911 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
10912 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
10913 // larger than FalseC (the false value).
10914 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
Eric Christopherfd179292009-08-27 18:07:15 +000010915
Chris Lattnerd1980a52009-03-12 06:52:53 +000010916 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
10917 CC = X86::GetOppositeBranchCondition(CC);
10918 std::swap(TrueC, FalseC);
10919 }
Eric Christopherfd179292009-08-27 18:07:15 +000010920
Chris Lattnerd1980a52009-03-12 06:52:53 +000010921 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000010922 // This is efficient for any integer data type (including i8/i16) and
10923 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000010924 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
10925 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +000010926 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
10927 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000010928
Chris Lattnerd1980a52009-03-12 06:52:53 +000010929 // Zero extend the condition if needed.
10930 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000010931
Chris Lattnerd1980a52009-03-12 06:52:53 +000010932 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
10933 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000010934 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000010935 if (N->getNumValues() == 2) // Dead flag value?
10936 return DCI.CombineTo(N, Cond, SDValue());
10937 return Cond;
10938 }
Eric Christopherfd179292009-08-27 18:07:15 +000010939
Chris Lattnercee56e72009-03-13 05:53:31 +000010940 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
10941 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000010942 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
10943 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +000010944 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
10945 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000010946
Chris Lattner97a29a52009-03-13 05:22:11 +000010947 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000010948 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
10949 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000010950 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
10951 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000010952
Chris Lattner97a29a52009-03-13 05:22:11 +000010953 if (N->getNumValues() == 2) // Dead flag value?
10954 return DCI.CombineTo(N, Cond, SDValue());
10955 return Cond;
10956 }
Eric Christopherfd179292009-08-27 18:07:15 +000010957
Chris Lattnercee56e72009-03-13 05:53:31 +000010958 // Optimize cases that will turn into an LEA instruction. This requires
10959 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000010960 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000010961 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000010962 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000010963
Chris Lattnercee56e72009-03-13 05:53:31 +000010964 bool isFastMultiplier = false;
10965 if (Diff < 10) {
10966 switch ((unsigned char)Diff) {
10967 default: break;
10968 case 1: // result = add base, cond
10969 case 2: // result = lea base( , cond*2)
10970 case 3: // result = lea base(cond, cond*2)
10971 case 4: // result = lea base( , cond*4)
10972 case 5: // result = lea base(cond, cond*4)
10973 case 8: // result = lea base( , cond*8)
10974 case 9: // result = lea base(cond, cond*8)
10975 isFastMultiplier = true;
10976 break;
10977 }
10978 }
Eric Christopherfd179292009-08-27 18:07:15 +000010979
Chris Lattnercee56e72009-03-13 05:53:31 +000010980 if (isFastMultiplier) {
10981 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
10982 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +000010983 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
10984 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000010985 // Zero extend the condition if needed.
10986 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
10987 Cond);
10988 // Scale the condition by the difference.
10989 if (Diff != 1)
10990 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
10991 DAG.getConstant(Diff, Cond.getValueType()));
10992
10993 // Add the base if non-zero.
10994 if (FalseC->getAPIntValue() != 0)
10995 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
10996 SDValue(FalseC, 0));
10997 if (N->getNumValues() == 2) // Dead flag value?
10998 return DCI.CombineTo(N, Cond, SDValue());
10999 return Cond;
11000 }
Eric Christopherfd179292009-08-27 18:07:15 +000011001 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000011002 }
11003 }
11004 return SDValue();
11005}
11006
11007
Evan Cheng0b0cd912009-03-28 05:57:29 +000011008/// PerformMulCombine - Optimize a single multiply with constant into two
11009/// in order to implement it with two cheaper instructions, e.g.
11010/// LEA + SHL, LEA + LEA.
11011static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
11012 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000011013 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
11014 return SDValue();
11015
Owen Andersone50ed302009-08-10 22:56:29 +000011016 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000011017 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000011018 return SDValue();
11019
11020 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
11021 if (!C)
11022 return SDValue();
11023 uint64_t MulAmt = C->getZExtValue();
11024 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
11025 return SDValue();
11026
11027 uint64_t MulAmt1 = 0;
11028 uint64_t MulAmt2 = 0;
11029 if ((MulAmt % 9) == 0) {
11030 MulAmt1 = 9;
11031 MulAmt2 = MulAmt / 9;
11032 } else if ((MulAmt % 5) == 0) {
11033 MulAmt1 = 5;
11034 MulAmt2 = MulAmt / 5;
11035 } else if ((MulAmt % 3) == 0) {
11036 MulAmt1 = 3;
11037 MulAmt2 = MulAmt / 3;
11038 }
11039 if (MulAmt2 &&
11040 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
11041 DebugLoc DL = N->getDebugLoc();
11042
11043 if (isPowerOf2_64(MulAmt2) &&
11044 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
11045 // If second multiplifer is pow2, issue it first. We want the multiply by
11046 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
11047 // is an add.
11048 std::swap(MulAmt1, MulAmt2);
11049
11050 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000011051 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000011052 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000011053 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000011054 else
Evan Cheng73f24c92009-03-30 21:36:47 +000011055 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000011056 DAG.getConstant(MulAmt1, VT));
11057
Eric Christopherfd179292009-08-27 18:07:15 +000011058 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000011059 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000011060 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000011061 else
Evan Cheng73f24c92009-03-30 21:36:47 +000011062 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000011063 DAG.getConstant(MulAmt2, VT));
11064
11065 // Do not add new nodes to DAG combiner worklist.
11066 DCI.CombineTo(N, NewMul, false);
11067 }
11068 return SDValue();
11069}
11070
Evan Chengad9c0a32009-12-15 00:53:42 +000011071static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
11072 SDValue N0 = N->getOperand(0);
11073 SDValue N1 = N->getOperand(1);
11074 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
11075 EVT VT = N0.getValueType();
11076
11077 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
11078 // since the result of setcc_c is all zero's or all ones.
11079 if (N1C && N0.getOpcode() == ISD::AND &&
11080 N0.getOperand(1).getOpcode() == ISD::Constant) {
11081 SDValue N00 = N0.getOperand(0);
11082 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
11083 ((N00.getOpcode() == ISD::ANY_EXTEND ||
11084 N00.getOpcode() == ISD::ZERO_EXTEND) &&
11085 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
11086 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
11087 APInt ShAmt = N1C->getAPIntValue();
11088 Mask = Mask.shl(ShAmt);
11089 if (Mask != 0)
11090 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
11091 N00, DAG.getConstant(Mask, VT));
11092 }
11093 }
11094
11095 return SDValue();
11096}
Evan Cheng0b0cd912009-03-28 05:57:29 +000011097
Nate Begeman740ab032009-01-26 00:52:55 +000011098/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
11099/// when possible.
11100static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
11101 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +000011102 EVT VT = N->getValueType(0);
11103 if (!VT.isVector() && VT.isInteger() &&
11104 N->getOpcode() == ISD::SHL)
11105 return PerformSHLCombine(N, DAG);
11106
Nate Begeman740ab032009-01-26 00:52:55 +000011107 // On X86 with SSE2 support, we can transform this to a vector shift if
11108 // all elements are shifted by the same amount. We can't do this in legalize
11109 // because the a constant vector is typically transformed to a constant pool
11110 // so we have no knowledge of the shift amount.
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011111 if (!Subtarget->hasSSE2())
11112 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000011113
Owen Anderson825b72b2009-08-11 20:47:22 +000011114 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011115 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000011116
Mon P Wang3becd092009-01-28 08:12:05 +000011117 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +000011118 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +000011119 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +000011120 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +000011121 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
11122 unsigned NumElts = VT.getVectorNumElements();
11123 unsigned i = 0;
11124 for (; i != NumElts; ++i) {
11125 SDValue Arg = ShAmtOp.getOperand(i);
11126 if (Arg.getOpcode() == ISD::UNDEF) continue;
11127 BaseShAmt = Arg;
11128 break;
11129 }
11130 for (; i != NumElts; ++i) {
11131 SDValue Arg = ShAmtOp.getOperand(i);
11132 if (Arg.getOpcode() == ISD::UNDEF) continue;
11133 if (Arg != BaseShAmt) {
11134 return SDValue();
11135 }
11136 }
11137 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +000011138 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +000011139 SDValue InVec = ShAmtOp.getOperand(0);
11140 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
11141 unsigned NumElts = InVec.getValueType().getVectorNumElements();
11142 unsigned i = 0;
11143 for (; i != NumElts; ++i) {
11144 SDValue Arg = InVec.getOperand(i);
11145 if (Arg.getOpcode() == ISD::UNDEF) continue;
11146 BaseShAmt = Arg;
11147 break;
11148 }
11149 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
11150 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +000011151 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +000011152 if (C->getZExtValue() == SplatIdx)
11153 BaseShAmt = InVec.getOperand(1);
11154 }
11155 }
11156 if (BaseShAmt.getNode() == 0)
11157 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
11158 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +000011159 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011160 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000011161
Mon P Wangefa42202009-09-03 19:56:25 +000011162 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +000011163 if (EltVT.bitsGT(MVT::i32))
11164 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
11165 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +000011166 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +000011167
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011168 // The shift amount is identical so we can do a vector shift.
11169 SDValue ValOp = N->getOperand(0);
11170 switch (N->getOpcode()) {
11171 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000011172 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011173 break;
11174 case ISD::SHL:
Owen Anderson825b72b2009-08-11 20:47:22 +000011175 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011176 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011177 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011178 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011179 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011180 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011181 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011182 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011183 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011184 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011185 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011186 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011187 break;
11188 case ISD::SRA:
Owen Anderson825b72b2009-08-11 20:47:22 +000011189 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011190 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011191 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011192 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011193 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011194 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011195 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011196 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011197 break;
11198 case ISD::SRL:
Owen Anderson825b72b2009-08-11 20:47:22 +000011199 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011200 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011201 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011202 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011203 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011204 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011205 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011206 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011207 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011208 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011209 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011210 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011211 break;
Nate Begeman740ab032009-01-26 00:52:55 +000011212 }
11213 return SDValue();
11214}
11215
Nate Begemanb65c1752010-12-17 22:55:37 +000011216
11217static SDValue PerformAndCombine(SDNode *N, SelectionDAG &DAG,
11218 TargetLowering::DAGCombinerInfo &DCI,
11219 const X86Subtarget *Subtarget) {
11220 if (DCI.isBeforeLegalizeOps())
11221 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011222
Nate Begemanb65c1752010-12-17 22:55:37 +000011223 // Want to form PANDN nodes, in the hopes of then easily combining them with
11224 // OR and AND nodes to form PBLEND/PSIGN.
11225 EVT VT = N->getValueType(0);
11226 if (VT != MVT::v2i64)
11227 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011228
Nate Begemanb65c1752010-12-17 22:55:37 +000011229 SDValue N0 = N->getOperand(0);
11230 SDValue N1 = N->getOperand(1);
11231 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011232
Nate Begemanb65c1752010-12-17 22:55:37 +000011233 // Check LHS for vnot
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011234 if (N0.getOpcode() == ISD::XOR &&
Nate Begemanb65c1752010-12-17 22:55:37 +000011235 ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode()))
11236 return DAG.getNode(X86ISD::PANDN, DL, VT, N0.getOperand(0), N1);
11237
11238 // Check RHS for vnot
11239 if (N1.getOpcode() == ISD::XOR &&
11240 ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode()))
11241 return DAG.getNode(X86ISD::PANDN, DL, VT, N1.getOperand(0), N0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011242
Nate Begemanb65c1752010-12-17 22:55:37 +000011243 return SDValue();
11244}
11245
Evan Cheng760d1942010-01-04 21:22:48 +000011246static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000011247 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000011248 const X86Subtarget *Subtarget) {
Evan Cheng39cfeec2010-04-28 02:25:18 +000011249 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000011250 return SDValue();
11251
Evan Cheng760d1942010-01-04 21:22:48 +000011252 EVT VT = N->getValueType(0);
Nate Begemanb65c1752010-12-17 22:55:37 +000011253 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64 && VT != MVT::v2i64)
Evan Cheng760d1942010-01-04 21:22:48 +000011254 return SDValue();
11255
Evan Cheng760d1942010-01-04 21:22:48 +000011256 SDValue N0 = N->getOperand(0);
11257 SDValue N1 = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011258
Nate Begemanb65c1752010-12-17 22:55:37 +000011259 // look for psign/blend
11260 if (Subtarget->hasSSSE3()) {
11261 if (VT == MVT::v2i64) {
11262 // Canonicalize pandn to RHS
11263 if (N0.getOpcode() == X86ISD::PANDN)
11264 std::swap(N0, N1);
11265 // or (and (m, x), (pandn m, y))
11266 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::PANDN) {
11267 SDValue Mask = N1.getOperand(0);
11268 SDValue X = N1.getOperand(1);
11269 SDValue Y;
11270 if (N0.getOperand(0) == Mask)
11271 Y = N0.getOperand(1);
11272 if (N0.getOperand(1) == Mask)
11273 Y = N0.getOperand(0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011274
Nate Begemanb65c1752010-12-17 22:55:37 +000011275 // Check to see if the mask appeared in both the AND and PANDN and
11276 if (!Y.getNode())
11277 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011278
Nate Begemanb65c1752010-12-17 22:55:37 +000011279 // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them.
11280 if (Mask.getOpcode() != ISD::BITCAST ||
11281 X.getOpcode() != ISD::BITCAST ||
11282 Y.getOpcode() != ISD::BITCAST)
11283 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011284
Nate Begemanb65c1752010-12-17 22:55:37 +000011285 // Look through mask bitcast.
11286 Mask = Mask.getOperand(0);
11287 EVT MaskVT = Mask.getValueType();
11288
11289 // Validate that the Mask operand is a vector sra node. The sra node
11290 // will be an intrinsic.
11291 if (Mask.getOpcode() != ISD::INTRINSIC_WO_CHAIN)
11292 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011293
Nate Begemanb65c1752010-12-17 22:55:37 +000011294 // FIXME: what to do for bytes, since there is a psignb/pblendvb, but
11295 // there is no psrai.b
11296 switch (cast<ConstantSDNode>(Mask.getOperand(0))->getZExtValue()) {
11297 case Intrinsic::x86_sse2_psrai_w:
11298 case Intrinsic::x86_sse2_psrai_d:
11299 break;
11300 default: return SDValue();
11301 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011302
Nate Begemanb65c1752010-12-17 22:55:37 +000011303 // Check that the SRA is all signbits.
11304 SDValue SraC = Mask.getOperand(2);
11305 unsigned SraAmt = cast<ConstantSDNode>(SraC)->getZExtValue();
11306 unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits();
11307 if ((SraAmt + 1) != EltBits)
11308 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011309
Nate Begemanb65c1752010-12-17 22:55:37 +000011310 DebugLoc DL = N->getDebugLoc();
11311
11312 // Now we know we at least have a plendvb with the mask val. See if
11313 // we can form a psignb/w/d.
11314 // psign = x.type == y.type == mask.type && y = sub(0, x);
11315 X = X.getOperand(0);
11316 Y = Y.getOperand(0);
11317 if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X &&
11318 ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) &&
11319 X.getValueType() == MaskVT && X.getValueType() == Y.getValueType()){
11320 unsigned Opc = 0;
11321 switch (EltBits) {
11322 case 8: Opc = X86ISD::PSIGNB; break;
11323 case 16: Opc = X86ISD::PSIGNW; break;
11324 case 32: Opc = X86ISD::PSIGND; break;
11325 default: break;
11326 }
11327 if (Opc) {
11328 SDValue Sign = DAG.getNode(Opc, DL, MaskVT, X, Mask.getOperand(1));
11329 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Sign);
11330 }
11331 }
11332 // PBLENDVB only available on SSE 4.1
11333 if (!Subtarget->hasSSE41())
11334 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011335
Nate Begemanb65c1752010-12-17 22:55:37 +000011336 X = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, X);
11337 Y = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Y);
11338 Mask = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Mask);
Nate Begeman672fb622010-12-20 22:04:24 +000011339 Mask = DAG.getNode(X86ISD::PBLENDVB, DL, MVT::v16i8, X, Y, Mask);
Nate Begemanb65c1752010-12-17 22:55:37 +000011340 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Mask);
11341 }
11342 }
11343 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011344
Nate Begemanb65c1752010-12-17 22:55:37 +000011345 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
Evan Cheng760d1942010-01-04 21:22:48 +000011346 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
11347 std::swap(N0, N1);
11348 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
11349 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000011350 if (!N0.hasOneUse() || !N1.hasOneUse())
11351 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000011352
11353 SDValue ShAmt0 = N0.getOperand(1);
11354 if (ShAmt0.getValueType() != MVT::i8)
11355 return SDValue();
11356 SDValue ShAmt1 = N1.getOperand(1);
11357 if (ShAmt1.getValueType() != MVT::i8)
11358 return SDValue();
11359 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
11360 ShAmt0 = ShAmt0.getOperand(0);
11361 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
11362 ShAmt1 = ShAmt1.getOperand(0);
11363
11364 DebugLoc DL = N->getDebugLoc();
11365 unsigned Opc = X86ISD::SHLD;
11366 SDValue Op0 = N0.getOperand(0);
11367 SDValue Op1 = N1.getOperand(0);
11368 if (ShAmt0.getOpcode() == ISD::SUB) {
11369 Opc = X86ISD::SHRD;
11370 std::swap(Op0, Op1);
11371 std::swap(ShAmt0, ShAmt1);
11372 }
11373
Evan Cheng8b1190a2010-04-28 01:18:01 +000011374 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000011375 if (ShAmt1.getOpcode() == ISD::SUB) {
11376 SDValue Sum = ShAmt1.getOperand(0);
11377 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000011378 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
11379 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
11380 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
11381 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000011382 return DAG.getNode(Opc, DL, VT,
11383 Op0, Op1,
11384 DAG.getNode(ISD::TRUNCATE, DL,
11385 MVT::i8, ShAmt0));
11386 }
11387 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
11388 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
11389 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000011390 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000011391 return DAG.getNode(Opc, DL, VT,
11392 N0.getOperand(0), N1.getOperand(0),
11393 DAG.getNode(ISD::TRUNCATE, DL,
11394 MVT::i8, ShAmt0));
11395 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011396
Evan Cheng760d1942010-01-04 21:22:48 +000011397 return SDValue();
11398}
11399
Chris Lattner149a4e52008-02-22 02:09:43 +000011400/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000011401static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000011402 const X86Subtarget *Subtarget) {
Chris Lattner149a4e52008-02-22 02:09:43 +000011403 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
11404 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000011405 // A preferable solution to the general problem is to figure out the right
11406 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000011407
11408 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng7e2ff772008-05-08 00:57:18 +000011409 StoreSDNode *St = cast<StoreSDNode>(N);
Owen Andersone50ed302009-08-10 22:56:29 +000011410 EVT VT = St->getValue().getValueType();
Evan Cheng536e6672009-03-12 05:59:15 +000011411 if (VT.getSizeInBits() != 64)
11412 return SDValue();
11413
Devang Patel578efa92009-06-05 21:57:13 +000011414 const Function *F = DAG.getMachineFunction().getFunction();
11415 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Eric Christopherfd179292009-08-27 18:07:15 +000011416 bool F64IsLegal = !UseSoftFloat && !NoImplicitFloatOps
Devang Patel578efa92009-06-05 21:57:13 +000011417 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +000011418 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000011419 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000011420 isa<LoadSDNode>(St->getValue()) &&
11421 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
11422 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000011423 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000011424 LoadSDNode *Ld = 0;
11425 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000011426 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000011427 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000011428 // Must be a store of a load. We currently handle two cases: the load
11429 // is a direct child, and it's under an intervening TokenFactor. It is
11430 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000011431 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000011432 Ld = cast<LoadSDNode>(St->getChain());
11433 else if (St->getValue().hasOneUse() &&
11434 ChainVal->getOpcode() == ISD::TokenFactor) {
11435 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000011436 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000011437 TokenFactorIndex = i;
11438 Ld = cast<LoadSDNode>(St->getValue());
11439 } else
11440 Ops.push_back(ChainVal->getOperand(i));
11441 }
11442 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000011443
Evan Cheng536e6672009-03-12 05:59:15 +000011444 if (!Ld || !ISD::isNormalLoad(Ld))
11445 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000011446
Evan Cheng536e6672009-03-12 05:59:15 +000011447 // If this is not the MMX case, i.e. we are just turning i64 load/store
11448 // into f64 load/store, avoid the transformation if there are multiple
11449 // uses of the loaded value.
11450 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
11451 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000011452
Evan Cheng536e6672009-03-12 05:59:15 +000011453 DebugLoc LdDL = Ld->getDebugLoc();
11454 DebugLoc StDL = N->getDebugLoc();
11455 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
11456 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
11457 // pair instead.
11458 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000011459 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000011460 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
11461 Ld->getPointerInfo(), Ld->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000011462 Ld->isNonTemporal(), Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000011463 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000011464 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000011465 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000011466 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000011467 Ops.size());
11468 }
Evan Cheng536e6672009-03-12 05:59:15 +000011469 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000011470 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000011471 St->isVolatile(), St->isNonTemporal(),
11472 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000011473 }
Evan Cheng536e6672009-03-12 05:59:15 +000011474
11475 // Otherwise, lower to two pairs of 32-bit loads / stores.
11476 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000011477 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
11478 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000011479
Owen Anderson825b72b2009-08-11 20:47:22 +000011480 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000011481 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000011482 Ld->isVolatile(), Ld->isNonTemporal(),
11483 Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000011484 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000011485 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000011486 Ld->isVolatile(), Ld->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000011487 MinAlign(Ld->getAlignment(), 4));
11488
11489 SDValue NewChain = LoLd.getValue(1);
11490 if (TokenFactorIndex != -1) {
11491 Ops.push_back(LoLd);
11492 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000011493 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000011494 Ops.size());
11495 }
11496
11497 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000011498 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
11499 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000011500
11501 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000011502 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000011503 St->isVolatile(), St->isNonTemporal(),
11504 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000011505 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000011506 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000011507 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000011508 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000011509 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000011510 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000011511 }
Dan Gohman475871a2008-07-27 21:46:04 +000011512 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000011513}
11514
Chris Lattner6cf73262008-01-25 06:14:17 +000011515/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
11516/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000011517static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000011518 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
11519 // F[X]OR(0.0, x) -> x
11520 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000011521 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
11522 if (C->getValueAPF().isPosZero())
11523 return N->getOperand(1);
11524 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
11525 if (C->getValueAPF().isPosZero())
11526 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000011527 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000011528}
11529
11530/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000011531static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000011532 // FAND(0.0, x) -> 0.0
11533 // FAND(x, 0.0) -> 0.0
11534 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
11535 if (C->getValueAPF().isPosZero())
11536 return N->getOperand(0);
11537 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
11538 if (C->getValueAPF().isPosZero())
11539 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000011540 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000011541}
11542
Dan Gohmane5af2d32009-01-29 01:59:02 +000011543static SDValue PerformBTCombine(SDNode *N,
11544 SelectionDAG &DAG,
11545 TargetLowering::DAGCombinerInfo &DCI) {
11546 // BT ignores high bits in the bit index operand.
11547 SDValue Op1 = N->getOperand(1);
11548 if (Op1.hasOneUse()) {
11549 unsigned BitWidth = Op1.getValueSizeInBits();
11550 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
11551 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000011552 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
11553 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000011554 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000011555 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
11556 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
11557 DCI.CommitTargetLoweringOpt(TLO);
11558 }
11559 return SDValue();
11560}
Chris Lattner83e6c992006-10-04 06:57:07 +000011561
Eli Friedman7a5e5552009-06-07 06:52:44 +000011562static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
11563 SDValue Op = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011564 if (Op.getOpcode() == ISD::BITCAST)
Eli Friedman7a5e5552009-06-07 06:52:44 +000011565 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000011566 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000011567 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000011568 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000011569 OpVT.getVectorElementType().getSizeInBits()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011570 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Eli Friedman7a5e5552009-06-07 06:52:44 +000011571 }
11572 return SDValue();
11573}
11574
Evan Cheng2e489c42009-12-16 00:53:11 +000011575static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG) {
11576 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
11577 // (and (i32 x86isd::setcc_carry), 1)
11578 // This eliminates the zext. This transformation is necessary because
11579 // ISD::SETCC is always legalized to i8.
11580 DebugLoc dl = N->getDebugLoc();
11581 SDValue N0 = N->getOperand(0);
11582 EVT VT = N->getValueType(0);
11583 if (N0.getOpcode() == ISD::AND &&
11584 N0.hasOneUse() &&
11585 N0.getOperand(0).hasOneUse()) {
11586 SDValue N00 = N0.getOperand(0);
11587 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
11588 return SDValue();
11589 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
11590 if (!C || C->getZExtValue() != 1)
11591 return SDValue();
11592 return DAG.getNode(ISD::AND, dl, VT,
11593 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
11594 N00.getOperand(0), N00.getOperand(1)),
11595 DAG.getConstant(1, VT));
11596 }
11597
11598 return SDValue();
11599}
11600
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011601// Optimize RES = X86ISD::SETCC CONDCODE, EFLAG_INPUT
11602static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG &DAG) {
11603 unsigned X86CC = N->getConstantOperandVal(0);
11604 SDValue EFLAG = N->getOperand(1);
11605 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011606
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011607 // Materialize "setb reg" as "sbb reg,reg", since it can be extended without
11608 // a zext and produces an all-ones bit which is more useful than 0/1 in some
11609 // cases.
11610 if (X86CC == X86::COND_B)
11611 return DAG.getNode(ISD::AND, DL, MVT::i8,
11612 DAG.getNode(X86ISD::SETCC_CARRY, DL, MVT::i8,
11613 DAG.getConstant(X86CC, MVT::i8), EFLAG),
11614 DAG.getConstant(1, MVT::i8));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011615
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011616 return SDValue();
11617}
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011618
Chris Lattner23a01992010-12-20 01:37:09 +000011619// Optimize RES, EFLAGS = X86ISD::ADC LHS, RHS, EFLAGS
11620static SDValue PerformADCCombine(SDNode *N, SelectionDAG &DAG,
11621 X86TargetLowering::DAGCombinerInfo &DCI) {
11622 // If the LHS and RHS of the ADC node are zero, then it can't overflow and
11623 // the result is either zero or one (depending on the input carry bit).
11624 // Strength reduce this down to a "set on carry" aka SETCC_CARRY&1.
11625 if (X86::isZeroNode(N->getOperand(0)) &&
11626 X86::isZeroNode(N->getOperand(1)) &&
11627 // We don't have a good way to replace an EFLAGS use, so only do this when
11628 // dead right now.
11629 SDValue(N, 1).use_empty()) {
11630 DebugLoc DL = N->getDebugLoc();
11631 EVT VT = N->getValueType(0);
11632 SDValue CarryOut = DAG.getConstant(0, N->getValueType(1));
11633 SDValue Res1 = DAG.getNode(ISD::AND, DL, VT,
11634 DAG.getNode(X86ISD::SETCC_CARRY, DL, VT,
11635 DAG.getConstant(X86::COND_B,MVT::i8),
11636 N->getOperand(2)),
11637 DAG.getConstant(1, VT));
11638 return DCI.CombineTo(N, Res1, CarryOut);
11639 }
11640
11641 return SDValue();
11642}
11643
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000011644// fold (add Y, (sete X, 0)) -> adc 0, Y
11645// (add Y, (setne X, 0)) -> sbb -1, Y
11646// (sub (sete X, 0), Y) -> sbb 0, Y
11647// (sub (setne X, 0), Y) -> adc -1, Y
11648static SDValue OptimizeConditonalInDecrement(SDNode *N, SelectionDAG &DAG) {
11649 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011650
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000011651 // Look through ZExts.
11652 SDValue Ext = N->getOperand(N->getOpcode() == ISD::SUB ? 1 : 0);
11653 if (Ext.getOpcode() != ISD::ZERO_EXTEND || !Ext.hasOneUse())
11654 return SDValue();
11655
11656 SDValue SetCC = Ext.getOperand(0);
11657 if (SetCC.getOpcode() != X86ISD::SETCC || !SetCC.hasOneUse())
11658 return SDValue();
11659
11660 X86::CondCode CC = (X86::CondCode)SetCC.getConstantOperandVal(0);
11661 if (CC != X86::COND_E && CC != X86::COND_NE)
11662 return SDValue();
11663
11664 SDValue Cmp = SetCC.getOperand(1);
11665 if (Cmp.getOpcode() != X86ISD::CMP || !Cmp.hasOneUse() ||
Chris Lattner9cd3da42011-01-16 02:56:53 +000011666 !X86::isZeroNode(Cmp.getOperand(1)) ||
11667 !Cmp.getOperand(0).getValueType().isInteger())
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000011668 return SDValue();
11669
11670 SDValue CmpOp0 = Cmp.getOperand(0);
11671 SDValue NewCmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32, CmpOp0,
11672 DAG.getConstant(1, CmpOp0.getValueType()));
11673
11674 SDValue OtherVal = N->getOperand(N->getOpcode() == ISD::SUB ? 0 : 1);
11675 if (CC == X86::COND_NE)
11676 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::ADC : X86ISD::SBB,
11677 DL, OtherVal.getValueType(), OtherVal,
11678 DAG.getConstant(-1ULL, OtherVal.getValueType()), NewCmp);
11679 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::SBB : X86ISD::ADC,
11680 DL, OtherVal.getValueType(), OtherVal,
11681 DAG.getConstant(0, OtherVal.getValueType()), NewCmp);
11682}
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011683
Dan Gohman475871a2008-07-27 21:46:04 +000011684SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000011685 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000011686 SelectionDAG &DAG = DCI.DAG;
11687 switch (N->getOpcode()) {
11688 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000011689 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011690 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, *this);
Chris Lattneraf723b92008-01-25 05:46:26 +000011691 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +000011692 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000011693 case ISD::ADD:
11694 case ISD::SUB: return OptimizeConditonalInDecrement(N, DAG);
Chris Lattner23a01992010-12-20 01:37:09 +000011695 case X86ISD::ADC: return PerformADCCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000011696 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000011697 case ISD::SHL:
11698 case ISD::SRA:
11699 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Nate Begemanb65c1752010-12-17 22:55:37 +000011700 case ISD::AND: return PerformAndCombine(N, DAG, DCI, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000011701 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000011702 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +000011703 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000011704 case X86ISD::FOR: return PerformFORCombine(N, DAG);
11705 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000011706 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000011707 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Evan Cheng2e489c42009-12-16 00:53:11 +000011708 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG);
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011709 case X86ISD::SETCC: return PerformSETCCCombine(N, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000011710 case X86ISD::SHUFPS: // Handle all target specific shuffles
11711 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +000011712 case X86ISD::PALIGN:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000011713 case X86ISD::PUNPCKHBW:
11714 case X86ISD::PUNPCKHWD:
11715 case X86ISD::PUNPCKHDQ:
11716 case X86ISD::PUNPCKHQDQ:
11717 case X86ISD::UNPCKHPS:
11718 case X86ISD::UNPCKHPD:
11719 case X86ISD::PUNPCKLBW:
11720 case X86ISD::PUNPCKLWD:
11721 case X86ISD::PUNPCKLDQ:
11722 case X86ISD::PUNPCKLQDQ:
11723 case X86ISD::UNPCKLPS:
11724 case X86ISD::UNPCKLPD:
11725 case X86ISD::MOVHLPS:
11726 case X86ISD::MOVLHPS:
11727 case X86ISD::PSHUFD:
11728 case X86ISD::PSHUFHW:
11729 case X86ISD::PSHUFLW:
11730 case X86ISD::MOVSS:
11731 case X86ISD::MOVSD:
Mon P Wanga0fd0d52010-12-19 23:55:53 +000011732 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, DCI);
Evan Cheng206ee9d2006-07-07 08:33:52 +000011733 }
11734
Dan Gohman475871a2008-07-27 21:46:04 +000011735 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000011736}
11737
Evan Chenge5b51ac2010-04-17 06:13:15 +000011738/// isTypeDesirableForOp - Return true if the target has native support for
11739/// the specified value type and it is 'desirable' to use the type for the
11740/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
11741/// instruction encodings are longer and some i16 instructions are slow.
11742bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
11743 if (!isTypeLegal(VT))
11744 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000011745 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000011746 return true;
11747
11748 switch (Opc) {
11749 default:
11750 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000011751 case ISD::LOAD:
11752 case ISD::SIGN_EXTEND:
11753 case ISD::ZERO_EXTEND:
11754 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000011755 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000011756 case ISD::SRL:
11757 case ISD::SUB:
11758 case ISD::ADD:
11759 case ISD::MUL:
11760 case ISD::AND:
11761 case ISD::OR:
11762 case ISD::XOR:
11763 return false;
11764 }
11765}
11766
11767/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000011768/// beneficial for dag combiner to promote the specified node. If true, it
11769/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000011770bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000011771 EVT VT = Op.getValueType();
11772 if (VT != MVT::i16)
11773 return false;
11774
Evan Cheng4c26e932010-04-19 19:29:22 +000011775 bool Promote = false;
11776 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000011777 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000011778 default: break;
11779 case ISD::LOAD: {
11780 LoadSDNode *LD = cast<LoadSDNode>(Op);
11781 // If the non-extending load has a single use and it's not live out, then it
11782 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000011783 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
11784 Op.hasOneUse()*/) {
11785 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
11786 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
11787 // The only case where we'd want to promote LOAD (rather then it being
11788 // promoted as an operand is when it's only use is liveout.
11789 if (UI->getOpcode() != ISD::CopyToReg)
11790 return false;
11791 }
11792 }
Evan Cheng4c26e932010-04-19 19:29:22 +000011793 Promote = true;
11794 break;
11795 }
11796 case ISD::SIGN_EXTEND:
11797 case ISD::ZERO_EXTEND:
11798 case ISD::ANY_EXTEND:
11799 Promote = true;
11800 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000011801 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000011802 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000011803 SDValue N0 = Op.getOperand(0);
11804 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000011805 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000011806 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000011807 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000011808 break;
11809 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000011810 case ISD::ADD:
11811 case ISD::MUL:
11812 case ISD::AND:
11813 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000011814 case ISD::XOR:
11815 Commute = true;
11816 // fallthrough
11817 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000011818 SDValue N0 = Op.getOperand(0);
11819 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000011820 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000011821 return false;
11822 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000011823 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000011824 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000011825 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000011826 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000011827 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000011828 }
11829 }
11830
11831 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000011832 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000011833}
11834
Evan Cheng60c07e12006-07-05 22:17:51 +000011835//===----------------------------------------------------------------------===//
11836// X86 Inline Assembly Support
11837//===----------------------------------------------------------------------===//
11838
Chris Lattnerb8105652009-07-20 17:51:36 +000011839bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
11840 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattnerb8105652009-07-20 17:51:36 +000011841
11842 std::string AsmStr = IA->getAsmString();
11843
11844 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000011845 SmallVector<StringRef, 4> AsmPieces;
Peter Collingbourne98361182010-11-13 19:54:23 +000011846 SplitString(AsmStr, AsmPieces, ";\n");
Chris Lattnerb8105652009-07-20 17:51:36 +000011847
11848 switch (AsmPieces.size()) {
11849 default: return false;
11850 case 1:
11851 AsmStr = AsmPieces[0];
11852 AsmPieces.clear();
11853 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
11854
Evan Cheng55d42002011-01-08 01:24:27 +000011855 // FIXME: this should verify that we are targetting a 486 or better. If not,
11856 // we will turn this bswap into something that will be lowered to logical ops
11857 // instead of emitting the bswap asm. For now, we don't support 486 or lower
11858 // so don't worry about this.
Chris Lattnerb8105652009-07-20 17:51:36 +000011859 // bswap $0
11860 if (AsmPieces.size() == 2 &&
11861 (AsmPieces[0] == "bswap" ||
11862 AsmPieces[0] == "bswapq" ||
11863 AsmPieces[0] == "bswapl") &&
11864 (AsmPieces[1] == "$0" ||
11865 AsmPieces[1] == "${0:q}")) {
11866 // No need to check constraints, nothing other than the equivalent of
11867 // "=r,0" would be valid here.
Evan Cheng55d42002011-01-08 01:24:27 +000011868 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
11869 if (!Ty || Ty->getBitWidth() % 16 != 0)
11870 return false;
11871 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000011872 }
11873 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000011874 if (CI->getType()->isIntegerTy(16) &&
Chris Lattnerb8105652009-07-20 17:51:36 +000011875 AsmPieces.size() == 3 &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000011876 (AsmPieces[0] == "rorw" || AsmPieces[0] == "rolw") &&
Chris Lattnerb8105652009-07-20 17:51:36 +000011877 AsmPieces[1] == "$$8," &&
11878 AsmPieces[2] == "${0:w}" &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000011879 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
11880 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000011881 const std::string &ConstraintsStr = IA->getConstraintString();
11882 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000011883 std::sort(AsmPieces.begin(), AsmPieces.end());
11884 if (AsmPieces.size() == 4 &&
11885 AsmPieces[0] == "~{cc}" &&
11886 AsmPieces[1] == "~{dirflag}" &&
11887 AsmPieces[2] == "~{flags}" &&
11888 AsmPieces[3] == "~{fpsr}") {
Evan Cheng55d42002011-01-08 01:24:27 +000011889 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
11890 if (!Ty || Ty->getBitWidth() % 16 != 0)
11891 return false;
11892 return IntrinsicLowering::LowerToByteSwap(CI);
Dan Gohman0ef701e2010-03-04 19:58:08 +000011893 }
Chris Lattnerb8105652009-07-20 17:51:36 +000011894 }
11895 break;
11896 case 3:
Peter Collingbourne948cf022010-11-13 19:54:30 +000011897 if (CI->getType()->isIntegerTy(32) &&
11898 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
11899 SmallVector<StringRef, 4> Words;
11900 SplitString(AsmPieces[0], Words, " \t,");
11901 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
11902 Words[2] == "${0:w}") {
11903 Words.clear();
11904 SplitString(AsmPieces[1], Words, " \t,");
11905 if (Words.size() == 3 && Words[0] == "rorl" && Words[1] == "$$16" &&
11906 Words[2] == "$0") {
11907 Words.clear();
11908 SplitString(AsmPieces[2], Words, " \t,");
11909 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
11910 Words[2] == "${0:w}") {
11911 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000011912 const std::string &ConstraintsStr = IA->getConstraintString();
11913 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Peter Collingbourne948cf022010-11-13 19:54:30 +000011914 std::sort(AsmPieces.begin(), AsmPieces.end());
11915 if (AsmPieces.size() == 4 &&
11916 AsmPieces[0] == "~{cc}" &&
11917 AsmPieces[1] == "~{dirflag}" &&
11918 AsmPieces[2] == "~{flags}" &&
11919 AsmPieces[3] == "~{fpsr}") {
Evan Cheng55d42002011-01-08 01:24:27 +000011920 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
11921 if (!Ty || Ty->getBitWidth() % 16 != 0)
11922 return false;
11923 return IntrinsicLowering::LowerToByteSwap(CI);
Peter Collingbourne948cf022010-11-13 19:54:30 +000011924 }
11925 }
11926 }
11927 }
11928 }
Evan Cheng55d42002011-01-08 01:24:27 +000011929
11930 if (CI->getType()->isIntegerTy(64)) {
11931 InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
11932 if (Constraints.size() >= 2 &&
11933 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
11934 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
11935 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
11936 SmallVector<StringRef, 4> Words;
11937 SplitString(AsmPieces[0], Words, " \t");
11938 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
Chris Lattnerb8105652009-07-20 17:51:36 +000011939 Words.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000011940 SplitString(AsmPieces[1], Words, " \t");
11941 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
11942 Words.clear();
11943 SplitString(AsmPieces[2], Words, " \t,");
11944 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
11945 Words[2] == "%edx") {
11946 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
11947 if (!Ty || Ty->getBitWidth() % 16 != 0)
11948 return false;
11949 return IntrinsicLowering::LowerToByteSwap(CI);
11950 }
Chris Lattnerb8105652009-07-20 17:51:36 +000011951 }
11952 }
11953 }
11954 }
11955 break;
11956 }
11957 return false;
11958}
11959
11960
11961
Chris Lattnerf4dff842006-07-11 02:54:03 +000011962/// getConstraintType - Given a constraint letter, return the type of
11963/// constraint it is for this target.
11964X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000011965X86TargetLowering::getConstraintType(const std::string &Constraint) const {
11966 if (Constraint.size() == 1) {
11967 switch (Constraint[0]) {
Chris Lattner4234f572007-03-25 02:14:49 +000011968 case 'R':
Chris Lattner4234f572007-03-25 02:14:49 +000011969 case 'q':
11970 case 'Q':
John Thompson44ab89e2010-10-29 17:29:13 +000011971 case 'f':
11972 case 't':
11973 case 'u':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000011974 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +000011975 case 'x':
Chris Lattner4234f572007-03-25 02:14:49 +000011976 case 'Y':
11977 return C_RegisterClass;
John Thompson44ab89e2010-10-29 17:29:13 +000011978 case 'a':
11979 case 'b':
11980 case 'c':
11981 case 'd':
11982 case 'S':
11983 case 'D':
11984 case 'A':
11985 return C_Register;
11986 case 'I':
11987 case 'J':
11988 case 'K':
11989 case 'L':
11990 case 'M':
11991 case 'N':
11992 case 'G':
11993 case 'C':
Dale Johannesen78e3e522009-02-12 20:58:09 +000011994 case 'e':
11995 case 'Z':
11996 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000011997 default:
11998 break;
11999 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000012000 }
Chris Lattner4234f572007-03-25 02:14:49 +000012001 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000012002}
12003
John Thompson44ab89e2010-10-29 17:29:13 +000012004/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +000012005/// This object must already have been set up with the operand type
12006/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +000012007TargetLowering::ConstraintWeight
12008 X86TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +000012009 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +000012010 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012011 Value *CallOperandVal = info.CallOperandVal;
12012 // If we don't have a value, we can't do a match,
12013 // but allow it at the lowest weight.
12014 if (CallOperandVal == NULL)
John Thompson44ab89e2010-10-29 17:29:13 +000012015 return CW_Default;
12016 const Type *type = CallOperandVal->getType();
John Thompsoneac6e1d2010-09-13 18:15:37 +000012017 // Look at the constraint type.
12018 switch (*constraint) {
12019 default:
John Thompson44ab89e2010-10-29 17:29:13 +000012020 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
12021 case 'R':
12022 case 'q':
12023 case 'Q':
12024 case 'a':
12025 case 'b':
12026 case 'c':
12027 case 'd':
12028 case 'S':
12029 case 'D':
12030 case 'A':
12031 if (CallOperandVal->getType()->isIntegerTy())
12032 weight = CW_SpecificReg;
12033 break;
12034 case 'f':
12035 case 't':
12036 case 'u':
12037 if (type->isFloatingPointTy())
12038 weight = CW_SpecificReg;
12039 break;
12040 case 'y':
Chris Lattner2a786eb2010-12-19 20:19:20 +000012041 if (type->isX86_MMXTy() && Subtarget->hasMMX())
John Thompson44ab89e2010-10-29 17:29:13 +000012042 weight = CW_SpecificReg;
12043 break;
12044 case 'x':
12045 case 'Y':
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012046 if ((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasXMM())
John Thompson44ab89e2010-10-29 17:29:13 +000012047 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012048 break;
12049 case 'I':
12050 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
12051 if (C->getZExtValue() <= 31)
John Thompson44ab89e2010-10-29 17:29:13 +000012052 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012053 }
12054 break;
John Thompson44ab89e2010-10-29 17:29:13 +000012055 case 'J':
12056 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12057 if (C->getZExtValue() <= 63)
12058 weight = CW_Constant;
12059 }
12060 break;
12061 case 'K':
12062 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12063 if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f))
12064 weight = CW_Constant;
12065 }
12066 break;
12067 case 'L':
12068 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12069 if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff))
12070 weight = CW_Constant;
12071 }
12072 break;
12073 case 'M':
12074 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12075 if (C->getZExtValue() <= 3)
12076 weight = CW_Constant;
12077 }
12078 break;
12079 case 'N':
12080 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12081 if (C->getZExtValue() <= 0xff)
12082 weight = CW_Constant;
12083 }
12084 break;
12085 case 'G':
12086 case 'C':
12087 if (dyn_cast<ConstantFP>(CallOperandVal)) {
12088 weight = CW_Constant;
12089 }
12090 break;
12091 case 'e':
12092 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12093 if ((C->getSExtValue() >= -0x80000000LL) &&
12094 (C->getSExtValue() <= 0x7fffffffLL))
12095 weight = CW_Constant;
12096 }
12097 break;
12098 case 'Z':
12099 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12100 if (C->getZExtValue() <= 0xffffffff)
12101 weight = CW_Constant;
12102 }
12103 break;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012104 }
12105 return weight;
12106}
12107
Dale Johannesenba2a0b92008-01-29 02:21:21 +000012108/// LowerXConstraint - try to replace an X constraint, which matches anything,
12109/// with another that has more specific requirements based on the type of the
12110/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000012111const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000012112LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000012113 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
12114 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000012115 if (ConstraintVT.isFloatingPoint()) {
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012116 if (Subtarget->hasXMMInt())
Chris Lattner5e764232008-04-26 23:02:14 +000012117 return "Y";
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012118 if (Subtarget->hasXMM())
Chris Lattner5e764232008-04-26 23:02:14 +000012119 return "x";
12120 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012121
Chris Lattner5e764232008-04-26 23:02:14 +000012122 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000012123}
12124
Chris Lattner48884cd2007-08-25 00:47:38 +000012125/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
12126/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000012127void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattner48884cd2007-08-25 00:47:38 +000012128 char Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000012129 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000012130 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000012131 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000012132
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012133 switch (Constraint) {
12134 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000012135 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000012136 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000012137 if (C->getZExtValue() <= 31) {
12138 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000012139 break;
12140 }
Devang Patel84f7fd22007-03-17 00:13:28 +000012141 }
Chris Lattner48884cd2007-08-25 00:47:38 +000012142 return;
Evan Cheng364091e2008-09-22 23:57:37 +000012143 case 'J':
12144 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000012145 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000012146 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
12147 break;
12148 }
12149 }
12150 return;
12151 case 'K':
12152 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000012153 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +000012154 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
12155 break;
12156 }
12157 }
12158 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000012159 case 'N':
12160 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000012161 if (C->getZExtValue() <= 255) {
12162 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000012163 break;
12164 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000012165 }
Chris Lattner48884cd2007-08-25 00:47:38 +000012166 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000012167 case 'e': {
12168 // 32-bit signed value
12169 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000012170 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
12171 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000012172 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000012173 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000012174 break;
12175 }
12176 // FIXME gcc accepts some relocatable values here too, but only in certain
12177 // memory models; it's complicated.
12178 }
12179 return;
12180 }
12181 case 'Z': {
12182 // 32-bit unsigned value
12183 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000012184 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
12185 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000012186 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
12187 break;
12188 }
12189 }
12190 // FIXME gcc accepts some relocatable values here too, but only in certain
12191 // memory models; it's complicated.
12192 return;
12193 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000012194 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012195 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000012196 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000012197 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000012198 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000012199 break;
12200 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012201
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000012202 // In any sort of PIC mode addresses need to be computed at runtime by
12203 // adding in a register or some sort of table lookup. These can't
12204 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000012205 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000012206 return;
12207
Chris Lattnerdc43a882007-05-03 16:52:29 +000012208 // If we are in non-pic codegen mode, we allow the address of a global (with
12209 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000012210 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000012211 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000012212
Chris Lattner49921962009-05-08 18:23:14 +000012213 // Match either (GA), (GA+C), (GA+C1+C2), etc.
12214 while (1) {
12215 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
12216 Offset += GA->getOffset();
12217 break;
12218 } else if (Op.getOpcode() == ISD::ADD) {
12219 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
12220 Offset += C->getZExtValue();
12221 Op = Op.getOperand(0);
12222 continue;
12223 }
12224 } else if (Op.getOpcode() == ISD::SUB) {
12225 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
12226 Offset += -C->getZExtValue();
12227 Op = Op.getOperand(0);
12228 continue;
12229 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000012230 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000012231
Chris Lattner49921962009-05-08 18:23:14 +000012232 // Otherwise, this isn't something we can handle, reject it.
12233 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000012234 }
Eric Christopherfd179292009-08-27 18:07:15 +000012235
Dan Gohman46510a72010-04-15 01:51:59 +000012236 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000012237 // If we require an extra load to get this address, as in PIC mode, we
12238 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000012239 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
12240 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000012241 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000012242
Devang Patel0d881da2010-07-06 22:08:15 +000012243 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
12244 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000012245 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012246 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000012247 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012248
Gabor Greifba36cb52008-08-28 21:40:38 +000012249 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000012250 Ops.push_back(Result);
12251 return;
12252 }
Dale Johannesen1784d162010-06-25 21:55:36 +000012253 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012254}
12255
Chris Lattner259e97c2006-01-31 19:43:35 +000012256std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +000012257getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000012258 EVT VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +000012259 if (Constraint.size() == 1) {
12260 // FIXME: not handling fp-stack yet!
Chris Lattner259e97c2006-01-31 19:43:35 +000012261 switch (Constraint[0]) { // GCC X86 Constraint Letters
Chris Lattnerf4dff842006-07-11 02:54:03 +000012262 default: break; // Unknown constraint letter
Evan Cheng47e9fab2009-07-17 22:13:25 +000012263 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
12264 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000012265 if (VT == MVT::i32)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012266 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX,
12267 X86::ESI, X86::EDI, X86::R8D, X86::R9D,
12268 X86::R10D,X86::R11D,X86::R12D,
12269 X86::R13D,X86::R14D,X86::R15D,
12270 X86::EBP, X86::ESP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012271 else if (VT == MVT::i16)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012272 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX,
12273 X86::SI, X86::DI, X86::R8W,X86::R9W,
12274 X86::R10W,X86::R11W,X86::R12W,
12275 X86::R13W,X86::R14W,X86::R15W,
12276 X86::BP, X86::SP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012277 else if (VT == MVT::i8)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012278 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL,
12279 X86::SIL, X86::DIL, X86::R8B,X86::R9B,
12280 X86::R10B,X86::R11B,X86::R12B,
12281 X86::R13B,X86::R14B,X86::R15B,
12282 X86::BPL, X86::SPL, 0);
12283
Owen Anderson825b72b2009-08-11 20:47:22 +000012284 else if (VT == MVT::i64)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012285 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX,
12286 X86::RSI, X86::RDI, X86::R8, X86::R9,
12287 X86::R10, X86::R11, X86::R12,
12288 X86::R13, X86::R14, X86::R15,
12289 X86::RBP, X86::RSP, 0);
12290
12291 break;
12292 }
Eric Christopherfd179292009-08-27 18:07:15 +000012293 // 32-bit fallthrough
Chris Lattner259e97c2006-01-31 19:43:35 +000012294 case 'Q': // Q_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +000012295 if (VT == MVT::i32)
Chris Lattner80a7ecc2006-05-06 00:29:37 +000012296 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012297 else if (VT == MVT::i16)
Chris Lattner80a7ecc2006-05-06 00:29:37 +000012298 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012299 else if (VT == MVT::i8)
Evan Cheng12914382007-08-13 23:27:11 +000012300 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012301 else if (VT == MVT::i64)
Chris Lattner03e6c702007-11-04 06:51:12 +000012302 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
12303 break;
Chris Lattner259e97c2006-01-31 19:43:35 +000012304 }
12305 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012306
Chris Lattner1efa40f2006-02-22 00:56:39 +000012307 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +000012308}
Chris Lattnerf76d1802006-07-31 23:26:50 +000012309
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012310std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000012311X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000012312 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000012313 // First, see if this is a constraint that directly corresponds to an LLVM
12314 // register class.
12315 if (Constraint.size() == 1) {
12316 // GCC Constraint Letters
12317 switch (Constraint[0]) {
12318 default: break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000012319 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000012320 case 'l': // INDEX_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +000012321 if (VT == MVT::i8)
Chris Lattner0f65cad2007-04-09 05:49:22 +000012322 return std::make_pair(0U, X86::GR8RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012323 if (VT == MVT::i16)
Chris Lattner1fa71982008-10-17 18:15:05 +000012324 return std::make_pair(0U, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012325 if (VT == MVT::i32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +000012326 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +000012327 return std::make_pair(0U, X86::GR64RegisterClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000012328 case 'R': // LEGACY_REGS
12329 if (VT == MVT::i8)
12330 return std::make_pair(0U, X86::GR8_NOREXRegisterClass);
12331 if (VT == MVT::i16)
12332 return std::make_pair(0U, X86::GR16_NOREXRegisterClass);
12333 if (VT == MVT::i32 || !Subtarget->is64Bit())
12334 return std::make_pair(0U, X86::GR32_NOREXRegisterClass);
12335 return std::make_pair(0U, X86::GR64_NOREXRegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000012336 case 'f': // FP Stack registers.
12337 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
12338 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000012339 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000012340 return std::make_pair(0U, X86::RFP32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012341 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000012342 return std::make_pair(0U, X86::RFP64RegisterClass);
12343 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000012344 case 'y': // MMX_REGS if MMX allowed.
12345 if (!Subtarget->hasMMX()) break;
12346 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000012347 case 'Y': // SSE_REGS if SSE2 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012348 if (!Subtarget->hasXMMInt()) break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000012349 // FALL THROUGH.
12350 case 'x': // SSE_REGS if SSE1 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012351 if (!Subtarget->hasXMM()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000012352
Owen Anderson825b72b2009-08-11 20:47:22 +000012353 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000012354 default: break;
12355 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000012356 case MVT::f32:
12357 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +000012358 return std::make_pair(0U, X86::FR32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012359 case MVT::f64:
12360 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +000012361 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000012362 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000012363 case MVT::v16i8:
12364 case MVT::v8i16:
12365 case MVT::v4i32:
12366 case MVT::v2i64:
12367 case MVT::v4f32:
12368 case MVT::v2f64:
Chris Lattner0f65cad2007-04-09 05:49:22 +000012369 return std::make_pair(0U, X86::VR128RegisterClass);
12370 }
Chris Lattnerad043e82007-04-09 05:11:28 +000012371 break;
12372 }
12373 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012374
Chris Lattnerf76d1802006-07-31 23:26:50 +000012375 // Use the default implementation in TargetLowering to convert the register
12376 // constraint into a member of a register class.
12377 std::pair<unsigned, const TargetRegisterClass*> Res;
12378 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000012379
12380 // Not found as a standard register?
12381 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000012382 // Map st(0) -> st(7) -> ST0
12383 if (Constraint.size() == 7 && Constraint[0] == '{' &&
12384 tolower(Constraint[1]) == 's' &&
12385 tolower(Constraint[2]) == 't' &&
12386 Constraint[3] == '(' &&
12387 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
12388 Constraint[5] == ')' &&
12389 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000012390
Chris Lattner56d77c72009-09-13 22:41:48 +000012391 Res.first = X86::ST0+Constraint[4]-'0';
12392 Res.second = X86::RFP80RegisterClass;
12393 return Res;
12394 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000012395
Chris Lattner56d77c72009-09-13 22:41:48 +000012396 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000012397 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000012398 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +000012399 Res.second = X86::RFP80RegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000012400 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000012401 }
Chris Lattner56d77c72009-09-13 22:41:48 +000012402
12403 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000012404 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000012405 Res.first = X86::EFLAGS;
12406 Res.second = X86::CCRRegisterClass;
12407 return Res;
12408 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000012409
Dale Johannesen330169f2008-11-13 21:52:36 +000012410 // 'A' means EAX + EDX.
12411 if (Constraint == "A") {
12412 Res.first = X86::EAX;
Dan Gohman68a31c22009-07-30 17:02:08 +000012413 Res.second = X86::GR32_ADRegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000012414 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000012415 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000012416 return Res;
12417 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012418
Chris Lattnerf76d1802006-07-31 23:26:50 +000012419 // Otherwise, check to see if this is a register class of the wrong value
12420 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
12421 // turn into {ax},{dx}.
12422 if (Res.second->hasType(VT))
12423 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012424
Chris Lattnerf76d1802006-07-31 23:26:50 +000012425 // All of the single-register GCC register classes map their values onto
12426 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
12427 // really want an 8-bit or 32-bit register, map to the appropriate register
12428 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +000012429 if (Res.second == X86::GR16RegisterClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000012430 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000012431 unsigned DestReg = 0;
12432 switch (Res.first) {
12433 default: break;
12434 case X86::AX: DestReg = X86::AL; break;
12435 case X86::DX: DestReg = X86::DL; break;
12436 case X86::CX: DestReg = X86::CL; break;
12437 case X86::BX: DestReg = X86::BL; break;
12438 }
12439 if (DestReg) {
12440 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000012441 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000012442 }
Owen Anderson825b72b2009-08-11 20:47:22 +000012443 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000012444 unsigned DestReg = 0;
12445 switch (Res.first) {
12446 default: break;
12447 case X86::AX: DestReg = X86::EAX; break;
12448 case X86::DX: DestReg = X86::EDX; break;
12449 case X86::CX: DestReg = X86::ECX; break;
12450 case X86::BX: DestReg = X86::EBX; break;
12451 case X86::SI: DestReg = X86::ESI; break;
12452 case X86::DI: DestReg = X86::EDI; break;
12453 case X86::BP: DestReg = X86::EBP; break;
12454 case X86::SP: DestReg = X86::ESP; break;
12455 }
12456 if (DestReg) {
12457 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000012458 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000012459 }
Owen Anderson825b72b2009-08-11 20:47:22 +000012460 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000012461 unsigned DestReg = 0;
12462 switch (Res.first) {
12463 default: break;
12464 case X86::AX: DestReg = X86::RAX; break;
12465 case X86::DX: DestReg = X86::RDX; break;
12466 case X86::CX: DestReg = X86::RCX; break;
12467 case X86::BX: DestReg = X86::RBX; break;
12468 case X86::SI: DestReg = X86::RSI; break;
12469 case X86::DI: DestReg = X86::RDI; break;
12470 case X86::BP: DestReg = X86::RBP; break;
12471 case X86::SP: DestReg = X86::RSP; break;
12472 }
12473 if (DestReg) {
12474 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000012475 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000012476 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000012477 }
Chris Lattner6ba50a92008-08-26 06:19:02 +000012478 } else if (Res.second == X86::FR32RegisterClass ||
12479 Res.second == X86::FR64RegisterClass ||
12480 Res.second == X86::VR128RegisterClass) {
12481 // Handle references to XMM physical registers that got mapped into the
12482 // wrong class. This can happen with constraints like {xmm0} where the
12483 // target independent register mapper will just pick the first match it can
12484 // find, ignoring the required type.
Owen Anderson825b72b2009-08-11 20:47:22 +000012485 if (VT == MVT::f32)
Chris Lattner6ba50a92008-08-26 06:19:02 +000012486 Res.second = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +000012487 else if (VT == MVT::f64)
Chris Lattner6ba50a92008-08-26 06:19:02 +000012488 Res.second = X86::FR64RegisterClass;
12489 else if (X86::VR128RegisterClass->hasType(VT))
12490 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000012491 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012492
Chris Lattnerf76d1802006-07-31 23:26:50 +000012493 return Res;
12494}