blob: 89ae4e8d966219755b4f58aac13f7464bc819bf1 [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"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000031#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng4a460802006-01-11 00:33:36 +000032#include "llvm/CodeGen/MachineFunction.h"
33#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner5e1df8d2010-01-25 23:38:14 +000034#include "llvm/CodeGen/MachineJumpTableInfo.h"
Evan Chenga844bde2008-02-02 04:07:54 +000035#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000036#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman69de1932008-02-06 22:27:42 +000037#include "llvm/CodeGen/PseudoSourceValue.h"
Chris Lattner589c6f62010-01-26 06:28:43 +000038#include "llvm/MC/MCAsmInfo.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000039#include "llvm/MC/MCContext.h"
Daniel Dunbar4e815f82010-03-15 23:51:06 +000040#include "llvm/MC/MCExpr.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000041#include "llvm/MC/MCSymbol.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000042#include "llvm/ADT/BitVector.h"
Evan Cheng14b32e12007-12-11 01:46:18 +000043#include "llvm/ADT/SmallSet.h"
Evan Chengb1712452010-01-27 06:25:16 +000044#include "llvm/ADT/Statistic.h"
Chris Lattner1a60aa72006-10-31 19:42:44 +000045#include "llvm/ADT/StringExtras.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000046#include "llvm/ADT/VectorExtras.h"
Mon P Wang3c81d352008-11-23 04:37:22 +000047#include "llvm/Support/CommandLine.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000048#include "llvm/Support/Debug.h"
Bill Wendlingec041eb2010-03-12 19:20:40 +000049#include "llvm/Support/Dwarf.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000050#include "llvm/Support/ErrorHandling.h"
51#include "llvm/Support/MathExtras.h"
Torok Edwindac237e2009-07-08 20:53:28 +000052#include "llvm/Support/raw_ostream.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000053using namespace llvm;
Bill Wendlingec041eb2010-03-12 19:20:40 +000054using namespace dwarf;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000055
Evan Chengb1712452010-01-27 06:25:16 +000056STATISTIC(NumTailCalls, "Number of tail calls");
57
Mon P Wang3c81d352008-11-23 04:37:22 +000058static cl::opt<bool>
Mon P Wang9f22a4a2008-11-24 02:10:43 +000059DisableMMX("disable-mmx", cl::Hidden, cl::desc("Disable use of MMX"));
Mon P Wang3c81d352008-11-23 04:37:22 +000060
Evan Cheng10e86422008-04-25 19:11:04 +000061// Forward declarations.
Owen Andersone50ed302009-08-10 22:56:29 +000062static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +000063 SDValue V2);
Evan Cheng10e86422008-04-25 19:11:04 +000064
Chris Lattnerf0144122009-07-28 03:13:23 +000065static TargetLoweringObjectFile *createTLOF(X86TargetMachine &TM) {
Michael J. Spencerec38de22010-10-10 22:04:20 +000066
Eric Christopher62f35a22010-07-05 19:26:33 +000067 bool is64Bit = TM.getSubtarget<X86Subtarget>().is64Bit();
Michael J. Spencerec38de22010-10-10 22:04:20 +000068
Eric Christopher62f35a22010-07-05 19:26:33 +000069 if (TM.getSubtarget<X86Subtarget>().isTargetDarwin()) {
70 if (is64Bit) return new X8664_MachoTargetObjectFile();
Anton Korobeynikov293d5922010-02-21 20:28:15 +000071 return new TargetLoweringObjectFileMachO();
Eric Christopher62f35a22010-07-05 19:26:33 +000072 } else if (TM.getSubtarget<X86Subtarget>().isTargetELF() ){
73 if (is64Bit) return new X8664_ELFTargetObjectFile(TM);
Anton Korobeynikov9184b252010-02-15 22:35:59 +000074 return new X8632_ELFTargetObjectFile(TM);
Eric Christopher62f35a22010-07-05 19:26:33 +000075 } else if (TM.getSubtarget<X86Subtarget>().isTargetCOFF()) {
Chris Lattnerf0144122009-07-28 03:13:23 +000076 return new TargetLoweringObjectFileCOFF();
Michael J. Spencerec38de22010-10-10 22:04:20 +000077 }
Eric Christopher62f35a22010-07-05 19:26:33 +000078 llvm_unreachable("unknown subtarget type");
Chris Lattnerf0144122009-07-28 03:13:23 +000079}
80
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +000081X86TargetLowering::X86TargetLowering(X86TargetMachine &TM)
Chris Lattnerf0144122009-07-28 03:13:23 +000082 : TargetLowering(TM, createTLOF(TM)) {
Evan Cheng559806f2006-01-27 08:10:46 +000083 Subtarget = &TM.getSubtarget<X86Subtarget>();
Dale Johannesenf1fc3a82007-09-23 14:52:20 +000084 X86ScalarSSEf64 = Subtarget->hasSSE2();
85 X86ScalarSSEf32 = Subtarget->hasSSE1();
Evan Cheng25ab6902006-09-08 06:48:29 +000086 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000087
Anton Korobeynikov2365f512007-07-14 14:06:15 +000088 RegInfo = TM.getRegisterInfo();
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000089 TD = getTargetData();
Anton Korobeynikov2365f512007-07-14 14:06:15 +000090
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000091 // Set up the TargetLowering object.
92
93 // X86 is weird, it always uses i8 for shift amounts and setcc results.
Owen Anderson825b72b2009-08-11 20:47:22 +000094 setShiftAmountType(MVT::i8);
Duncan Sands03228082008-11-23 15:47:28 +000095 setBooleanContents(ZeroOrOneBooleanContent);
Evan Cheng211ffa12010-05-19 20:19:50 +000096 setSchedulingPreference(Sched::RegPressure);
Evan Cheng25ab6902006-09-08 06:48:29 +000097 setStackPointerRegisterToSaveRestore(X86StackPtr);
Evan Cheng714554d2006-03-16 21:47:42 +000098
Anton Korobeynikovd27a2582006-12-10 23:12:42 +000099 if (Subtarget->isTargetDarwin()) {
Evan Chengdf57fa02006-03-17 20:31:41 +0000100 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000101 setUseUnderscoreSetJmp(false);
102 setUseUnderscoreLongJmp(false);
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000103 } else if (Subtarget->isTargetMingw()) {
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000104 // MS runtime is weird: it exports _setjmp, but longjmp!
105 setUseUnderscoreSetJmp(true);
106 setUseUnderscoreLongJmp(false);
107 } else {
108 setUseUnderscoreSetJmp(true);
109 setUseUnderscoreLongJmp(true);
110 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000111
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000112 // Set up the register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000113 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
Dan Gohman71edb242010-04-30 18:30:26 +0000114 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000115 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
Evan Cheng25ab6902006-09-08 06:48:29 +0000116 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000117 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000118
Owen Anderson825b72b2009-08-11 20:47:22 +0000119 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Evan Chengc5484282006-10-04 00:56:09 +0000120
Scott Michelfdc40a02009-02-17 22:15:04 +0000121 // We don't accept any truncstore of integer registers.
Owen Anderson825b72b2009-08-11 20:47:22 +0000122 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000123 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000124 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000125 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000126 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
127 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
Evan Cheng7f042682008-10-15 02:05:31 +0000128
129 // SETOEQ and SETUNE require checking two conditions.
Owen Anderson825b72b2009-08-11 20:47:22 +0000130 setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand);
131 setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand);
132 setCondCodeAction(ISD::SETOEQ, MVT::f80, Expand);
133 setCondCodeAction(ISD::SETUNE, MVT::f32, Expand);
134 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
135 setCondCodeAction(ISD::SETUNE, MVT::f80, Expand);
Chris Lattnerddf89562008-01-17 19:59:44 +0000136
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000137 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
138 // operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000139 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
140 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
141 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +0000142
Evan Cheng25ab6902006-09-08 06:48:29 +0000143 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000144 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
145 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
Eli Friedman948e95a2009-05-23 09:59:16 +0000146 } else if (!UseSoftFloat) {
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000147 // We have an algorithm for SSE2->double, and we turn this into a
148 // 64-bit FILD followed by conditional FADD for other targets.
149 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Eli Friedman948e95a2009-05-23 09:59:16 +0000150 // We have an algorithm for SSE2, and we turn this into a 64-bit
151 // FILD for other targets.
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000152 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000153 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000154
155 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
156 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000157 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
158 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000159
Devang Patel6a784892009-06-05 18:48:29 +0000160 if (!UseSoftFloat) {
Bill Wendling105be5a2009-03-13 08:41:47 +0000161 // SSE has no i16 to fp conversion, only i32
162 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000163 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000164 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000165 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000166 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000167 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
168 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000169 }
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000170 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000171 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
172 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Promote);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000173 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000174
Dale Johannesen73328d12007-09-19 23:55:34 +0000175 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
176 // are Legal, f80 is custom lowered.
Owen Anderson825b72b2009-08-11 20:47:22 +0000177 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
178 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Evan Cheng6dab0532006-01-30 08:02:57 +0000179
Evan Cheng02568ff2006-01-30 22:13:22 +0000180 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
181 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000182 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
183 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
Evan Cheng02568ff2006-01-30 22:13:22 +0000184
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000185 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000186 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000187 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000188 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000189 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000190 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
191 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000192 }
193
194 // Handle FP_TO_UINT by promoting the destination to a larger signed
195 // conversion.
Owen Anderson825b72b2009-08-11 20:47:22 +0000196 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
197 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
198 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000199
Evan Cheng25ab6902006-09-08 06:48:29 +0000200 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000201 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
202 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Eli Friedman948e95a2009-05-23 09:59:16 +0000203 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000204 if (X86ScalarSSEf32 && !Subtarget->hasSSE3())
Evan Cheng25ab6902006-09-08 06:48:29 +0000205 // Expand FP_TO_UINT into a select.
206 // FIXME: We would like to use a Custom expander here eventually to do
207 // the optimal thing for SSE vs. the default expansion in the legalizer.
Owen Anderson825b72b2009-08-11 20:47:22 +0000208 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000209 else
Eli Friedman948e95a2009-05-23 09:59:16 +0000210 // With SSE3 we can use fisttpll to convert to a signed i64; without
211 // SSE, we're stuck with a fistpll.
Owen Anderson825b72b2009-08-11 20:47:22 +0000212 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000213 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000214
Chris Lattner399610a2006-12-05 18:22:22 +0000215 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Michael J. Spencerec38de22010-10-10 22:04:20 +0000216 if (!X86ScalarSSEf64) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000217 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
218 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
Dale Johannesene39859a2010-05-21 18:40:15 +0000219 if (Subtarget->is64Bit()) {
Dale Johannesen7d07b482010-05-21 00:52:33 +0000220 setOperationAction(ISD::BIT_CONVERT , MVT::f64 , Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000221 // Without SSE, i64->f64 goes through memory.
222 setOperationAction(ISD::BIT_CONVERT , MVT::i64 , Expand);
Dale Johannesen7d07b482010-05-21 00:52:33 +0000223 }
Chris Lattnerf3597a12006-12-05 18:45:06 +0000224 }
Chris Lattner21f66852005-12-23 05:15:23 +0000225
Dan Gohmanb00ee212008-02-18 19:34:53 +0000226 // Scalar integer divide and remainder are lowered to use operations that
227 // produce two results, to match the available instructions. This exposes
228 // the two-result form to trivial CSE, which is able to combine x/y and x%y
229 // into a single instruction.
230 //
231 // Scalar integer multiply-high is also lowered to use two-result
232 // operations, to match the available instructions. However, plain multiply
233 // (low) operations are left as Legal, as there are single-result
234 // instructions for this in x86. Using the two-result multiply instructions
235 // when both high and low results are needed must be arranged by dagcombine.
Owen Anderson825b72b2009-08-11 20:47:22 +0000236 setOperationAction(ISD::MULHS , MVT::i8 , Expand);
237 setOperationAction(ISD::MULHU , MVT::i8 , Expand);
238 setOperationAction(ISD::SDIV , MVT::i8 , Expand);
239 setOperationAction(ISD::UDIV , MVT::i8 , Expand);
240 setOperationAction(ISD::SREM , MVT::i8 , Expand);
241 setOperationAction(ISD::UREM , MVT::i8 , Expand);
242 setOperationAction(ISD::MULHS , MVT::i16 , Expand);
243 setOperationAction(ISD::MULHU , MVT::i16 , Expand);
244 setOperationAction(ISD::SDIV , MVT::i16 , Expand);
245 setOperationAction(ISD::UDIV , MVT::i16 , Expand);
246 setOperationAction(ISD::SREM , MVT::i16 , Expand);
247 setOperationAction(ISD::UREM , MVT::i16 , Expand);
248 setOperationAction(ISD::MULHS , MVT::i32 , Expand);
249 setOperationAction(ISD::MULHU , MVT::i32 , Expand);
250 setOperationAction(ISD::SDIV , MVT::i32 , Expand);
251 setOperationAction(ISD::UDIV , MVT::i32 , Expand);
252 setOperationAction(ISD::SREM , MVT::i32 , Expand);
253 setOperationAction(ISD::UREM , MVT::i32 , Expand);
254 setOperationAction(ISD::MULHS , MVT::i64 , Expand);
255 setOperationAction(ISD::MULHU , MVT::i64 , Expand);
256 setOperationAction(ISD::SDIV , MVT::i64 , Expand);
257 setOperationAction(ISD::UDIV , MVT::i64 , Expand);
258 setOperationAction(ISD::SREM , MVT::i64 , Expand);
259 setOperationAction(ISD::UREM , MVT::i64 , Expand);
Dan Gohmana37c9f72007-09-25 18:23:27 +0000260
Owen Anderson825b72b2009-08-11 20:47:22 +0000261 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
262 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
263 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
264 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000265 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000266 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
267 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
268 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
269 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
270 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
271 setOperationAction(ISD::FREM , MVT::f32 , Expand);
272 setOperationAction(ISD::FREM , MVT::f64 , Expand);
273 setOperationAction(ISD::FREM , MVT::f80 , Expand);
274 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000275
Owen Anderson825b72b2009-08-11 20:47:22 +0000276 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
277 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
278 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
279 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000280 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
281 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000282 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
283 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
284 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000285 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000286 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
287 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
288 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000289 }
290
Owen Anderson825b72b2009-08-11 20:47:22 +0000291 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
292 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000293
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000294 // These should be promoted to a larger select which is supported.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000295 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000296 // X86 wants to expand cmov itself.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000297 setOperationAction(ISD::SELECT , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000298 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000299 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
300 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
301 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
302 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
303 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000304 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000305 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
306 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
307 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
308 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000309 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000310 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
311 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000312 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000313 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000314
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000315 // Darwin ABI issue.
Owen Anderson825b72b2009-08-11 20:47:22 +0000316 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
317 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
318 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
319 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000320 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000321 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
322 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000323 setOperationAction(ISD::BlockAddress , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000324 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000325 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
326 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
327 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
328 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000329 setOperationAction(ISD::BlockAddress , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000330 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000331 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Owen Anderson825b72b2009-08-11 20:47:22 +0000332 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
333 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
334 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000335 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000336 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
337 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
338 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000339 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000340
Evan Chengd2cde682008-03-10 19:38:10 +0000341 if (Subtarget->hasSSE1())
Owen Anderson825b72b2009-08-11 20:47:22 +0000342 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Cheng27b7db52008-03-08 00:58:38 +0000343
Eric Christopher9a9d2752010-07-22 02:48:34 +0000344 // We may not have a libcall for MEMBARRIER so we should lower this.
345 setOperationAction(ISD::MEMBARRIER , MVT::Other, Custom);
Michael J. Spencerec38de22010-10-10 22:04:20 +0000346
Jim Grosbachf1ab49e2010-06-23 16:25:07 +0000347 // On X86 and X86-64, atomic operations are lowered to locked instructions.
348 // Locked instructions, in turn, have implicit fence semantics (all memory
349 // operations are flushed before issuing the locked instruction, and they
350 // are not buffered), so we can fold away the common pattern of
351 // fence-atomic-fence.
352 setShouldFoldAtomicFences(true);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000353
Mon P Wang63307c32008-05-05 19:05:59 +0000354 // Expand certain atomics
Owen Anderson825b72b2009-08-11 20:47:22 +0000355 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i8, Custom);
356 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i16, Custom);
357 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Custom);
358 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Custom);
Bill Wendling5bf1b4e2008-08-20 00:28:16 +0000359
Owen Anderson825b72b2009-08-11 20:47:22 +0000360 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i8, Custom);
361 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i16, Custom);
362 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Custom);
363 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000364
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000365 if (!Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000366 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
367 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
368 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
369 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
370 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
371 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
372 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000373 }
374
Evan Cheng3c992d22006-03-07 02:02:57 +0000375 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000376 if (!Subtarget->isTargetDarwin() &&
377 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000378 !Subtarget->isTargetCygMing()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000379 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Dan Gohman44066042008-07-01 00:05:16 +0000380 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000381
Owen Anderson825b72b2009-08-11 20:47:22 +0000382 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
383 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
384 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
385 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000386 if (Subtarget->is64Bit()) {
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000387 setExceptionPointerRegister(X86::RAX);
388 setExceptionSelectorRegister(X86::RDX);
389 } else {
390 setExceptionPointerRegister(X86::EAX);
391 setExceptionSelectorRegister(X86::EDX);
392 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000393 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
394 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000395
Owen Anderson825b72b2009-08-11 20:47:22 +0000396 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000397
Owen Anderson825b72b2009-08-11 20:47:22 +0000398 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000399
Nate Begemanacc398c2006-01-25 18:21:52 +0000400 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson825b72b2009-08-11 20:47:22 +0000401 setOperationAction(ISD::VASTART , MVT::Other, Custom);
402 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000403 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000404 setOperationAction(ISD::VAARG , MVT::Other, Custom);
405 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000406 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000407 setOperationAction(ISD::VAARG , MVT::Other, Expand);
408 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000409 }
Evan Chengae642192007-03-02 23:16:35 +0000410
Owen Anderson825b72b2009-08-11 20:47:22 +0000411 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
412 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000413 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000414 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +0000415 if (Subtarget->isTargetCygMing())
Owen Anderson825b72b2009-08-11 20:47:22 +0000416 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +0000417 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000418 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000419
Evan Chengc7ce29b2009-02-13 22:36:38 +0000420 if (!UseSoftFloat && X86ScalarSSEf64) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000421 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000422 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000423 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
424 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000425
Evan Cheng223547a2006-01-31 22:28:30 +0000426 // Use ANDPD to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000427 setOperationAction(ISD::FABS , MVT::f64, Custom);
428 setOperationAction(ISD::FABS , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000429
430 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000431 setOperationAction(ISD::FNEG , MVT::f64, Custom);
432 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000433
Evan Cheng68c47cb2007-01-05 07:55:56 +0000434 // Use ANDPD and ORPD to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000435 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
436 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000437
Evan Chengd25e9e82006-02-02 00:28:23 +0000438 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000439 setOperationAction(ISD::FSIN , MVT::f64, Expand);
440 setOperationAction(ISD::FCOS , MVT::f64, Expand);
441 setOperationAction(ISD::FSIN , MVT::f32, Expand);
442 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000443
Chris Lattnera54aa942006-01-29 06:26:08 +0000444 // Expand FP immediates into loads from the stack, except for the special
445 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000446 addLegalFPImmediate(APFloat(+0.0)); // xorpd
447 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Evan Chengc7ce29b2009-02-13 22:36:38 +0000448 } else if (!UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000449 // Use SSE for f32, x87 for f64.
450 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000451 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
452 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000453
454 // Use ANDPS to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000455 setOperationAction(ISD::FABS , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000456
457 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000458 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000459
Owen Anderson825b72b2009-08-11 20:47:22 +0000460 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000461
462 // Use ANDPS and ORPS to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000463 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
464 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000465
466 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000467 setOperationAction(ISD::FSIN , MVT::f32, Expand);
468 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000469
Nate Begemane1795842008-02-14 08:57:00 +0000470 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000471 addLegalFPImmediate(APFloat(+0.0f)); // xorps
472 addLegalFPImmediate(APFloat(+0.0)); // FLD0
473 addLegalFPImmediate(APFloat(+1.0)); // FLD1
474 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
475 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
476
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000477 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000478 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
479 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000480 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000481 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000482 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000483 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000484 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
485 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000486
Owen Anderson825b72b2009-08-11 20:47:22 +0000487 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
488 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
489 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
490 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000491
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000492 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000493 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
494 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000495 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000496 addLegalFPImmediate(APFloat(+0.0)); // FLD0
497 addLegalFPImmediate(APFloat(+1.0)); // FLD1
498 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
499 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000500 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
501 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
502 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
503 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000504 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000505
Dale Johannesen59a58732007-08-05 18:49:15 +0000506 // Long double always uses X87.
Evan Cheng92722532009-03-26 23:06:32 +0000507 if (!UseSoftFloat) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000508 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
509 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
510 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000511 {
512 bool ignored;
513 APFloat TmpFlt(+0.0);
514 TmpFlt.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
515 &ignored);
516 addLegalFPImmediate(TmpFlt); // FLD0
517 TmpFlt.changeSign();
518 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
519 APFloat TmpFlt2(+1.0);
520 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
521 &ignored);
522 addLegalFPImmediate(TmpFlt2); // FLD1
523 TmpFlt2.changeSign();
524 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
525 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000526
Evan Chengc7ce29b2009-02-13 22:36:38 +0000527 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000528 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
529 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000530 }
Dale Johannesen2f429012007-09-26 21:10:55 +0000531 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000532
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000533 // Always use a library call for pow.
Owen Anderson825b72b2009-08-11 20:47:22 +0000534 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
535 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
536 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000537
Owen Anderson825b72b2009-08-11 20:47:22 +0000538 setOperationAction(ISD::FLOG, MVT::f80, Expand);
539 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
540 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
541 setOperationAction(ISD::FEXP, MVT::f80, Expand);
542 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000543
Mon P Wangf007a8b2008-11-06 05:31:54 +0000544 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000545 // (for widening) or expand (for scalarization). Then we will selectively
546 // turn on ones that can be effectively codegen'd.
Owen Anderson825b72b2009-08-11 20:47:22 +0000547 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
548 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
549 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
550 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
551 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
552 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
553 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
554 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
555 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
556 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
557 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
558 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
559 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
560 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
561 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
562 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
563 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
564 setOperationAction(ISD::EXTRACT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
565 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
566 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
567 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
568 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
569 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
570 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
571 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
572 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
573 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
574 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
575 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
576 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
577 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
578 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
579 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
580 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
581 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
582 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
583 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
584 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
585 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
586 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
587 setOperationAction(ISD::VSETCC, (MVT::SimpleValueType)VT, Expand);
588 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
589 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
590 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
591 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
592 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
593 setOperationAction(ISD::FP_TO_UINT, (MVT::SimpleValueType)VT, Expand);
594 setOperationAction(ISD::FP_TO_SINT, (MVT::SimpleValueType)VT, Expand);
595 setOperationAction(ISD::UINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
596 setOperationAction(ISD::SINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
Dan Gohman87862e72009-12-11 21:31:27 +0000597 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,Expand);
Dan Gohman2e141d72009-12-14 23:40:38 +0000598 setOperationAction(ISD::TRUNCATE, (MVT::SimpleValueType)VT, Expand);
599 setOperationAction(ISD::SIGN_EXTEND, (MVT::SimpleValueType)VT, Expand);
600 setOperationAction(ISD::ZERO_EXTEND, (MVT::SimpleValueType)VT, Expand);
601 setOperationAction(ISD::ANY_EXTEND, (MVT::SimpleValueType)VT, Expand);
602 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
603 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
604 setTruncStoreAction((MVT::SimpleValueType)VT,
605 (MVT::SimpleValueType)InnerVT, Expand);
606 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
607 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
608 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000609 }
610
Evan Chengc7ce29b2009-02-13 22:36:38 +0000611 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
612 // with -msoft-float, disable use of MMX as well.
Evan Cheng92722532009-03-26 23:06:32 +0000613 if (!UseSoftFloat && !DisableMMX && Subtarget->hasMMX()) {
Bill Wendlingd8dd5752010-09-07 20:03:56 +0000614 addRegisterClass(MVT::x86mmx, X86::VR64RegisterClass, false);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000615 // No operations on x86mmx supported, everything uses intrinsics.
Evan Cheng470a6ad2006-02-22 02:26:30 +0000616 }
617
Dale Johannesen0488fb62010-09-30 23:57:10 +0000618 // MMX-sized vectors (other than x86mmx) are expected to be expanded
619 // into smaller operations.
620 setOperationAction(ISD::MULHS, MVT::v8i8, Expand);
621 setOperationAction(ISD::MULHS, MVT::v4i16, Expand);
622 setOperationAction(ISD::MULHS, MVT::v2i32, Expand);
623 setOperationAction(ISD::MULHS, MVT::v1i64, Expand);
624 setOperationAction(ISD::AND, MVT::v8i8, Expand);
625 setOperationAction(ISD::AND, MVT::v4i16, Expand);
626 setOperationAction(ISD::AND, MVT::v2i32, Expand);
627 setOperationAction(ISD::AND, MVT::v1i64, Expand);
628 setOperationAction(ISD::OR, MVT::v8i8, Expand);
629 setOperationAction(ISD::OR, MVT::v4i16, Expand);
630 setOperationAction(ISD::OR, MVT::v2i32, Expand);
631 setOperationAction(ISD::OR, MVT::v1i64, Expand);
632 setOperationAction(ISD::XOR, MVT::v8i8, Expand);
633 setOperationAction(ISD::XOR, MVT::v4i16, Expand);
634 setOperationAction(ISD::XOR, MVT::v2i32, Expand);
635 setOperationAction(ISD::XOR, MVT::v1i64, Expand);
636 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Expand);
637 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Expand);
638 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Expand);
639 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Expand);
640 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v1i64, Expand);
641 setOperationAction(ISD::SELECT, MVT::v8i8, Expand);
642 setOperationAction(ISD::SELECT, MVT::v4i16, Expand);
643 setOperationAction(ISD::SELECT, MVT::v2i32, Expand);
644 setOperationAction(ISD::SELECT, MVT::v1i64, Expand);
645 setOperationAction(ISD::BIT_CONVERT, MVT::v8i8, Expand);
646 setOperationAction(ISD::BIT_CONVERT, MVT::v4i16, Expand);
647 setOperationAction(ISD::BIT_CONVERT, MVT::v2i32, Expand);
648 setOperationAction(ISD::BIT_CONVERT, MVT::v1i64, Expand);
649
Evan Cheng92722532009-03-26 23:06:32 +0000650 if (!UseSoftFloat && Subtarget->hasSSE1()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000651 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000652
Owen Anderson825b72b2009-08-11 20:47:22 +0000653 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
654 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
655 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
656 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
657 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
658 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
659 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
660 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
661 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
662 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
663 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
664 setOperationAction(ISD::VSETCC, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000665 }
666
Evan Cheng92722532009-03-26 23:06:32 +0000667 if (!UseSoftFloat && Subtarget->hasSSE2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000668 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000669
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000670 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
671 // registers cannot be used even for integer operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000672 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
673 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
674 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
675 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000676
Owen Anderson825b72b2009-08-11 20:47:22 +0000677 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
678 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
679 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
680 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
681 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
682 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
683 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
684 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
685 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
686 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
687 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
688 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
689 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
690 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
691 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
692 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000693
Owen Anderson825b72b2009-08-11 20:47:22 +0000694 setOperationAction(ISD::VSETCC, MVT::v2f64, Custom);
695 setOperationAction(ISD::VSETCC, MVT::v16i8, Custom);
696 setOperationAction(ISD::VSETCC, MVT::v8i16, Custom);
697 setOperationAction(ISD::VSETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000698
Owen Anderson825b72b2009-08-11 20:47:22 +0000699 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
700 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
701 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
702 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
703 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000704
Mon P Wangeb38ebf2010-01-24 00:05:03 +0000705 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2f64, Custom);
706 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2i64, Custom);
707 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i8, Custom);
708 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i16, Custom);
709 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i32, Custom);
710
Evan Cheng2c3ae372006-04-12 21:21:57 +0000711 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Owen Anderson825b72b2009-08-11 20:47:22 +0000712 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
713 EVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000714 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000715 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000716 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000717 // Do not attempt to custom lower non-128-bit vectors
718 if (!VT.is128BitVector())
719 continue;
Owen Anderson825b72b2009-08-11 20:47:22 +0000720 setOperationAction(ISD::BUILD_VECTOR,
721 VT.getSimpleVT().SimpleTy, Custom);
722 setOperationAction(ISD::VECTOR_SHUFFLE,
723 VT.getSimpleVT().SimpleTy, Custom);
724 setOperationAction(ISD::EXTRACT_VECTOR_ELT,
725 VT.getSimpleVT().SimpleTy, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000726 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000727
Owen Anderson825b72b2009-08-11 20:47:22 +0000728 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
729 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
730 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
731 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
732 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
733 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000734
Nate Begemancdd1eec2008-02-12 22:51:28 +0000735 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000736 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
737 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000738 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000739
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000740 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Owen Anderson825b72b2009-08-11 20:47:22 +0000741 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; i++) {
742 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
Owen Andersone50ed302009-08-10 22:56:29 +0000743 EVT VT = SVT;
David Greene9b9838d2009-06-29 16:47:10 +0000744
745 // Do not attempt to promote non-128-bit vectors
Chris Lattner32b4b5a2010-07-05 05:53:14 +0000746 if (!VT.is128BitVector())
David Greene9b9838d2009-06-29 16:47:10 +0000747 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +0000748
Owen Andersond6662ad2009-08-10 20:46:15 +0000749 setOperationAction(ISD::AND, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000750 AddPromotedToType (ISD::AND, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000751 setOperationAction(ISD::OR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000752 AddPromotedToType (ISD::OR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000753 setOperationAction(ISD::XOR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000754 AddPromotedToType (ISD::XOR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000755 setOperationAction(ISD::LOAD, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000756 AddPromotedToType (ISD::LOAD, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000757 setOperationAction(ISD::SELECT, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000758 AddPromotedToType (ISD::SELECT, SVT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000759 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000760
Owen Anderson825b72b2009-08-11 20:47:22 +0000761 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000762
Evan Cheng2c3ae372006-04-12 21:21:57 +0000763 // Custom lower v2i64 and v2f64 selects.
Owen Anderson825b72b2009-08-11 20:47:22 +0000764 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
765 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
766 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
767 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000768
Owen Anderson825b72b2009-08-11 20:47:22 +0000769 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
770 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000771 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000772
Nate Begeman14d12ca2008-02-11 04:19:36 +0000773 if (Subtarget->hasSSE41()) {
Dale Johannesen54feef22010-05-27 20:12:41 +0000774 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
775 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
776 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
777 setOperationAction(ISD::FRINT, MVT::f32, Legal);
778 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
779 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
780 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
781 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
782 setOperationAction(ISD::FRINT, MVT::f64, Legal);
783 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
784
Nate Begeman14d12ca2008-02-11 04:19:36 +0000785 // FIXME: Do we need to handle scalar-to-vector here?
Owen Anderson825b72b2009-08-11 20:47:22 +0000786 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000787
Nate Begemanbdcb5af2010-07-27 22:37:06 +0000788 // Can turn SHL into an integer multiply.
789 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
Nate Begeman51409212010-07-28 00:21:48 +0000790 setOperationAction(ISD::SHL, MVT::v16i8, Custom);
Nate Begemanbdcb5af2010-07-27 22:37:06 +0000791
Nate Begeman14d12ca2008-02-11 04:19:36 +0000792 // i8 and i16 vectors are custom , because the source register and source
793 // source memory operand types are not the same width. f32 vectors are
794 // custom since the immediate controlling the insert encodes additional
795 // information.
Owen Anderson825b72b2009-08-11 20:47:22 +0000796 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
797 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
798 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
799 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000800
Owen Anderson825b72b2009-08-11 20:47:22 +0000801 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
802 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
803 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
804 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000805
806 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000807 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
808 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000809 }
810 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000811
Nate Begeman30a0de92008-07-17 16:51:19 +0000812 if (Subtarget->hasSSE42()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000813 setOperationAction(ISD::VSETCC, MVT::v2i64, Custom);
Nate Begeman30a0de92008-07-17 16:51:19 +0000814 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000815
David Greene9b9838d2009-06-29 16:47:10 +0000816 if (!UseSoftFloat && Subtarget->hasAVX()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000817 addRegisterClass(MVT::v8f32, X86::VR256RegisterClass);
818 addRegisterClass(MVT::v4f64, X86::VR256RegisterClass);
819 addRegisterClass(MVT::v8i32, X86::VR256RegisterClass);
820 addRegisterClass(MVT::v4i64, X86::VR256RegisterClass);
Bruno Cardoso Lopes405f11b2010-08-10 01:43:16 +0000821 addRegisterClass(MVT::v32i8, X86::VR256RegisterClass);
David Greened94c1012009-06-29 22:50:51 +0000822
Owen Anderson825b72b2009-08-11 20:47:22 +0000823 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
824 setOperationAction(ISD::LOAD, MVT::v8i32, Legal);
825 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
826 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
827 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
828 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
829 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
830 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
831 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
832 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +0000833 setOperationAction(ISD::BUILD_VECTOR, MVT::v8f32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000834 //setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8f32, Custom);
835 //setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8f32, Custom);
836 //setOperationAction(ISD::SELECT, MVT::v8f32, Custom);
837 //setOperationAction(ISD::VSETCC, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000838
839 // Operations to consider commented out -v16i16 v32i8
Owen Anderson825b72b2009-08-11 20:47:22 +0000840 //setOperationAction(ISD::ADD, MVT::v16i16, Legal);
841 setOperationAction(ISD::ADD, MVT::v8i32, Custom);
842 setOperationAction(ISD::ADD, MVT::v4i64, Custom);
843 //setOperationAction(ISD::SUB, MVT::v32i8, Legal);
844 //setOperationAction(ISD::SUB, MVT::v16i16, Legal);
845 setOperationAction(ISD::SUB, MVT::v8i32, Custom);
846 setOperationAction(ISD::SUB, MVT::v4i64, Custom);
847 //setOperationAction(ISD::MUL, MVT::v16i16, Legal);
848 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
849 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
850 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
851 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
852 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
853 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000854
Owen Anderson825b72b2009-08-11 20:47:22 +0000855 setOperationAction(ISD::VSETCC, MVT::v4f64, Custom);
856 // setOperationAction(ISD::VSETCC, MVT::v32i8, Custom);
857 // setOperationAction(ISD::VSETCC, MVT::v16i16, Custom);
858 setOperationAction(ISD::VSETCC, MVT::v8i32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000859
Owen Anderson825b72b2009-08-11 20:47:22 +0000860 // setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v32i8, Custom);
861 // setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i16, Custom);
862 // setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i16, Custom);
863 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i32, Custom);
864 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000865
Owen Anderson825b72b2009-08-11 20:47:22 +0000866 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f64, Custom);
867 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i64, Custom);
868 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f64, Custom);
869 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i64, Custom);
870 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f64, Custom);
871 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000872
873#if 0
874 // Not sure we want to do this since there are no 256-bit integer
875 // operations in AVX
876
877 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
878 // This includes 256-bit vectors
Owen Anderson825b72b2009-08-11 20:47:22 +0000879 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v4i64; ++i) {
880 EVT VT = (MVT::SimpleValueType)i;
David Greene9b9838d2009-06-29 16:47:10 +0000881
882 // Do not attempt to custom lower non-power-of-2 vectors
883 if (!isPowerOf2_32(VT.getVectorNumElements()))
884 continue;
885
886 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
887 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
888 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
889 }
890
891 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000892 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i64, Custom);
893 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i64, Custom);
Eric Christopherfd179292009-08-27 18:07:15 +0000894 }
David Greene9b9838d2009-06-29 16:47:10 +0000895#endif
896
897#if 0
898 // Not sure we want to do this since there are no 256-bit integer
899 // operations in AVX
900
901 // Promote v32i8, v16i16, v8i32 load, select, and, or, xor to v4i64.
902 // Including 256-bit vectors
Owen Anderson825b72b2009-08-11 20:47:22 +0000903 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v4i64; i++) {
904 EVT VT = (MVT::SimpleValueType)i;
David Greene9b9838d2009-06-29 16:47:10 +0000905
906 if (!VT.is256BitVector()) {
907 continue;
908 }
909 setOperationAction(ISD::AND, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000910 AddPromotedToType (ISD::AND, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000911 setOperationAction(ISD::OR, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000912 AddPromotedToType (ISD::OR, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000913 setOperationAction(ISD::XOR, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000914 AddPromotedToType (ISD::XOR, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000915 setOperationAction(ISD::LOAD, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000916 AddPromotedToType (ISD::LOAD, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000917 setOperationAction(ISD::SELECT, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000918 AddPromotedToType (ISD::SELECT, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000919 }
920
Owen Anderson825b72b2009-08-11 20:47:22 +0000921 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
David Greene9b9838d2009-06-29 16:47:10 +0000922#endif
923 }
924
Evan Cheng6be2c582006-04-05 23:38:46 +0000925 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000926 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +0000927
Bill Wendling74c37652008-12-09 22:08:41 +0000928 // Add/Sub/Mul with overflow operations are custom lowered.
Owen Anderson825b72b2009-08-11 20:47:22 +0000929 setOperationAction(ISD::SADDO, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000930 setOperationAction(ISD::UADDO, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000931 setOperationAction(ISD::SSUBO, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000932 setOperationAction(ISD::USUBO, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000933 setOperationAction(ISD::SMULO, MVT::i32, Custom);
Dan Gohman71c62a22010-06-02 19:13:40 +0000934
Eli Friedman962f5492010-06-02 19:35:46 +0000935 // Only custom-lower 64-bit SADDO and friends on 64-bit because we don't
936 // handle type legalization for these operations here.
Dan Gohman71c62a22010-06-02 19:13:40 +0000937 //
Eli Friedman962f5492010-06-02 19:35:46 +0000938 // FIXME: We really should do custom legalization for addition and
939 // subtraction on x86-32 once PR3203 is fixed. We really can't do much better
940 // than generic legalization for 64-bit multiplication-with-overflow, though.
Eli Friedmana993f0a2010-06-02 00:27:18 +0000941 if (Subtarget->is64Bit()) {
942 setOperationAction(ISD::SADDO, MVT::i64, Custom);
943 setOperationAction(ISD::UADDO, MVT::i64, Custom);
944 setOperationAction(ISD::SSUBO, MVT::i64, Custom);
945 setOperationAction(ISD::USUBO, MVT::i64, Custom);
946 setOperationAction(ISD::SMULO, MVT::i64, Custom);
947 }
Bill Wendling41ea7e72008-11-24 19:21:46 +0000948
Evan Chengd54f2d52009-03-31 19:38:51 +0000949 if (!Subtarget->is64Bit()) {
950 // These libcalls are not available in 32-bit.
951 setLibcallName(RTLIB::SHL_I128, 0);
952 setLibcallName(RTLIB::SRL_I128, 0);
953 setLibcallName(RTLIB::SRA_I128, 0);
954 }
955
Evan Cheng206ee9d2006-07-07 08:33:52 +0000956 // We have target-specific dag combine patterns for the following nodes:
957 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Dan Gohman1bbf72b2010-03-15 23:23:03 +0000958 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Evan Chengd880b972008-05-09 21:53:03 +0000959 setTargetDAGCombine(ISD::BUILD_VECTOR);
Chris Lattner83e6c992006-10-04 06:57:07 +0000960 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +0000961 setTargetDAGCombine(ISD::SHL);
962 setTargetDAGCombine(ISD::SRA);
963 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +0000964 setTargetDAGCombine(ISD::OR);
Chris Lattner149a4e52008-02-22 02:09:43 +0000965 setTargetDAGCombine(ISD::STORE);
Evan Cheng2e489c42009-12-16 00:53:11 +0000966 setTargetDAGCombine(ISD::ZERO_EXTEND);
Evan Cheng0b0cd912009-03-28 05:57:29 +0000967 if (Subtarget->is64Bit())
968 setTargetDAGCombine(ISD::MUL);
Evan Cheng206ee9d2006-07-07 08:33:52 +0000969
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000970 computeRegisterProperties();
971
Evan Cheng87ed7162006-02-14 08:25:08 +0000972 // FIXME: These should be based on subtarget info. Plus, the values should
973 // be smaller when we are in optimizing for size mode.
Dan Gohman87060f52008-06-30 21:00:56 +0000974 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
Evan Cheng255f20f2010-04-01 06:04:33 +0000975 maxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
Dan Gohman87060f52008-06-30 21:00:56 +0000976 maxStoresPerMemmove = 3; // For @llvm.memmove -> sequence of stores
Evan Chengfb8075d2008-02-28 00:43:03 +0000977 setPrefLoopAlignment(16);
Evan Cheng6ebf7bc2009-05-13 21:42:09 +0000978 benefitFromCodePlacementOpt = true;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000979}
980
Scott Michel5b8f82e2008-03-10 15:42:14 +0000981
Owen Anderson825b72b2009-08-11 20:47:22 +0000982MVT::SimpleValueType X86TargetLowering::getSetCCResultType(EVT VT) const {
983 return MVT::i8;
Scott Michel5b8f82e2008-03-10 15:42:14 +0000984}
985
986
Evan Cheng29286502008-01-23 23:17:41 +0000987/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
988/// the desired ByVal argument alignment.
989static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
990 if (MaxAlign == 16)
991 return;
992 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
993 if (VTy->getBitWidth() == 128)
994 MaxAlign = 16;
Evan Cheng29286502008-01-23 23:17:41 +0000995 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
996 unsigned EltAlign = 0;
997 getMaxByValAlign(ATy->getElementType(), EltAlign);
998 if (EltAlign > MaxAlign)
999 MaxAlign = EltAlign;
1000 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
1001 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1002 unsigned EltAlign = 0;
1003 getMaxByValAlign(STy->getElementType(i), EltAlign);
1004 if (EltAlign > MaxAlign)
1005 MaxAlign = EltAlign;
1006 if (MaxAlign == 16)
1007 break;
1008 }
1009 }
1010 return;
1011}
1012
1013/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1014/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001015/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1016/// are at 4-byte boundaries.
Evan Cheng29286502008-01-23 23:17:41 +00001017unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001018 if (Subtarget->is64Bit()) {
1019 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001020 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001021 if (TyAlign > 8)
1022 return TyAlign;
1023 return 8;
1024 }
1025
Evan Cheng29286502008-01-23 23:17:41 +00001026 unsigned Align = 4;
Dale Johannesen0c191872008-02-08 19:48:20 +00001027 if (Subtarget->hasSSE1())
1028 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001029 return Align;
1030}
Chris Lattner2b02a442007-02-25 08:29:00 +00001031
Evan Chengf0df0312008-05-15 08:39:06 +00001032/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengc3b0c342010-04-08 07:37:57 +00001033/// and store operations as a result of memset, memcpy, and memmove
1034/// lowering. If DstAlign is zero that means it's safe to destination
1035/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1036/// means there isn't a need to check it against alignment requirement,
1037/// probably because the source does not need to be loaded. If
1038/// 'NonScalarIntSafe' is true, that means it's safe to return a
1039/// non-scalar-integer type, e.g. empty string source, constant, or loaded
1040/// from memory. 'MemcpyStrSrc' indicates whether the memcpy source is
1041/// constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001042/// It returns EVT::Other if the type should be determined using generic
1043/// target-independent logic.
Owen Andersone50ed302009-08-10 22:56:29 +00001044EVT
Evan Cheng255f20f2010-04-01 06:04:33 +00001045X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1046 unsigned DstAlign, unsigned SrcAlign,
Evan Chengf28f8bc2010-04-02 19:36:14 +00001047 bool NonScalarIntSafe,
Evan Chengc3b0c342010-04-08 07:37:57 +00001048 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00001049 MachineFunction &MF) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001050 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
1051 // linux. This is because the stack realignment code can't handle certain
1052 // cases like PR2962. This should be removed when PR2962 is fixed.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001053 const Function *F = MF.getFunction();
Evan Chengf28f8bc2010-04-02 19:36:14 +00001054 if (NonScalarIntSafe &&
1055 !F->hasFnAttr(Attribute::NoImplicitFloat)) {
Evan Cheng255f20f2010-04-01 06:04:33 +00001056 if (Size >= 16 &&
1057 (Subtarget->isUnalignedMemAccessFast() ||
Chandler Carruthae1d41c2010-04-02 01:31:24 +00001058 ((DstAlign == 0 || DstAlign >= 16) &&
1059 (SrcAlign == 0 || SrcAlign >= 16))) &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001060 Subtarget->getStackAlignment() >= 16) {
1061 if (Subtarget->hasSSE2())
1062 return MVT::v4i32;
Evan Chengf28f8bc2010-04-02 19:36:14 +00001063 if (Subtarget->hasSSE1())
Evan Cheng255f20f2010-04-01 06:04:33 +00001064 return MVT::v4f32;
Evan Chengc3b0c342010-04-08 07:37:57 +00001065 } else if (!MemcpyStrSrc && Size >= 8 &&
Evan Cheng3ea97552010-04-01 20:27:45 +00001066 !Subtarget->is64Bit() &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001067 Subtarget->getStackAlignment() >= 8 &&
Evan Chengc3b0c342010-04-08 07:37:57 +00001068 Subtarget->hasSSE2()) {
1069 // Do not use f64 to lower memcpy if source is string constant. It's
1070 // better to use i32 to avoid the loads.
Evan Cheng255f20f2010-04-01 06:04:33 +00001071 return MVT::f64;
Evan Chengc3b0c342010-04-08 07:37:57 +00001072 }
Chris Lattner4002a1b2008-10-28 05:49:35 +00001073 }
Evan Chengf0df0312008-05-15 08:39:06 +00001074 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001075 return MVT::i64;
1076 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001077}
1078
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001079/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1080/// current function. The returned value is a member of the
1081/// MachineJumpTableInfo::JTEntryKind enum.
1082unsigned X86TargetLowering::getJumpTableEncoding() const {
1083 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1084 // symbol.
1085 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1086 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001087 return MachineJumpTableInfo::EK_Custom32;
Michael J. Spencerec38de22010-10-10 22:04:20 +00001088
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001089 // Otherwise, use the normal jump table encoding heuristics.
1090 return TargetLowering::getJumpTableEncoding();
1091}
1092
Chris Lattner589c6f62010-01-26 06:28:43 +00001093/// getPICBaseSymbol - Return the X86-32 PIC base.
1094MCSymbol *
1095X86TargetLowering::getPICBaseSymbol(const MachineFunction *MF,
1096 MCContext &Ctx) const {
1097 const MCAsmInfo &MAI = *getTargetMachine().getMCAsmInfo();
Chris Lattner9b97a732010-03-30 18:10:53 +00001098 return Ctx.GetOrCreateSymbol(Twine(MAI.getPrivateGlobalPrefix())+
1099 Twine(MF->getFunctionNumber())+"$pb");
Chris Lattner589c6f62010-01-26 06:28:43 +00001100}
1101
1102
Chris Lattnerc64daab2010-01-26 05:02:42 +00001103const MCExpr *
1104X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1105 const MachineBasicBlock *MBB,
1106 unsigned uid,MCContext &Ctx) const{
1107 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1108 Subtarget->isPICStyleGOT());
1109 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1110 // entries.
Daniel Dunbar4e815f82010-03-15 23:51:06 +00001111 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1112 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001113}
1114
Evan Chengcc415862007-11-09 01:32:10 +00001115/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1116/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001117SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001118 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001119 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001120 // This doesn't have DebugLoc associated with it, but is not really the
1121 // same as a Register.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001122 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001123 return Table;
1124}
1125
Chris Lattner589c6f62010-01-26 06:28:43 +00001126/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1127/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1128/// MCExpr.
1129const MCExpr *X86TargetLowering::
1130getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1131 MCContext &Ctx) const {
1132 // X86-64 uses RIP relative addressing based on the jump table label.
1133 if (Subtarget->isPICStyleRIPRel())
1134 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1135
1136 // Otherwise, the reference is relative to the PIC base.
1137 return MCSymbolRefExpr::Create(getPICBaseSymbol(MF, Ctx), Ctx);
1138}
1139
Bill Wendlingb4202b82009-07-01 18:50:55 +00001140/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +00001141unsigned X86TargetLowering::getFunctionAlignment(const Function *F) const {
Dan Gohman25103a22009-08-18 00:20:06 +00001142 return F->hasFnAttr(Attribute::OptimizeForSize) ? 0 : 4;
Bill Wendling20c568f2009-06-30 22:38:32 +00001143}
1144
Evan Chengdee81012010-07-26 21:50:05 +00001145std::pair<const TargetRegisterClass*, uint8_t>
1146X86TargetLowering::findRepresentativeClass(EVT VT) const{
1147 const TargetRegisterClass *RRC = 0;
1148 uint8_t Cost = 1;
1149 switch (VT.getSimpleVT().SimpleTy) {
1150 default:
1151 return TargetLowering::findRepresentativeClass(VT);
1152 case MVT::i8: case MVT::i16: case MVT::i32: case MVT::i64:
1153 RRC = (Subtarget->is64Bit()
1154 ? X86::GR64RegisterClass : X86::GR32RegisterClass);
1155 break;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001156 case MVT::x86mmx:
Evan Chengdee81012010-07-26 21:50:05 +00001157 RRC = X86::VR64RegisterClass;
1158 break;
1159 case MVT::f32: case MVT::f64:
1160 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1161 case MVT::v4f32: case MVT::v2f64:
1162 case MVT::v32i8: case MVT::v8i32: case MVT::v4i64: case MVT::v8f32:
1163 case MVT::v4f64:
1164 RRC = X86::VR128RegisterClass;
1165 break;
1166 }
1167 return std::make_pair(RRC, Cost);
1168}
1169
Evan Cheng70017e42010-07-24 00:39:05 +00001170unsigned
1171X86TargetLowering::getRegPressureLimit(const TargetRegisterClass *RC,
1172 MachineFunction &MF) const {
1173 unsigned FPDiff = RegInfo->hasFP(MF) ? 1 : 0;
1174 switch (RC->getID()) {
1175 default:
1176 return 0;
1177 case X86::GR32RegClassID:
1178 return 4 - FPDiff;
1179 case X86::GR64RegClassID:
1180 return 8 - FPDiff;
1181 case X86::VR128RegClassID:
1182 return Subtarget->is64Bit() ? 10 : 4;
1183 case X86::VR64RegClassID:
1184 return 4;
1185 }
1186}
1187
Eric Christopherf7a0c7b2010-07-06 05:18:56 +00001188bool X86TargetLowering::getStackCookieLocation(unsigned &AddressSpace,
1189 unsigned &Offset) const {
1190 if (!Subtarget->isTargetLinux())
1191 return false;
1192
1193 if (Subtarget->is64Bit()) {
1194 // %fs:0x28, unless we're using a Kernel code model, in which case it's %gs:
1195 Offset = 0x28;
1196 if (getTargetMachine().getCodeModel() == CodeModel::Kernel)
1197 AddressSpace = 256;
1198 else
1199 AddressSpace = 257;
1200 } else {
1201 // %gs:0x14 on i386
1202 Offset = 0x14;
1203 AddressSpace = 256;
1204 }
1205 return true;
1206}
1207
1208
Chris Lattner2b02a442007-02-25 08:29:00 +00001209//===----------------------------------------------------------------------===//
1210// Return Value Calling Convention Implementation
1211//===----------------------------------------------------------------------===//
1212
Chris Lattner59ed56b2007-02-28 04:55:35 +00001213#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001214
Michael J. Spencerec38de22010-10-10 22:04:20 +00001215bool
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001216X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv, bool isVarArg,
Dan Gohman84023e02010-07-10 09:00:22 +00001217 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001218 LLVMContext &Context) const {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001219 SmallVector<CCValAssign, 16> RVLocs;
1220 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001221 RVLocs, Context);
Dan Gohman84023e02010-07-10 09:00:22 +00001222 return CCInfo.CheckReturn(Outs, RetCC_X86);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001223}
1224
Dan Gohman98ca4f22009-08-05 01:29:28 +00001225SDValue
1226X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001227 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001228 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001229 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001230 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001231 MachineFunction &MF = DAG.getMachineFunction();
1232 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001233
Chris Lattner9774c912007-02-27 05:28:59 +00001234 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001235 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1236 RVLocs, *DAG.getContext());
1237 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001238
Evan Chengdcea1632010-02-04 02:40:39 +00001239 // Add the regs to the liveout set for the function.
1240 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1241 for (unsigned i = 0; i != RVLocs.size(); ++i)
1242 if (RVLocs[i].isRegLoc() && !MRI.isLiveOut(RVLocs[i].getLocReg()))
1243 MRI.addLiveOut(RVLocs[i].getLocReg());
Scott Michelfdc40a02009-02-17 22:15:04 +00001244
Dan Gohman475871a2008-07-27 21:46:04 +00001245 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001246
Dan Gohman475871a2008-07-27 21:46:04 +00001247 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001248 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1249 // Operand #1 = Bytes To Pop
Dan Gohman1e93df62010-04-17 14:41:14 +00001250 RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1251 MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001252
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001253 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001254 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1255 CCValAssign &VA = RVLocs[i];
1256 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohmanc9403652010-07-07 15:54:55 +00001257 SDValue ValToCopy = OutVals[i];
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001258 EVT ValVT = ValToCopy.getValueType();
1259
Dale Johannesenc4510512010-09-24 19:05:48 +00001260 // If this is x86-64, and we disabled SSE, we can't return FP values,
1261 // or SSE or MMX vectors.
1262 if ((ValVT == MVT::f32 || ValVT == MVT::f64 ||
1263 VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) &&
1264 (Subtarget->is64Bit() && !Subtarget->hasSSE1())) {
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001265 report_fatal_error("SSE register return with SSE disabled");
1266 }
1267 // Likewise we can't return F64 values with SSE1 only. gcc does so, but
1268 // llvm-gcc has never done it right and no one has noticed, so this
1269 // should be OK for now.
1270 if (ValVT == MVT::f64 &&
Chris Lattner83069682010-08-26 05:51:22 +00001271 (Subtarget->is64Bit() && !Subtarget->hasSSE2()))
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001272 report_fatal_error("SSE2 register return with SSE2 disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001273
Chris Lattner447ff682008-03-11 03:23:40 +00001274 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1275 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001276 if (VA.getLocReg() == X86::ST0 ||
1277 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001278 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1279 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001280 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001281 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001282 RetOps.push_back(ValToCopy);
1283 // Don't emit a copytoreg.
1284 continue;
1285 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001286
Evan Cheng242b38b2009-02-23 09:03:22 +00001287 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1288 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001289 if (Subtarget->is64Bit()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001290 if (ValVT == MVT::x86mmx) {
Chris Lattner97a2a562010-08-26 05:24:29 +00001291 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001292 ValToCopy = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, ValToCopy);
Eric Christopher90eb4022010-07-22 00:26:08 +00001293 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
1294 ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001295 // If we don't have SSE2 available, convert to v4f32 so the generated
1296 // register is legal.
1297 if (!Subtarget->hasSSE2())
1298 ValToCopy = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4f32,ValToCopy);
1299 }
Evan Cheng242b38b2009-02-23 09:03:22 +00001300 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001301 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00001302
Dale Johannesendd64c412009-02-04 00:33:20 +00001303 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001304 Flag = Chain.getValue(1);
1305 }
Dan Gohman61a92132008-04-21 23:59:07 +00001306
1307 // The x86-64 ABI for returning structs by value requires that we copy
1308 // the sret argument into %rax for the return. We saved the argument into
1309 // a virtual register in the entry block, so now we copy the value out
1310 // and into %rax.
1311 if (Subtarget->is64Bit() &&
1312 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1313 MachineFunction &MF = DAG.getMachineFunction();
1314 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1315 unsigned Reg = FuncInfo->getSRetReturnReg();
Michael J. Spencerec38de22010-10-10 22:04:20 +00001316 assert(Reg &&
Zhongxing Xuc2798a12010-05-26 08:10:02 +00001317 "SRetReturnReg should have been set in LowerFormalArguments().");
Dale Johannesendd64c412009-02-04 00:33:20 +00001318 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001319
Dale Johannesendd64c412009-02-04 00:33:20 +00001320 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001321 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001322
1323 // RAX now acts like a return value.
Evan Chengdcea1632010-02-04 02:40:39 +00001324 MRI.addLiveOut(X86::RAX);
Dan Gohman61a92132008-04-21 23:59:07 +00001325 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001326
Chris Lattner447ff682008-03-11 03:23:40 +00001327 RetOps[0] = Chain; // Update chain.
1328
1329 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001330 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001331 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001332
1333 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001334 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001335}
1336
Dan Gohman98ca4f22009-08-05 01:29:28 +00001337/// LowerCallResult - Lower the result values of a call into the
1338/// appropriate copies out of appropriate physical registers.
1339///
1340SDValue
1341X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001342 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001343 const SmallVectorImpl<ISD::InputArg> &Ins,
1344 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001345 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001346
Chris Lattnere32bbf62007-02-28 07:09:55 +00001347 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001348 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001349 bool Is64Bit = Subtarget->is64Bit();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001350 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +00001351 RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001352 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001353
Chris Lattner3085e152007-02-25 08:59:22 +00001354 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001355 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001356 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001357 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001358
Torok Edwin3f142c32009-02-01 18:15:56 +00001359 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001360 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Dan Gohman98ca4f22009-08-05 01:29:28 +00001361 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasSSE1())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001362 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001363 }
1364
Evan Cheng79fb3b42009-02-20 20:43:02 +00001365 SDValue Val;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001366
1367 // If this is a call to a function that returns an fp value on the floating
1368 // point stack, we must guarantee the the value is popped from the stack, so
1369 // a CopyFromReg is not good enough - the copy instruction may be eliminated
1370 // if the return value is not used. We use the FpGET_ST0 instructions
1371 // instead.
1372 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1) {
1373 // If we prefer to use the value in xmm registers, copy it out as f80 and
1374 // use a truncate to move it from fp stack reg to xmm reg.
1375 if (isScalarFPTypeInSSEReg(VA.getValVT())) CopyVT = MVT::f80;
1376 bool isST0 = VA.getLocReg() == X86::ST0;
1377 unsigned Opc = 0;
1378 if (CopyVT == MVT::f32) Opc = isST0 ? X86::FpGET_ST0_32:X86::FpGET_ST1_32;
1379 if (CopyVT == MVT::f64) Opc = isST0 ? X86::FpGET_ST0_64:X86::FpGET_ST1_64;
1380 if (CopyVT == MVT::f80) Opc = isST0 ? X86::FpGET_ST0_80:X86::FpGET_ST1_80;
1381 SDValue Ops[] = { Chain, InFlag };
1382 Chain = SDValue(DAG.getMachineNode(Opc, dl, CopyVT, MVT::Other, MVT::Flag,
1383 Ops, 2), 1);
1384 Val = Chain.getValue(0);
1385
1386 // Round the f80 to the right size, which also moves it to the appropriate
1387 // xmm register.
1388 if (CopyVT != VA.getValVT())
1389 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
1390 // This truncation won't change the value.
1391 DAG.getIntPtrConstant(1));
1392 } else if (Is64Bit && CopyVT.isVector() && CopyVT.getSizeInBits() == 64) {
Evan Cheng242b38b2009-02-23 09:03:22 +00001393 // For x86-64, MMX values are returned in XMM0 / XMM1 except for v1i64.
1394 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
1395 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001396 MVT::v2i64, InFlag).getValue(1);
Evan Cheng242b38b2009-02-23 09:03:22 +00001397 Val = Chain.getValue(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00001398 Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64,
1399 Val, DAG.getConstant(0, MVT::i64));
Evan Cheng242b38b2009-02-23 09:03:22 +00001400 } else {
1401 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001402 MVT::i64, InFlag).getValue(1);
Evan Cheng242b38b2009-02-23 09:03:22 +00001403 Val = Chain.getValue(0);
1404 }
Evan Cheng79fb3b42009-02-20 20:43:02 +00001405 Val = DAG.getNode(ISD::BIT_CONVERT, dl, CopyVT, Val);
1406 } else {
1407 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1408 CopyVT, InFlag).getValue(1);
1409 Val = Chain.getValue(0);
1410 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001411 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001412 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001413 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001414
Dan Gohman98ca4f22009-08-05 01:29:28 +00001415 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001416}
1417
1418
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001419//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001420// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001421//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001422// StdCall calling convention seems to be standard for many Windows' API
1423// routines and around. It differs from C calling convention just a little:
1424// callee should clean up the stack, not caller. Symbols should be also
1425// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001426// For info on fast calling convention see Fast Calling Convention (tail call)
1427// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001428
Dan Gohman98ca4f22009-08-05 01:29:28 +00001429/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001430/// semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001431static bool CallIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
1432 if (Outs.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001433 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001434
Dan Gohman98ca4f22009-08-05 01:29:28 +00001435 return Outs[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001436}
1437
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001438/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001439/// return semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001440static bool
1441ArgsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
1442 if (Ins.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001443 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001444
Dan Gohman98ca4f22009-08-05 01:29:28 +00001445 return Ins[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001446}
1447
Dan Gohman095cc292008-09-13 01:54:27 +00001448/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1449/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001450CCAssignFn *X86TargetLowering::CCAssignFnForNode(CallingConv::ID CC) const {
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001451 if (Subtarget->is64Bit()) {
Chris Lattner29689432010-03-11 00:22:57 +00001452 if (CC == CallingConv::GHC)
1453 return CC_X86_64_GHC;
1454 else if (Subtarget->isTargetWin64())
Anton Korobeynikov8f88cb02008-03-22 20:37:30 +00001455 return CC_X86_Win64_C;
Evan Chenge9ac9e62008-09-07 09:07:23 +00001456 else
1457 return CC_X86_64_C;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001458 }
1459
Gordon Henriksen86737662008-01-05 16:56:59 +00001460 if (CC == CallingConv::X86_FastCall)
1461 return CC_X86_32_FastCall;
Anton Korobeynikovded05e32010-05-16 09:08:45 +00001462 else if (CC == CallingConv::X86_ThisCall)
1463 return CC_X86_32_ThisCall;
Evan Chengb188dd92008-09-10 18:25:29 +00001464 else if (CC == CallingConv::Fast)
1465 return CC_X86_32_FastCC;
Chris Lattner29689432010-03-11 00:22:57 +00001466 else if (CC == CallingConv::GHC)
1467 return CC_X86_32_GHC;
Gordon Henriksen86737662008-01-05 16:56:59 +00001468 else
1469 return CC_X86_32_C;
1470}
1471
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001472/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1473/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001474/// the specific parameter attribute. The copy will be passed as a byval
1475/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001476static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001477CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001478 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1479 DebugLoc dl) {
Chris Lattnere72f2022010-09-21 05:40:29 +00001480 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Michael J. Spencerec38de22010-10-10 22:04:20 +00001481
Dale Johannesendd64c412009-02-04 00:33:20 +00001482 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Mon P Wang20adc9d2010-04-04 03:10:48 +00001483 /*isVolatile*/false, /*AlwaysInline=*/true,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001484 MachinePointerInfo(), MachinePointerInfo());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001485}
1486
Chris Lattner29689432010-03-11 00:22:57 +00001487/// IsTailCallConvention - Return true if the calling convention is one that
1488/// supports tail call optimization.
1489static bool IsTailCallConvention(CallingConv::ID CC) {
1490 return (CC == CallingConv::Fast || CC == CallingConv::GHC);
1491}
1492
Evan Cheng0c439eb2010-01-27 00:07:07 +00001493/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1494/// a tailcall target by changing its ABI.
1495static bool FuncIsMadeTailCallSafe(CallingConv::ID CC) {
Chris Lattner29689432010-03-11 00:22:57 +00001496 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001497}
1498
Dan Gohman98ca4f22009-08-05 01:29:28 +00001499SDValue
1500X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001501 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001502 const SmallVectorImpl<ISD::InputArg> &Ins,
1503 DebugLoc dl, SelectionDAG &DAG,
1504 const CCValAssign &VA,
1505 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00001506 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00001507 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001508 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Evan Cheng0c439eb2010-01-27 00:07:07 +00001509 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001510 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001511 EVT ValVT;
1512
1513 // If value is passed by pointer we have address passed instead of the value
1514 // itself.
1515 if (VA.getLocInfo() == CCValAssign::Indirect)
1516 ValVT = VA.getLocVT();
1517 else
1518 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001519
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001520 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001521 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001522 // In case of tail call optimization mark all arguments mutable. Since they
1523 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001524 if (Flags.isByVal()) {
1525 int FI = MFI->CreateFixedObject(Flags.getByValSize(),
Evan Chenged2ae132010-07-03 00:40:23 +00001526 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001527 return DAG.getFrameIndex(FI, getPointerTy());
1528 } else {
1529 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00001530 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001531 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1532 return DAG.getLoad(ValVT, dl, Chain, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001533 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00001534 false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001535 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001536}
1537
Dan Gohman475871a2008-07-27 21:46:04 +00001538SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001539X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001540 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001541 bool isVarArg,
1542 const SmallVectorImpl<ISD::InputArg> &Ins,
1543 DebugLoc dl,
1544 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001545 SmallVectorImpl<SDValue> &InVals)
1546 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00001547 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001548 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001549
Gordon Henriksen86737662008-01-05 16:56:59 +00001550 const Function* Fn = MF.getFunction();
1551 if (Fn->hasExternalLinkage() &&
1552 Subtarget->isTargetCygMing() &&
1553 Fn->getName() == "main")
1554 FuncInfo->setForceFramePointer(true);
1555
Evan Cheng1bc78042006-04-26 01:20:17 +00001556 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001557 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001558 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001559
Chris Lattner29689432010-03-11 00:22:57 +00001560 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1561 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001562
Chris Lattner638402b2007-02-28 07:00:42 +00001563 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001564 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001565 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1566 ArgLocs, *DAG.getContext());
1567 CCInfo.AnalyzeFormalArguments(Ins, CCAssignFnForNode(CallConv));
Scott Michelfdc40a02009-02-17 22:15:04 +00001568
Chris Lattnerf39f7712007-02-28 05:46:49 +00001569 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001570 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001571 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1572 CCValAssign &VA = ArgLocs[i];
1573 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1574 // places.
1575 assert(VA.getValNo() != LastVal &&
1576 "Don't support value assigned to multiple locs yet");
1577 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001578
Chris Lattnerf39f7712007-02-28 05:46:49 +00001579 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001580 EVT RegVT = VA.getLocVT();
Devang Patel8a84e442009-01-05 17:31:22 +00001581 TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001582 if (RegVT == MVT::i32)
Chris Lattnerf39f7712007-02-28 05:46:49 +00001583 RC = X86::GR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001584 else if (Is64Bit && RegVT == MVT::i64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001585 RC = X86::GR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001586 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001587 RC = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001588 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001589 RC = X86::FR64RegisterClass;
Bruno Cardoso Lopesac098352010-08-05 23:35:51 +00001590 else if (RegVT.isVector() && RegVT.getSizeInBits() == 256)
1591 RC = X86::VR256RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001592 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001593 RC = X86::VR128RegisterClass;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001594 else if (RegVT == MVT::x86mmx)
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001595 RC = X86::VR64RegisterClass;
1596 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001597 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001598
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001599 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001600 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001601
Chris Lattnerf39f7712007-02-28 05:46:49 +00001602 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1603 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1604 // right size.
1605 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001606 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001607 DAG.getValueType(VA.getValVT()));
1608 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001609 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001610 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001611 else if (VA.getLocInfo() == CCValAssign::BCvt)
Anton Korobeynikov6dde14b2009-08-03 08:14:14 +00001612 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001613
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001614 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001615 // Handle MMX values passed in XMM regs.
1616 if (RegVT.isVector()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001617 ArgValue = DAG.getNode(X86ISD::MOVDQ2Q, dl, VA.getValVT(),
1618 ArgValue);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001619 } else
1620 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001621 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001622 } else {
1623 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001624 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001625 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001626
1627 // If value is passed via pointer - do a load.
1628 if (VA.getLocInfo() == CCValAssign::Indirect)
Chris Lattner51abfe42010-09-21 06:02:19 +00001629 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue,
1630 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001631
Dan Gohman98ca4f22009-08-05 01:29:28 +00001632 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001633 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001634
Dan Gohman61a92132008-04-21 23:59:07 +00001635 // The x86-64 ABI for returning structs by value requires that we copy
1636 // the sret argument into %rax for the return. Save the argument into
1637 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001638 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001639 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1640 unsigned Reg = FuncInfo->getSRetReturnReg();
1641 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001642 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001643 FuncInfo->setSRetReturnReg(Reg);
1644 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001645 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001646 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001647 }
1648
Chris Lattnerf39f7712007-02-28 05:46:49 +00001649 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00001650 // Align stack specially for tail calls.
1651 if (FuncIsMadeTailCallSafe(CallConv))
Gordon Henriksenae636f82008-01-03 16:47:34 +00001652 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001653
Evan Cheng1bc78042006-04-26 01:20:17 +00001654 // If the function takes variable number of arguments, make a frame index for
1655 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001656 if (isVarArg) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001657 if (!IsWin64 && (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
1658 CallConv != CallingConv::X86_ThisCall))) {
Jakob Stoklund Olesenb2eeed72010-07-29 17:42:27 +00001659 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,true));
Gordon Henriksen86737662008-01-05 16:56:59 +00001660 }
1661 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001662 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1663
1664 // FIXME: We should really autogenerate these arrays
1665 static const unsigned GPR64ArgRegsWin64[] = {
1666 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001667 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001668 static const unsigned GPR64ArgRegs64Bit[] = {
1669 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1670 };
1671 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001672 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1673 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1674 };
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001675 const unsigned *GPR64ArgRegs;
1676 unsigned NumXMMRegs = 0;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001677
1678 if (IsWin64) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001679 // The XMM registers which might contain var arg parameters are shadowed
1680 // in their paired GPR. So we only need to save the GPR to their home
1681 // slots.
1682 TotalNumIntRegs = 4;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001683 GPR64ArgRegs = GPR64ArgRegsWin64;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001684 } else {
1685 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1686 GPR64ArgRegs = GPR64ArgRegs64Bit;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001687
1688 NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs64Bit, TotalNumXMMRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001689 }
1690 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1691 TotalNumIntRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001692
Devang Patel578efa92009-06-05 21:57:13 +00001693 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Evan Chengc7ce29b2009-02-13 22:36:38 +00001694 assert(!(NumXMMRegs && !Subtarget->hasSSE1()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001695 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001696 assert(!(NumXMMRegs && UseSoftFloat && NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00001697 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001698 if (UseSoftFloat || NoImplicitFloatOps || !Subtarget->hasSSE1())
Torok Edwin3f142c32009-02-01 18:15:56 +00001699 // Kernel mode asks for SSE to be disabled, so don't push them
1700 // on the stack.
1701 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001702
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001703 if (IsWin64) {
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001704 const TargetFrameInfo &TFI = *getTargetMachine().getFrameInfo();
1705 // Get to the caller-allocated home save location. Add 8 to account
1706 // for the return address.
1707 int HomeOffset = TFI.getOffsetOfLocalArea() + 8;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001708 FuncInfo->setRegSaveFrameIndex(
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001709 MFI->CreateFixedObject(1, NumIntRegs * 8 + HomeOffset, false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001710 FuncInfo->setVarArgsFrameIndex(FuncInfo->getRegSaveFrameIndex());
1711 } else {
1712 // For X86-64, if there are vararg parameters that are passed via
1713 // registers, then we must store them to their spots on the stack so they
1714 // may be loaded by deferencing the result of va_next.
1715 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
1716 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
1717 FuncInfo->setRegSaveFrameIndex(
1718 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
Dan Gohman1e93df62010-04-17 14:41:14 +00001719 false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001720 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001721
Gordon Henriksen86737662008-01-05 16:56:59 +00001722 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001723 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00001724 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
1725 getPointerTy());
1726 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001727 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00001728 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
1729 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00001730 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
1731 X86::GR64RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00001732 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00001733 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001734 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001735 MachinePointerInfo::getFixedStack(
1736 FuncInfo->getRegSaveFrameIndex(), Offset),
1737 false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001738 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001739 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001740 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001741
Dan Gohmanface41a2009-08-16 21:24:25 +00001742 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
1743 // Now store the XMM (fp + vector) parameter registers.
1744 SmallVector<SDValue, 11> SaveXMMOps;
1745 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001746
Dan Gohmanface41a2009-08-16 21:24:25 +00001747 unsigned AL = MF.addLiveIn(X86::AL, X86::GR8RegisterClass);
1748 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
1749 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001750
Dan Gohman1e93df62010-04-17 14:41:14 +00001751 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1752 FuncInfo->getRegSaveFrameIndex()));
1753 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1754 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00001755
Dan Gohmanface41a2009-08-16 21:24:25 +00001756 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001757 unsigned VReg = MF.addLiveIn(XMMArgRegs64Bit[NumXMMRegs],
Dan Gohmanface41a2009-08-16 21:24:25 +00001758 X86::VR128RegisterClass);
1759 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
1760 SaveXMMOps.push_back(Val);
1761 }
1762 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
1763 MVT::Other,
1764 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00001765 }
Dan Gohmanface41a2009-08-16 21:24:25 +00001766
1767 if (!MemOps.empty())
1768 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1769 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001770 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001771 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001772
Gordon Henriksen86737662008-01-05 16:56:59 +00001773 // Some CCs need callee pop.
Dan Gohman4d3d6e12010-05-27 18:43:40 +00001774 if (Subtarget->IsCalleePop(isVarArg, CallConv)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001775 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001776 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00001777 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001778 // If this is an sret function, the return should pop the hidden pointer.
Chris Lattner29689432010-03-11 00:22:57 +00001779 if (!Is64Bit && !IsTailCallConvention(CallConv) && ArgsAreStructReturn(Ins))
Dan Gohman1e93df62010-04-17 14:41:14 +00001780 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001781 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001782
Gordon Henriksen86737662008-01-05 16:56:59 +00001783 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001784 // RegSaveFrameIndex is X86-64 only.
1785 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Anton Korobeynikovded05e32010-05-16 09:08:45 +00001786 if (CallConv == CallingConv::X86_FastCall ||
1787 CallConv == CallingConv::X86_ThisCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00001788 // fastcc functions can't have varargs.
1789 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00001790 }
Evan Cheng25caf632006-05-23 21:06:34 +00001791
Dan Gohman98ca4f22009-08-05 01:29:28 +00001792 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001793}
1794
Dan Gohman475871a2008-07-27 21:46:04 +00001795SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001796X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
1797 SDValue StackPtr, SDValue Arg,
1798 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00001799 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001800 ISD::ArgFlagsTy Flags) const {
Anton Korobeynikovc7c62bb2010-09-02 22:31:32 +00001801 const unsigned FirstStackArgOffset = (Subtarget->isTargetWin64() ? 32 : 0);
1802 unsigned LocMemOffset = FirstStackArgOffset + VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00001803 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00001804 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001805 if (Flags.isByVal())
Dale Johannesendd64c412009-02-04 00:33:20 +00001806 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001807
1808 return DAG.getStore(Chain, dl, Arg, PtrOff,
1809 MachinePointerInfo::getStack(LocMemOffset),
David Greene67c9d422010-02-15 16:53:33 +00001810 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00001811}
1812
Bill Wendling64e87322009-01-16 19:25:27 +00001813/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001814/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00001815SDValue
1816X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00001817 SDValue &OutRetAddr, SDValue Chain,
1818 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00001819 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001820 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00001821 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001822 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00001823
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001824 // Load the "old" Return address.
Chris Lattner51abfe42010-09-21 06:02:19 +00001825 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, MachinePointerInfo(),
1826 false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001827 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001828}
1829
1830/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1831/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00001832static SDValue
1833EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00001834 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00001835 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001836 // Store the return address to the appropriate stack slot.
1837 if (!FPDiff) return Chain;
1838 // Calculate the new stack slot for the return address.
1839 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00001840 int NewReturnAddrFI =
Evan Chenged2ae132010-07-03 00:40:23 +00001841 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false);
Owen Anderson825b72b2009-08-11 20:47:22 +00001842 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00001843 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001844 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00001845 MachinePointerInfo::getFixedStack(NewReturnAddrFI),
David Greene67c9d422010-02-15 16:53:33 +00001846 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001847 return Chain;
1848}
1849
Dan Gohman98ca4f22009-08-05 01:29:28 +00001850SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001851X86TargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001852 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001853 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001854 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001855 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001856 const SmallVectorImpl<ISD::InputArg> &Ins,
1857 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001858 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001859 MachineFunction &MF = DAG.getMachineFunction();
1860 bool Is64Bit = Subtarget->is64Bit();
1861 bool IsStructRet = CallIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00001862 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001863
Evan Cheng5f941932010-02-05 02:21:12 +00001864 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00001865 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00001866 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1867 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001868 Outs, OutVals, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00001869
1870 // Sibcalls are automatically detected tailcalls which do not require
1871 // ABI changes.
Dan Gohman1797ed52010-02-08 20:27:50 +00001872 if (!GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00001873 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00001874
1875 if (isTailCall)
1876 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00001877 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00001878
Chris Lattner29689432010-03-11 00:22:57 +00001879 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1880 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001881
Chris Lattner638402b2007-02-28 07:00:42 +00001882 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00001883 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001884 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1885 ArgLocs, *DAG.getContext());
1886 CCInfo.AnalyzeCallOperands(Outs, CCAssignFnForNode(CallConv));
Scott Michelfdc40a02009-02-17 22:15:04 +00001887
Chris Lattner423c5f42007-02-28 05:31:48 +00001888 // Get a count of how many bytes are to be pushed on the stack.
1889 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00001890 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00001891 // This is a sibcall. The memory operands are available in caller's
1892 // own caller's stack.
1893 NumBytes = 0;
Chris Lattner29689432010-03-11 00:22:57 +00001894 else if (GuaranteedTailCallOpt && IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00001895 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001896
Gordon Henriksen86737662008-01-05 16:56:59 +00001897 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00001898 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001899 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00001900 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00001901 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1902 FPDiff = NumBytesCallerPushed - NumBytes;
1903
1904 // Set the delta of movement of the returnaddr stackslot.
1905 // But only set if delta is greater than previous delta.
1906 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1907 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1908 }
1909
Evan Chengf22f9b32010-02-06 03:28:46 +00001910 if (!IsSibcall)
1911 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001912
Dan Gohman475871a2008-07-27 21:46:04 +00001913 SDValue RetAddrFrIdx;
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001914 // Load return adress for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00001915 if (isTailCall && FPDiff)
1916 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
1917 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00001918
Dan Gohman475871a2008-07-27 21:46:04 +00001919 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
1920 SmallVector<SDValue, 8> MemOpChains;
1921 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00001922
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001923 // Walk the register/memloc assignments, inserting copies/loads. In the case
1924 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00001925 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1926 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001927 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00001928 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00001929 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00001930 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00001931
Chris Lattner423c5f42007-02-28 05:31:48 +00001932 // Promote the value if needed.
1933 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001934 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00001935 case CCValAssign::Full: break;
1936 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001937 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001938 break;
1939 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001940 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001941 break;
1942 case CCValAssign::AExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001943 if (RegVT.isVector() && RegVT.getSizeInBits() == 128) {
1944 // Special case: passing MMX values in XMM registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001945 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, Arg);
1946 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
1947 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001948 } else
1949 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
1950 break;
1951 case CCValAssign::BCvt:
1952 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001953 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001954 case CCValAssign::Indirect: {
1955 // Store the argument.
1956 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00001957 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001958 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00001959 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00001960 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001961 Arg = SpillSlot;
1962 break;
1963 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00001964 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001965
Chris Lattner423c5f42007-02-28 05:31:48 +00001966 if (VA.isRegLoc()) {
1967 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00001968 if (isVarArg && Subtarget->isTargetWin64()) {
1969 // Win64 ABI requires argument XMM reg to be copied to the corresponding
1970 // shadow reg if callee is a varargs function.
1971 unsigned ShadowReg = 0;
1972 switch (VA.getLocReg()) {
1973 case X86::XMM0: ShadowReg = X86::RCX; break;
1974 case X86::XMM1: ShadowReg = X86::RDX; break;
1975 case X86::XMM2: ShadowReg = X86::R8; break;
1976 case X86::XMM3: ShadowReg = X86::R9; break;
1977 }
1978 if (ShadowReg)
1979 RegsToPass.push_back(std::make_pair(ShadowReg, Arg));
1980 }
Evan Chengf22f9b32010-02-06 03:28:46 +00001981 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00001982 assert(VA.isMemLoc());
1983 if (StackPtr.getNode() == 0)
1984 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
1985 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1986 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001987 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001988 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001989
Evan Cheng32fe1032006-05-25 00:59:30 +00001990 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001991 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001992 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001993
Evan Cheng347d5f72006-04-28 21:29:37 +00001994 // Build a sequence of copy-to-reg nodes chained together with token chain
1995 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001996 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001997 // Tail call byval lowering might overwrite argument registers so in case of
1998 // tail call optimization the copies to registers are lowered later.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001999 if (!isTailCall)
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002000 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002001 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002002 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002003 InFlag = Chain.getValue(1);
2004 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002005
Chris Lattner88e1fd52009-07-09 04:24:46 +00002006 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002007 // ELF / PIC requires GOT in the EBX register before function calls via PLT
2008 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002009 if (!isTailCall) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002010 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
2011 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00002012 DebugLoc(), getPointerTy()),
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002013 InFlag);
2014 InFlag = Chain.getValue(1);
2015 } else {
2016 // If we are tail calling and generating PIC/GOT style code load the
2017 // address of the callee into ECX. The value in ecx is used as target of
2018 // the tail jump. This is done to circumvent the ebx/callee-saved problem
2019 // for tail calls on PIC/GOT architectures. Normally we would just put the
2020 // address of GOT into ebx and then call target@PLT. But for tail calls
2021 // ebx would be restored (since ebx is callee saved) before jumping to the
2022 // target@PLT.
2023
2024 // Note: The actual moving to ECX is done further down.
2025 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
2026 if (G && !G->getGlobal()->hasHiddenVisibility() &&
2027 !G->getGlobal()->hasProtectedVisibility())
2028 Callee = LowerGlobalAddress(Callee, DAG);
2029 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00002030 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002031 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002032 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002033
Nate Begemanc8ea6732010-07-21 20:49:52 +00002034 if (Is64Bit && isVarArg && !Subtarget->isTargetWin64()) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002035 // From AMD64 ABI document:
2036 // For calls that may call functions that use varargs or stdargs
2037 // (prototype-less calls or calls to functions containing ellipsis (...) in
2038 // the declaration) %al is used as hidden argument to specify the number
2039 // of SSE registers used. The contents of %al do not need to match exactly
2040 // the number of registers, but must be an ubound on the number of SSE
2041 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002042
Gordon Henriksen86737662008-01-05 16:56:59 +00002043 // Count the number of XMM registers allocated.
2044 static const unsigned XMMArgRegs[] = {
2045 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2046 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2047 };
2048 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Scott Michelfdc40a02009-02-17 22:15:04 +00002049 assert((Subtarget->hasSSE1() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00002050 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00002051
Dale Johannesendd64c412009-02-04 00:33:20 +00002052 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Owen Anderson825b72b2009-08-11 20:47:22 +00002053 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002054 InFlag = Chain.getValue(1);
2055 }
2056
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002057
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002058 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002059 if (isTailCall) {
2060 // Force all the incoming stack arguments to be loaded from the stack
2061 // before any new outgoing arguments are stored to the stack, because the
2062 // outgoing stack slots may alias the incoming argument stack slots, and
2063 // the alias isn't otherwise explicit. This is slightly more conservative
2064 // than necessary, because it means that each store effectively depends
2065 // on every argument instead of just those arguments it would clobber.
2066 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2067
Dan Gohman475871a2008-07-27 21:46:04 +00002068 SmallVector<SDValue, 8> MemOpChains2;
2069 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002070 int FI = 0;
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002071 // Do not flag preceeding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00002072 InFlag = SDValue();
Dan Gohman1797ed52010-02-08 20:27:50 +00002073 if (GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002074 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2075 CCValAssign &VA = ArgLocs[i];
2076 if (VA.isRegLoc())
2077 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002078 assert(VA.isMemLoc());
Dan Gohmanc9403652010-07-07 15:54:55 +00002079 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002080 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002081 // Create frame index.
2082 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002083 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002084 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002085 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002086
Duncan Sands276dcbd2008-03-21 09:14:45 +00002087 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002088 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002089 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002090 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00002091 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00002092 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002093 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002094
Dan Gohman98ca4f22009-08-05 01:29:28 +00002095 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2096 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002097 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002098 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002099 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002100 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002101 DAG.getStore(ArgChain, dl, Arg, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002102 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002103 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002104 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002105 }
2106 }
2107
2108 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002109 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002110 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002111
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002112 // Copy arguments to their registers.
2113 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002114 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002115 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002116 InFlag = Chain.getValue(1);
2117 }
Dan Gohman475871a2008-07-27 21:46:04 +00002118 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002119
Gordon Henriksen86737662008-01-05 16:56:59 +00002120 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002121 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00002122 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002123 }
2124
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002125 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2126 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2127 // In the 64-bit large code model, we have to make all calls
2128 // through a register, since the call instruction's 32-bit
2129 // pc-relative offset may not be large enough to hold the whole
2130 // address.
2131 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002132 // If the callee is a GlobalAddress node (quite common, every direct call
2133 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2134 // it.
2135
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002136 // We should use extra load for direct calls to dllimported functions in
2137 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002138 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002139 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002140 unsigned char OpFlags = 0;
Eric Christopherfd179292009-08-27 18:07:15 +00002141
Chris Lattner48a7d022009-07-09 05:02:21 +00002142 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2143 // external symbols most go through the PLT in PIC mode. If the symbol
2144 // has hidden or protected visibility, or if it is static or local, then
2145 // we don't need to use the PLT - we can directly call it.
2146 if (Subtarget->isTargetELF() &&
2147 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002148 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002149 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002150 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner80945782010-09-27 06:34:01 +00002151 (GV->isDeclaration() || GV->isWeakForLinker()) &&
2152 Subtarget->getDarwinVers() < 9) {
Chris Lattner74e726e2009-07-09 05:27:35 +00002153 // 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;
2157 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002158
Devang Patel0d881da2010-07-06 22:08:15 +00002159 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002160 G->getOffset(), OpFlags);
2161 }
Bill Wendling056292f2008-09-16 21:48:12 +00002162 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002163 unsigned char OpFlags = 0;
2164
2165 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to external
2166 // symbols should go through the PLT.
2167 if (Subtarget->isTargetELF() &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002168 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002169 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002170 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner80945782010-09-27 06:34:01 +00002171 Subtarget->getDarwinVers() < 9) {
Chris Lattner74e726e2009-07-09 05:27:35 +00002172 // PC-relative references to external symbols should go through $stub,
2173 // unless we're building with the leopard linker or later, which
2174 // automatically synthesizes these stubs.
2175 OpFlags = X86II::MO_DARWIN_STUB;
2176 }
Eric Christopherfd179292009-08-27 18:07:15 +00002177
Chris Lattner48a7d022009-07-09 05:02:21 +00002178 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2179 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002180 }
2181
Chris Lattnerd96d0722007-02-25 06:40:16 +00002182 // Returns a chain & a flag for retval copy to use.
Owen Anderson825b72b2009-08-11 20:47:22 +00002183 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00002184 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002185
Evan Chengf22f9b32010-02-06 03:28:46 +00002186 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002187 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2188 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002189 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002190 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002191
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002192 Ops.push_back(Chain);
2193 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002194
Dan Gohman98ca4f22009-08-05 01:29:28 +00002195 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002196 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002197
Gordon Henriksen86737662008-01-05 16:56:59 +00002198 // Add argument registers to the end of the list so that they are known live
2199 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002200 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2201 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2202 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002203
Evan Cheng586ccac2008-03-18 23:36:35 +00002204 // Add an implicit use GOT pointer in EBX.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002205 if (!isTailCall && Subtarget->isPICStyleGOT())
Evan Cheng586ccac2008-03-18 23:36:35 +00002206 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
2207
Anton Korobeynikov3a1e54a2010-08-17 21:06:07 +00002208 // Add an implicit use of AL for non-Windows x86 64-bit vararg functions.
2209 if (Is64Bit && isVarArg && !Subtarget->isTargetWin64())
Owen Anderson825b72b2009-08-11 20:47:22 +00002210 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
Evan Cheng586ccac2008-03-18 23:36:35 +00002211
Gabor Greifba36cb52008-08-28 21:40:38 +00002212 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002213 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002214
Dan Gohman98ca4f22009-08-05 01:29:28 +00002215 if (isTailCall) {
Dale Johannesen88004c22010-06-05 00:30:45 +00002216 // We used to do:
2217 //// If this is the first return lowered for this function, add the regs
2218 //// to the liveout set for the function.
2219 // This isn't right, although it's probably harmless on x86; liveouts
2220 // should be computed from returns not tail calls. Consider a void
2221 // function making a tail call to a function returning int.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002222 return DAG.getNode(X86ISD::TC_RETURN, dl,
2223 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002224 }
2225
Dale Johannesenace16102009-02-03 19:33:06 +00002226 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002227 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002228
Chris Lattner2d297092006-05-23 18:50:38 +00002229 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002230 unsigned NumBytesForCalleeToPush;
Dan Gohman4d3d6e12010-05-27 18:43:40 +00002231 if (Subtarget->IsCalleePop(isVarArg, CallConv))
Gordon Henriksen86737662008-01-05 16:56:59 +00002232 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Chris Lattner29689432010-03-11 00:22:57 +00002233 else if (!Is64Bit && !IsTailCallConvention(CallConv) && IsStructRet)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002234 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002235 // pops the hidden struct pointer, so we have to push it back.
2236 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002237 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002238 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002239 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002240
Gordon Henriksenae636f82008-01-03 16:47:34 +00002241 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002242 if (!IsSibcall) {
2243 Chain = DAG.getCALLSEQ_END(Chain,
2244 DAG.getIntPtrConstant(NumBytes, true),
2245 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2246 true),
2247 InFlag);
2248 InFlag = Chain.getValue(1);
2249 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002250
Chris Lattner3085e152007-02-25 08:59:22 +00002251 // Handle result values, copying them out of physregs into vregs that we
2252 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002253 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2254 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002255}
2256
Evan Cheng25ab6902006-09-08 06:48:29 +00002257
2258//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002259// Fast Calling Convention (tail call) implementation
2260//===----------------------------------------------------------------------===//
2261
2262// Like std call, callee cleans arguments, convention except that ECX is
2263// reserved for storing the tail called function address. Only 2 registers are
2264// free for argument passing (inreg). Tail call optimization is performed
2265// provided:
2266// * tailcallopt is enabled
2267// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002268// On X86_64 architecture with GOT-style position independent code only local
2269// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002270// To keep the stack aligned according to platform abi the function
2271// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2272// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002273// If a tail called function callee has more arguments than the caller the
2274// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002275// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002276// original REtADDR, but before the saved framepointer or the spilled registers
2277// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2278// stack layout:
2279// arg1
2280// arg2
2281// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002282// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002283// move area ]
2284// (possible EBP)
2285// ESI
2286// EDI
2287// local1 ..
2288
2289/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2290/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002291unsigned
2292X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2293 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002294 MachineFunction &MF = DAG.getMachineFunction();
2295 const TargetMachine &TM = MF.getTarget();
2296 const TargetFrameInfo &TFI = *TM.getFrameInfo();
2297 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002298 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002299 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002300 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002301 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2302 // Number smaller than 12 so just add the difference.
2303 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2304 } else {
2305 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002306 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002307 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002308 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002309 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002310}
2311
Evan Cheng5f941932010-02-05 02:21:12 +00002312/// MatchingStackOffset - Return true if the given stack call argument is
2313/// already available in the same position (relatively) of the caller's
2314/// incoming argument stack.
2315static
2316bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2317 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2318 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002319 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2320 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002321 if (Arg.getOpcode() == ISD::CopyFromReg) {
2322 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
2323 if (!VR || TargetRegisterInfo::isPhysicalRegister(VR))
2324 return false;
2325 MachineInstr *Def = MRI->getVRegDef(VR);
2326 if (!Def)
2327 return false;
2328 if (!Flags.isByVal()) {
2329 if (!TII->isLoadFromStackSlot(Def, FI))
2330 return false;
2331 } else {
2332 unsigned Opcode = Def->getOpcode();
2333 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2334 Def->getOperand(1).isFI()) {
2335 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002336 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002337 } else
2338 return false;
2339 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002340 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2341 if (Flags.isByVal())
2342 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002343 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002344 // define @foo(%struct.X* %A) {
2345 // tail call @bar(%struct.X* byval %A)
2346 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002347 return false;
2348 SDValue Ptr = Ld->getBasePtr();
2349 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2350 if (!FINode)
2351 return false;
2352 FI = FINode->getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002353 } else
2354 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002355
Evan Cheng4cae1332010-03-05 08:38:04 +00002356 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002357 if (!MFI->isFixedObjectIndex(FI))
2358 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002359 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002360}
2361
Dan Gohman98ca4f22009-08-05 01:29:28 +00002362/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2363/// for tail call optimization. Targets which want to do tail call
2364/// optimization should implement this function.
2365bool
2366X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002367 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002368 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002369 bool isCalleeStructRet,
2370 bool isCallerStructRet,
Evan Chengb1712452010-01-27 06:25:16 +00002371 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002372 const SmallVectorImpl<SDValue> &OutVals,
Evan Chengb1712452010-01-27 06:25:16 +00002373 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002374 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002375 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002376 CalleeCC != CallingConv::C)
2377 return false;
2378
Evan Cheng7096ae42010-01-29 06:45:59 +00002379 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002380 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002381 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Cheng13617962010-04-30 01:12:32 +00002382 CallingConv::ID CallerCC = CallerF->getCallingConv();
2383 bool CCMatch = CallerCC == CalleeCC;
2384
Dan Gohman1797ed52010-02-08 20:27:50 +00002385 if (GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002386 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002387 return true;
2388 return false;
2389 }
2390
Dale Johannesen2f05cc02010-05-28 23:24:28 +00002391 // Look for obvious safe cases to perform tail call optimization that do not
2392 // require ABI changes. This is what gcc calls sibcall.
Evan Chengb2c92902010-02-02 02:22:50 +00002393
Evan Cheng2c12cb42010-03-26 16:26:03 +00002394 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2395 // emit a special epilogue.
2396 if (RegInfo->needsStackRealignment(MF))
2397 return false;
2398
Eric Christopher90eb4022010-07-22 00:26:08 +00002399 // Do not sibcall optimize vararg calls unless the call site is not passing
2400 // any arguments.
Evan Cheng3c262ee2010-03-26 02:13:13 +00002401 if (isVarArg && !Outs.empty())
Evan Cheng843bd692010-01-31 06:44:49 +00002402 return false;
2403
Evan Chenga375d472010-03-15 18:54:48 +00002404 // Also avoid sibcall optimization if either caller or callee uses struct
2405 // return semantics.
2406 if (isCalleeStructRet || isCallerStructRet)
2407 return false;
2408
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002409 // If the call result is in ST0 / ST1, it needs to be popped off the x87 stack.
2410 // Therefore if it's not used by the call it is not safe to optimize this into
2411 // a sibcall.
2412 bool Unused = false;
2413 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2414 if (!Ins[i].Used) {
2415 Unused = true;
2416 break;
2417 }
2418 }
2419 if (Unused) {
2420 SmallVector<CCValAssign, 16> RVLocs;
2421 CCState CCInfo(CalleeCC, false, getTargetMachine(),
2422 RVLocs, *DAG.getContext());
2423 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002424 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002425 CCValAssign &VA = RVLocs[i];
2426 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2427 return false;
2428 }
2429 }
2430
Evan Cheng13617962010-04-30 01:12:32 +00002431 // If the calling conventions do not match, then we'd better make sure the
2432 // results are returned in the same way as what the caller expects.
2433 if (!CCMatch) {
2434 SmallVector<CCValAssign, 16> RVLocs1;
2435 CCState CCInfo1(CalleeCC, false, getTargetMachine(),
2436 RVLocs1, *DAG.getContext());
2437 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2438
2439 SmallVector<CCValAssign, 16> RVLocs2;
2440 CCState CCInfo2(CallerCC, false, getTargetMachine(),
2441 RVLocs2, *DAG.getContext());
2442 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2443
2444 if (RVLocs1.size() != RVLocs2.size())
2445 return false;
2446 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2447 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2448 return false;
2449 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2450 return false;
2451 if (RVLocs1[i].isRegLoc()) {
2452 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2453 return false;
2454 } else {
2455 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2456 return false;
2457 }
2458 }
2459 }
2460
Evan Chenga6bff982010-01-30 01:22:00 +00002461 // If the callee takes no arguments then go on to check the results of the
2462 // call.
2463 if (!Outs.empty()) {
2464 // Check if stack adjustment is needed. For now, do not do this if any
2465 // argument is passed on the stack.
2466 SmallVector<CCValAssign, 16> ArgLocs;
2467 CCState CCInfo(CalleeCC, isVarArg, getTargetMachine(),
2468 ArgLocs, *DAG.getContext());
2469 CCInfo.AnalyzeCallOperands(Outs, CCAssignFnForNode(CalleeCC));
Evan Chengb2c92902010-02-02 02:22:50 +00002470 if (CCInfo.getNextStackOffset()) {
2471 MachineFunction &MF = DAG.getMachineFunction();
2472 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2473 return false;
2474 if (Subtarget->isTargetWin64())
2475 // Win64 ABI has additional complications.
2476 return false;
2477
2478 // Check if the arguments are already laid out in the right way as
2479 // the caller's fixed stack objects.
2480 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002481 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2482 const X86InstrInfo *TII =
2483 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002484 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2485 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00002486 SDValue Arg = OutVals[i];
Evan Chengb2c92902010-02-02 02:22:50 +00002487 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002488 if (VA.getLocInfo() == CCValAssign::Indirect)
2489 return false;
2490 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002491 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2492 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002493 return false;
2494 }
2495 }
2496 }
Evan Cheng9c044672010-05-29 01:35:22 +00002497
2498 // If the tailcall address may be in a register, then make sure it's
2499 // possible to register allocate for it. In 32-bit, the call address can
2500 // only target EAX, EDX, or ECX since the tail call must be scheduled after
Evan Chengdedd9742010-07-14 06:44:01 +00002501 // callee-saved registers are restored. These happen to be the same
2502 // registers used to pass 'inreg' arguments so watch out for those.
2503 if (!Subtarget->is64Bit() &&
2504 !isa<GlobalAddressSDNode>(Callee) &&
Evan Cheng9c044672010-05-29 01:35:22 +00002505 !isa<ExternalSymbolSDNode>(Callee)) {
Evan Cheng9c044672010-05-29 01:35:22 +00002506 unsigned NumInRegs = 0;
2507 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2508 CCValAssign &VA = ArgLocs[i];
Evan Chengdedd9742010-07-14 06:44:01 +00002509 if (!VA.isRegLoc())
2510 continue;
2511 unsigned Reg = VA.getLocReg();
2512 switch (Reg) {
2513 default: break;
2514 case X86::EAX: case X86::EDX: case X86::ECX:
2515 if (++NumInRegs == 3)
Evan Cheng9c044672010-05-29 01:35:22 +00002516 return false;
Evan Chengdedd9742010-07-14 06:44:01 +00002517 break;
Evan Cheng9c044672010-05-29 01:35:22 +00002518 }
2519 }
2520 }
Evan Chenga6bff982010-01-30 01:22:00 +00002521 }
Evan Chengb1712452010-01-27 06:25:16 +00002522
Evan Cheng86809cc2010-02-03 03:28:02 +00002523 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002524}
2525
Dan Gohman3df24e62008-09-03 23:12:08 +00002526FastISel *
Dan Gohmana4160c32010-07-07 16:29:44 +00002527X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
2528 return X86::createFastISel(funcInfo);
Dan Gohmand9f3c482008-08-19 21:32:53 +00002529}
2530
2531
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002532//===----------------------------------------------------------------------===//
2533// Other Lowering Hooks
2534//===----------------------------------------------------------------------===//
2535
Bruno Cardoso Lopese654b562010-09-01 00:51:36 +00002536static bool MayFoldLoad(SDValue Op) {
2537 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
2538}
2539
2540static bool MayFoldIntoStore(SDValue Op) {
2541 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
2542}
2543
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002544static bool isTargetShuffle(unsigned Opcode) {
2545 switch(Opcode) {
2546 default: return false;
2547 case X86ISD::PSHUFD:
2548 case X86ISD::PSHUFHW:
2549 case X86ISD::PSHUFLW:
2550 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002551 case X86ISD::PALIGN:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002552 case X86ISD::SHUFPS:
2553 case X86ISD::MOVLHPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002554 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002555 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002556 case X86ISD::MOVLPS:
2557 case X86ISD::MOVLPD:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002558 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002559 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002560 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002561 case X86ISD::MOVSS:
2562 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002563 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002564 case X86ISD::UNPCKLPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002565 case X86ISD::PUNPCKLWD:
2566 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002567 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002568 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002569 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002570 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002571 case X86ISD::PUNPCKHWD:
2572 case X86ISD::PUNPCKHBW:
2573 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002574 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002575 return true;
2576 }
2577 return false;
2578}
2579
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002580static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002581 SDValue V1, SelectionDAG &DAG) {
2582 switch(Opc) {
2583 default: llvm_unreachable("Unknown x86 shuffle node");
2584 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002585 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002586 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002587 return DAG.getNode(Opc, dl, VT, V1);
2588 }
2589
2590 return SDValue();
2591}
2592
2593static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00002594 SDValue V1, unsigned TargetMask, SelectionDAG &DAG) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002595 switch(Opc) {
2596 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002597 case X86ISD::PSHUFD:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002598 case X86ISD::PSHUFHW:
2599 case X86ISD::PSHUFLW:
2600 return DAG.getNode(Opc, dl, VT, V1, DAG.getConstant(TargetMask, MVT::i8));
2601 }
2602
2603 return SDValue();
2604}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002605
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002606static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2607 SDValue V1, SDValue V2, unsigned TargetMask, SelectionDAG &DAG) {
2608 switch(Opc) {
2609 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002610 case X86ISD::PALIGN:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002611 case X86ISD::SHUFPD:
2612 case X86ISD::SHUFPS:
2613 return DAG.getNode(Opc, dl, VT, V1, V2,
2614 DAG.getConstant(TargetMask, MVT::i8));
2615 }
2616 return SDValue();
2617}
2618
2619static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2620 SDValue V1, SDValue V2, SelectionDAG &DAG) {
2621 switch(Opc) {
2622 default: llvm_unreachable("Unknown x86 shuffle node");
2623 case X86ISD::MOVLHPS:
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00002624 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002625 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002626 case X86ISD::MOVLPS:
2627 case X86ISD::MOVLPD:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002628 case X86ISD::MOVSS:
2629 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002630 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002631 case X86ISD::UNPCKLPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002632 case X86ISD::PUNPCKLWD:
2633 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002634 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002635 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002636 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002637 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002638 case X86ISD::PUNPCKHWD:
2639 case X86ISD::PUNPCKHBW:
2640 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002641 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002642 return DAG.getNode(Opc, dl, VT, V1, V2);
2643 }
2644 return SDValue();
2645}
2646
Dan Gohmand858e902010-04-17 15:26:15 +00002647SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002648 MachineFunction &MF = DAG.getMachineFunction();
2649 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2650 int ReturnAddrIndex = FuncInfo->getRAIndex();
2651
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002652 if (ReturnAddrIndex == 0) {
2653 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002654 uint64_t SlotSize = TD->getPointerSize();
David Greene3f2bf852009-11-12 20:49:22 +00002655 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00002656 false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002657 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002658 }
2659
Evan Cheng25ab6902006-09-08 06:48:29 +00002660 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002661}
2662
2663
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002664bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
2665 bool hasSymbolicDisplacement) {
2666 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00002667 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002668 return false;
2669
2670 // If we don't have a symbolic displacement - we don't have any extra
2671 // restrictions.
2672 if (!hasSymbolicDisplacement)
2673 return true;
2674
2675 // FIXME: Some tweaks might be needed for medium code model.
2676 if (M != CodeModel::Small && M != CodeModel::Kernel)
2677 return false;
2678
2679 // For small code model we assume that latest object is 16MB before end of 31
2680 // bits boundary. We may also accept pretty large negative constants knowing
2681 // that all objects are in the positive half of address space.
2682 if (M == CodeModel::Small && Offset < 16*1024*1024)
2683 return true;
2684
2685 // For kernel code model we know that all object resist in the negative half
2686 // of 32bits address space. We may not accept negative offsets, since they may
2687 // be just off and we may accept pretty large positive ones.
2688 if (M == CodeModel::Kernel && Offset > 0)
2689 return true;
2690
2691 return false;
2692}
2693
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002694/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
2695/// specific condition code, returning the condition code and the LHS/RHS of the
2696/// comparison to make.
2697static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2698 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00002699 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002700 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2701 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2702 // X > -1 -> X == 0, jump !sign.
2703 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002704 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002705 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2706 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002707 return X86::COND_S;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002708 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00002709 // X < 1 -> X <= 0
2710 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002711 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002712 }
Chris Lattnerf9570512006-09-13 03:22:10 +00002713 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002714
Evan Chengd9558e02006-01-06 00:43:03 +00002715 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002716 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002717 case ISD::SETEQ: return X86::COND_E;
2718 case ISD::SETGT: return X86::COND_G;
2719 case ISD::SETGE: return X86::COND_GE;
2720 case ISD::SETLT: return X86::COND_L;
2721 case ISD::SETLE: return X86::COND_LE;
2722 case ISD::SETNE: return X86::COND_NE;
2723 case ISD::SETULT: return X86::COND_B;
2724 case ISD::SETUGT: return X86::COND_A;
2725 case ISD::SETULE: return X86::COND_BE;
2726 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00002727 }
Chris Lattner4c78e022008-12-23 23:42:27 +00002728 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002729
Chris Lattner4c78e022008-12-23 23:42:27 +00002730 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00002731
Chris Lattner4c78e022008-12-23 23:42:27 +00002732 // If LHS is a foldable load, but RHS is not, flip the condition.
2733 if ((ISD::isNON_EXTLoad(LHS.getNode()) && LHS.hasOneUse()) &&
2734 !(ISD::isNON_EXTLoad(RHS.getNode()) && RHS.hasOneUse())) {
2735 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
2736 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00002737 }
2738
Chris Lattner4c78e022008-12-23 23:42:27 +00002739 switch (SetCCOpcode) {
2740 default: break;
2741 case ISD::SETOLT:
2742 case ISD::SETOLE:
2743 case ISD::SETUGT:
2744 case ISD::SETUGE:
2745 std::swap(LHS, RHS);
2746 break;
2747 }
2748
2749 // On a floating point condition, the flags are set as follows:
2750 // ZF PF CF op
2751 // 0 | 0 | 0 | X > Y
2752 // 0 | 0 | 1 | X < Y
2753 // 1 | 0 | 0 | X == Y
2754 // 1 | 1 | 1 | unordered
2755 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002756 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00002757 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002758 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00002759 case ISD::SETOLT: // flipped
2760 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002761 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00002762 case ISD::SETOLE: // flipped
2763 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002764 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002765 case ISD::SETUGT: // flipped
2766 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002767 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00002768 case ISD::SETUGE: // flipped
2769 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002770 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002771 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002772 case ISD::SETNE: return X86::COND_NE;
2773 case ISD::SETUO: return X86::COND_P;
2774 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00002775 case ISD::SETOEQ:
2776 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00002777 }
Evan Chengd9558e02006-01-06 00:43:03 +00002778}
2779
Evan Cheng4a460802006-01-11 00:33:36 +00002780/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2781/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00002782/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00002783static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00002784 switch (X86CC) {
2785 default:
2786 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00002787 case X86::COND_B:
2788 case X86::COND_BE:
2789 case X86::COND_E:
2790 case X86::COND_P:
2791 case X86::COND_A:
2792 case X86::COND_AE:
2793 case X86::COND_NE:
2794 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00002795 return true;
2796 }
2797}
2798
Evan Chengeb2f9692009-10-27 19:56:55 +00002799/// isFPImmLegal - Returns true if the target can instruction select the
2800/// specified FP immediate natively. If false, the legalizer will
2801/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00002802bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00002803 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
2804 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
2805 return true;
2806 }
2807 return false;
2808}
2809
Nate Begeman9008ca62009-04-27 18:41:29 +00002810/// isUndefOrInRange - Return true if Val is undef or if its value falls within
2811/// the specified range (L, H].
2812static bool isUndefOrInRange(int Val, int Low, int Hi) {
2813 return (Val < 0) || (Val >= Low && Val < Hi);
2814}
2815
2816/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
2817/// specified value.
2818static bool isUndefOrEqual(int Val, int CmpVal) {
2819 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00002820 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00002821 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00002822}
2823
Nate Begeman9008ca62009-04-27 18:41:29 +00002824/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
2825/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
2826/// the second operand.
Owen Andersone50ed302009-08-10 22:56:29 +00002827static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00002828 if (VT == MVT::v4f32 || VT == MVT::v4i32 )
Nate Begeman9008ca62009-04-27 18:41:29 +00002829 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002830 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00002831 return (Mask[0] < 2 && Mask[1] < 2);
2832 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002833}
2834
Nate Begeman9008ca62009-04-27 18:41:29 +00002835bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002836 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002837 N->getMask(M);
2838 return ::isPSHUFDMask(M, N->getValueType(0));
2839}
Evan Cheng0188ecb2006-03-22 18:59:22 +00002840
Nate Begeman9008ca62009-04-27 18:41:29 +00002841/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
2842/// is suitable for input to PSHUFHW.
Owen Andersone50ed302009-08-10 22:56:29 +00002843static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002844 if (VT != MVT::v8i16)
Evan Cheng0188ecb2006-03-22 18:59:22 +00002845 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002846
Nate Begeman9008ca62009-04-27 18:41:29 +00002847 // Lower quadword copied in order or undef.
2848 for (int i = 0; i != 4; ++i)
2849 if (Mask[i] >= 0 && Mask[i] != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00002850 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002851
Evan Cheng506d3df2006-03-29 23:07:14 +00002852 // Upper quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002853 for (int i = 4; i != 8; ++i)
2854 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Evan Cheng506d3df2006-03-29 23:07:14 +00002855 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002856
Evan Cheng506d3df2006-03-29 23:07:14 +00002857 return true;
2858}
2859
Nate Begeman9008ca62009-04-27 18:41:29 +00002860bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002861 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002862 N->getMask(M);
2863 return ::isPSHUFHWMask(M, N->getValueType(0));
2864}
Evan Cheng506d3df2006-03-29 23:07:14 +00002865
Nate Begeman9008ca62009-04-27 18:41:29 +00002866/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
2867/// is suitable for input to PSHUFLW.
Owen Andersone50ed302009-08-10 22:56:29 +00002868static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002869 if (VT != MVT::v8i16)
Evan Cheng506d3df2006-03-29 23:07:14 +00002870 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002871
Rafael Espindola15684b22009-04-24 12:40:33 +00002872 // Upper quadword copied in order.
Nate Begeman9008ca62009-04-27 18:41:29 +00002873 for (int i = 4; i != 8; ++i)
2874 if (Mask[i] >= 0 && Mask[i] != i)
Rafael Espindola15684b22009-04-24 12:40:33 +00002875 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002876
Rafael Espindola15684b22009-04-24 12:40:33 +00002877 // Lower quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002878 for (int i = 0; i != 4; ++i)
2879 if (Mask[i] >= 4)
Rafael Espindola15684b22009-04-24 12:40:33 +00002880 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002881
Rafael Espindola15684b22009-04-24 12:40:33 +00002882 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002883}
2884
Nate Begeman9008ca62009-04-27 18:41:29 +00002885bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002886 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002887 N->getMask(M);
2888 return ::isPSHUFLWMask(M, N->getValueType(0));
2889}
2890
Nate Begemana09008b2009-10-19 02:17:23 +00002891/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
2892/// is suitable for input to PALIGNR.
2893static bool isPALIGNRMask(const SmallVectorImpl<int> &Mask, EVT VT,
2894 bool hasSSSE3) {
2895 int i, e = VT.getVectorNumElements();
Michael J. Spencerec38de22010-10-10 22:04:20 +00002896
Nate Begemana09008b2009-10-19 02:17:23 +00002897 // Do not handle v2i64 / v2f64 shuffles with palignr.
2898 if (e < 4 || !hasSSSE3)
2899 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00002900
Nate Begemana09008b2009-10-19 02:17:23 +00002901 for (i = 0; i != e; ++i)
2902 if (Mask[i] >= 0)
2903 break;
Michael J. Spencerec38de22010-10-10 22:04:20 +00002904
Nate Begemana09008b2009-10-19 02:17:23 +00002905 // All undef, not a palignr.
2906 if (i == e)
2907 return false;
2908
2909 // Determine if it's ok to perform a palignr with only the LHS, since we
2910 // don't have access to the actual shuffle elements to see if RHS is undef.
2911 bool Unary = Mask[i] < (int)e;
2912 bool NeedsUnary = false;
2913
2914 int s = Mask[i] - i;
Michael J. Spencerec38de22010-10-10 22:04:20 +00002915
Nate Begemana09008b2009-10-19 02:17:23 +00002916 // Check the rest of the elements to see if they are consecutive.
2917 for (++i; i != e; ++i) {
2918 int m = Mask[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00002919 if (m < 0)
Nate Begemana09008b2009-10-19 02:17:23 +00002920 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00002921
Nate Begemana09008b2009-10-19 02:17:23 +00002922 Unary = Unary && (m < (int)e);
2923 NeedsUnary = NeedsUnary || (m < s);
2924
2925 if (NeedsUnary && !Unary)
2926 return false;
2927 if (Unary && m != ((s+i) & (e-1)))
2928 return false;
2929 if (!Unary && m != (s+i))
2930 return false;
2931 }
2932 return true;
2933}
2934
2935bool X86::isPALIGNRMask(ShuffleVectorSDNode *N) {
2936 SmallVector<int, 8> M;
2937 N->getMask(M);
2938 return ::isPALIGNRMask(M, N->getValueType(0), true);
2939}
2940
Evan Cheng14aed5e2006-03-24 01:18:28 +00002941/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2942/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Owen Andersone50ed302009-08-10 22:56:29 +00002943static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002944 int NumElems = VT.getVectorNumElements();
2945 if (NumElems != 2 && NumElems != 4)
2946 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002947
Nate Begeman9008ca62009-04-27 18:41:29 +00002948 int Half = NumElems / 2;
2949 for (int i = 0; i < Half; ++i)
2950 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002951 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002952 for (int i = Half; i < NumElems; ++i)
2953 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002954 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002955
Evan Cheng14aed5e2006-03-24 01:18:28 +00002956 return true;
2957}
2958
Nate Begeman9008ca62009-04-27 18:41:29 +00002959bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
2960 SmallVector<int, 8> M;
2961 N->getMask(M);
2962 return ::isSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002963}
2964
Evan Cheng213d2cf2007-05-17 18:45:50 +00002965/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00002966/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2967/// half elements to come from vector 1 (which would equal the dest.) and
2968/// the upper half to come from vector 2.
Owen Andersone50ed302009-08-10 22:56:29 +00002969static bool isCommutedSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002970 int NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00002971
2972 if (NumElems != 2 && NumElems != 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00002973 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002974
Nate Begeman9008ca62009-04-27 18:41:29 +00002975 int Half = NumElems / 2;
2976 for (int i = 0; i < Half; ++i)
2977 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002978 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002979 for (int i = Half; i < NumElems; ++i)
2980 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002981 return false;
2982 return true;
2983}
2984
Nate Begeman9008ca62009-04-27 18:41:29 +00002985static bool isCommutedSHUFP(ShuffleVectorSDNode *N) {
2986 SmallVector<int, 8> M;
2987 N->getMask(M);
2988 return isCommutedSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002989}
2990
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002991/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2992/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00002993bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
2994 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002995 return false;
2996
Evan Cheng2064a2b2006-03-28 06:50:32 +00002997 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman9008ca62009-04-27 18:41:29 +00002998 return isUndefOrEqual(N->getMaskElt(0), 6) &&
2999 isUndefOrEqual(N->getMaskElt(1), 7) &&
3000 isUndefOrEqual(N->getMaskElt(2), 2) &&
3001 isUndefOrEqual(N->getMaskElt(3), 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00003002}
3003
Nate Begeman0b10b912009-11-07 23:17:15 +00003004/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
3005/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
3006/// <2, 3, 2, 3>
3007bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
3008 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Michael J. Spencerec38de22010-10-10 22:04:20 +00003009
Nate Begeman0b10b912009-11-07 23:17:15 +00003010 if (NumElems != 4)
3011 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003012
Nate Begeman0b10b912009-11-07 23:17:15 +00003013 return isUndefOrEqual(N->getMaskElt(0), 2) &&
3014 isUndefOrEqual(N->getMaskElt(1), 3) &&
3015 isUndefOrEqual(N->getMaskElt(2), 2) &&
3016 isUndefOrEqual(N->getMaskElt(3), 3);
3017}
3018
Evan Cheng5ced1d82006-04-06 23:23:56 +00003019/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
3020/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman9008ca62009-04-27 18:41:29 +00003021bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
3022 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003023
Evan Cheng5ced1d82006-04-06 23:23:56 +00003024 if (NumElems != 2 && NumElems != 4)
3025 return false;
3026
Evan Chengc5cdff22006-04-07 21:53:05 +00003027 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003028 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003029 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003030
Evan Chengc5cdff22006-04-07 21:53:05 +00003031 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003032 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003033 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003034
3035 return true;
3036}
3037
Nate Begeman0b10b912009-11-07 23:17:15 +00003038/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
3039/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
3040bool X86::isMOVLHPSMask(ShuffleVectorSDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003041 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003042
Evan Cheng5ced1d82006-04-06 23:23:56 +00003043 if (NumElems != 2 && NumElems != 4)
3044 return false;
3045
Evan Chengc5cdff22006-04-07 21:53:05 +00003046 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003047 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003048 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003049
Nate Begeman9008ca62009-04-27 18:41:29 +00003050 for (unsigned i = 0; i < NumElems/2; ++i)
3051 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003052 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003053
3054 return true;
3055}
3056
Evan Cheng0038e592006-03-28 00:39:58 +00003057/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
3058/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Owen Andersone50ed302009-08-10 22:56:29 +00003059static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003060 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003061 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003062 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng0038e592006-03-28 00:39:58 +00003063 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003064
Nate Begeman9008ca62009-04-27 18:41:29 +00003065 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
3066 int BitI = Mask[i];
3067 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00003068 if (!isUndefOrEqual(BitI, j))
3069 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00003070 if (V2IsSplat) {
Mon P Wang7bcaefa2009-02-04 01:16:59 +00003071 if (!isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003072 return false;
3073 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00003074 if (!isUndefOrEqual(BitI1, j + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003075 return false;
3076 }
Evan Cheng0038e592006-03-28 00:39:58 +00003077 }
Evan Cheng0038e592006-03-28 00:39:58 +00003078 return true;
3079}
3080
Nate Begeman9008ca62009-04-27 18:41:29 +00003081bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3082 SmallVector<int, 8> M;
3083 N->getMask(M);
3084 return ::isUNPCKLMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003085}
3086
Evan Cheng4fcb9222006-03-28 02:43:26 +00003087/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3088/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Eric Christopherfd179292009-08-27 18:07:15 +00003089static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003090 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003091 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003092 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng4fcb9222006-03-28 02:43:26 +00003093 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003094
Nate Begeman9008ca62009-04-27 18:41:29 +00003095 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
3096 int BitI = Mask[i];
3097 int BitI1 = Mask[i+1];
Chris Lattner5a88b832007-02-25 07:10:00 +00003098 if (!isUndefOrEqual(BitI, j + NumElts/2))
Evan Chengc5cdff22006-04-07 21:53:05 +00003099 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00003100 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00003101 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003102 return false;
3103 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00003104 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003105 return false;
3106 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003107 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003108 return true;
3109}
3110
Nate Begeman9008ca62009-04-27 18:41:29 +00003111bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3112 SmallVector<int, 8> M;
3113 N->getMask(M);
3114 return ::isUNPCKHMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003115}
3116
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003117/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3118/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3119/// <0, 0, 1, 1>
Owen Andersone50ed302009-08-10 22:56:29 +00003120static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003121 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003122 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003123 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003124
Nate Begeman9008ca62009-04-27 18:41:29 +00003125 for (int i = 0, j = 0; i != NumElems; i += 2, ++j) {
3126 int BitI = Mask[i];
3127 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00003128 if (!isUndefOrEqual(BitI, j))
3129 return false;
3130 if (!isUndefOrEqual(BitI1, j))
3131 return false;
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003132 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003133 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003134}
3135
Nate Begeman9008ca62009-04-27 18:41:29 +00003136bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
3137 SmallVector<int, 8> M;
3138 N->getMask(M);
3139 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
3140}
3141
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003142/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3143/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3144/// <2, 2, 3, 3>
Owen Andersone50ed302009-08-10 22:56:29 +00003145static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003146 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003147 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
3148 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003149
Nate Begeman9008ca62009-04-27 18:41:29 +00003150 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
3151 int BitI = Mask[i];
3152 int BitI1 = Mask[i+1];
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003153 if (!isUndefOrEqual(BitI, j))
3154 return false;
3155 if (!isUndefOrEqual(BitI1, j))
3156 return false;
3157 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003158 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003159}
3160
Nate Begeman9008ca62009-04-27 18:41:29 +00003161bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
3162 SmallVector<int, 8> M;
3163 N->getMask(M);
3164 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
3165}
3166
Evan Cheng017dcc62006-04-21 01:05:10 +00003167/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3168/// specifies a shuffle of elements that is suitable for input to MOVSS,
3169/// MOVSD, and MOVD, i.e. setting the lowest element.
Owen Andersone50ed302009-08-10 22:56:29 +00003170static bool isMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003171 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003172 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003173
3174 int NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003175
Nate Begeman9008ca62009-04-27 18:41:29 +00003176 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003177 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003178
Nate Begeman9008ca62009-04-27 18:41:29 +00003179 for (int i = 1; i < NumElts; ++i)
3180 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003181 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003182
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003183 return true;
3184}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003185
Nate Begeman9008ca62009-04-27 18:41:29 +00003186bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
3187 SmallVector<int, 8> M;
3188 N->getMask(M);
3189 return ::isMOVLMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003190}
3191
Evan Cheng017dcc62006-04-21 01:05:10 +00003192/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
3193/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003194/// element of vector 2 and the other elements to come from vector 1 in order.
Owen Andersone50ed302009-08-10 22:56:29 +00003195static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003196 bool V2IsSplat = false, bool V2IsUndef = false) {
3197 int NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003198 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003199 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003200
Nate Begeman9008ca62009-04-27 18:41:29 +00003201 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003202 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003203
Nate Begeman9008ca62009-04-27 18:41:29 +00003204 for (int i = 1; i < NumOps; ++i)
3205 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3206 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3207 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003208 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003209
Evan Cheng39623da2006-04-20 08:58:49 +00003210 return true;
3211}
3212
Nate Begeman9008ca62009-04-27 18:41:29 +00003213static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00003214 bool V2IsUndef = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003215 SmallVector<int, 8> M;
3216 N->getMask(M);
3217 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00003218}
3219
Evan Chengd9539472006-04-14 21:59:03 +00003220/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3221/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003222bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N) {
3223 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003224 return false;
3225
3226 // Expect 1, 1, 3, 3
Rafael Espindola15684b22009-04-24 12:40:33 +00003227 for (unsigned i = 0; i < 2; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003228 int Elt = N->getMaskElt(i);
3229 if (Elt >= 0 && Elt != 1)
3230 return false;
Rafael Espindola15684b22009-04-24 12:40:33 +00003231 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003232
3233 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003234 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003235 int Elt = N->getMaskElt(i);
3236 if (Elt >= 0 && Elt != 3)
3237 return false;
3238 if (Elt == 3)
3239 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003240 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003241 // Don't use movshdup if it can be done with a shufps.
Nate Begeman9008ca62009-04-27 18:41:29 +00003242 // FIXME: verify that matching u, u, 3, 3 is what we want.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003243 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003244}
3245
3246/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3247/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003248bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N) {
3249 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003250 return false;
3251
3252 // Expect 0, 0, 2, 2
Nate Begeman9008ca62009-04-27 18:41:29 +00003253 for (unsigned i = 0; i < 2; ++i)
3254 if (N->getMaskElt(i) > 0)
3255 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003256
3257 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003258 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003259 int Elt = N->getMaskElt(i);
3260 if (Elt >= 0 && Elt != 2)
3261 return false;
3262 if (Elt == 2)
3263 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003264 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003265 // Don't use movsldup if it can be done with a shufps.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003266 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003267}
3268
Evan Cheng0b457f02008-09-25 20:50:48 +00003269/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3270/// specifies a shuffle of elements that is suitable for input to MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003271bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
3272 int e = N->getValueType(0).getVectorNumElements() / 2;
Eric Christopherfd179292009-08-27 18:07:15 +00003273
Nate Begeman9008ca62009-04-27 18:41:29 +00003274 for (int i = 0; i < e; ++i)
3275 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003276 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003277 for (int i = 0; i < e; ++i)
3278 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003279 return false;
3280 return true;
3281}
3282
Evan Cheng63d33002006-03-22 08:01:21 +00003283/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003284/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Evan Cheng63d33002006-03-22 08:01:21 +00003285unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003286 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3287 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
3288
Evan Chengb9df0ca2006-03-22 02:53:00 +00003289 unsigned Shift = (NumOperands == 4) ? 2 : 1;
3290 unsigned Mask = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00003291 for (int i = 0; i < NumOperands; ++i) {
3292 int Val = SVOp->getMaskElt(NumOperands-i-1);
3293 if (Val < 0) Val = 0;
Evan Cheng14aed5e2006-03-24 01:18:28 +00003294 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00003295 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00003296 if (i != NumOperands - 1)
3297 Mask <<= Shift;
3298 }
Evan Cheng63d33002006-03-22 08:01:21 +00003299 return Mask;
3300}
3301
Evan Cheng506d3df2006-03-29 23:07:14 +00003302/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003303/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003304unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003305 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003306 unsigned Mask = 0;
3307 // 8 nodes, but we only care about the last 4.
3308 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003309 int Val = SVOp->getMaskElt(i);
3310 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00003311 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00003312 if (i != 4)
3313 Mask <<= 2;
3314 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003315 return Mask;
3316}
3317
3318/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003319/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003320unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003321 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003322 unsigned Mask = 0;
3323 // 8 nodes, but we only care about the first 4.
3324 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003325 int Val = SVOp->getMaskElt(i);
3326 if (Val >= 0)
3327 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00003328 if (i != 0)
3329 Mask <<= 2;
3330 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003331 return Mask;
3332}
3333
Nate Begemana09008b2009-10-19 02:17:23 +00003334/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
3335/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
3336unsigned X86::getShufflePALIGNRImmediate(SDNode *N) {
3337 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3338 EVT VVT = N->getValueType(0);
3339 unsigned EltSize = VVT.getVectorElementType().getSizeInBits() >> 3;
3340 int Val = 0;
3341
3342 unsigned i, e;
3343 for (i = 0, e = VVT.getVectorNumElements(); i != e; ++i) {
3344 Val = SVOp->getMaskElt(i);
3345 if (Val >= 0)
3346 break;
3347 }
3348 return (Val - i) * EltSize;
3349}
3350
Evan Cheng37b73872009-07-30 08:33:02 +00003351/// isZeroNode - Returns true if Elt is a constant zero or a floating point
3352/// constant +0.0.
3353bool X86::isZeroNode(SDValue Elt) {
3354 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmane368b462010-06-18 14:22:04 +00003355 cast<ConstantSDNode>(Elt)->isNullValue()) ||
Evan Cheng37b73872009-07-30 08:33:02 +00003356 (isa<ConstantFPSDNode>(Elt) &&
3357 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
3358}
3359
Nate Begeman9008ca62009-04-27 18:41:29 +00003360/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
3361/// their permute mask.
3362static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
3363 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003364 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003365 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00003366 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00003367
Nate Begeman5a5ca152009-04-29 05:20:52 +00003368 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003369 int idx = SVOp->getMaskElt(i);
3370 if (idx < 0)
3371 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003372 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003373 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003374 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003375 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003376 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003377 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
3378 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003379}
3380
Evan Cheng779ccea2007-12-07 21:30:01 +00003381/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3382/// the two vector operands have swapped position.
Owen Andersone50ed302009-08-10 22:56:29 +00003383static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00003384 unsigned NumElems = VT.getVectorNumElements();
3385 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003386 int idx = Mask[i];
3387 if (idx < 0)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003388 continue;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003389 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003390 Mask[i] = idx + NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003391 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003392 Mask[i] = idx - NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003393 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003394}
3395
Evan Cheng533a0aa2006-04-19 20:35:22 +00003396/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
3397/// match movhlps. The lower half elements should come from upper half of
3398/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003399/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00003400static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
3401 if (Op->getValueType(0).getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00003402 return false;
3403 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003404 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003405 return false;
3406 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003407 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003408 return false;
3409 return true;
3410}
3411
Evan Cheng5ced1d82006-04-06 23:23:56 +00003412/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00003413/// is promoted to a vector. It also returns the LoadSDNode by reference if
3414/// required.
3415static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00003416 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
3417 return false;
3418 N = N->getOperand(0).getNode();
3419 if (!ISD::isNON_EXTLoad(N))
3420 return false;
3421 if (LD)
3422 *LD = cast<LoadSDNode>(N);
3423 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003424}
3425
Evan Cheng533a0aa2006-04-19 20:35:22 +00003426/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
3427/// match movlp{s|d}. The lower half elements should come from lower half of
3428/// V1 (and in order), and the upper half elements should come from the upper
3429/// half of V2 (and in order). And since V1 will become the source of the
3430/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003431static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
3432 ShuffleVectorSDNode *Op) {
Evan Cheng466685d2006-10-09 20:57:25 +00003433 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003434 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00003435 // Is V2 is a vector load, don't do this transformation. We will try to use
3436 // load folding shufps op.
3437 if (ISD::isNON_EXTLoad(V2))
3438 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003439
Nate Begeman5a5ca152009-04-29 05:20:52 +00003440 unsigned NumElems = Op->getValueType(0).getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003441
Evan Cheng533a0aa2006-04-19 20:35:22 +00003442 if (NumElems != 2 && NumElems != 4)
3443 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003444 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003445 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003446 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003447 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003448 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003449 return false;
3450 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003451}
3452
Evan Cheng39623da2006-04-20 08:58:49 +00003453/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
3454/// all the same.
3455static bool isSplatVector(SDNode *N) {
3456 if (N->getOpcode() != ISD::BUILD_VECTOR)
3457 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003458
Dan Gohman475871a2008-07-27 21:46:04 +00003459 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00003460 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
3461 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003462 return false;
3463 return true;
3464}
3465
Evan Cheng213d2cf2007-05-17 18:45:50 +00003466/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00003467/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00003468/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00003469static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00003470 SDValue V1 = N->getOperand(0);
3471 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003472 unsigned NumElems = N->getValueType(0).getVectorNumElements();
3473 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003474 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003475 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003476 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00003477 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
3478 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003479 if (Opc != ISD::BUILD_VECTOR ||
3480 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00003481 return false;
3482 } else if (Idx >= 0) {
3483 unsigned Opc = V1.getOpcode();
3484 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
3485 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003486 if (Opc != ISD::BUILD_VECTOR ||
3487 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00003488 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00003489 }
3490 }
3491 return true;
3492}
3493
3494/// getZeroVector - Returns a vector of specified type with all zero elements.
3495///
Owen Andersone50ed302009-08-10 22:56:29 +00003496static SDValue getZeroVector(EVT VT, bool HasSSE2, SelectionDAG &DAG,
Dale Johannesenace16102009-02-03 19:33:06 +00003497 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003498 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003499
Dale Johannesen0488fb62010-09-30 23:57:10 +00003500 // Always build SSE zero vectors as <4 x i32> bitcasted
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003501 // to their dest type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00003502 SDValue Vec;
Dale Johannesen0488fb62010-09-30 23:57:10 +00003503 if (VT.getSizeInBits() == 128) { // SSE
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003504 if (HasSSE2) { // SSE2
3505 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3506 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
3507 } else { // SSE1
3508 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
3509 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
3510 }
3511 } else if (VT.getSizeInBits() == 256) { // AVX
3512 // 256-bit logic and arithmetic instructions in AVX are
3513 // all floating-point, no support for integer ops. Default
3514 // to emitting fp zeroed vectors then.
Owen Anderson825b72b2009-08-11 20:47:22 +00003515 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003516 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
3517 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops, 8);
Evan Chengf0df0312008-05-15 08:39:06 +00003518 }
Dale Johannesenace16102009-02-03 19:33:06 +00003519 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00003520}
3521
Chris Lattner8a594482007-11-25 00:24:49 +00003522/// getOnesVector - Returns a vector of specified type with all bits set.
3523///
Owen Andersone50ed302009-08-10 22:56:29 +00003524static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003525 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003526
Chris Lattner8a594482007-11-25 00:24:49 +00003527 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
3528 // type. This ensures they get CSE'd.
Owen Anderson825b72b2009-08-11 20:47:22 +00003529 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00003530 SDValue Vec;
Dale Johannesen0488fb62010-09-30 23:57:10 +00003531 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Dale Johannesenace16102009-02-03 19:33:06 +00003532 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00003533}
3534
3535
Evan Cheng39623da2006-04-20 08:58:49 +00003536/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
3537/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00003538static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003539 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003540 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003541
Evan Cheng39623da2006-04-20 08:58:49 +00003542 bool Changed = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003543 SmallVector<int, 8> MaskVec;
3544 SVOp->getMask(MaskVec);
Eric Christopherfd179292009-08-27 18:07:15 +00003545
Nate Begeman5a5ca152009-04-29 05:20:52 +00003546 for (unsigned i = 0; i != NumElems; ++i) {
3547 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003548 MaskVec[i] = NumElems;
3549 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00003550 }
Evan Cheng39623da2006-04-20 08:58:49 +00003551 }
Evan Cheng39623da2006-04-20 08:58:49 +00003552 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00003553 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
3554 SVOp->getOperand(1), &MaskVec[0]);
3555 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00003556}
3557
Evan Cheng017dcc62006-04-21 01:05:10 +00003558/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
3559/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00003560static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003561 SDValue V2) {
3562 unsigned NumElems = VT.getVectorNumElements();
3563 SmallVector<int, 8> Mask;
3564 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00003565 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003566 Mask.push_back(i);
3567 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00003568}
3569
Nate Begeman9008ca62009-04-27 18:41:29 +00003570/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003571static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003572 SDValue V2) {
3573 unsigned NumElems = VT.getVectorNumElements();
3574 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00003575 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003576 Mask.push_back(i);
3577 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00003578 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003579 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00003580}
3581
Nate Begeman9008ca62009-04-27 18:41:29 +00003582/// getUnpackhMask - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003583static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003584 SDValue V2) {
3585 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00003586 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00003587 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00003588 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003589 Mask.push_back(i + Half);
3590 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00003591 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003592 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00003593}
3594
Bruno Cardoso Lopesbb0a9482010-08-13 17:50:47 +00003595/// PromoteSplat - Promote a splat of v4i32, v8i16 or v16i8 to v4f32.
3596static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003597 EVT PVT = MVT::v4f32;
Owen Andersone50ed302009-08-10 22:56:29 +00003598 EVT VT = SV->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00003599 DebugLoc dl = SV->getDebugLoc();
3600 SDValue V1 = SV->getOperand(0);
3601 int NumElems = VT.getVectorNumElements();
3602 int EltNo = SV->getSplatIndex();
Rafael Espindola15684b22009-04-24 12:40:33 +00003603
Nate Begeman9008ca62009-04-27 18:41:29 +00003604 // unpack elements to the correct location
3605 while (NumElems > 4) {
3606 if (EltNo < NumElems/2) {
3607 V1 = getUnpackl(DAG, dl, VT, V1, V1);
3608 } else {
3609 V1 = getUnpackh(DAG, dl, VT, V1, V1);
3610 EltNo -= NumElems/2;
3611 }
3612 NumElems >>= 1;
3613 }
Eric Christopherfd179292009-08-27 18:07:15 +00003614
Nate Begeman9008ca62009-04-27 18:41:29 +00003615 // Perform the splat.
3616 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Dale Johannesenace16102009-02-03 19:33:06 +00003617 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, PVT, V1);
Nate Begeman9008ca62009-04-27 18:41:29 +00003618 V1 = DAG.getVectorShuffle(PVT, dl, V1, DAG.getUNDEF(PVT), &SplatMask[0]);
3619 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, V1);
Evan Chengc575ca22006-04-17 20:43:08 +00003620}
3621
Evan Chengba05f722006-04-21 23:03:30 +00003622/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00003623/// vector of zero or undef vector. This produces a shuffle where the low
3624/// element of V2 is swizzled into the zero/undef vector, landing at element
3625/// 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 +00003626static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Chengf0df0312008-05-15 08:39:06 +00003627 bool isZero, bool HasSSE2,
3628 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003629 EVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00003630 SDValue V1 = isZero
Nate Begeman9008ca62009-04-27 18:41:29 +00003631 ? getZeroVector(VT, HasSSE2, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
3632 unsigned NumElems = VT.getVectorNumElements();
3633 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00003634 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003635 // If this is the insertion idx, put the low elt of V2 here.
3636 MaskVec.push_back(i == Idx ? NumElems : i);
3637 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00003638}
3639
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003640/// getShuffleScalarElt - Returns the scalar element that will make up the ith
3641/// element of the result of the vector shuffle.
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003642SDValue getShuffleScalarElt(SDNode *N, int Index, SelectionDAG &DAG,
3643 unsigned Depth) {
3644 if (Depth == 6)
3645 return SDValue(); // Limit search depth.
3646
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003647 SDValue V = SDValue(N, 0);
3648 EVT VT = V.getValueType();
3649 unsigned Opcode = V.getOpcode();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003650
3651 // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
3652 if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
3653 Index = SV->getMaskElt(Index);
3654
3655 if (Index < 0)
3656 return DAG.getUNDEF(VT.getVectorElementType());
3657
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003658 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003659 SDValue NewV = (Index < NumElems) ? SV->getOperand(0) : SV->getOperand(1);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003660 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG, Depth+1);
Evan Chengf26ffe92008-05-29 08:22:04 +00003661 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003662
3663 // Recurse into target specific vector shuffles to find scalars.
3664 if (isTargetShuffle(Opcode)) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003665 int NumElems = VT.getVectorNumElements();
3666 SmallVector<unsigned, 16> ShuffleMask;
3667 SDValue ImmN;
3668
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003669 switch(Opcode) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003670 case X86ISD::SHUFPS:
3671 case X86ISD::SHUFPD:
3672 ImmN = N->getOperand(N->getNumOperands()-1);
3673 DecodeSHUFPSMask(NumElems,
3674 cast<ConstantSDNode>(ImmN)->getZExtValue(),
3675 ShuffleMask);
3676 break;
3677 case X86ISD::PUNPCKHBW:
3678 case X86ISD::PUNPCKHWD:
3679 case X86ISD::PUNPCKHDQ:
3680 case X86ISD::PUNPCKHQDQ:
3681 DecodePUNPCKHMask(NumElems, ShuffleMask);
3682 break;
3683 case X86ISD::UNPCKHPS:
3684 case X86ISD::UNPCKHPD:
3685 DecodeUNPCKHPMask(NumElems, ShuffleMask);
3686 break;
3687 case X86ISD::PUNPCKLBW:
3688 case X86ISD::PUNPCKLWD:
3689 case X86ISD::PUNPCKLDQ:
3690 case X86ISD::PUNPCKLQDQ:
3691 DecodePUNPCKLMask(NumElems, ShuffleMask);
3692 break;
3693 case X86ISD::UNPCKLPS:
3694 case X86ISD::UNPCKLPD:
3695 DecodeUNPCKLPMask(NumElems, ShuffleMask);
3696 break;
3697 case X86ISD::MOVHLPS:
3698 DecodeMOVHLPSMask(NumElems, ShuffleMask);
3699 break;
3700 case X86ISD::MOVLHPS:
3701 DecodeMOVLHPSMask(NumElems, ShuffleMask);
3702 break;
3703 case X86ISD::PSHUFD:
3704 ImmN = N->getOperand(N->getNumOperands()-1);
3705 DecodePSHUFMask(NumElems,
3706 cast<ConstantSDNode>(ImmN)->getZExtValue(),
3707 ShuffleMask);
3708 break;
3709 case X86ISD::PSHUFHW:
3710 ImmN = N->getOperand(N->getNumOperands()-1);
3711 DecodePSHUFHWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
3712 ShuffleMask);
3713 break;
3714 case X86ISD::PSHUFLW:
3715 ImmN = N->getOperand(N->getNumOperands()-1);
3716 DecodePSHUFLWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
3717 ShuffleMask);
3718 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003719 case X86ISD::MOVSS:
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00003720 case X86ISD::MOVSD: {
3721 // The index 0 always comes from the first element of the second source,
3722 // this is why MOVSS and MOVSD are used in the first place. The other
3723 // elements come from the other positions of the first source vector.
3724 unsigned OpNum = (Index == 0) ? 1 : 0;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003725 return getShuffleScalarElt(V.getOperand(OpNum).getNode(), Index, DAG,
3726 Depth+1);
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00003727 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003728 default:
3729 assert("not implemented for target shuffle node");
3730 return SDValue();
3731 }
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003732
3733 Index = ShuffleMask[Index];
3734 if (Index < 0)
3735 return DAG.getUNDEF(VT.getVectorElementType());
3736
3737 SDValue NewV = (Index < NumElems) ? N->getOperand(0) : N->getOperand(1);
3738 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG,
3739 Depth+1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003740 }
3741
3742 // Actual nodes that may contain scalar elements
3743 if (Opcode == ISD::BIT_CONVERT) {
3744 V = V.getOperand(0);
3745 EVT SrcVT = V.getValueType();
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003746 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003747
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003748 if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003749 return SDValue();
3750 }
3751
3752 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
3753 return (Index == 0) ? V.getOperand(0)
3754 : DAG.getUNDEF(VT.getVectorElementType());
3755
3756 if (V.getOpcode() == ISD::BUILD_VECTOR)
3757 return V.getOperand(Index);
3758
3759 return SDValue();
3760}
3761
3762/// getNumOfConsecutiveZeros - Return the number of elements of a vector
3763/// shuffle operation which come from a consecutively from a zero. The
3764/// search can start in two diferent directions, from left or right.
3765static
3766unsigned getNumOfConsecutiveZeros(SDNode *N, int NumElems,
3767 bool ZerosFromLeft, SelectionDAG &DAG) {
3768 int i = 0;
3769
3770 while (i < NumElems) {
3771 unsigned Index = ZerosFromLeft ? i : NumElems-i-1;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003772 SDValue Elt = getShuffleScalarElt(N, Index, DAG, 0);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003773 if (!(Elt.getNode() &&
3774 (Elt.getOpcode() == ISD::UNDEF || X86::isZeroNode(Elt))))
3775 break;
3776 ++i;
3777 }
3778
3779 return i;
3780}
3781
3782/// isShuffleMaskConsecutive - Check if the shuffle mask indicies from MaskI to
3783/// MaskE correspond consecutively to elements from one of the vector operands,
3784/// starting from its index OpIdx. Also tell OpNum which source vector operand.
3785static
3786bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp, int MaskI, int MaskE,
3787 int OpIdx, int NumElems, unsigned &OpNum) {
3788 bool SeenV1 = false;
3789 bool SeenV2 = false;
3790
3791 for (int i = MaskI; i <= MaskE; ++i, ++OpIdx) {
3792 int Idx = SVOp->getMaskElt(i);
3793 // Ignore undef indicies
3794 if (Idx < 0)
3795 continue;
3796
3797 if (Idx < NumElems)
3798 SeenV1 = true;
3799 else
3800 SeenV2 = true;
3801
3802 // Only accept consecutive elements from the same vector
3803 if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
3804 return false;
3805 }
3806
3807 OpNum = SeenV1 ? 0 : 1;
3808 return true;
3809}
3810
3811/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
3812/// logical left shift of a vector.
3813static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
3814 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
3815 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
3816 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
3817 false /* check zeros from right */, DAG);
3818 unsigned OpSrc;
3819
3820 if (!NumZeros)
3821 return false;
3822
3823 // Considering the elements in the mask that are not consecutive zeros,
3824 // check if they consecutively come from only one of the source vectors.
3825 //
3826 // V1 = {X, A, B, C} 0
3827 // \ \ \ /
3828 // vector_shuffle V1, V2 <1, 2, 3, X>
3829 //
3830 if (!isShuffleMaskConsecutive(SVOp,
3831 0, // Mask Start Index
3832 NumElems-NumZeros-1, // Mask End Index
3833 NumZeros, // Where to start looking in the src vector
3834 NumElems, // Number of elements in vector
3835 OpSrc)) // Which source operand ?
3836 return false;
3837
3838 isLeft = false;
3839 ShAmt = NumZeros;
3840 ShVal = SVOp->getOperand(OpSrc);
3841 return true;
3842}
3843
3844/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
3845/// logical left shift of a vector.
3846static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
3847 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
3848 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
3849 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
3850 true /* check zeros from left */, DAG);
3851 unsigned OpSrc;
3852
3853 if (!NumZeros)
3854 return false;
3855
3856 // Considering the elements in the mask that are not consecutive zeros,
3857 // check if they consecutively come from only one of the source vectors.
3858 //
3859 // 0 { A, B, X, X } = V2
3860 // / \ / /
3861 // vector_shuffle V1, V2 <X, X, 4, 5>
3862 //
3863 if (!isShuffleMaskConsecutive(SVOp,
3864 NumZeros, // Mask Start Index
3865 NumElems-1, // Mask End Index
3866 0, // Where to start looking in the src vector
3867 NumElems, // Number of elements in vector
3868 OpSrc)) // Which source operand ?
3869 return false;
3870
3871 isLeft = true;
3872 ShAmt = NumZeros;
3873 ShVal = SVOp->getOperand(OpSrc);
3874 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00003875}
3876
3877/// isVectorShift - Returns true if the shuffle can be implemented as a
3878/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003879static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00003880 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003881 if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
3882 isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
3883 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00003884
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003885 return false;
Evan Chengf26ffe92008-05-29 08:22:04 +00003886}
3887
Evan Chengc78d3b42006-04-24 18:01:45 +00003888/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
3889///
Dan Gohman475871a2008-07-27 21:46:04 +00003890static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00003891 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00003892 SelectionDAG &DAG,
3893 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003894 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00003895 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00003896
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003897 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003898 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003899 bool First = true;
3900 for (unsigned i = 0; i < 16; ++i) {
3901 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
3902 if (ThisIsNonZero && First) {
3903 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003904 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00003905 else
Owen Anderson825b72b2009-08-11 20:47:22 +00003906 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00003907 First = false;
3908 }
3909
3910 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00003911 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003912 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
3913 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003914 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003915 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00003916 }
3917 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003918 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
3919 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
3920 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00003921 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003922 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00003923 } else
3924 ThisElt = LastElt;
3925
Gabor Greifba36cb52008-08-28 21:40:38 +00003926 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00003927 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00003928 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00003929 }
3930 }
3931
Owen Anderson825b72b2009-08-11 20:47:22 +00003932 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00003933}
3934
Bill Wendlinga348c562007-03-22 18:42:45 +00003935/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00003936///
Dan Gohman475871a2008-07-27 21:46:04 +00003937static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00003938 unsigned NumNonZero, unsigned NumZero,
3939 SelectionDAG &DAG,
3940 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003941 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00003942 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00003943
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003944 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003945 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003946 bool First = true;
3947 for (unsigned i = 0; i < 8; ++i) {
3948 bool isNonZero = (NonZeros & (1 << i)) != 0;
3949 if (isNonZero) {
3950 if (First) {
3951 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003952 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00003953 else
Owen Anderson825b72b2009-08-11 20:47:22 +00003954 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00003955 First = false;
3956 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003957 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003958 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00003959 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00003960 }
3961 }
3962
3963 return V;
3964}
3965
Evan Chengf26ffe92008-05-29 08:22:04 +00003966/// getVShift - Return a vector logical shift node.
3967///
Owen Andersone50ed302009-08-10 22:56:29 +00003968static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00003969 unsigned NumBits, SelectionDAG &DAG,
3970 const TargetLowering &TLI, DebugLoc dl) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00003971 EVT ShVT = MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00003972 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Dale Johannesenace16102009-02-03 19:33:06 +00003973 SrcOp = DAG.getNode(ISD::BIT_CONVERT, dl, ShVT, SrcOp);
3974 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3975 DAG.getNode(Opc, dl, ShVT, SrcOp,
Gabor Greif327ef032008-08-28 23:19:51 +00003976 DAG.getConstant(NumBits, TLI.getShiftAmountTy())));
Evan Chengf26ffe92008-05-29 08:22:04 +00003977}
3978
Dan Gohman475871a2008-07-27 21:46:04 +00003979SDValue
Evan Chengc3630942009-12-09 21:00:30 +00003980X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00003981 SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00003982
Evan Chengc3630942009-12-09 21:00:30 +00003983 // Check if the scalar load can be widened into a vector load. And if
3984 // the address is "base + cst" see if the cst can be "absorbed" into
3985 // the shuffle mask.
3986 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
3987 SDValue Ptr = LD->getBasePtr();
3988 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
3989 return SDValue();
3990 EVT PVT = LD->getValueType(0);
3991 if (PVT != MVT::i32 && PVT != MVT::f32)
3992 return SDValue();
3993
3994 int FI = -1;
3995 int64_t Offset = 0;
3996 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
3997 FI = FINode->getIndex();
3998 Offset = 0;
3999 } else if (Ptr.getOpcode() == ISD::ADD &&
4000 isa<ConstantSDNode>(Ptr.getOperand(1)) &&
4001 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4002 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4003 Offset = Ptr.getConstantOperandVal(1);
4004 Ptr = Ptr.getOperand(0);
4005 } else {
4006 return SDValue();
4007 }
4008
4009 SDValue Chain = LD->getChain();
4010 // Make sure the stack object alignment is at least 16.
4011 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
4012 if (DAG.InferPtrAlignment(Ptr) < 16) {
4013 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00004014 // Can't change the alignment. FIXME: It's possible to compute
4015 // the exact stack offset and reference FI + adjust offset instead.
4016 // If someone *really* cares about this. That's the way to implement it.
4017 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004018 } else {
4019 MFI->setObjectAlignment(FI, 16);
4020 }
4021 }
4022
4023 // (Offset % 16) must be multiple of 4. Then address is then
4024 // Ptr + (Offset & ~15).
4025 if (Offset < 0)
4026 return SDValue();
4027 if ((Offset % 16) & 3)
4028 return SDValue();
4029 int64_t StartOffset = Offset & ~15;
4030 if (StartOffset)
4031 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
4032 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
4033
4034 int EltNo = (Offset - StartOffset) >> 2;
4035 int Mask[4] = { EltNo, EltNo, EltNo, EltNo };
4036 EVT VT = (PVT == MVT::i32) ? MVT::v4i32 : MVT::v4f32;
Chris Lattner51abfe42010-09-21 06:02:19 +00004037 SDValue V1 = DAG.getLoad(VT, dl, Chain, Ptr,
4038 LD->getPointerInfo().getWithOffset(StartOffset),
David Greene67c9d422010-02-15 16:53:33 +00004039 false, false, 0);
Evan Chengc3630942009-12-09 21:00:30 +00004040 // Canonicalize it to a v4i32 shuffle.
4041 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4i32, V1);
4042 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
4043 DAG.getVectorShuffle(MVT::v4i32, dl, V1,
Chris Lattner51abfe42010-09-21 06:02:19 +00004044 DAG.getUNDEF(MVT::v4i32),&Mask[0]));
Evan Chengc3630942009-12-09 21:00:30 +00004045 }
4046
4047 return SDValue();
4048}
4049
Michael J. Spencerec38de22010-10-10 22:04:20 +00004050/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
4051/// vector of type 'VT', see if the elements can be replaced by a single large
Nate Begeman1449f292010-03-24 22:19:06 +00004052/// load which has the same value as a build_vector whose operands are 'elts'.
4053///
4054/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
Michael J. Spencerec38de22010-10-10 22:04:20 +00004055///
Nate Begeman1449f292010-03-24 22:19:06 +00004056/// FIXME: we'd also like to handle the case where the last elements are zero
4057/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
4058/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004059static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
Chris Lattner88641552010-09-22 00:34:38 +00004060 DebugLoc &DL, SelectionDAG &DAG) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00004061 EVT EltVT = VT.getVectorElementType();
4062 unsigned NumElems = Elts.size();
Michael J. Spencerec38de22010-10-10 22:04:20 +00004063
Nate Begemanfdea31a2010-03-24 20:49:50 +00004064 LoadSDNode *LDBase = NULL;
4065 unsigned LastLoadedElt = -1U;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004066
Nate Begeman1449f292010-03-24 22:19:06 +00004067 // For each element in the initializer, see if we've found a load or an undef.
Michael J. Spencerec38de22010-10-10 22:04:20 +00004068 // If we don't find an initial load element, or later load elements are
Nate Begeman1449f292010-03-24 22:19:06 +00004069 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004070 for (unsigned i = 0; i < NumElems; ++i) {
4071 SDValue Elt = Elts[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00004072
Nate Begemanfdea31a2010-03-24 20:49:50 +00004073 if (!Elt.getNode() ||
4074 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
4075 return SDValue();
4076 if (!LDBase) {
4077 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
4078 return SDValue();
4079 LDBase = cast<LoadSDNode>(Elt.getNode());
4080 LastLoadedElt = i;
4081 continue;
4082 }
4083 if (Elt.getOpcode() == ISD::UNDEF)
4084 continue;
4085
4086 LoadSDNode *LD = cast<LoadSDNode>(Elt);
4087 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
4088 return SDValue();
4089 LastLoadedElt = i;
4090 }
Nate Begeman1449f292010-03-24 22:19:06 +00004091
4092 // If we have found an entire vector of loads and undefs, then return a large
4093 // load of the entire vector width starting at the base pointer. If we found
4094 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004095 if (LastLoadedElt == NumElems - 1) {
4096 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
Chris Lattner88641552010-09-22 00:34:38 +00004097 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004098 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004099 LDBase->isVolatile(), LDBase->isNonTemporal(), 0);
Chris Lattner88641552010-09-22 00:34:38 +00004100 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004101 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004102 LDBase->isVolatile(), LDBase->isNonTemporal(),
4103 LDBase->getAlignment());
4104 } else if (NumElems == 4 && LastLoadedElt == 1) {
4105 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
4106 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
Chris Lattner88641552010-09-22 00:34:38 +00004107 SDValue ResNode = DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys,
4108 Ops, 2, MVT::i32,
4109 LDBase->getMemOperand());
4110 return DAG.getNode(ISD::BIT_CONVERT, DL, VT, ResNode);
Nate Begemanfdea31a2010-03-24 20:49:50 +00004111 }
4112 return SDValue();
4113}
4114
Evan Chengc3630942009-12-09 21:00:30 +00004115SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004116X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004117 DebugLoc dl = Op.getDebugLoc();
Chris Lattner6e80e442010-08-28 17:15:43 +00004118 // All zero's are handled with pxor in SSE2 and above, xorps in SSE1.
4119 // All one's are handled with pcmpeqd. In AVX, zero's are handled with
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004120 // vpxor in 128-bit and xor{pd,ps} in 256-bit, but no 256 version of pcmpeqd
4121 // is present, so AllOnes is ignored.
4122 if (ISD::isBuildVectorAllZeros(Op.getNode()) ||
4123 (Op.getValueType().getSizeInBits() != 256 &&
4124 ISD::isBuildVectorAllOnes(Op.getNode()))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004125 // Canonicalize this to <4 x i32> (SSE) to
Chris Lattner8a594482007-11-25 00:24:49 +00004126 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
4127 // eliminated on x86-32 hosts.
Dale Johannesen0488fb62010-09-30 23:57:10 +00004128 if (Op.getValueType() == MVT::v4i32)
Chris Lattner8a594482007-11-25 00:24:49 +00004129 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004130
Gabor Greifba36cb52008-08-28 21:40:38 +00004131 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00004132 return getOnesVector(Op.getValueType(), DAG, dl);
4133 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00004134 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004135
Owen Andersone50ed302009-08-10 22:56:29 +00004136 EVT VT = Op.getValueType();
4137 EVT ExtVT = VT.getVectorElementType();
4138 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004139
4140 unsigned NumElems = Op.getNumOperands();
4141 unsigned NumZero = 0;
4142 unsigned NumNonZero = 0;
4143 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004144 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00004145 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004146 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00004147 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00004148 if (Elt.getOpcode() == ISD::UNDEF)
4149 continue;
4150 Values.insert(Elt);
4151 if (Elt.getOpcode() != ISD::Constant &&
4152 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004153 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00004154 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00004155 NumZero++;
4156 else {
4157 NonZeros |= (1 << i);
4158 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004159 }
4160 }
4161
Chris Lattner97a2a562010-08-26 05:24:29 +00004162 // All undef vector. Return an UNDEF. All zero vectors were handled above.
4163 if (NumNonZero == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00004164 return DAG.getUNDEF(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004165
Chris Lattner67f453a2008-03-09 05:42:06 +00004166 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00004167 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004168 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00004169 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00004170
Chris Lattner62098042008-03-09 01:05:04 +00004171 // If this is an insertion of an i64 value on x86-32, and if the top bits of
4172 // the value are obviously zero, truncate the value to i32 and do the
4173 // insertion that way. Only do this if the value is non-constant or if the
4174 // value is a constant being inserted into element 0. It is cheaper to do
4175 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00004176 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00004177 (!IsAllConstants || Idx == 0)) {
4178 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004179 // Handle SSE only.
4180 assert(VT == MVT::v2i64 && "Expected an SSE value type!");
4181 EVT VecVT = MVT::v4i32;
4182 unsigned VecElts = 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00004183
Chris Lattner62098042008-03-09 01:05:04 +00004184 // Truncate the value (which may itself be a constant) to i32, and
4185 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00004186 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00004187 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00004188 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
4189 Subtarget->hasSSE2(), DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00004190
Chris Lattner62098042008-03-09 01:05:04 +00004191 // Now we have our 32-bit value zero extended in the low element of
4192 // a vector. If Idx != 0, swizzle it into place.
4193 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004194 SmallVector<int, 4> Mask;
4195 Mask.push_back(Idx);
4196 for (unsigned i = 1; i != VecElts; ++i)
4197 Mask.push_back(i);
4198 Item = DAG.getVectorShuffle(VecVT, dl, Item,
Eric Christopherfd179292009-08-27 18:07:15 +00004199 DAG.getUNDEF(Item.getValueType()),
Nate Begeman9008ca62009-04-27 18:41:29 +00004200 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00004201 }
Dale Johannesenace16102009-02-03 19:33:06 +00004202 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00004203 }
4204 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004205
Chris Lattner19f79692008-03-08 22:59:52 +00004206 // If we have a constant or non-constant insertion into the low element of
4207 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
4208 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00004209 // depending on what the source datatype is.
4210 if (Idx == 0) {
4211 if (NumZero == 0) {
4212 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Owen Anderson825b72b2009-08-11 20:47:22 +00004213 } else if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
4214 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Eli Friedman10415532009-06-06 06:05:10 +00004215 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
4216 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
4217 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget->hasSSE2(),
4218 DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00004219 } else if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
4220 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
Dale Johannesen0488fb62010-09-30 23:57:10 +00004221 assert(VT.getSizeInBits() == 128 && "Expected an SSE value type!");
4222 EVT MiddleVT = MVT::v4i32;
Eli Friedman10415532009-06-06 06:05:10 +00004223 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MiddleVT, Item);
4224 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
4225 Subtarget->hasSSE2(), DAG);
4226 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Item);
4227 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004228 }
Evan Chengf26ffe92008-05-29 08:22:04 +00004229
4230 // Is it a vector logical left shift?
4231 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00004232 X86::isZeroNode(Op.getOperand(0)) &&
4233 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004234 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00004235 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00004236 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004237 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00004238 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004239 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004240
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004241 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00004242 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004243
Chris Lattner19f79692008-03-08 22:59:52 +00004244 // Otherwise, if this is a vector with i32 or f32 elements, and the element
4245 // is a non-constant being inserted into an element other than the low one,
4246 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
4247 // movd/movss) to move this into the low element, then shuffle it into
4248 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004249 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00004250 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00004251
Evan Cheng0db9fe62006-04-25 20:13:52 +00004252 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00004253 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
4254 Subtarget->hasSSE2(), DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00004255 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004256 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00004257 MaskVec.push_back(i == Idx ? 0 : 1);
4258 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004259 }
4260 }
4261
Chris Lattner67f453a2008-03-09 05:42:06 +00004262 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00004263 if (Values.size() == 1) {
4264 if (EVTBits == 32) {
4265 // Instead of a shuffle like this:
4266 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
4267 // Check if it's possible to issue this instead.
4268 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
4269 unsigned Idx = CountTrailingZeros_32(NonZeros);
4270 SDValue Item = Op.getOperand(Idx);
4271 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
4272 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
4273 }
Dan Gohman475871a2008-07-27 21:46:04 +00004274 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004275 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004276
Dan Gohmana3941172007-07-24 22:55:08 +00004277 // A vector full of immediates; various special cases are already
4278 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004279 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00004280 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00004281
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00004282 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00004283 if (EVTBits == 64) {
4284 if (NumNonZero == 1) {
4285 // One half is zero or undef.
4286 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00004287 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00004288 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00004289 return getShuffleVectorZeroOrUndef(V2, Idx, true,
4290 Subtarget->hasSSE2(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00004291 }
Dan Gohman475871a2008-07-27 21:46:04 +00004292 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00004293 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004294
4295 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00004296 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00004297 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00004298 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004299 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004300 }
4301
Bill Wendling826f36f2007-03-28 00:57:11 +00004302 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00004303 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Chris Lattner97a2a562010-08-26 05:24:29 +00004304 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004305 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004306 }
4307
4308 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00004309 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00004310 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004311 if (NumElems == 4 && NumZero > 0) {
4312 for (unsigned i = 0; i < 4; ++i) {
4313 bool isZero = !(NonZeros & (1 << i));
4314 if (isZero)
Dale Johannesenace16102009-02-03 19:33:06 +00004315 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004316 else
Dale Johannesenace16102009-02-03 19:33:06 +00004317 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004318 }
4319
4320 for (unsigned i = 0; i < 2; ++i) {
4321 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
4322 default: break;
4323 case 0:
4324 V[i] = V[i*2]; // Must be a zero vector.
4325 break;
4326 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00004327 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004328 break;
4329 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00004330 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004331 break;
4332 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00004333 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004334 break;
4335 }
4336 }
4337
Nate Begeman9008ca62009-04-27 18:41:29 +00004338 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004339 bool Reverse = (NonZeros & 0x3) == 2;
4340 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004341 MaskVec.push_back(Reverse ? 1-i : i);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004342 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
4343 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004344 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
4345 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004346 }
4347
Nate Begemanfdea31a2010-03-24 20:49:50 +00004348 if (Values.size() > 1 && VT.getSizeInBits() == 128) {
4349 // Check for a build vector of consecutive loads.
4350 for (unsigned i = 0; i < NumElems; ++i)
4351 V[i] = Op.getOperand(i);
Michael J. Spencerec38de22010-10-10 22:04:20 +00004352
Nate Begemanfdea31a2010-03-24 20:49:50 +00004353 // Check for elements which are consecutive loads.
4354 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
4355 if (LD.getNode())
4356 return LD;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004357
4358 // For SSE 4.1, use insertps to put the high elements into the low element.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004359 if (getSubtarget()->hasSSE41()) {
Chris Lattner24faf612010-08-28 17:59:08 +00004360 SDValue Result;
4361 if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
4362 Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
4363 else
4364 Result = DAG.getUNDEF(VT);
Michael J. Spencerec38de22010-10-10 22:04:20 +00004365
Chris Lattner24faf612010-08-28 17:59:08 +00004366 for (unsigned i = 1; i < NumElems; ++i) {
4367 if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
4368 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
Nate Begeman9008ca62009-04-27 18:41:29 +00004369 Op.getOperand(i), DAG.getIntPtrConstant(i));
Chris Lattner24faf612010-08-28 17:59:08 +00004370 }
4371 return Result;
Nate Begeman9008ca62009-04-27 18:41:29 +00004372 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00004373
Chris Lattner6e80e442010-08-28 17:15:43 +00004374 // Otherwise, expand into a number of unpckl*, start by extending each of
4375 // our (non-undef) elements to the full vector width with the element in the
4376 // bottom slot of the vector (which generates no code for SSE).
4377 for (unsigned i = 0; i < NumElems; ++i) {
4378 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
4379 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
4380 else
4381 V[i] = DAG.getUNDEF(VT);
4382 }
4383
4384 // Next, we iteratively mix elements, e.g. for v4f32:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004385 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
4386 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
4387 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Chris Lattner6e80e442010-08-28 17:15:43 +00004388 unsigned EltStride = NumElems >> 1;
4389 while (EltStride != 0) {
Chris Lattner3ddcc432010-08-28 17:28:30 +00004390 for (unsigned i = 0; i < EltStride; ++i) {
4391 // If V[i+EltStride] is undef and this is the first round of mixing,
4392 // then it is safe to just drop this shuffle: V[i] is already in the
4393 // right place, the one element (since it's the first round) being
4394 // inserted as undef can be dropped. This isn't safe for successive
4395 // rounds because they will permute elements within both vectors.
4396 if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
4397 EltStride == NumElems/2)
4398 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004399
Chris Lattner6e80e442010-08-28 17:15:43 +00004400 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
Chris Lattner3ddcc432010-08-28 17:28:30 +00004401 }
Chris Lattner6e80e442010-08-28 17:15:43 +00004402 EltStride >>= 1;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004403 }
4404 return V[0];
4405 }
Dan Gohman475871a2008-07-27 21:46:04 +00004406 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004407}
4408
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004409SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004410X86TargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const {
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004411 // We support concatenate two MMX registers and place them in a MMX
4412 // register. This is better than doing a stack convert.
4413 DebugLoc dl = Op.getDebugLoc();
4414 EVT ResVT = Op.getValueType();
4415 assert(Op.getNumOperands() == 2);
4416 assert(ResVT == MVT::v2i64 || ResVT == MVT::v4i32 ||
4417 ResVT == MVT::v8i16 || ResVT == MVT::v16i8);
4418 int Mask[2];
4419 SDValue InVec = DAG.getNode(ISD::BIT_CONVERT,dl, MVT::v1i64, Op.getOperand(0));
4420 SDValue VecOp = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
4421 InVec = Op.getOperand(1);
4422 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4423 unsigned NumElts = ResVT.getVectorNumElements();
4424 VecOp = DAG.getNode(ISD::BIT_CONVERT, dl, ResVT, VecOp);
4425 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, ResVT, VecOp,
4426 InVec.getOperand(0), DAG.getIntPtrConstant(NumElts/2+1));
4427 } else {
4428 InVec = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v1i64, InVec);
4429 SDValue VecOp2 = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
4430 Mask[0] = 0; Mask[1] = 2;
4431 VecOp = DAG.getVectorShuffle(MVT::v2i64, dl, VecOp, VecOp2, Mask);
4432 }
4433 return DAG.getNode(ISD::BIT_CONVERT, dl, ResVT, VecOp);
4434}
4435
Nate Begemanb9a47b82009-02-23 08:49:38 +00004436// v8i16 shuffles - Prefer shuffles in the following order:
4437// 1. [all] pshuflw, pshufhw, optional move
4438// 2. [ssse3] 1 x pshufb
4439// 3. [ssse3] 2 x pshufb + 1 x por
4440// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004441SDValue
4442X86TargetLowering::LowerVECTOR_SHUFFLEv8i16(SDValue Op,
4443 SelectionDAG &DAG) const {
4444 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00004445 SDValue V1 = SVOp->getOperand(0);
4446 SDValue V2 = SVOp->getOperand(1);
4447 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00004448 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00004449
Nate Begemanb9a47b82009-02-23 08:49:38 +00004450 // Determine if more than 1 of the words in each of the low and high quadwords
4451 // of the result come from the same quadword of one of the two inputs. Undef
4452 // mask values count as coming from any quadword, for better codegen.
4453 SmallVector<unsigned, 4> LoQuad(4);
4454 SmallVector<unsigned, 4> HiQuad(4);
4455 BitVector InputQuads(4);
4456 for (unsigned i = 0; i < 8; ++i) {
4457 SmallVectorImpl<unsigned> &Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00004458 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004459 MaskVals.push_back(EltIdx);
4460 if (EltIdx < 0) {
4461 ++Quad[0];
4462 ++Quad[1];
4463 ++Quad[2];
4464 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00004465 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004466 }
4467 ++Quad[EltIdx / 4];
4468 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00004469 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004470
Nate Begemanb9a47b82009-02-23 08:49:38 +00004471 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00004472 unsigned MaxQuad = 1;
4473 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004474 if (LoQuad[i] > MaxQuad) {
4475 BestLoQuad = i;
4476 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00004477 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004478 }
4479
Nate Begemanb9a47b82009-02-23 08:49:38 +00004480 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00004481 MaxQuad = 1;
4482 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004483 if (HiQuad[i] > MaxQuad) {
4484 BestHiQuad = i;
4485 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00004486 }
4487 }
4488
Nate Begemanb9a47b82009-02-23 08:49:38 +00004489 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00004490 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00004491 // single pshufb instruction is necessary. If There are more than 2 input
4492 // quads, disable the next transformation since it does not help SSSE3.
4493 bool V1Used = InputQuads[0] || InputQuads[1];
4494 bool V2Used = InputQuads[2] || InputQuads[3];
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004495 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004496 if (InputQuads.count() == 2 && V1Used && V2Used) {
4497 BestLoQuad = InputQuads.find_first();
4498 BestHiQuad = InputQuads.find_next(BestLoQuad);
4499 }
4500 if (InputQuads.count() > 2) {
4501 BestLoQuad = -1;
4502 BestHiQuad = -1;
4503 }
4504 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004505
Nate Begemanb9a47b82009-02-23 08:49:38 +00004506 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
4507 // the shuffle mask. If a quad is scored as -1, that means that it contains
4508 // words from all 4 input quadwords.
4509 SDValue NewV;
4510 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004511 SmallVector<int, 8> MaskV;
4512 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
4513 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
Eric Christopherfd179292009-08-27 18:07:15 +00004514 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004515 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, V1),
4516 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, V2), &MaskV[0]);
4517 NewV = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00004518
Nate Begemanb9a47b82009-02-23 08:49:38 +00004519 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
4520 // source words for the shuffle, to aid later transformations.
4521 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00004522 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00004523 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004524 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00004525 if (idx != (int)i)
4526 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004527 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00004528 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004529 AllWordsInNewV = false;
4530 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00004531 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004532
Nate Begemanb9a47b82009-02-23 08:49:38 +00004533 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
4534 if (AllWordsInNewV) {
4535 for (int i = 0; i != 8; ++i) {
4536 int idx = MaskVals[i];
4537 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00004538 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004539 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004540 if ((idx != i) && idx < 4)
4541 pshufhw = false;
4542 if ((idx != i) && idx > 3)
4543 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00004544 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00004545 V1 = NewV;
4546 V2Used = false;
4547 BestLoQuad = 0;
4548 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004549 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004550
Nate Begemanb9a47b82009-02-23 08:49:38 +00004551 // If we've eliminated the use of V2, and the new mask is a pshuflw or
4552 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00004553 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00004554 unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
4555 unsigned TargetMask = 0;
4556 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00004557 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00004558 TargetMask = pshufhw ? X86::getShufflePSHUFHWImmediate(NewV.getNode()):
4559 X86::getShufflePSHUFLWImmediate(NewV.getNode());
4560 V1 = NewV.getOperand(0);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00004561 return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
Evan Cheng14b32e12007-12-11 01:46:18 +00004562 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004563 }
Eric Christopherfd179292009-08-27 18:07:15 +00004564
Nate Begemanb9a47b82009-02-23 08:49:38 +00004565 // If we have SSSE3, and all words of the result are from 1 input vector,
4566 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
4567 // is present, fall back to case 4.
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004568 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004569 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00004570
Nate Begemanb9a47b82009-02-23 08:49:38 +00004571 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00004572 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00004573 // mask, and elements that come from V1 in the V2 mask, so that the two
4574 // results can be OR'd together.
4575 bool TwoInputs = V1Used && V2Used;
4576 for (unsigned i = 0; i != 8; ++i) {
4577 int EltIdx = MaskVals[i] * 2;
4578 if (TwoInputs && (EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004579 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4580 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004581 continue;
4582 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004583 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
4584 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004585 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004586 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004587 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00004588 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004589 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004590 if (!TwoInputs)
Owen Anderson825b72b2009-08-11 20:47:22 +00004591 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004592
Nate Begemanb9a47b82009-02-23 08:49:38 +00004593 // Calculate the shuffle mask for the second input, shuffle it, and
4594 // OR it with the first shuffled input.
4595 pshufbMask.clear();
4596 for (unsigned i = 0; i != 8; ++i) {
4597 int EltIdx = MaskVals[i] * 2;
4598 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004599 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4600 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004601 continue;
4602 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004603 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
4604 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004605 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004606 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00004607 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00004608 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004609 MVT::v16i8, &pshufbMask[0], 16));
4610 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
4611 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004612 }
4613
4614 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
4615 // and update MaskVals with new element order.
4616 BitVector InOrder(8);
4617 if (BestLoQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004618 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004619 for (int i = 0; i != 4; ++i) {
4620 int idx = MaskVals[i];
4621 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004622 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004623 InOrder.set(i);
4624 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004625 MaskV.push_back(idx & 3);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004626 InOrder.set(i);
4627 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004628 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004629 }
4630 }
4631 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004632 MaskV.push_back(i);
Owen Anderson825b72b2009-08-11 20:47:22 +00004633 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00004634 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00004635
4636 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3())
4637 NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
4638 NewV.getOperand(0),
4639 X86::getShufflePSHUFLWImmediate(NewV.getNode()),
4640 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004641 }
Eric Christopherfd179292009-08-27 18:07:15 +00004642
Nate Begemanb9a47b82009-02-23 08:49:38 +00004643 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
4644 // and update MaskVals with the new element order.
4645 if (BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004646 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004647 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004648 MaskV.push_back(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004649 for (unsigned i = 4; i != 8; ++i) {
4650 int idx = MaskVals[i];
4651 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004652 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004653 InOrder.set(i);
4654 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004655 MaskV.push_back((idx & 3) + 4);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004656 InOrder.set(i);
4657 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004658 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004659 }
4660 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004661 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00004662 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00004663
4664 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3())
4665 NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
4666 NewV.getOperand(0),
4667 X86::getShufflePSHUFHWImmediate(NewV.getNode()),
4668 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004669 }
Eric Christopherfd179292009-08-27 18:07:15 +00004670
Nate Begemanb9a47b82009-02-23 08:49:38 +00004671 // In case BestHi & BestLo were both -1, which means each quadword has a word
4672 // from each of the four input quadwords, calculate the InOrder bitvector now
4673 // before falling through to the insert/extract cleanup.
4674 if (BestLoQuad == -1 && BestHiQuad == -1) {
4675 NewV = V1;
4676 for (int i = 0; i != 8; ++i)
4677 if (MaskVals[i] < 0 || MaskVals[i] == i)
4678 InOrder.set(i);
4679 }
Eric Christopherfd179292009-08-27 18:07:15 +00004680
Nate Begemanb9a47b82009-02-23 08:49:38 +00004681 // The other elements are put in the right place using pextrw and pinsrw.
4682 for (unsigned i = 0; i != 8; ++i) {
4683 if (InOrder[i])
4684 continue;
4685 int EltIdx = MaskVals[i];
4686 if (EltIdx < 0)
4687 continue;
4688 SDValue ExtOp = (EltIdx < 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00004689 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004690 DAG.getIntPtrConstant(EltIdx))
Owen Anderson825b72b2009-08-11 20:47:22 +00004691 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004692 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00004693 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004694 DAG.getIntPtrConstant(i));
4695 }
4696 return NewV;
4697}
4698
4699// v16i8 shuffles - Prefer shuffles in the following order:
4700// 1. [ssse3] 1 x pshufb
4701// 2. [ssse3] 2 x pshufb + 1 x por
4702// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
4703static
Nate Begeman9008ca62009-04-27 18:41:29 +00004704SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00004705 SelectionDAG &DAG,
4706 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004707 SDValue V1 = SVOp->getOperand(0);
4708 SDValue V2 = SVOp->getOperand(1);
4709 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00004710 SmallVector<int, 16> MaskVals;
Nate Begeman9008ca62009-04-27 18:41:29 +00004711 SVOp->getMask(MaskVals);
Eric Christopherfd179292009-08-27 18:07:15 +00004712
Nate Begemanb9a47b82009-02-23 08:49:38 +00004713 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00004714 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00004715 // present, fall back to case 3.
4716 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
4717 bool V1Only = true;
4718 bool V2Only = true;
4719 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004720 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00004721 if (EltIdx < 0)
4722 continue;
4723 if (EltIdx < 16)
4724 V2Only = false;
4725 else
4726 V1Only = false;
4727 }
Eric Christopherfd179292009-08-27 18:07:15 +00004728
Nate Begemanb9a47b82009-02-23 08:49:38 +00004729 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
4730 if (TLI.getSubtarget()->hasSSSE3()) {
4731 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00004732
Nate Begemanb9a47b82009-02-23 08:49:38 +00004733 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00004734 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00004735 //
4736 // Otherwise, we have elements from both input vectors, and must zero out
4737 // elements that come from V2 in the first mask, and V1 in the second mask
4738 // so that we can OR them together.
4739 bool TwoInputs = !(V1Only || V2Only);
4740 for (unsigned i = 0; i != 16; ++i) {
4741 int EltIdx = MaskVals[i];
4742 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004743 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004744 continue;
4745 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004746 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004747 }
4748 // If all the elements are from V2, assign it to V1 and return after
4749 // building the first pshufb.
4750 if (V2Only)
4751 V1 = V2;
Owen Anderson825b72b2009-08-11 20:47:22 +00004752 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00004753 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004754 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004755 if (!TwoInputs)
4756 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00004757
Nate Begemanb9a47b82009-02-23 08:49:38 +00004758 // Calculate the shuffle mask for the second input, shuffle it, and
4759 // OR it with the first shuffled input.
4760 pshufbMask.clear();
4761 for (unsigned i = 0; i != 16; ++i) {
4762 int EltIdx = MaskVals[i];
4763 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004764 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004765 continue;
4766 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004767 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004768 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004769 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00004770 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004771 MVT::v16i8, &pshufbMask[0], 16));
4772 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004773 }
Eric Christopherfd179292009-08-27 18:07:15 +00004774
Nate Begemanb9a47b82009-02-23 08:49:38 +00004775 // No SSSE3 - Calculate in place words and then fix all out of place words
4776 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
4777 // the 16 different words that comprise the two doublequadword input vectors.
Owen Anderson825b72b2009-08-11 20:47:22 +00004778 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
4779 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004780 SDValue NewV = V2Only ? V2 : V1;
4781 for (int i = 0; i != 8; ++i) {
4782 int Elt0 = MaskVals[i*2];
4783 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00004784
Nate Begemanb9a47b82009-02-23 08:49:38 +00004785 // This word of the result is all undef, skip it.
4786 if (Elt0 < 0 && Elt1 < 0)
4787 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004788
Nate Begemanb9a47b82009-02-23 08:49:38 +00004789 // This word of the result is already in the correct place, skip it.
4790 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
4791 continue;
4792 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
4793 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004794
Nate Begemanb9a47b82009-02-23 08:49:38 +00004795 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
4796 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
4797 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00004798
4799 // If Elt0 and Elt1 are defined, are consecutive, and can be load
4800 // using a single extract together, load it and store it.
4801 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004802 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00004803 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00004804 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00004805 DAG.getIntPtrConstant(i));
4806 continue;
4807 }
4808
Nate Begemanb9a47b82009-02-23 08:49:38 +00004809 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00004810 // source byte is not also odd, shift the extracted word left 8 bits
4811 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00004812 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004813 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004814 DAG.getIntPtrConstant(Elt1 / 2));
4815 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004816 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004817 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00004818 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004819 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
4820 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004821 }
4822 // If Elt0 is defined, extract it from the appropriate source. If the
4823 // source byte is not also even, shift the extracted word right 8 bits. If
4824 // Elt1 was also defined, OR the extracted values together before
4825 // inserting them in the result.
4826 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004827 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004828 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
4829 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004830 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004831 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00004832 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004833 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
4834 DAG.getConstant(0x00FF, MVT::i16));
4835 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00004836 : InsElt0;
4837 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004838 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004839 DAG.getIntPtrConstant(i));
4840 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004841 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00004842}
4843
Evan Cheng7a831ce2007-12-15 03:00:47 +00004844/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00004845/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00004846/// done when every pair / quad of shuffle mask elements point to elements in
4847/// the right sequence. e.g.
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00004848/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
Evan Cheng14b32e12007-12-11 01:46:18 +00004849static
Nate Begeman9008ca62009-04-27 18:41:29 +00004850SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00004851 SelectionDAG &DAG, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00004852 EVT VT = SVOp->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00004853 SDValue V1 = SVOp->getOperand(0);
4854 SDValue V2 = SVOp->getOperand(1);
4855 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00004856 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00004857 EVT NewVT;
Owen Anderson825b72b2009-08-11 20:47:22 +00004858 switch (VT.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004859 default: assert(false && "Unexpected!");
Owen Anderson825b72b2009-08-11 20:47:22 +00004860 case MVT::v4f32: NewVT = MVT::v2f64; break;
4861 case MVT::v4i32: NewVT = MVT::v2i64; break;
4862 case MVT::v8i16: NewVT = MVT::v4i32; break;
4863 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00004864 }
4865
Nate Begeman9008ca62009-04-27 18:41:29 +00004866 int Scale = NumElems / NewWidth;
4867 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00004868 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004869 int StartIdx = -1;
4870 for (int j = 0; j < Scale; ++j) {
4871 int EltIdx = SVOp->getMaskElt(i+j);
4872 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00004873 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00004874 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00004875 StartIdx = EltIdx - (EltIdx % Scale);
4876 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00004877 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00004878 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004879 if (StartIdx == -1)
4880 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00004881 else
Nate Begeman9008ca62009-04-27 18:41:29 +00004882 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004883 }
4884
Dale Johannesenace16102009-02-03 19:33:06 +00004885 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V1);
4886 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00004887 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004888}
4889
Evan Chengd880b972008-05-09 21:53:03 +00004890/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00004891///
Owen Andersone50ed302009-08-10 22:56:29 +00004892static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00004893 SDValue SrcOp, SelectionDAG &DAG,
4894 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004895 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004896 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00004897 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00004898 LD = dyn_cast<LoadSDNode>(SrcOp);
4899 if (!LD) {
4900 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
4901 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00004902 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
4903 if ((ExtVT.SimpleTy != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00004904 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
4905 SrcOp.getOperand(0).getOpcode() == ISD::BIT_CONVERT &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00004906 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004907 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00004908 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Dale Johannesenace16102009-02-03 19:33:06 +00004909 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
4910 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
4911 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
4912 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00004913 SrcOp.getOperand(0)
4914 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00004915 }
4916 }
4917 }
4918
Dale Johannesenace16102009-02-03 19:33:06 +00004919 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
4920 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Scott Michelfdc40a02009-02-17 22:15:04 +00004921 DAG.getNode(ISD::BIT_CONVERT, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00004922 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00004923}
4924
Evan Chengace3c172008-07-22 21:13:36 +00004925/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
4926/// shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00004927static SDValue
Nate Begeman9008ca62009-04-27 18:41:29 +00004928LowerVECTOR_SHUFFLE_4wide(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
4929 SDValue V1 = SVOp->getOperand(0);
4930 SDValue V2 = SVOp->getOperand(1);
4931 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00004932 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00004933
Evan Chengace3c172008-07-22 21:13:36 +00004934 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00004935 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00004936 SmallVector<int, 8> Mask1(4U, -1);
4937 SmallVector<int, 8> PermMask;
4938 SVOp->getMask(PermMask);
4939
Evan Chengace3c172008-07-22 21:13:36 +00004940 unsigned NumHi = 0;
4941 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00004942 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004943 int Idx = PermMask[i];
4944 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00004945 Locs[i] = std::make_pair(-1, -1);
4946 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004947 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
4948 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00004949 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00004950 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004951 NumLo++;
4952 } else {
4953 Locs[i] = std::make_pair(1, NumHi);
4954 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00004955 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004956 NumHi++;
4957 }
4958 }
4959 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004960
Evan Chengace3c172008-07-22 21:13:36 +00004961 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004962 // If no more than two elements come from either vector. This can be
4963 // implemented with two shuffles. First shuffle gather the elements.
4964 // The second shuffle, which takes the first shuffle as both of its
4965 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00004966 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004967
Nate Begeman9008ca62009-04-27 18:41:29 +00004968 SmallVector<int, 8> Mask2(4U, -1);
Eric Christopherfd179292009-08-27 18:07:15 +00004969
Evan Chengace3c172008-07-22 21:13:36 +00004970 for (unsigned i = 0; i != 4; ++i) {
4971 if (Locs[i].first == -1)
4972 continue;
4973 else {
4974 unsigned Idx = (i < 2) ? 0 : 4;
4975 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00004976 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004977 }
4978 }
4979
Nate Begeman9008ca62009-04-27 18:41:29 +00004980 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004981 } else if (NumLo == 3 || NumHi == 3) {
4982 // Otherwise, we must have three elements from one vector, call it X, and
4983 // one element from the other, call it Y. First, use a shufps to build an
4984 // intermediate vector with the one element from Y and the element from X
4985 // that will be in the same half in the final destination (the indexes don't
4986 // matter). Then, use a shufps to build the final vector, taking the half
4987 // containing the element from Y from the intermediate, and the other half
4988 // from X.
4989 if (NumHi == 3) {
4990 // Normalize it so the 3 elements come from V1.
Nate Begeman9008ca62009-04-27 18:41:29 +00004991 CommuteVectorShuffleMask(PermMask, VT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004992 std::swap(V1, V2);
4993 }
4994
4995 // Find the element from V2.
4996 unsigned HiIndex;
4997 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004998 int Val = PermMask[HiIndex];
4999 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005000 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005001 if (Val >= 4)
5002 break;
5003 }
5004
Nate Begeman9008ca62009-04-27 18:41:29 +00005005 Mask1[0] = PermMask[HiIndex];
5006 Mask1[1] = -1;
5007 Mask1[2] = PermMask[HiIndex^1];
5008 Mask1[3] = -1;
5009 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005010
5011 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005012 Mask1[0] = PermMask[0];
5013 Mask1[1] = PermMask[1];
5014 Mask1[2] = HiIndex & 1 ? 6 : 4;
5015 Mask1[3] = HiIndex & 1 ? 4 : 6;
5016 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005017 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005018 Mask1[0] = HiIndex & 1 ? 2 : 0;
5019 Mask1[1] = HiIndex & 1 ? 0 : 2;
5020 Mask1[2] = PermMask[2];
5021 Mask1[3] = PermMask[3];
5022 if (Mask1[2] >= 0)
5023 Mask1[2] += 4;
5024 if (Mask1[3] >= 0)
5025 Mask1[3] += 4;
5026 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005027 }
Evan Chengace3c172008-07-22 21:13:36 +00005028 }
5029
5030 // Break it into (shuffle shuffle_hi, shuffle_lo).
5031 Locs.clear();
Nate Begeman9008ca62009-04-27 18:41:29 +00005032 SmallVector<int,8> LoMask(4U, -1);
5033 SmallVector<int,8> HiMask(4U, -1);
5034
5035 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00005036 unsigned MaskIdx = 0;
5037 unsigned LoIdx = 0;
5038 unsigned HiIdx = 2;
5039 for (unsigned i = 0; i != 4; ++i) {
5040 if (i == 2) {
5041 MaskPtr = &HiMask;
5042 MaskIdx = 1;
5043 LoIdx = 0;
5044 HiIdx = 2;
5045 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005046 int Idx = PermMask[i];
5047 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00005048 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00005049 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00005050 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00005051 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005052 LoIdx++;
5053 } else {
5054 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00005055 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005056 HiIdx++;
5057 }
5058 }
5059
Nate Begeman9008ca62009-04-27 18:41:29 +00005060 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
5061 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
5062 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00005063 for (unsigned i = 0; i != 4; ++i) {
5064 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005065 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00005066 } else {
5067 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00005068 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00005069 }
5070 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005071 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00005072}
5073
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005074static bool MayFoldVectorLoad(SDValue V) {
5075 if (V.hasOneUse() && V.getOpcode() == ISD::BIT_CONVERT)
5076 V = V.getOperand(0);
5077 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
5078 V = V.getOperand(0);
5079 if (MayFoldLoad(V))
5080 return true;
5081 return false;
5082}
5083
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005084// FIXME: the version above should always be used. Since there's
5085// a bug where several vector shuffles can't be folded because the
5086// DAG is not updated during lowering and a node claims to have two
5087// uses while it only has one, use this version, and let isel match
5088// another instruction if the load really happens to have more than
5089// one use. Remove this version after this bug get fixed.
Evan Cheng835580f2010-10-07 20:50:20 +00005090// rdar://8434668, PR8156
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005091static bool RelaxedMayFoldVectorLoad(SDValue V) {
5092 if (V.hasOneUse() && V.getOpcode() == ISD::BIT_CONVERT)
5093 V = V.getOperand(0);
5094 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
5095 V = V.getOperand(0);
5096 if (ISD::isNormalLoad(V.getNode()))
5097 return true;
5098 return false;
5099}
5100
5101/// CanFoldShuffleIntoVExtract - Check if the current shuffle is used by
5102/// a vector extract, and if both can be later optimized into a single load.
5103/// This is done in visitEXTRACT_VECTOR_ELT and the conditions are checked
5104/// here because otherwise a target specific shuffle node is going to be
5105/// emitted for this shuffle, and the optimization not done.
5106/// FIXME: This is probably not the best approach, but fix the problem
5107/// until the right path is decided.
5108static
5109bool CanXFormVExtractWithShuffleIntoLoad(SDValue V, SelectionDAG &DAG,
5110 const TargetLowering &TLI) {
5111 EVT VT = V.getValueType();
5112 ShuffleVectorSDNode *SVOp = dyn_cast<ShuffleVectorSDNode>(V);
5113
5114 // Be sure that the vector shuffle is present in a pattern like this:
5115 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), c) -> (f32 load $addr)
5116 if (!V.hasOneUse())
5117 return false;
5118
5119 SDNode *N = *V.getNode()->use_begin();
5120 if (N->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
5121 return false;
5122
5123 SDValue EltNo = N->getOperand(1);
5124 if (!isa<ConstantSDNode>(EltNo))
5125 return false;
5126
5127 // If the bit convert changed the number of elements, it is unsafe
5128 // to examine the mask.
5129 bool HasShuffleIntoBitcast = false;
5130 if (V.getOpcode() == ISD::BIT_CONVERT) {
5131 EVT SrcVT = V.getOperand(0).getValueType();
5132 if (SrcVT.getVectorNumElements() != VT.getVectorNumElements())
5133 return false;
5134 V = V.getOperand(0);
5135 HasShuffleIntoBitcast = true;
5136 }
5137
5138 // Select the input vector, guarding against out of range extract vector.
5139 unsigned NumElems = VT.getVectorNumElements();
5140 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
5141 int Idx = (Elt > NumElems) ? -1 : SVOp->getMaskElt(Elt);
5142 V = (Idx < (int)NumElems) ? V.getOperand(0) : V.getOperand(1);
5143
5144 // Skip one more bit_convert if necessary
5145 if (V.getOpcode() == ISD::BIT_CONVERT)
5146 V = V.getOperand(0);
5147
5148 if (ISD::isNormalLoad(V.getNode())) {
5149 // Is the original load suitable?
5150 LoadSDNode *LN0 = cast<LoadSDNode>(V);
5151
5152 // FIXME: avoid the multi-use bug that is preventing lots of
5153 // of foldings to be detected, this is still wrong of course, but
5154 // give the temporary desired behavior, and if it happens that
5155 // the load has real more uses, during isel it will not fold, and
5156 // will generate poor code.
5157 if (!LN0 || LN0->isVolatile()) // || !LN0->hasOneUse()
5158 return false;
5159
5160 if (!HasShuffleIntoBitcast)
5161 return true;
5162
5163 // If there's a bitcast before the shuffle, check if the load type and
5164 // alignment is valid.
5165 unsigned Align = LN0->getAlignment();
5166 unsigned NewAlign =
5167 TLI.getTargetData()->getABITypeAlignment(
5168 VT.getTypeForEVT(*DAG.getContext()));
5169
5170 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
5171 return false;
5172 }
5173
5174 return true;
5175}
5176
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005177static
Evan Cheng835580f2010-10-07 20:50:20 +00005178SDValue getMOVDDup(SDValue &Op, DebugLoc &dl, SDValue V1, SelectionDAG &DAG) {
5179 EVT VT = Op.getValueType();
5180
5181 // Canonizalize to v2f64.
5182 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, V1);
5183 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
5184 getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64,
5185 V1, DAG));
5186}
5187
5188static
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005189SDValue getMOVLowToHigh(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG,
5190 bool HasSSE2) {
5191 SDValue V1 = Op.getOperand(0);
5192 SDValue V2 = Op.getOperand(1);
5193 EVT VT = Op.getValueType();
5194
5195 assert(VT != MVT::v2i64 && "unsupported shuffle type");
5196
5197 if (HasSSE2 && VT == MVT::v2f64)
5198 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
5199
5200 // v4f32 or v4i32
5201 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V2, DAG);
5202}
5203
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00005204static
5205SDValue getMOVHighToLow(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG) {
5206 SDValue V1 = Op.getOperand(0);
5207 SDValue V2 = Op.getOperand(1);
5208 EVT VT = Op.getValueType();
5209
5210 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
5211 "unsupported shuffle type");
5212
5213 if (V2.getOpcode() == ISD::UNDEF)
5214 V2 = V1;
5215
5216 // v4i32 or v4f32
5217 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
5218}
5219
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005220static
5221SDValue getMOVLP(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG, bool HasSSE2) {
5222 SDValue V1 = Op.getOperand(0);
5223 SDValue V2 = Op.getOperand(1);
5224 EVT VT = Op.getValueType();
5225 unsigned NumElems = VT.getVectorNumElements();
5226
5227 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
5228 // operand of these instructions is only memory, so check if there's a
5229 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
5230 // same masks.
5231 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005232
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00005233 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005234 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005235 CanFoldLoad = true;
5236
5237 // When V1 is a load, it can be folded later into a store in isel, example:
5238 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
5239 // turns into:
5240 // (MOVLPSmr addr:$src1, VR128:$src2)
5241 // So, recognize this potential and also use MOVLPS or MOVLPD
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005242 if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005243 CanFoldLoad = true;
5244
5245 if (CanFoldLoad) {
5246 if (HasSSE2 && NumElems == 2)
5247 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
5248
5249 if (NumElems == 4)
5250 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
5251 }
5252
5253 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5254 // movl and movlp will both match v2i64, but v2i64 is never matched by
5255 // movl earlier because we make it strict to avoid messing with the movlp load
5256 // folding logic (see the code above getMOVLP call). Match it here then,
5257 // this is horrible, but will stay like this until we move all shuffle
5258 // matching to x86 specific nodes. Note that for the 1st condition all
5259 // types are matched with movsd.
5260 if ((HasSSE2 && NumElems == 2) || !X86::isMOVLMask(SVOp))
5261 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
5262 else if (HasSSE2)
5263 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
5264
5265
5266 assert(VT != MVT::v4i32 && "unsupported shuffle type");
5267
5268 // Invert the operand order and use SHUFPS to match it.
5269 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V2, V1,
5270 X86::getShuffleSHUFImmediate(SVOp), DAG);
5271}
5272
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005273static inline unsigned getUNPCKLOpcode(EVT VT) {
5274 switch(VT.getSimpleVT().SimpleTy) {
5275 case MVT::v4i32: return X86ISD::PUNPCKLDQ;
5276 case MVT::v2i64: return X86ISD::PUNPCKLQDQ;
5277 case MVT::v4f32: return X86ISD::UNPCKLPS;
5278 case MVT::v2f64: return X86ISD::UNPCKLPD;
5279 case MVT::v16i8: return X86ISD::PUNPCKLBW;
5280 case MVT::v8i16: return X86ISD::PUNPCKLWD;
5281 default:
5282 llvm_unreachable("Unknow type for unpckl");
5283 }
5284 return 0;
5285}
5286
5287static inline unsigned getUNPCKHOpcode(EVT VT) {
5288 switch(VT.getSimpleVT().SimpleTy) {
5289 case MVT::v4i32: return X86ISD::PUNPCKHDQ;
5290 case MVT::v2i64: return X86ISD::PUNPCKHQDQ;
5291 case MVT::v4f32: return X86ISD::UNPCKHPS;
5292 case MVT::v2f64: return X86ISD::UNPCKHPD;
5293 case MVT::v16i8: return X86ISD::PUNPCKHBW;
5294 case MVT::v8i16: return X86ISD::PUNPCKHWD;
5295 default:
5296 llvm_unreachable("Unknow type for unpckh");
5297 }
5298 return 0;
5299}
5300
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005301static
5302SDValue NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG,
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005303 const TargetLowering &TLI,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005304 const X86Subtarget *Subtarget) {
5305 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5306 EVT VT = Op.getValueType();
5307 DebugLoc dl = Op.getDebugLoc();
5308 SDValue V1 = Op.getOperand(0);
5309 SDValue V2 = Op.getOperand(1);
5310
5311 if (isZeroShuffle(SVOp))
5312 return getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
5313
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005314 // Handle splat operations
5315 if (SVOp->isSplat()) {
5316 // Special case, this is the only place now where it's
5317 // allowed to return a vector_shuffle operation without
5318 // using a target specific node, because *hopefully* it
5319 // will be optimized away by the dag combiner.
5320 if (VT.getVectorNumElements() <= 4 &&
5321 CanXFormVExtractWithShuffleIntoLoad(Op, DAG, TLI))
5322 return Op;
5323
5324 // Handle splats by matching through known masks
5325 if (VT.getVectorNumElements() <= 4)
5326 return SDValue();
5327
Evan Cheng835580f2010-10-07 20:50:20 +00005328 // Canonicalize all of the remaining to v4f32.
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005329 return PromoteSplat(SVOp, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005330 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005331
5332 // If the shuffle can be profitably rewritten as a narrower shuffle, then
5333 // do it!
5334 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
5335 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5336 if (NewOp.getNode())
5337 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, NewOp);
5338 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
5339 // FIXME: Figure out a cleaner way to do this.
5340 // Try to make use of movq to zero out the top part.
5341 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
5342 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5343 if (NewOp.getNode()) {
5344 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
5345 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
5346 DAG, Subtarget, dl);
5347 }
5348 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
5349 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5350 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
5351 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
5352 DAG, Subtarget, dl);
5353 }
5354 }
5355 return SDValue();
5356}
5357
Dan Gohman475871a2008-07-27 21:46:04 +00005358SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005359X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00005360 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00005361 SDValue V1 = Op.getOperand(0);
5362 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00005363 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005364 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00005365 unsigned NumElems = VT.getVectorNumElements();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005366 bool isMMX = VT.getSizeInBits() == 64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005367 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
5368 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00005369 bool V1IsSplat = false;
5370 bool V2IsSplat = false;
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005371 bool HasSSE2 = Subtarget->hasSSE2() || Subtarget->hasAVX();
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00005372 bool HasSSE3 = Subtarget->hasSSE3() || Subtarget->hasAVX();
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00005373 bool HasSSSE3 = Subtarget->hasSSSE3() || Subtarget->hasAVX();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005374 MachineFunction &MF = DAG.getMachineFunction();
5375 bool OptForSize = MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005376
Dale Johannesen0488fb62010-09-30 23:57:10 +00005377 // Shuffle operations on MMX not supported.
5378 if (isMMX)
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00005379 return Op;
5380
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005381 // Vector shuffle lowering takes 3 steps:
5382 //
5383 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
5384 // narrowing and commutation of operands should be handled.
5385 // 2) Matching of shuffles with known shuffle masks to x86 target specific
5386 // shuffle nodes.
5387 // 3) Rewriting of unmatched masks into new generic shuffle operations,
5388 // so the shuffle can be broken into other shuffles and the legalizer can
5389 // try the lowering again.
5390 //
5391 // The general ideia is that no vector_shuffle operation should be left to
5392 // be matched during isel, all of them must be converted to a target specific
5393 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00005394
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005395 // Normalize the input vectors. Here splats, zeroed vectors, profitable
5396 // narrowing and commutation of operands should be handled. The actual code
5397 // doesn't include all of those, work in progress...
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005398 SDValue NewOp = NormalizeVectorShuffle(Op, DAG, *this, Subtarget);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005399 if (NewOp.getNode())
5400 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00005401
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005402 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
5403 // unpckh_undef). Only use pshufd if speed is more important than size.
5404 if (OptForSize && X86::isUNPCKL_v_undef_Mask(SVOp))
5405 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5406 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V1, DAG);
5407 if (OptForSize && X86::isUNPCKH_v_undef_Mask(SVOp))
5408 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5409 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00005410
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005411 if (X86::isMOVDDUPMask(SVOp) && HasSSE3 && V2IsUndef &&
Dale Johannesen0488fb62010-09-30 23:57:10 +00005412 RelaxedMayFoldVectorLoad(V1))
Evan Cheng835580f2010-10-07 20:50:20 +00005413 return getMOVDDup(Op, dl, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005414
Dale Johannesen0488fb62010-09-30 23:57:10 +00005415 if (X86::isMOVHLPS_v_undef_Mask(SVOp))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005416 return getMOVHighToLow(Op, dl, DAG);
5417
5418 // Use to match splats
5419 if (HasSSE2 && X86::isUNPCKHMask(SVOp) && V2IsUndef &&
5420 (VT == MVT::v2f64 || VT == MVT::v2i64))
5421 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
5422
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005423 if (X86::isPSHUFDMask(SVOp)) {
5424 // The actual implementation will match the mask in the if above and then
5425 // during isel it can match several different instructions, not only pshufd
5426 // as its name says, sad but true, emulate the behavior for now...
5427 if (X86::isMOVDDUPMask(SVOp) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
5428 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
5429
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005430 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
5431
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005432 if (HasSSE2 && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005433 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
5434
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005435 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005436 return getTargetShuffleNode(X86ISD::SHUFPD, dl, VT, V1, V1,
5437 TargetMask, DAG);
5438
5439 if (VT == MVT::v4f32)
5440 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V1, V1,
5441 TargetMask, DAG);
5442 }
Eric Christopherfd179292009-08-27 18:07:15 +00005443
Evan Chengf26ffe92008-05-29 08:22:04 +00005444 // Check if this can be converted into a logical shift.
5445 bool isLeft = false;
5446 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00005447 SDValue ShVal;
Nate Begeman9008ca62009-04-27 18:41:29 +00005448 bool isShift = getSubtarget()->hasSSE2() &&
Evan Chengc3630942009-12-09 21:00:30 +00005449 isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00005450 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00005451 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00005452 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00005453 EVT EltVT = VT.getVectorElementType();
5454 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00005455 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005456 }
Eric Christopherfd179292009-08-27 18:07:15 +00005457
Nate Begeman9008ca62009-04-27 18:41:29 +00005458 if (X86::isMOVLMask(SVOp)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005459 if (V1IsUndef)
5460 return V2;
Gabor Greifba36cb52008-08-28 21:40:38 +00005461 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00005462 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Dale Johannesen0488fb62010-09-30 23:57:10 +00005463 if (!X86::isMOVLPMask(SVOp)) {
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005464 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00005465 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
5466
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005467 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00005468 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
5469 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00005470 }
Eric Christopherfd179292009-08-27 18:07:15 +00005471
Nate Begeman9008ca62009-04-27 18:41:29 +00005472 // FIXME: fold these into legal mask.
Dale Johannesen0488fb62010-09-30 23:57:10 +00005473 if (X86::isMOVLHPSMask(SVOp) && !X86::isUNPCKLMask(SVOp))
5474 return getMOVLowToHigh(Op, dl, DAG, HasSSE2);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005475
Dale Johannesen0488fb62010-09-30 23:57:10 +00005476 if (X86::isMOVHLPSMask(SVOp))
5477 return getMOVHighToLow(Op, dl, DAG);
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00005478
Dale Johannesen0488fb62010-09-30 23:57:10 +00005479 if (X86::isMOVSHDUPMask(SVOp) && HasSSE3 && V2IsUndef && NumElems == 4)
5480 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00005481
Dale Johannesen0488fb62010-09-30 23:57:10 +00005482 if (X86::isMOVSLDUPMask(SVOp) && HasSSE3 && V2IsUndef && NumElems == 4)
5483 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00005484
Dale Johannesen0488fb62010-09-30 23:57:10 +00005485 if (X86::isMOVLPMask(SVOp))
5486 return getMOVLP(Op, dl, DAG, HasSSE2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005487
Nate Begeman9008ca62009-04-27 18:41:29 +00005488 if (ShouldXformToMOVHLPS(SVOp) ||
5489 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
5490 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005491
Evan Chengf26ffe92008-05-29 08:22:04 +00005492 if (isShift) {
5493 // No better options. Use a vshl / vsrl.
Dan Gohman8a55ce42009-09-23 21:02:20 +00005494 EVT EltVT = VT.getVectorElementType();
5495 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00005496 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005497 }
Eric Christopherfd179292009-08-27 18:07:15 +00005498
Evan Cheng9eca5e82006-10-25 21:49:50 +00005499 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00005500 // FIXME: This should also accept a bitcast of a splat? Be careful, not
5501 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00005502 V1IsSplat = isSplatVector(V1.getNode());
5503 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00005504
Chris Lattner8a594482007-11-25 00:24:49 +00005505 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00005506 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005507 Op = CommuteVectorShuffle(SVOp, DAG);
5508 SVOp = cast<ShuffleVectorSDNode>(Op);
5509 V1 = SVOp->getOperand(0);
5510 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00005511 std::swap(V1IsSplat, V2IsSplat);
5512 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00005513 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00005514 }
5515
Nate Begeman9008ca62009-04-27 18:41:29 +00005516 if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) {
5517 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00005518 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00005519 return V1;
5520 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
5521 // the instruction selector will not match, so get a canonical MOVL with
5522 // swapped operands to undo the commute.
5523 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00005524 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005525
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005526 if (X86::isUNPCKLMask(SVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005527 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005528
5529 if (X86::isUNPCKHMask(SVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005530 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V2, DAG);
Evan Chenge1113032006-10-04 18:33:38 +00005531
Evan Cheng9bbbb982006-10-25 20:48:19 +00005532 if (V2IsSplat) {
5533 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005534 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00005535 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00005536 SDValue NewMask = NormalizeMask(SVOp, DAG);
5537 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
5538 if (NSVOp != SVOp) {
5539 if (X86::isUNPCKLMask(NSVOp, true)) {
5540 return NewMask;
5541 } else if (X86::isUNPCKHMask(NSVOp, true)) {
5542 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005543 }
5544 }
5545 }
5546
Evan Cheng9eca5e82006-10-25 21:49:50 +00005547 if (Commuted) {
5548 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00005549 // FIXME: this seems wrong.
5550 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
5551 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005552
5553 if (X86::isUNPCKLMask(NewSVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005554 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V2, V1, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005555
5556 if (X86::isUNPCKHMask(NewSVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005557 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V2, V1, DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00005558 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005559
Nate Begeman9008ca62009-04-27 18:41:29 +00005560 // Normalize the node to match x86 shuffle ops if needed
Dale Johannesen0488fb62010-09-30 23:57:10 +00005561 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp))
Nate Begeman9008ca62009-04-27 18:41:29 +00005562 return CommuteVectorShuffle(SVOp, DAG);
5563
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00005564 // The checks below are all present in isShuffleMaskLegal, but they are
5565 // inlined here right now to enable us to directly emit target specific
5566 // nodes, and remove one by one until they don't return Op anymore.
5567 SmallVector<int, 16> M;
5568 SVOp->getMask(M);
5569
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00005570 if (isPALIGNRMask(M, VT, HasSSSE3))
5571 return getTargetShuffleNode(X86ISD::PALIGN, dl, VT, V1, V2,
5572 X86::getShufflePALIGNRImmediate(SVOp),
5573 DAG);
5574
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00005575 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
5576 SVOp->getSplatIndex() == 0 && V2IsUndef) {
5577 if (VT == MVT::v2f64)
5578 return getTargetShuffleNode(X86ISD::UNPCKLPD, dl, VT, V1, V1, DAG);
5579 if (VT == MVT::v2i64)
5580 return getTargetShuffleNode(X86ISD::PUNPCKLQDQ, dl, VT, V1, V1, DAG);
5581 }
5582
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00005583 if (isPSHUFHWMask(M, VT))
5584 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
5585 X86::getShufflePSHUFHWImmediate(SVOp),
5586 DAG);
5587
5588 if (isPSHUFLWMask(M, VT))
5589 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
5590 X86::getShufflePSHUFLWImmediate(SVOp),
5591 DAG);
5592
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00005593 if (isSHUFPMask(M, VT)) {
5594 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
5595 if (VT == MVT::v4f32 || VT == MVT::v4i32)
5596 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V1, V2,
5597 TargetMask, DAG);
5598 if (VT == MVT::v2f64 || VT == MVT::v2i64)
5599 return getTargetShuffleNode(X86ISD::SHUFPD, dl, VT, V1, V2,
5600 TargetMask, DAG);
5601 }
5602
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005603 if (X86::isUNPCKL_v_undef_Mask(SVOp))
5604 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5605 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V1, DAG);
5606 if (X86::isUNPCKH_v_undef_Mask(SVOp))
5607 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5608 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
5609
Evan Cheng14b32e12007-12-11 01:46:18 +00005610 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
Owen Anderson825b72b2009-08-11 20:47:22 +00005611 if (VT == MVT::v8i16) {
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005612 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00005613 if (NewOp.getNode())
Evan Cheng14b32e12007-12-11 01:46:18 +00005614 return NewOp;
5615 }
5616
Owen Anderson825b72b2009-08-11 20:47:22 +00005617 if (VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005618 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005619 if (NewOp.getNode())
5620 return NewOp;
5621 }
Eric Christopherfd179292009-08-27 18:07:15 +00005622
Dale Johannesen0488fb62010-09-30 23:57:10 +00005623 // Handle all 4 wide cases with a number of shuffles.
5624 if (NumElems == 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00005625 return LowerVECTOR_SHUFFLE_4wide(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005626
Dan Gohman475871a2008-07-27 21:46:04 +00005627 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005628}
5629
Dan Gohman475871a2008-07-27 21:46:04 +00005630SDValue
5631X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00005632 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005633 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005634 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005635 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005636 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005637 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00005638 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005639 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00005640 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005641 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00005642 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
5643 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
5644 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005645 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
5646 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Dale Johannesenace16102009-02-03 19:33:06 +00005647 DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005648 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00005649 Op.getOperand(0)),
5650 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00005651 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005652 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00005653 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005654 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00005655 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Owen Anderson825b72b2009-08-11 20:47:22 +00005656 } else if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00005657 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
5658 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00005659 // result has a single use which is a store or a bitcast to i32. And in
5660 // the case of a store, it's not worth it if the index is a constant 0,
5661 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00005662 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00005663 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00005664 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00005665 if ((User->getOpcode() != ISD::STORE ||
5666 (isa<ConstantSDNode>(Op.getOperand(1)) &&
5667 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Dan Gohman171c11e2008-04-16 02:32:24 +00005668 (User->getOpcode() != ISD::BIT_CONVERT ||
Owen Anderson825b72b2009-08-11 20:47:22 +00005669 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00005670 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00005671 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
5672 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00005673 Op.getOperand(0)),
5674 Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00005675 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Extract);
5676 } else if (VT == MVT::i32) {
Mon P Wangf0fcdd82009-01-15 21:10:20 +00005677 // ExtractPS works with constant index.
5678 if (isa<ConstantSDNode>(Op.getOperand(1)))
5679 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00005680 }
Dan Gohman475871a2008-07-27 21:46:04 +00005681 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00005682}
5683
5684
Dan Gohman475871a2008-07-27 21:46:04 +00005685SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005686X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
5687 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005688 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00005689 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005690
Evan Cheng62a3f152008-03-24 21:52:23 +00005691 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005692 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00005693 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00005694 return Res;
5695 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00005696
Owen Andersone50ed302009-08-10 22:56:29 +00005697 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005698 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005699 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005700 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00005701 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005702 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00005703 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005704 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
5705 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Scott Michelfdc40a02009-02-17 22:15:04 +00005706 DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005707 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00005708 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005709 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00005710 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00005711 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00005712 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00005713 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00005714 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00005715 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005716 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005717 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005718 if (Idx == 0)
5719 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00005720
Evan Cheng0db9fe62006-04-25 20:13:52 +00005721 // SHUFPS the element to the lowest double word, then movss.
Nate Begeman9008ca62009-04-27 18:41:29 +00005722 int Mask[4] = { Idx, -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00005723 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00005724 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00005725 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00005726 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00005727 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00005728 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00005729 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
5730 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
5731 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005732 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005733 if (Idx == 0)
5734 return Op;
5735
5736 // UNPCKHPD the element to the lowest double word, then movsd.
5737 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
5738 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00005739 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00005740 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00005741 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00005742 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00005743 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00005744 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005745 }
5746
Dan Gohman475871a2008-07-27 21:46:04 +00005747 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005748}
5749
Dan Gohman475871a2008-07-27 21:46:04 +00005750SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005751X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op,
5752 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005753 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00005754 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005755 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00005756
Dan Gohman475871a2008-07-27 21:46:04 +00005757 SDValue N0 = Op.getOperand(0);
5758 SDValue N1 = Op.getOperand(1);
5759 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00005760
Dan Gohman8a55ce42009-09-23 21:02:20 +00005761 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00005762 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00005763 unsigned Opc;
5764 if (VT == MVT::v8i16)
5765 Opc = X86ISD::PINSRW;
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00005766 else if (VT == MVT::v16i8)
5767 Opc = X86ISD::PINSRB;
5768 else
5769 Opc = X86ISD::PINSRB;
5770
Nate Begeman14d12ca2008-02-11 04:19:36 +00005771 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
5772 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00005773 if (N1.getValueType() != MVT::i32)
5774 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
5775 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005776 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00005777 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00005778 } else if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00005779 // Bits [7:6] of the constant are the source select. This will always be
5780 // zero here. The DAG Combiner may combine an extract_elt index into these
5781 // bits. For example (insert (extract, 3), 2) could be matched by putting
5782 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00005783 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00005784 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00005785 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00005786 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005787 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00005788 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00005789 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00005790 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00005791 } else if (EltVT == MVT::i32 && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00005792 // PINSR* works with constant index.
5793 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00005794 }
Dan Gohman475871a2008-07-27 21:46:04 +00005795 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00005796}
5797
Dan Gohman475871a2008-07-27 21:46:04 +00005798SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005799X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005800 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00005801 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00005802
5803 if (Subtarget->hasSSE41())
5804 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
5805
Dan Gohman8a55ce42009-09-23 21:02:20 +00005806 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00005807 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00005808
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005809 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00005810 SDValue N0 = Op.getOperand(0);
5811 SDValue N1 = Op.getOperand(1);
5812 SDValue N2 = Op.getOperand(2);
Evan Cheng794405e2007-12-12 07:55:34 +00005813
Dan Gohman8a55ce42009-09-23 21:02:20 +00005814 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00005815 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
5816 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00005817 if (N1.getValueType() != MVT::i32)
5818 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
5819 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005820 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesen0488fb62010-09-30 23:57:10 +00005821 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005822 }
Dan Gohman475871a2008-07-27 21:46:04 +00005823 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005824}
5825
Dan Gohman475871a2008-07-27 21:46:04 +00005826SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005827X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005828 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00005829
Chris Lattnerf172ecd2010-07-04 23:07:25 +00005830 if (Op.getValueType() == MVT::v1i64 &&
5831 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00005832 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00005833
Owen Anderson825b72b2009-08-11 20:47:22 +00005834 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Dale Johannesen0488fb62010-09-30 23:57:10 +00005835 assert(Op.getValueType().getSimpleVT().getSizeInBits() == 128 &&
5836 "Expected an SSE type!");
Dale Johannesenace16102009-02-03 19:33:06 +00005837 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(),
Dale Johannesen0488fb62010-09-30 23:57:10 +00005838 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005839}
5840
Bill Wendling056292f2008-09-16 21:48:12 +00005841// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
5842// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
5843// one of the above mentioned nodes. It has to be wrapped because otherwise
5844// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
5845// be used to form addressing mode. These wrapped nodes will be selected
5846// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00005847SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005848X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005849 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00005850
Chris Lattner41621a22009-06-26 19:22:52 +00005851 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
5852 // global base reg.
5853 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00005854 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005855 CodeModel::Model M = getTargetMachine().getCodeModel();
5856
Chris Lattner4f066492009-07-11 20:29:19 +00005857 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005858 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00005859 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00005860 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005861 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00005862 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005863 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00005864
Evan Cheng1606e8e2009-03-13 07:51:59 +00005865 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00005866 CP->getAlignment(),
5867 CP->getOffset(), OpFlag);
5868 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00005869 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00005870 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00005871 if (OpFlag) {
5872 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00005873 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00005874 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00005875 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005876 }
5877
5878 return Result;
5879}
5880
Dan Gohmand858e902010-04-17 15:26:15 +00005881SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00005882 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00005883
Chris Lattner18c59872009-06-27 04:16:01 +00005884 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
5885 // global base reg.
5886 unsigned char OpFlag = 0;
5887 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005888 CodeModel::Model M = getTargetMachine().getCodeModel();
5889
Chris Lattner4f066492009-07-11 20:29:19 +00005890 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005891 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00005892 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00005893 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005894 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00005895 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005896 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00005897
Chris Lattner18c59872009-06-27 04:16:01 +00005898 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
5899 OpFlag);
5900 DebugLoc DL = JT->getDebugLoc();
5901 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00005902
Chris Lattner18c59872009-06-27 04:16:01 +00005903 // With PIC, the address is actually $g + Offset.
5904 if (OpFlag) {
5905 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
5906 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00005907 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00005908 Result);
5909 }
Eric Christopherfd179292009-08-27 18:07:15 +00005910
Chris Lattner18c59872009-06-27 04:16:01 +00005911 return Result;
5912}
5913
5914SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005915X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00005916 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00005917
Chris Lattner18c59872009-06-27 04:16:01 +00005918 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
5919 // global base reg.
5920 unsigned char OpFlag = 0;
5921 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005922 CodeModel::Model M = getTargetMachine().getCodeModel();
5923
Chris Lattner4f066492009-07-11 20:29:19 +00005924 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005925 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00005926 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00005927 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005928 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00005929 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005930 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00005931
Chris Lattner18c59872009-06-27 04:16:01 +00005932 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00005933
Chris Lattner18c59872009-06-27 04:16:01 +00005934 DebugLoc DL = Op.getDebugLoc();
5935 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00005936
5937
Chris Lattner18c59872009-06-27 04:16:01 +00005938 // With PIC, the address is actually $g + Offset.
5939 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00005940 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00005941 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
5942 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00005943 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00005944 Result);
5945 }
Eric Christopherfd179292009-08-27 18:07:15 +00005946
Chris Lattner18c59872009-06-27 04:16:01 +00005947 return Result;
5948}
5949
Dan Gohman475871a2008-07-27 21:46:04 +00005950SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005951X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00005952 // Create the TargetBlockAddressAddress node.
5953 unsigned char OpFlags =
5954 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00005955 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00005956 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Dan Gohman29cbade2009-11-20 23:18:13 +00005957 DebugLoc dl = Op.getDebugLoc();
5958 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(),
5959 /*isTarget=*/true, OpFlags);
5960
Dan Gohmanf705adb2009-10-30 01:28:02 +00005961 if (Subtarget->isPICStyleRIPRel() &&
5962 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00005963 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
5964 else
5965 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00005966
Dan Gohman29cbade2009-11-20 23:18:13 +00005967 // With PIC, the address is actually $g + Offset.
5968 if (isGlobalRelativeToPICBase(OpFlags)) {
5969 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
5970 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
5971 Result);
5972 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00005973
5974 return Result;
5975}
5976
5977SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00005978X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00005979 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00005980 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00005981 // Create the TargetGlobalAddress node, folding in the constant
5982 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00005983 unsigned char OpFlags =
5984 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005985 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00005986 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005987 if (OpFlags == X86II::MO_NO_FLAG &&
5988 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00005989 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00005990 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00005991 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00005992 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00005993 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00005994 }
Eric Christopherfd179292009-08-27 18:07:15 +00005995
Chris Lattner4f066492009-07-11 20:29:19 +00005996 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005997 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00005998 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
5999 else
6000 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00006001
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006002 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00006003 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00006004 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
6005 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006006 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006007 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006008
Chris Lattner36c25012009-07-10 07:34:39 +00006009 // For globals that require a load from a stub to get the address, emit the
6010 // load.
6011 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00006012 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00006013 MachinePointerInfo::getGOT(), false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006014
Dan Gohman6520e202008-10-18 02:06:02 +00006015 // If there was a non-zero offset that we didn't fold, create an explicit
6016 // addition for it.
6017 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00006018 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00006019 DAG.getConstant(Offset, getPointerTy()));
6020
Evan Cheng0db9fe62006-04-25 20:13:52 +00006021 return Result;
6022}
6023
Evan Chengda43bcf2008-09-24 00:05:32 +00006024SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006025X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00006026 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00006027 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006028 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00006029}
6030
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006031static SDValue
6032GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00006033 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Chris Lattnerb903bed2009-06-26 21:20:29 +00006034 unsigned char OperandFlags) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006035 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Owen Anderson825b72b2009-08-11 20:47:22 +00006036 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006037 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00006038 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006039 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00006040 GA->getOffset(),
6041 OperandFlags);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006042 if (InFlag) {
6043 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00006044 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006045 } else {
6046 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00006047 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006048 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006049
6050 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00006051 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006052
Rafael Espindola15f1b662009-04-24 12:59:40 +00006053 SDValue Flag = Chain.getValue(1);
6054 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006055}
6056
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006057// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00006058static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006059LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006060 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00006061 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00006062 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
6063 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006064 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006065 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006066 InFlag = Chain.getValue(1);
6067
Chris Lattnerb903bed2009-06-26 21:20:29 +00006068 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006069}
6070
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006071// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00006072static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006073LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006074 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00006075 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
6076 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006077}
6078
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006079// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
6080// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00006081static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006082 const EVT PtrVT, TLSModel::Model model,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00006083 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00006084 DebugLoc dl = GA->getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00006085
Chris Lattnerf93b90c2010-09-22 04:39:11 +00006086 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
6087 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
6088 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00006089
Michael J. Spencerec38de22010-10-10 22:04:20 +00006090 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Chris Lattnerf93b90c2010-09-22 04:39:11 +00006091 DAG.getIntPtrConstant(0),
6092 MachinePointerInfo(Ptr), false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00006093
Chris Lattnerb903bed2009-06-26 21:20:29 +00006094 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00006095 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
6096 // initialexec.
6097 unsigned WrapperKind = X86ISD::Wrapper;
6098 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00006099 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Chris Lattner18c59872009-06-27 04:16:01 +00006100 } else if (is64Bit) {
6101 assert(model == TLSModel::InitialExec);
6102 OperandFlags = X86II::MO_GOTTPOFF;
6103 WrapperKind = X86ISD::WrapperRIP;
6104 } else {
6105 assert(model == TLSModel::InitialExec);
6106 OperandFlags = X86II::MO_INDNTPOFF;
Chris Lattnerb903bed2009-06-26 21:20:29 +00006107 }
Eric Christopherfd179292009-08-27 18:07:15 +00006108
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006109 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
6110 // exec)
Michael J. Spencerec38de22010-10-10 22:04:20 +00006111 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Devang Patel0d881da2010-07-06 22:08:15 +00006112 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00006113 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00006114 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00006115
Rafael Espindola9a580232009-02-27 13:37:18 +00006116 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00006117 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00006118 MachinePointerInfo::getGOT(), false, false, 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00006119
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006120 // The address of the thread local variable is the add of the thread
6121 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00006122 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006123}
6124
Dan Gohman475871a2008-07-27 21:46:04 +00006125SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006126X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00006127
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006128 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00006129 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00006130
Eric Christopher30ef0e52010-06-03 04:07:48 +00006131 if (Subtarget->isTargetELF()) {
6132 // TODO: implement the "local dynamic" model
6133 // TODO: implement the "initial exec"model for pic executables
Michael J. Spencerec38de22010-10-10 22:04:20 +00006134
Eric Christopher30ef0e52010-06-03 04:07:48 +00006135 // If GV is an alias then use the aliasee for determining
6136 // thread-localness.
6137 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
6138 GV = GA->resolveAliasedGlobal(false);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006139
6140 TLSModel::Model model
Eric Christopher30ef0e52010-06-03 04:07:48 +00006141 = getTLSModel(GV, getTargetMachine().getRelocationModel());
Michael J. Spencerec38de22010-10-10 22:04:20 +00006142
Eric Christopher30ef0e52010-06-03 04:07:48 +00006143 switch (model) {
6144 case TLSModel::GeneralDynamic:
6145 case TLSModel::LocalDynamic: // not implemented
6146 if (Subtarget->is64Bit())
6147 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
6148 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Michael J. Spencerec38de22010-10-10 22:04:20 +00006149
Eric Christopher30ef0e52010-06-03 04:07:48 +00006150 case TLSModel::InitialExec:
6151 case TLSModel::LocalExec:
6152 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
6153 Subtarget->is64Bit());
6154 }
6155 } else if (Subtarget->isTargetDarwin()) {
6156 // Darwin only has one model of TLS. Lower to that.
6157 unsigned char OpFlag = 0;
6158 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
6159 X86ISD::WrapperRIP : X86ISD::Wrapper;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006160
Eric Christopher30ef0e52010-06-03 04:07:48 +00006161 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6162 // global base reg.
6163 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
6164 !Subtarget->is64Bit();
6165 if (PIC32)
6166 OpFlag = X86II::MO_TLVP_PIC_BASE;
6167 else
6168 OpFlag = X86II::MO_TLVP;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006169 DebugLoc DL = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00006170 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopher30ef0e52010-06-03 04:07:48 +00006171 getPointerTy(),
6172 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00006173 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006174
Eric Christopher30ef0e52010-06-03 04:07:48 +00006175 // With PIC32, the address is actually $g + Offset.
6176 if (PIC32)
6177 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
6178 DAG.getNode(X86ISD::GlobalBaseReg,
6179 DebugLoc(), getPointerTy()),
6180 Offset);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006181
Eric Christopher30ef0e52010-06-03 04:07:48 +00006182 // Lowering the machine isd will make sure everything is in the right
6183 // location.
6184 SDValue Args[] = { Offset };
6185 SDValue Chain = DAG.getNode(X86ISD::TLSCALL, DL, MVT::Other, Args, 1);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006186
Eric Christopher30ef0e52010-06-03 04:07:48 +00006187 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
6188 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
6189 MFI->setAdjustsStack(true);
Eric Christopherfd179292009-08-27 18:07:15 +00006190
Eric Christopher30ef0e52010-06-03 04:07:48 +00006191 // And our return value (tls address) is in the standard call return value
6192 // location.
6193 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
6194 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy());
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006195 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00006196
Eric Christopher30ef0e52010-06-03 04:07:48 +00006197 assert(false &&
6198 "TLS not implemented for this target.");
Eric Christopherfd179292009-08-27 18:07:15 +00006199
Torok Edwinc23197a2009-07-14 16:55:14 +00006200 llvm_unreachable("Unreachable");
Chris Lattner5867de12009-04-01 22:14:45 +00006201 return SDValue();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006202}
6203
Evan Cheng0db9fe62006-04-25 20:13:52 +00006204
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006205/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michelfdc40a02009-02-17 22:15:04 +00006206/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00006207SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman4c1fa612008-03-03 22:22:09 +00006208 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00006209 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006210 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006211 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006212 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00006213 SDValue ShOpLo = Op.getOperand(0);
6214 SDValue ShOpHi = Op.getOperand(1);
6215 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00006216 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00006217 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00006218 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00006219
Dan Gohman475871a2008-07-27 21:46:04 +00006220 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006221 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00006222 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
6223 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006224 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00006225 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
6226 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006227 }
Evan Chenge3413162006-01-09 18:33:28 +00006228
Owen Anderson825b72b2009-08-11 20:47:22 +00006229 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
6230 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00006231 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00006232 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00006233
Dan Gohman475871a2008-07-27 21:46:04 +00006234 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00006235 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00006236 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
6237 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00006238
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006239 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00006240 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
6241 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006242 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00006243 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
6244 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006245 }
6246
Dan Gohman475871a2008-07-27 21:46:04 +00006247 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00006248 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006249}
Evan Chenga3195e82006-01-12 22:54:21 +00006250
Dan Gohmand858e902010-04-17 15:26:15 +00006251SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
6252 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006253 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00006254
Dale Johannesen0488fb62010-09-30 23:57:10 +00006255 if (SrcVT.isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00006256 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00006257
Owen Anderson825b72b2009-08-11 20:47:22 +00006258 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00006259 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00006260
Eli Friedman36df4992009-05-27 00:47:34 +00006261 // These are really Legal; return the operand so the caller accepts it as
6262 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00006263 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00006264 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00006265 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00006266 Subtarget->is64Bit()) {
6267 return Op;
6268 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006269
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006270 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006271 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006272 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00006273 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006274 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00006275 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00006276 StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006277 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006278 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00006279 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
6280}
Evan Cheng0db9fe62006-04-25 20:13:52 +00006281
Owen Andersone50ed302009-08-10 22:56:29 +00006282SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Michael J. Spencerec38de22010-10-10 22:04:20 +00006283 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00006284 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006285 // Build the FILD
Chris Lattner492a43e2010-09-22 01:28:21 +00006286 DebugLoc DL = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00006287 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00006288 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006289 if (useSSE)
Owen Anderson825b72b2009-08-11 20:47:22 +00006290 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
Chris Lattner5a88b832007-02-25 07:10:00 +00006291 else
Owen Anderson825b72b2009-08-11 20:47:22 +00006292 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006293
Chris Lattner492a43e2010-09-22 01:28:21 +00006294 unsigned ByteSize = SrcVT.getSizeInBits()/8;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006295
Chris Lattner492a43e2010-09-22 01:28:21 +00006296 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
6297 MachineMemOperand *MMO =
6298 DAG.getMachineFunction()
6299 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6300 MachineMemOperand::MOLoad, ByteSize, ByteSize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006301
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006302 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Chris Lattner492a43e2010-09-22 01:28:21 +00006303 SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
6304 X86ISD::FILD, DL,
6305 Tys, Ops, array_lengthof(Ops),
6306 SrcVT, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006307
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006308 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006309 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00006310 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006311
6312 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
6313 // shouldn't be necessary except that RFP cannot be live across
6314 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006315 MachineFunction &MF = DAG.getMachineFunction();
Bob Wilsoneafca4e2010-09-22 17:35:14 +00006316 unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
6317 int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006318 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00006319 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006320 SDValue Ops[] = {
6321 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
6322 };
Chris Lattner492a43e2010-09-22 01:28:21 +00006323 MachineMemOperand *MMO =
6324 DAG.getMachineFunction()
6325 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
Bob Wilsoneafca4e2010-09-22 17:35:14 +00006326 MachineMemOperand::MOStore, SSFISize, SSFISize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006327
Chris Lattner492a43e2010-09-22 01:28:21 +00006328 Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
6329 Ops, array_lengthof(Ops),
6330 Op.getValueType(), MMO);
6331 Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006332 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006333 false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006334 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006335
Evan Cheng0db9fe62006-04-25 20:13:52 +00006336 return Result;
6337}
6338
Bill Wendling8b8a6362009-01-17 03:56:04 +00006339// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00006340SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
6341 SelectionDAG &DAG) const {
Bill Wendling8b8a6362009-01-17 03:56:04 +00006342 // This algorithm is not obvious. Here it is in C code, more or less:
6343 /*
6344 double uint64_to_double( uint32_t hi, uint32_t lo ) {
6345 static const __m128i exp = { 0x4330000045300000ULL, 0 };
6346 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00006347
Bill Wendling8b8a6362009-01-17 03:56:04 +00006348 // Copy ints to xmm registers.
6349 __m128i xh = _mm_cvtsi32_si128( hi );
6350 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00006351
Bill Wendling8b8a6362009-01-17 03:56:04 +00006352 // Combine into low half of a single xmm register.
6353 __m128i x = _mm_unpacklo_epi32( xh, xl );
6354 __m128d d;
6355 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00006356
Bill Wendling8b8a6362009-01-17 03:56:04 +00006357 // Merge in appropriate exponents to give the integer bits the right
6358 // magnitude.
6359 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00006360
Bill Wendling8b8a6362009-01-17 03:56:04 +00006361 // Subtract away the biases to deal with the IEEE-754 double precision
6362 // implicit 1.
6363 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00006364
Bill Wendling8b8a6362009-01-17 03:56:04 +00006365 // All conversions up to here are exact. The correctly rounded result is
6366 // calculated using the current rounding mode using the following
6367 // horizontal add.
6368 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
6369 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
6370 // store doesn't really need to be here (except
6371 // maybe to zero the other double)
6372 return sd;
6373 }
6374 */
Dale Johannesen040225f2008-10-21 23:07:49 +00006375
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006376 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00006377 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00006378
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006379 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00006380 std::vector<Constant*> CV0;
Owen Andersoneed707b2009-07-24 23:12:02 +00006381 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x45300000)));
6382 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x43300000)));
6383 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
6384 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
Owen Andersonaf7ec972009-07-28 21:19:26 +00006385 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006386 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006387
Bill Wendling8b8a6362009-01-17 03:56:04 +00006388 std::vector<Constant*> CV1;
Owen Andersona90b3dc2009-07-15 21:51:10 +00006389 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006390 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
Owen Andersona90b3dc2009-07-15 21:51:10 +00006391 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006392 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Owen Andersonaf7ec972009-07-28 21:19:26 +00006393 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006394 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006395
Owen Anderson825b72b2009-08-11 20:47:22 +00006396 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6397 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00006398 Op.getOperand(0),
6399 DAG.getIntPtrConstant(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006400 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6401 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00006402 Op.getOperand(0),
6403 DAG.getIntPtrConstant(0)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006404 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
6405 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Chris Lattnere8639032010-09-21 06:22:23 +00006406 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006407 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00006408 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
6409 SDValue XR2F = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Unpck2);
6410 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00006411 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006412 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00006413 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006414
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006415 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman9008ca62009-04-27 18:41:29 +00006416 int ShufMask[2] = { 1, -1 };
Owen Anderson825b72b2009-08-11 20:47:22 +00006417 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
6418 DAG.getUNDEF(MVT::v2f64), ShufMask);
6419 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
6420 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006421 DAG.getIntPtrConstant(0));
6422}
6423
Bill Wendling8b8a6362009-01-17 03:56:04 +00006424// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00006425SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
6426 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006427 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00006428 // FP constant to bias correct the final result.
6429 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00006430 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006431
6432 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00006433 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6434 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Bill Wendling8b8a6362009-01-17 03:56:04 +00006435 Op.getOperand(0),
6436 DAG.getIntPtrConstant(0)));
6437
Owen Anderson825b72b2009-08-11 20:47:22 +00006438 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
6439 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00006440 DAG.getIntPtrConstant(0));
6441
6442 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00006443 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
6444 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00006445 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006446 MVT::v2f64, Load)),
6447 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00006448 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006449 MVT::v2f64, Bias)));
6450 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
6451 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00006452 DAG.getIntPtrConstant(0));
6453
6454 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00006455 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006456
6457 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00006458 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00006459
Owen Anderson825b72b2009-08-11 20:47:22 +00006460 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006461 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00006462 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00006463 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006464 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00006465 }
6466
6467 // Handle final rounding.
6468 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00006469}
6470
Dan Gohmand858e902010-04-17 15:26:15 +00006471SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
6472 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00006473 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006474 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00006475
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006476 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00006477 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
6478 // the optimization here.
6479 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00006480 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00006481
Owen Andersone50ed302009-08-10 22:56:29 +00006482 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006483 EVT DstVT = Op.getValueType();
6484 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00006485 return LowerUINT_TO_FP_i64(Op, DAG);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006486 else if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00006487 return LowerUINT_TO_FP_i32(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00006488
6489 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00006490 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006491 if (SrcVT == MVT::i32) {
6492 SDValue WordOff = DAG.getConstant(4, getPointerTy());
6493 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
6494 getPointerTy(), StackSlot, WordOff);
6495 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006496 StackSlot, MachinePointerInfo(),
6497 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006498 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006499 OffsetSlot, MachinePointerInfo(),
6500 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006501 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
6502 return Fild;
6503 }
6504
6505 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
6506 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006507 StackSlot, MachinePointerInfo(),
6508 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006509 // For i64 source, we need to add the appropriate power of 2 if the input
6510 // was negative. This is the same as the optimization in
6511 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
6512 // we must be careful to do the computation in x87 extended precision, not
6513 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00006514 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
6515 MachineMemOperand *MMO =
6516 DAG.getMachineFunction()
6517 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6518 MachineMemOperand::MOLoad, 8, 8);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006519
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006520 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
6521 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Chris Lattner492a43e2010-09-22 01:28:21 +00006522 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, 3,
6523 MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006524
6525 APInt FF(32, 0x5F800000ULL);
6526
6527 // Check whether the sign bit is set.
6528 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
6529 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
6530 ISD::SETLT);
6531
6532 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
6533 SDValue FudgePtr = DAG.getConstantPool(
6534 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
6535 getPointerTy());
6536
6537 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
6538 SDValue Zero = DAG.getIntPtrConstant(0);
6539 SDValue Four = DAG.getIntPtrConstant(4);
6540 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
6541 Zero, Four);
6542 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
6543
6544 // Load the value out, extending it from f32 to f80.
6545 // FIXME: Avoid the extend by constructing the right constant pool?
Evan Chengbcc80172010-07-07 22:15:37 +00006546 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, MVT::f80, dl, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00006547 FudgePtr, MachinePointerInfo::getConstantPool(),
6548 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006549 // Extend everything to 80 bits to force it to be done on x87.
6550 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
6551 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00006552}
6553
Dan Gohman475871a2008-07-27 21:46:04 +00006554std::pair<SDValue,SDValue> X86TargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00006555FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) const {
Chris Lattner07290932010-09-22 01:05:16 +00006556 DebugLoc DL = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00006557
Owen Andersone50ed302009-08-10 22:56:29 +00006558 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00006559
6560 if (!IsSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006561 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
6562 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00006563 }
6564
Owen Anderson825b72b2009-08-11 20:47:22 +00006565 assert(DstTy.getSimpleVT() <= MVT::i64 &&
6566 DstTy.getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00006567 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00006568
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006569 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00006570 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00006571 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00006572 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00006573 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00006574 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00006575 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00006576 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006577
Evan Cheng87c89352007-10-15 20:11:21 +00006578 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
6579 // stack slot.
6580 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00006581 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00006582 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006583 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00006584
Michael J. Spencerec38de22010-10-10 22:04:20 +00006585
6586
Evan Cheng0db9fe62006-04-25 20:13:52 +00006587 unsigned Opc;
Owen Anderson825b72b2009-08-11 20:47:22 +00006588 switch (DstTy.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00006589 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
Owen Anderson825b72b2009-08-11 20:47:22 +00006590 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
6591 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
6592 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006593 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006594
Dan Gohman475871a2008-07-27 21:46:04 +00006595 SDValue Chain = DAG.getEntryNode();
6596 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00006597 EVT TheVT = Op.getOperand(0).getValueType();
6598 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006599 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00006600 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006601 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006602 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00006603 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00006604 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00006605 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00006606 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00006607
Chris Lattner492a43e2010-09-22 01:28:21 +00006608 MachineMemOperand *MMO =
6609 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6610 MachineMemOperand::MOLoad, MemSize, MemSize);
6611 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, 3,
6612 DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006613 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00006614 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006615 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
6616 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00006617
Chris Lattner07290932010-09-22 01:05:16 +00006618 MachineMemOperand *MMO =
6619 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6620 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006621
Evan Cheng0db9fe62006-04-25 20:13:52 +00006622 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00006623 SDValue Ops[] = { Chain, Value, StackSlot };
Chris Lattner07290932010-09-22 01:05:16 +00006624 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
6625 Ops, 3, DstTy, MMO);
Evan Chengd9558e02006-01-06 00:43:03 +00006626
Chris Lattner27a6c732007-11-24 07:07:01 +00006627 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006628}
6629
Dan Gohmand858e902010-04-17 15:26:15 +00006630SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
6631 SelectionDAG &DAG) const {
Dale Johannesen0488fb62010-09-30 23:57:10 +00006632 if (Op.getValueType().isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00006633 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00006634
Eli Friedman948e95a2009-05-23 09:59:16 +00006635 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00006636 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00006637 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
6638 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00006639
Chris Lattner27a6c732007-11-24 07:07:01 +00006640 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006641 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00006642 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00006643}
6644
Dan Gohmand858e902010-04-17 15:26:15 +00006645SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
6646 SelectionDAG &DAG) const {
Eli Friedman948e95a2009-05-23 09:59:16 +00006647 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
6648 SDValue FIST = Vals.first, StackSlot = Vals.second;
6649 assert(FIST.getNode() && "Unexpected failure");
6650
6651 // Load the result.
6652 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00006653 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00006654}
6655
Dan Gohmand858e902010-04-17 15:26:15 +00006656SDValue X86TargetLowering::LowerFABS(SDValue Op,
6657 SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00006658 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006659 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00006660 EVT VT = Op.getValueType();
6661 EVT EltVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00006662 if (VT.isVector())
6663 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006664 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00006665 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006666 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00006667 CV.push_back(C);
6668 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006669 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006670 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00006671 CV.push_back(C);
6672 CV.push_back(C);
6673 CV.push_back(C);
6674 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006675 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00006676 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006677 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006678 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00006679 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006680 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006681 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006682}
6683
Dan Gohmand858e902010-04-17 15:26:15 +00006684SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00006685 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006686 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00006687 EVT VT = Op.getValueType();
6688 EVT EltVT = VT;
Duncan Sandsda9ad382009-09-06 19:29:07 +00006689 if (VT.isVector())
Duncan Sands83ec4b62008-06-06 12:08:01 +00006690 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006691 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00006692 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006693 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00006694 CV.push_back(C);
6695 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006696 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006697 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00006698 CV.push_back(C);
6699 CV.push_back(C);
6700 CV.push_back(C);
6701 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006702 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00006703 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006704 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006705 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00006706 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006707 false, false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006708 if (VT.isVector()) {
Dale Johannesenace16102009-02-03 19:33:06 +00006709 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00006710 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
6711 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00006712 Op.getOperand(0)),
Owen Anderson825b72b2009-08-11 20:47:22 +00006713 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00006714 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00006715 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00006716 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006717}
6718
Dan Gohmand858e902010-04-17 15:26:15 +00006719SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00006720 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00006721 SDValue Op0 = Op.getOperand(0);
6722 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006723 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00006724 EVT VT = Op.getValueType();
6725 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00006726
6727 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00006728 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006729 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00006730 SrcVT = VT;
6731 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00006732 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00006733 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006734 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00006735 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00006736 }
6737
6738 // At this point the operands and the result should have the same
6739 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00006740
Evan Cheng68c47cb2007-01-05 07:55:56 +00006741 // First get the sign bit of second operand.
6742 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00006743 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006744 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
6745 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00006746 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006747 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
6748 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
6749 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
6750 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00006751 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00006752 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006753 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006754 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00006755 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006756 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006757 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00006758
6759 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00006760 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006761 // Op0 is MVT::f32, Op1 is MVT::f64.
6762 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
6763 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
6764 DAG.getConstant(32, MVT::i32));
6765 SignBit = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4f32, SignBit);
6766 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00006767 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00006768 }
6769
Evan Cheng73d6cf12007-01-05 21:37:56 +00006770 // Clear first operand sign bit.
6771 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00006772 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006773 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
6774 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00006775 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006776 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
6777 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
6778 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
6779 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00006780 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00006781 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006782 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006783 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00006784 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006785 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006786 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00006787
6788 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00006789 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00006790}
6791
Dan Gohman076aee32009-03-04 19:44:21 +00006792/// Emit nodes that will be selected as "test Op0,Op0", or something
6793/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00006794SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00006795 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00006796 DebugLoc dl = Op.getDebugLoc();
6797
Dan Gohman31125812009-03-07 01:58:32 +00006798 // CF and OF aren't always set the way we want. Determine which
6799 // of these we need.
6800 bool NeedCF = false;
6801 bool NeedOF = false;
6802 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006803 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00006804 case X86::COND_A: case X86::COND_AE:
6805 case X86::COND_B: case X86::COND_BE:
6806 NeedCF = true;
6807 break;
6808 case X86::COND_G: case X86::COND_GE:
6809 case X86::COND_L: case X86::COND_LE:
6810 case X86::COND_O: case X86::COND_NO:
6811 NeedOF = true;
6812 break;
Dan Gohman31125812009-03-07 01:58:32 +00006813 }
6814
Dan Gohman076aee32009-03-04 19:44:21 +00006815 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00006816 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
6817 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006818 if (Op.getResNo() != 0 || NeedOF || NeedCF)
6819 // Emit a CMP with 0, which is the TEST pattern.
6820 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
6821 DAG.getConstant(0, Op.getValueType()));
6822
6823 unsigned Opcode = 0;
6824 unsigned NumOperands = 0;
6825 switch (Op.getNode()->getOpcode()) {
6826 case ISD::ADD:
6827 // Due to an isel shortcoming, be conservative if this add is likely to be
6828 // selected as part of a load-modify-store instruction. When the root node
6829 // in a match is a store, isel doesn't know how to remap non-chain non-flag
6830 // uses of other nodes in the match, such as the ADD in this case. This
6831 // leads to the ADD being left around and reselected, with the result being
6832 // two adds in the output. Alas, even if none our users are stores, that
6833 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
6834 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
6835 // climbing the DAG back to the root, and it doesn't seem to be worth the
6836 // effort.
6837 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Dan Gohman076aee32009-03-04 19:44:21 +00006838 UE = Op.getNode()->use_end(); UI != UE; ++UI)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006839 if (UI->getOpcode() != ISD::CopyToReg && UI->getOpcode() != ISD::SETCC)
6840 goto default_case;
6841
6842 if (ConstantSDNode *C =
6843 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
6844 // An add of one will be selected as an INC.
6845 if (C->getAPIntValue() == 1) {
6846 Opcode = X86ISD::INC;
6847 NumOperands = 1;
6848 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00006849 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006850
6851 // An add of negative one (subtract of one) will be selected as a DEC.
6852 if (C->getAPIntValue().isAllOnesValue()) {
6853 Opcode = X86ISD::DEC;
6854 NumOperands = 1;
6855 break;
6856 }
Dan Gohman076aee32009-03-04 19:44:21 +00006857 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006858
6859 // Otherwise use a regular EFLAGS-setting add.
6860 Opcode = X86ISD::ADD;
6861 NumOperands = 2;
6862 break;
6863 case ISD::AND: {
6864 // If the primary and result isn't used, don't bother using X86ISD::AND,
6865 // because a TEST instruction will be better.
6866 bool NonFlagUse = false;
6867 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
6868 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
6869 SDNode *User = *UI;
6870 unsigned UOpNo = UI.getOperandNo();
6871 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
6872 // Look pass truncate.
6873 UOpNo = User->use_begin().getOperandNo();
6874 User = *User->use_begin();
6875 }
6876
6877 if (User->getOpcode() != ISD::BRCOND &&
6878 User->getOpcode() != ISD::SETCC &&
6879 (User->getOpcode() != ISD::SELECT || UOpNo != 0)) {
6880 NonFlagUse = true;
6881 break;
6882 }
Dan Gohman076aee32009-03-04 19:44:21 +00006883 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006884
6885 if (!NonFlagUse)
6886 break;
6887 }
6888 // FALL THROUGH
6889 case ISD::SUB:
6890 case ISD::OR:
6891 case ISD::XOR:
6892 // Due to the ISEL shortcoming noted above, be conservative if this op is
6893 // likely to be selected as part of a load-modify-store instruction.
6894 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
6895 UE = Op.getNode()->use_end(); UI != UE; ++UI)
6896 if (UI->getOpcode() == ISD::STORE)
6897 goto default_case;
6898
6899 // Otherwise use a regular EFLAGS-setting instruction.
6900 switch (Op.getNode()->getOpcode()) {
6901 default: llvm_unreachable("unexpected operator!");
6902 case ISD::SUB: Opcode = X86ISD::SUB; break;
6903 case ISD::OR: Opcode = X86ISD::OR; break;
6904 case ISD::XOR: Opcode = X86ISD::XOR; break;
6905 case ISD::AND: Opcode = X86ISD::AND; break;
6906 }
6907
6908 NumOperands = 2;
6909 break;
6910 case X86ISD::ADD:
6911 case X86ISD::SUB:
6912 case X86ISD::INC:
6913 case X86ISD::DEC:
6914 case X86ISD::OR:
6915 case X86ISD::XOR:
6916 case X86ISD::AND:
6917 return SDValue(Op.getNode(), 1);
6918 default:
6919 default_case:
6920 break;
Dan Gohman076aee32009-03-04 19:44:21 +00006921 }
6922
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006923 if (Opcode == 0)
6924 // Emit a CMP with 0, which is the TEST pattern.
6925 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
6926 DAG.getConstant(0, Op.getValueType()));
6927
6928 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
6929 SmallVector<SDValue, 4> Ops;
6930 for (unsigned i = 0; i != NumOperands; ++i)
6931 Ops.push_back(Op.getOperand(i));
6932
6933 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
6934 DAG.ReplaceAllUsesWith(Op, New);
6935 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00006936}
6937
6938/// Emit nodes that will be selected as "cmp Op0,Op1", or something
6939/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00006940SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00006941 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00006942 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
6943 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00006944 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00006945
6946 DebugLoc dl = Op0.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00006947 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00006948}
6949
Evan Chengd40d03e2010-01-06 19:38:29 +00006950/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
6951/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00006952SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
6953 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00006954 SDValue Op0 = And.getOperand(0);
6955 SDValue Op1 = And.getOperand(1);
6956 if (Op0.getOpcode() == ISD::TRUNCATE)
6957 Op0 = Op0.getOperand(0);
6958 if (Op1.getOpcode() == ISD::TRUNCATE)
6959 Op1 = Op1.getOperand(0);
6960
Evan Chengd40d03e2010-01-06 19:38:29 +00006961 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00006962 if (Op1.getOpcode() == ISD::SHL)
6963 std::swap(Op0, Op1);
6964 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00006965 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
6966 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00006967 // If we looked past a truncate, check that it's only truncating away
6968 // known zeros.
6969 unsigned BitWidth = Op0.getValueSizeInBits();
6970 unsigned AndBitWidth = And.getValueSizeInBits();
6971 if (BitWidth > AndBitWidth) {
6972 APInt Mask = APInt::getAllOnesValue(BitWidth), Zeros, Ones;
6973 DAG.ComputeMaskedBits(Op0, Mask, Zeros, Ones);
6974 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
6975 return SDValue();
6976 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00006977 LHS = Op1;
6978 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00006979 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00006980 } else if (Op1.getOpcode() == ISD::Constant) {
6981 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
6982 SDValue AndLHS = Op0;
Evan Chengd40d03e2010-01-06 19:38:29 +00006983 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
6984 LHS = AndLHS.getOperand(0);
6985 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00006986 }
Evan Chengd40d03e2010-01-06 19:38:29 +00006987 }
Evan Cheng0488db92007-09-25 01:57:46 +00006988
Evan Chengd40d03e2010-01-06 19:38:29 +00006989 if (LHS.getNode()) {
Evan Chenge5b51ac2010-04-17 06:13:15 +00006990 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00006991 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00006992 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00006993 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00006994 // Also promote i16 to i32 for performance / code size reason.
6995 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00006996 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00006997 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00006998
Evan Chengd40d03e2010-01-06 19:38:29 +00006999 // If the operand types disagree, extend the shift amount to match. Since
7000 // BT ignores high bits (like shifts) we can use anyextend.
7001 if (LHS.getValueType() != RHS.getValueType())
7002 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00007003
Evan Chengd40d03e2010-01-06 19:38:29 +00007004 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
7005 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
7006 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
7007 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00007008 }
7009
Evan Cheng54de3ea2010-01-05 06:52:31 +00007010 return SDValue();
7011}
7012
Dan Gohmand858e902010-04-17 15:26:15 +00007013SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng54de3ea2010-01-05 06:52:31 +00007014 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
7015 SDValue Op0 = Op.getOperand(0);
7016 SDValue Op1 = Op.getOperand(1);
7017 DebugLoc dl = Op.getDebugLoc();
7018 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
7019
7020 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00007021 // Lower (X & (1 << N)) == 0 to BT(X, N).
7022 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
7023 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
7024 if (Op0.getOpcode() == ISD::AND &&
7025 Op0.hasOneUse() &&
7026 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane368b462010-06-18 14:22:04 +00007027 cast<ConstantSDNode>(Op1)->isNullValue() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00007028 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
7029 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
7030 if (NewSetCC.getNode())
7031 return NewSetCC;
7032 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00007033
Evan Cheng2c755ba2010-02-27 07:36:59 +00007034 // Look for "(setcc) == / != 1" to avoid unncessary setcc.
7035 if (Op0.getOpcode() == X86ISD::SETCC &&
7036 Op1.getOpcode() == ISD::Constant &&
7037 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
7038 cast<ConstantSDNode>(Op1)->isNullValue()) &&
7039 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
7040 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
7041 bool Invert = (CC == ISD::SETNE) ^
7042 cast<ConstantSDNode>(Op1)->isNullValue();
7043 if (Invert)
7044 CCode = X86::GetOppositeBranchCondition(CCode);
7045 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
7046 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
7047 }
7048
Evan Chenge5b51ac2010-04-17 06:13:15 +00007049 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00007050 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00007051 if (X86CC == X86::COND_INVALID)
7052 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00007053
Evan Cheng552f09a2010-04-26 19:06:11 +00007054 SDValue Cond = EmitCmp(Op0, Op1, X86CC, DAG);
Evan Chengad9c0a32009-12-15 00:53:42 +00007055
7056 // Use sbb x, x to materialize carry bit into a GPR.
Evan Cheng2e489c42009-12-16 00:53:11 +00007057 if (X86CC == X86::COND_B)
Evan Chengad9c0a32009-12-15 00:53:42 +00007058 return DAG.getNode(ISD::AND, dl, MVT::i8,
7059 DAG.getNode(X86ISD::SETCC_CARRY, dl, MVT::i8,
7060 DAG.getConstant(X86CC, MVT::i8), Cond),
7061 DAG.getConstant(1, MVT::i8));
Evan Chengad9c0a32009-12-15 00:53:42 +00007062
Owen Anderson825b72b2009-08-11 20:47:22 +00007063 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
7064 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00007065}
7066
Dan Gohmand858e902010-04-17 15:26:15 +00007067SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00007068 SDValue Cond;
7069 SDValue Op0 = Op.getOperand(0);
7070 SDValue Op1 = Op.getOperand(1);
7071 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00007072 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00007073 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
7074 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007075 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00007076
7077 if (isFP) {
7078 unsigned SSECC = 8;
Owen Andersone50ed302009-08-10 22:56:29 +00007079 EVT VT0 = Op0.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00007080 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
7081 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00007082 bool Swap = false;
7083
7084 switch (SetCCOpcode) {
7085 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00007086 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00007087 case ISD::SETEQ: SSECC = 0; break;
Scott Michelfdc40a02009-02-17 22:15:04 +00007088 case ISD::SETOGT:
Nate Begeman30a0de92008-07-17 16:51:19 +00007089 case ISD::SETGT: Swap = true; // Fallthrough
7090 case ISD::SETLT:
7091 case ISD::SETOLT: SSECC = 1; break;
7092 case ISD::SETOGE:
7093 case ISD::SETGE: Swap = true; // Fallthrough
7094 case ISD::SETLE:
7095 case ISD::SETOLE: SSECC = 2; break;
7096 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00007097 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00007098 case ISD::SETNE: SSECC = 4; break;
7099 case ISD::SETULE: Swap = true;
7100 case ISD::SETUGE: SSECC = 5; break;
7101 case ISD::SETULT: Swap = true;
7102 case ISD::SETUGT: SSECC = 6; break;
7103 case ISD::SETO: SSECC = 7; break;
7104 }
7105 if (Swap)
7106 std::swap(Op0, Op1);
7107
Nate Begemanfb8ead02008-07-25 19:05:58 +00007108 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00007109 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00007110 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00007111 SDValue UNORD, EQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00007112 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
7113 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00007114 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00007115 }
7116 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00007117 SDValue ORD, NEQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00007118 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
7119 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00007120 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00007121 }
Torok Edwinc23197a2009-07-14 16:55:14 +00007122 llvm_unreachable("Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00007123 }
7124 // Handle all other FP comparisons here.
Owen Anderson825b72b2009-08-11 20:47:22 +00007125 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00007126 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007127
Nate Begeman30a0de92008-07-17 16:51:19 +00007128 // We are handling one of the integer comparisons here. Since SSE only has
7129 // GT and EQ comparisons for integer, swapping operands and multiple
7130 // operations may be required for some comparisons.
7131 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
7132 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00007133
Owen Anderson825b72b2009-08-11 20:47:22 +00007134 switch (VT.getSimpleVT().SimpleTy) {
Nate Begeman30a0de92008-07-17 16:51:19 +00007135 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007136 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007137 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007138 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
7139 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00007140 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007141
Nate Begeman30a0de92008-07-17 16:51:19 +00007142 switch (SetCCOpcode) {
7143 default: break;
7144 case ISD::SETNE: Invert = true;
7145 case ISD::SETEQ: Opc = EQOpc; break;
7146 case ISD::SETLT: Swap = true;
7147 case ISD::SETGT: Opc = GTOpc; break;
7148 case ISD::SETGE: Swap = true;
7149 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
7150 case ISD::SETULT: Swap = true;
7151 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
7152 case ISD::SETUGE: Swap = true;
7153 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
7154 }
7155 if (Swap)
7156 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007157
Nate Begeman30a0de92008-07-17 16:51:19 +00007158 // Since SSE has no unsigned integer comparisons, we need to flip the sign
7159 // bits of the inputs before performing those operations.
7160 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00007161 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00007162 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
7163 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00007164 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00007165 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
7166 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00007167 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
7168 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00007169 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007170
Dale Johannesenace16102009-02-03 19:33:06 +00007171 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00007172
7173 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00007174 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00007175 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00007176
Nate Begeman30a0de92008-07-17 16:51:19 +00007177 return Result;
7178}
Evan Cheng0488db92007-09-25 01:57:46 +00007179
Evan Cheng370e5342008-12-03 08:38:43 +00007180// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00007181static bool isX86LogicalCmp(SDValue Op) {
7182 unsigned Opc = Op.getNode()->getOpcode();
7183 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
7184 return true;
7185 if (Op.getResNo() == 1 &&
7186 (Opc == X86ISD::ADD ||
7187 Opc == X86ISD::SUB ||
7188 Opc == X86ISD::SMUL ||
7189 Opc == X86ISD::UMUL ||
7190 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00007191 Opc == X86ISD::DEC ||
7192 Opc == X86ISD::OR ||
7193 Opc == X86ISD::XOR ||
7194 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00007195 return true;
7196
7197 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00007198}
7199
Dan Gohmand858e902010-04-17 15:26:15 +00007200SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00007201 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00007202 SDValue Cond = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007203 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00007204 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00007205
Dan Gohman1a492952009-10-20 16:22:37 +00007206 if (Cond.getOpcode() == ISD::SETCC) {
7207 SDValue NewCond = LowerSETCC(Cond, DAG);
7208 if (NewCond.getNode())
7209 Cond = NewCond;
7210 }
Evan Cheng734503b2006-09-11 02:19:56 +00007211
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007212 // (select (x == 0), -1, 0) -> (sign_bit (x - 1))
7213 SDValue Op1 = Op.getOperand(1);
7214 SDValue Op2 = Op.getOperand(2);
7215 if (Cond.getOpcode() == X86ISD::SETCC &&
7216 cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue() == X86::COND_E) {
7217 SDValue Cmp = Cond.getOperand(1);
7218 if (Cmp.getOpcode() == X86ISD::CMP) {
7219 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op1);
7220 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
7221 ConstantSDNode *RHSC =
7222 dyn_cast<ConstantSDNode>(Cmp.getOperand(1).getNode());
7223 if (N1C && N1C->isAllOnesValue() &&
7224 N2C && N2C->isNullValue() &&
7225 RHSC && RHSC->isNullValue()) {
7226 SDValue CmpOp0 = Cmp.getOperand(0);
Chris Lattnerda0688e2010-03-14 18:44:35 +00007227 Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007228 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
7229 return DAG.getNode(X86ISD::SETCC_CARRY, dl, Op.getValueType(),
7230 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
7231 }
7232 }
7233 }
7234
Evan Chengad9c0a32009-12-15 00:53:42 +00007235 // Look pass (and (setcc_carry (cmp ...)), 1).
7236 if (Cond.getOpcode() == ISD::AND &&
7237 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
7238 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00007239 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00007240 Cond = Cond.getOperand(0);
7241 }
7242
Evan Cheng3f41d662007-10-08 22:16:29 +00007243 // If condition flag is set by a X86ISD::CMP, then use it as the condition
7244 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00007245 if (Cond.getOpcode() == X86ISD::SETCC ||
7246 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00007247 CC = Cond.getOperand(0);
7248
Dan Gohman475871a2008-07-27 21:46:04 +00007249 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00007250 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00007251 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00007252
Evan Cheng3f41d662007-10-08 22:16:29 +00007253 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007254 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00007255 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00007256 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00007257
Chris Lattnerd1980a52009-03-12 06:52:53 +00007258 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
7259 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00007260 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00007261 addTest = false;
7262 }
7263 }
7264
7265 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007266 // Look pass the truncate.
7267 if (Cond.getOpcode() == ISD::TRUNCATE)
7268 Cond = Cond.getOperand(0);
7269
7270 // We know the result of AND is compared against zero. Try to match
7271 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00007272 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007273 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
7274 if (NewSetCC.getNode()) {
7275 CC = NewSetCC.getOperand(0);
7276 Cond = NewSetCC.getOperand(1);
7277 addTest = false;
7278 }
7279 }
7280 }
7281
7282 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007283 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00007284 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00007285 }
7286
Evan Cheng0488db92007-09-25 01:57:46 +00007287 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
7288 // condition is true.
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007289 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Flag);
7290 SDValue Ops[] = { Op2, Op1, CC, Cond };
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007291 return DAG.getNode(X86ISD::CMOV, dl, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00007292}
7293
Evan Cheng370e5342008-12-03 08:38:43 +00007294// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
7295// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
7296// from the AND / OR.
7297static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
7298 Opc = Op.getOpcode();
7299 if (Opc != ISD::OR && Opc != ISD::AND)
7300 return false;
7301 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
7302 Op.getOperand(0).hasOneUse() &&
7303 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
7304 Op.getOperand(1).hasOneUse());
7305}
7306
Evan Cheng961d6d42009-02-02 08:19:07 +00007307// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
7308// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00007309static bool isXor1OfSetCC(SDValue Op) {
7310 if (Op.getOpcode() != ISD::XOR)
7311 return false;
7312 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
7313 if (N1C && N1C->getAPIntValue() == 1) {
7314 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
7315 Op.getOperand(0).hasOneUse();
7316 }
7317 return false;
7318}
7319
Dan Gohmand858e902010-04-17 15:26:15 +00007320SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00007321 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00007322 SDValue Chain = Op.getOperand(0);
7323 SDValue Cond = Op.getOperand(1);
7324 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007325 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00007326 SDValue CC;
Evan Cheng734503b2006-09-11 02:19:56 +00007327
Dan Gohman1a492952009-10-20 16:22:37 +00007328 if (Cond.getOpcode() == ISD::SETCC) {
7329 SDValue NewCond = LowerSETCC(Cond, DAG);
7330 if (NewCond.getNode())
7331 Cond = NewCond;
7332 }
Chris Lattnere55484e2008-12-25 05:34:37 +00007333#if 0
7334 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00007335 else if (Cond.getOpcode() == X86ISD::ADD ||
7336 Cond.getOpcode() == X86ISD::SUB ||
7337 Cond.getOpcode() == X86ISD::SMUL ||
7338 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00007339 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00007340#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00007341
Evan Chengad9c0a32009-12-15 00:53:42 +00007342 // Look pass (and (setcc_carry (cmp ...)), 1).
7343 if (Cond.getOpcode() == ISD::AND &&
7344 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
7345 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00007346 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00007347 Cond = Cond.getOperand(0);
7348 }
7349
Evan Cheng3f41d662007-10-08 22:16:29 +00007350 // If condition flag is set by a X86ISD::CMP, then use it as the condition
7351 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00007352 if (Cond.getOpcode() == X86ISD::SETCC ||
7353 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00007354 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007355
Dan Gohman475871a2008-07-27 21:46:04 +00007356 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00007357 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00007358 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00007359 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00007360 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00007361 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00007362 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00007363 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00007364 default: break;
7365 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00007366 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00007367 // These can only come from an arithmetic instruction with overflow,
7368 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00007369 Cond = Cond.getNode()->getOperand(1);
7370 addTest = false;
7371 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00007372 }
Evan Cheng0488db92007-09-25 01:57:46 +00007373 }
Evan Cheng370e5342008-12-03 08:38:43 +00007374 } else {
7375 unsigned CondOpc;
7376 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
7377 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00007378 if (CondOpc == ISD::OR) {
7379 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
7380 // two branches instead of an explicit OR instruction with a
7381 // separate test.
7382 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00007383 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00007384 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007385 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00007386 Chain, Dest, CC, Cmp);
7387 CC = Cond.getOperand(1).getOperand(0);
7388 Cond = Cmp;
7389 addTest = false;
7390 }
7391 } else { // ISD::AND
7392 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
7393 // two branches instead of an explicit AND instruction with a
7394 // separate test. However, we only do this if this block doesn't
7395 // have a fall-through edge, because this requires an explicit
7396 // jmp when the condition is false.
7397 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00007398 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00007399 Op.getNode()->hasOneUse()) {
7400 X86::CondCode CCode =
7401 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
7402 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007403 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00007404 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00007405 // Look for an unconditional branch following this conditional branch.
7406 // We need this because we need to reverse the successors in order
7407 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00007408 if (User->getOpcode() == ISD::BR) {
7409 SDValue FalseBB = User->getOperand(1);
7410 SDNode *NewBR =
7411 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00007412 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00007413 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00007414 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00007415
Dale Johannesene4d209d2009-02-03 20:21:25 +00007416 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00007417 Chain, Dest, CC, Cmp);
7418 X86::CondCode CCode =
7419 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
7420 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007421 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00007422 Cond = Cmp;
7423 addTest = false;
7424 }
7425 }
Dan Gohman279c22e2008-10-21 03:29:32 +00007426 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00007427 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
7428 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
7429 // It should be transformed during dag combiner except when the condition
7430 // is set by a arithmetics with overflow node.
7431 X86::CondCode CCode =
7432 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
7433 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007434 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00007435 Cond = Cond.getOperand(0).getOperand(1);
7436 addTest = false;
Dan Gohman279c22e2008-10-21 03:29:32 +00007437 }
Evan Cheng0488db92007-09-25 01:57:46 +00007438 }
7439
7440 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007441 // Look pass the truncate.
7442 if (Cond.getOpcode() == ISD::TRUNCATE)
7443 Cond = Cond.getOperand(0);
7444
7445 // We know the result of AND is compared against zero. Try to match
7446 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00007447 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007448 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
7449 if (NewSetCC.getNode()) {
7450 CC = NewSetCC.getOperand(0);
7451 Cond = NewSetCC.getOperand(1);
7452 addTest = false;
7453 }
7454 }
7455 }
7456
7457 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007458 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00007459 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00007460 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00007461 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00007462 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00007463}
7464
Anton Korobeynikove060b532007-04-17 19:34:00 +00007465
7466// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
7467// Calls to _alloca is needed to probe the stack when allocating more than 4k
7468// bytes in one go. Touching the stack at 4K increments is necessary to ensure
7469// that the guard pages used by the OS virtual memory manager are allocated in
7470// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00007471SDValue
7472X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00007473 SelectionDAG &DAG) const {
Anton Korobeynikove060b532007-04-17 19:34:00 +00007474 assert(Subtarget->isTargetCygMing() &&
7475 "This should be used only on Cygwin/Mingw targets");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007476 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007477
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007478 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00007479 SDValue Chain = Op.getOperand(0);
7480 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007481 // FIXME: Ensure alignment here
7482
Dan Gohman475871a2008-07-27 21:46:04 +00007483 SDValue Flag;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007484
Owen Anderson825b72b2009-08-11 20:47:22 +00007485 EVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007486
Dale Johannesendd64c412009-02-04 00:33:20 +00007487 Chain = DAG.getCopyToReg(Chain, dl, X86::EAX, Size, Flag);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00007488 Flag = Chain.getValue(1);
7489
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00007490 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00007491
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00007492 Chain = DAG.getNode(X86ISD::MINGW_ALLOCA, dl, NodeTys, Chain, Flag);
7493 Flag = Chain.getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007494
Dale Johannesendd64c412009-02-04 00:33:20 +00007495 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007496
Dan Gohman475871a2008-07-27 21:46:04 +00007497 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Dale Johannesene4d209d2009-02-03 20:21:25 +00007498 return DAG.getMergeValues(Ops1, 2, dl);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007499}
7500
Dan Gohmand858e902010-04-17 15:26:15 +00007501SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00007502 MachineFunction &MF = DAG.getMachineFunction();
7503 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
7504
Dan Gohman69de1932008-02-06 22:27:42 +00007505 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00007506 DebugLoc DL = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00007507
Anton Korobeynikove7beda12010-10-03 22:52:07 +00007508 if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00007509 // vastart just stores the address of the VarArgsFrameIndex slot into the
7510 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +00007511 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
7512 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00007513 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
7514 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007515 }
7516
7517 // __va_list_tag:
7518 // gp_offset (0 - 6 * 8)
7519 // fp_offset (48 - 48 + 8 * 16)
7520 // overflow_arg_area (point to parameters coming in memory).
7521 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00007522 SmallVector<SDValue, 8> MemOps;
7523 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00007524 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00007525 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00007526 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
7527 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007528 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007529 MemOps.push_back(Store);
7530
7531 // Store fp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00007532 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007533 FIN, DAG.getIntPtrConstant(4));
Chris Lattner8026a9d2010-09-21 17:50:43 +00007534 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00007535 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
7536 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007537 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007538 MemOps.push_back(Store);
7539
7540 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +00007541 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007542 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +00007543 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
7544 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00007545 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
7546 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +00007547 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007548 MemOps.push_back(Store);
7549
7550 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +00007551 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007552 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +00007553 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
7554 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00007555 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
7556 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007557 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +00007558 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00007559 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00007560}
7561
Dan Gohmand858e902010-04-17 15:26:15 +00007562SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman9018e832008-05-10 01:26:14 +00007563 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
7564 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_arg!");
Dan Gohman9018e832008-05-10 01:26:14 +00007565
Chris Lattner75361b62010-04-07 22:58:41 +00007566 report_fatal_error("VAArgInst is not yet implemented for x86-64!");
Dan Gohman475871a2008-07-27 21:46:04 +00007567 return SDValue();
Dan Gohman9018e832008-05-10 01:26:14 +00007568}
7569
Dan Gohmand858e902010-04-17 15:26:15 +00007570SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) const {
Evan Chengae642192007-03-02 23:16:35 +00007571 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00007572 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00007573 SDValue Chain = Op.getOperand(0);
7574 SDValue DstPtr = Op.getOperand(1);
7575 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00007576 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
7577 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Chris Lattnere72f2022010-09-21 05:40:29 +00007578 DebugLoc DL = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00007579
Chris Lattnere72f2022010-09-21 05:40:29 +00007580 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +00007581 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Michael J. Spencerec38de22010-10-10 22:04:20 +00007582 false,
Chris Lattnere72f2022010-09-21 05:40:29 +00007583 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +00007584}
7585
Dan Gohman475871a2008-07-27 21:46:04 +00007586SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007587X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007588 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007589 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007590 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00007591 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00007592 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00007593 case Intrinsic::x86_sse_comieq_ss:
7594 case Intrinsic::x86_sse_comilt_ss:
7595 case Intrinsic::x86_sse_comile_ss:
7596 case Intrinsic::x86_sse_comigt_ss:
7597 case Intrinsic::x86_sse_comige_ss:
7598 case Intrinsic::x86_sse_comineq_ss:
7599 case Intrinsic::x86_sse_ucomieq_ss:
7600 case Intrinsic::x86_sse_ucomilt_ss:
7601 case Intrinsic::x86_sse_ucomile_ss:
7602 case Intrinsic::x86_sse_ucomigt_ss:
7603 case Intrinsic::x86_sse_ucomige_ss:
7604 case Intrinsic::x86_sse_ucomineq_ss:
7605 case Intrinsic::x86_sse2_comieq_sd:
7606 case Intrinsic::x86_sse2_comilt_sd:
7607 case Intrinsic::x86_sse2_comile_sd:
7608 case Intrinsic::x86_sse2_comigt_sd:
7609 case Intrinsic::x86_sse2_comige_sd:
7610 case Intrinsic::x86_sse2_comineq_sd:
7611 case Intrinsic::x86_sse2_ucomieq_sd:
7612 case Intrinsic::x86_sse2_ucomilt_sd:
7613 case Intrinsic::x86_sse2_ucomile_sd:
7614 case Intrinsic::x86_sse2_ucomigt_sd:
7615 case Intrinsic::x86_sse2_ucomige_sd:
7616 case Intrinsic::x86_sse2_ucomineq_sd: {
7617 unsigned Opc = 0;
7618 ISD::CondCode CC = ISD::SETCC_INVALID;
7619 switch (IntNo) {
7620 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007621 case Intrinsic::x86_sse_comieq_ss:
7622 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007623 Opc = X86ISD::COMI;
7624 CC = ISD::SETEQ;
7625 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00007626 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007627 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007628 Opc = X86ISD::COMI;
7629 CC = ISD::SETLT;
7630 break;
7631 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007632 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007633 Opc = X86ISD::COMI;
7634 CC = ISD::SETLE;
7635 break;
7636 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007637 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007638 Opc = X86ISD::COMI;
7639 CC = ISD::SETGT;
7640 break;
7641 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007642 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007643 Opc = X86ISD::COMI;
7644 CC = ISD::SETGE;
7645 break;
7646 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007647 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007648 Opc = X86ISD::COMI;
7649 CC = ISD::SETNE;
7650 break;
7651 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007652 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007653 Opc = X86ISD::UCOMI;
7654 CC = ISD::SETEQ;
7655 break;
7656 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007657 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007658 Opc = X86ISD::UCOMI;
7659 CC = ISD::SETLT;
7660 break;
7661 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007662 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007663 Opc = X86ISD::UCOMI;
7664 CC = ISD::SETLE;
7665 break;
7666 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007667 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007668 Opc = X86ISD::UCOMI;
7669 CC = ISD::SETGT;
7670 break;
7671 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007672 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007673 Opc = X86ISD::UCOMI;
7674 CC = ISD::SETGE;
7675 break;
7676 case Intrinsic::x86_sse_ucomineq_ss:
7677 case Intrinsic::x86_sse2_ucomineq_sd:
7678 Opc = X86ISD::UCOMI;
7679 CC = ISD::SETNE;
7680 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00007681 }
Evan Cheng734503b2006-09-11 02:19:56 +00007682
Dan Gohman475871a2008-07-27 21:46:04 +00007683 SDValue LHS = Op.getOperand(1);
7684 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00007685 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00007686 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00007687 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
7688 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
7689 DAG.getConstant(X86CC, MVT::i8), Cond);
7690 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00007691 }
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007692 // ptest and testp intrinsics. The intrinsic these come from are designed to
7693 // return an integer value, not just an instruction so lower it to the ptest
7694 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00007695 case Intrinsic::x86_sse41_ptestz:
7696 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007697 case Intrinsic::x86_sse41_ptestnzc:
7698 case Intrinsic::x86_avx_ptestz_256:
7699 case Intrinsic::x86_avx_ptestc_256:
7700 case Intrinsic::x86_avx_ptestnzc_256:
7701 case Intrinsic::x86_avx_vtestz_ps:
7702 case Intrinsic::x86_avx_vtestc_ps:
7703 case Intrinsic::x86_avx_vtestnzc_ps:
7704 case Intrinsic::x86_avx_vtestz_pd:
7705 case Intrinsic::x86_avx_vtestc_pd:
7706 case Intrinsic::x86_avx_vtestnzc_pd:
7707 case Intrinsic::x86_avx_vtestz_ps_256:
7708 case Intrinsic::x86_avx_vtestc_ps_256:
7709 case Intrinsic::x86_avx_vtestnzc_ps_256:
7710 case Intrinsic::x86_avx_vtestz_pd_256:
7711 case Intrinsic::x86_avx_vtestc_pd_256:
7712 case Intrinsic::x86_avx_vtestnzc_pd_256: {
7713 bool IsTestPacked = false;
Eric Christopher71c67532009-07-29 00:28:05 +00007714 unsigned X86CC = 0;
7715 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00007716 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007717 case Intrinsic::x86_avx_vtestz_ps:
7718 case Intrinsic::x86_avx_vtestz_pd:
7719 case Intrinsic::x86_avx_vtestz_ps_256:
7720 case Intrinsic::x86_avx_vtestz_pd_256:
7721 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00007722 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007723 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +00007724 // ZF = 1
7725 X86CC = X86::COND_E;
7726 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007727 case Intrinsic::x86_avx_vtestc_ps:
7728 case Intrinsic::x86_avx_vtestc_pd:
7729 case Intrinsic::x86_avx_vtestc_ps_256:
7730 case Intrinsic::x86_avx_vtestc_pd_256:
7731 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00007732 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007733 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00007734 // CF = 1
7735 X86CC = X86::COND_B;
7736 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007737 case Intrinsic::x86_avx_vtestnzc_ps:
7738 case Intrinsic::x86_avx_vtestnzc_pd:
7739 case Intrinsic::x86_avx_vtestnzc_ps_256:
7740 case Intrinsic::x86_avx_vtestnzc_pd_256:
7741 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +00007742 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007743 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00007744 // ZF and CF = 0
7745 X86CC = X86::COND_A;
7746 break;
7747 }
Eric Christopherfd179292009-08-27 18:07:15 +00007748
Eric Christopher71c67532009-07-29 00:28:05 +00007749 SDValue LHS = Op.getOperand(1);
7750 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007751 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
7752 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00007753 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
7754 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
7755 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +00007756 }
Evan Cheng5759f972008-05-04 09:15:50 +00007757
7758 // Fix vector shift instructions where the last operand is a non-immediate
7759 // i32 value.
7760 case Intrinsic::x86_sse2_pslli_w:
7761 case Intrinsic::x86_sse2_pslli_d:
7762 case Intrinsic::x86_sse2_pslli_q:
7763 case Intrinsic::x86_sse2_psrli_w:
7764 case Intrinsic::x86_sse2_psrli_d:
7765 case Intrinsic::x86_sse2_psrli_q:
7766 case Intrinsic::x86_sse2_psrai_w:
7767 case Intrinsic::x86_sse2_psrai_d:
7768 case Intrinsic::x86_mmx_pslli_w:
7769 case Intrinsic::x86_mmx_pslli_d:
7770 case Intrinsic::x86_mmx_pslli_q:
7771 case Intrinsic::x86_mmx_psrli_w:
7772 case Intrinsic::x86_mmx_psrli_d:
7773 case Intrinsic::x86_mmx_psrli_q:
7774 case Intrinsic::x86_mmx_psrai_w:
7775 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00007776 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00007777 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00007778 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00007779
7780 unsigned NewIntNo = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00007781 EVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00007782 switch (IntNo) {
7783 case Intrinsic::x86_sse2_pslli_w:
7784 NewIntNo = Intrinsic::x86_sse2_psll_w;
7785 break;
7786 case Intrinsic::x86_sse2_pslli_d:
7787 NewIntNo = Intrinsic::x86_sse2_psll_d;
7788 break;
7789 case Intrinsic::x86_sse2_pslli_q:
7790 NewIntNo = Intrinsic::x86_sse2_psll_q;
7791 break;
7792 case Intrinsic::x86_sse2_psrli_w:
7793 NewIntNo = Intrinsic::x86_sse2_psrl_w;
7794 break;
7795 case Intrinsic::x86_sse2_psrli_d:
7796 NewIntNo = Intrinsic::x86_sse2_psrl_d;
7797 break;
7798 case Intrinsic::x86_sse2_psrli_q:
7799 NewIntNo = Intrinsic::x86_sse2_psrl_q;
7800 break;
7801 case Intrinsic::x86_sse2_psrai_w:
7802 NewIntNo = Intrinsic::x86_sse2_psra_w;
7803 break;
7804 case Intrinsic::x86_sse2_psrai_d:
7805 NewIntNo = Intrinsic::x86_sse2_psra_d;
7806 break;
7807 default: {
Owen Anderson825b72b2009-08-11 20:47:22 +00007808 ShAmtVT = MVT::v2i32;
Evan Cheng5759f972008-05-04 09:15:50 +00007809 switch (IntNo) {
7810 case Intrinsic::x86_mmx_pslli_w:
7811 NewIntNo = Intrinsic::x86_mmx_psll_w;
7812 break;
7813 case Intrinsic::x86_mmx_pslli_d:
7814 NewIntNo = Intrinsic::x86_mmx_psll_d;
7815 break;
7816 case Intrinsic::x86_mmx_pslli_q:
7817 NewIntNo = Intrinsic::x86_mmx_psll_q;
7818 break;
7819 case Intrinsic::x86_mmx_psrli_w:
7820 NewIntNo = Intrinsic::x86_mmx_psrl_w;
7821 break;
7822 case Intrinsic::x86_mmx_psrli_d:
7823 NewIntNo = Intrinsic::x86_mmx_psrl_d;
7824 break;
7825 case Intrinsic::x86_mmx_psrli_q:
7826 NewIntNo = Intrinsic::x86_mmx_psrl_q;
7827 break;
7828 case Intrinsic::x86_mmx_psrai_w:
7829 NewIntNo = Intrinsic::x86_mmx_psra_w;
7830 break;
7831 case Intrinsic::x86_mmx_psrai_d:
7832 NewIntNo = Intrinsic::x86_mmx_psra_d;
7833 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00007834 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng5759f972008-05-04 09:15:50 +00007835 }
7836 break;
7837 }
7838 }
Mon P Wangefa42202009-09-03 19:56:25 +00007839
7840 // The vector shift intrinsics with scalars uses 32b shift amounts but
7841 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
7842 // to be zero.
7843 SDValue ShOps[4];
7844 ShOps[0] = ShAmt;
7845 ShOps[1] = DAG.getConstant(0, MVT::i32);
7846 if (ShAmtVT == MVT::v4i32) {
7847 ShOps[2] = DAG.getUNDEF(MVT::i32);
7848 ShOps[3] = DAG.getUNDEF(MVT::i32);
7849 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 4);
7850 } else {
7851 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 2);
Dale Johannesen0488fb62010-09-30 23:57:10 +00007852// FIXME this must be lowered to get rid of the invalid type.
Mon P Wangefa42202009-09-03 19:56:25 +00007853 }
7854
Owen Andersone50ed302009-08-10 22:56:29 +00007855 EVT VT = Op.getValueType();
Mon P Wangefa42202009-09-03 19:56:25 +00007856 ShAmt = DAG.getNode(ISD::BIT_CONVERT, dl, VT, ShAmt);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007857 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007858 DAG.getConstant(NewIntNo, MVT::i32),
Evan Cheng5759f972008-05-04 09:15:50 +00007859 Op.getOperand(1), ShAmt);
7860 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00007861 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007862}
Evan Cheng72261582005-12-20 06:22:03 +00007863
Dan Gohmand858e902010-04-17 15:26:15 +00007864SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
7865 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +00007866 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7867 MFI->setReturnAddressIsTaken(true);
7868
Bill Wendling64e87322009-01-16 19:25:27 +00007869 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007870 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00007871
7872 if (Depth > 0) {
7873 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
7874 SDValue Offset =
7875 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +00007876 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007877 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00007878 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007879 FrameAddr, Offset),
Chris Lattner51abfe42010-09-21 06:02:19 +00007880 MachinePointerInfo(), false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +00007881 }
7882
7883 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00007884 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00007885 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Chris Lattner51abfe42010-09-21 06:02:19 +00007886 RetAddrFI, MachinePointerInfo(), false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00007887}
7888
Dan Gohmand858e902010-04-17 15:26:15 +00007889SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +00007890 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7891 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00007892
Owen Andersone50ed302009-08-10 22:56:29 +00007893 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007894 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00007895 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
7896 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00007897 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00007898 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +00007899 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
7900 MachinePointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +00007901 false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00007902 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00007903}
7904
Dan Gohman475871a2008-07-27 21:46:04 +00007905SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00007906 SelectionDAG &DAG) const {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00007907 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007908}
7909
Dan Gohmand858e902010-04-17 15:26:15 +00007910SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007911 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00007912 SDValue Chain = Op.getOperand(0);
7913 SDValue Offset = Op.getOperand(1);
7914 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007915 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007916
Dan Gohmand8816272010-08-11 18:14:00 +00007917 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
7918 Subtarget->is64Bit() ? X86::RBP : X86::EBP,
7919 getPointerTy());
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00007920 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007921
Dan Gohmand8816272010-08-11 18:14:00 +00007922 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Frame,
7923 DAG.getIntPtrConstant(TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007924 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +00007925 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
7926 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00007927 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00007928 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007929
Dale Johannesene4d209d2009-02-03 20:21:25 +00007930 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007931 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00007932 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007933}
7934
Dan Gohman475871a2008-07-27 21:46:04 +00007935SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00007936 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00007937 SDValue Root = Op.getOperand(0);
7938 SDValue Trmp = Op.getOperand(1); // trampoline
7939 SDValue FPtr = Op.getOperand(2); // nested function
7940 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007941 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00007942
Dan Gohman69de1932008-02-06 22:27:42 +00007943 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00007944
7945 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00007946 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00007947
7948 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +00007949 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
7950 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +00007951
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00007952 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
7953 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00007954
7955 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
7956
7957 // Load the pointer to the nested function into R11.
7958 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00007959 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00007960 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007961 Addr, MachinePointerInfo(TrmpAddr),
7962 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00007963
Owen Anderson825b72b2009-08-11 20:47:22 +00007964 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7965 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00007966 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
7967 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +00007968 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00007969
7970 // Load the 'nest' parameter value into R10.
7971 // R10 is specified in X86CallingConv.td
7972 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +00007973 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7974 DAG.getConstant(10, MVT::i64));
7975 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007976 Addr, MachinePointerInfo(TrmpAddr, 10),
7977 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00007978
Owen Anderson825b72b2009-08-11 20:47:22 +00007979 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7980 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00007981 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
7982 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +00007983 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00007984
7985 // Jump to the nested function.
7986 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +00007987 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7988 DAG.getConstant(20, MVT::i64));
7989 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007990 Addr, MachinePointerInfo(TrmpAddr, 20),
7991 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00007992
7993 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +00007994 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7995 DAG.getConstant(22, MVT::i64));
7996 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00007997 MachinePointerInfo(TrmpAddr, 22),
7998 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00007999
Dan Gohman475871a2008-07-27 21:46:04 +00008000 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00008001 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008002 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008003 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00008004 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00008005 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00008006 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00008007 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008008
8009 switch (CC) {
8010 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00008011 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +00008012 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00008013 case CallingConv::X86_StdCall: {
8014 // Pass 'nest' parameter in ECX.
8015 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00008016 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008017
8018 // Check that ECX wasn't needed by an 'inreg' parameter.
8019 const FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00008020 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008021
Chris Lattner58d74912008-03-12 17:45:29 +00008022 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00008023 unsigned InRegCount = 0;
8024 unsigned Idx = 1;
8025
8026 for (FunctionType::param_iterator I = FTy->param_begin(),
8027 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00008028 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00008029 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00008030 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008031
8032 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +00008033 report_fatal_error("Nest register in use - reduce number of inreg"
8034 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +00008035 }
8036 }
8037 break;
8038 }
8039 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +00008040 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00008041 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00008042 // Pass 'nest' parameter in EAX.
8043 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00008044 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008045 break;
8046 }
8047
Dan Gohman475871a2008-07-27 21:46:04 +00008048 SDValue OutChains[4];
8049 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008050
Owen Anderson825b72b2009-08-11 20:47:22 +00008051 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8052 DAG.getConstant(10, MVT::i32));
8053 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008054
Chris Lattnera62fe662010-02-05 19:20:30 +00008055 // This is storing the opcode for MOV32ri.
8056 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00008057 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00008058 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008059 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008060 Trmp, MachinePointerInfo(TrmpAddr),
8061 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008062
Owen Anderson825b72b2009-08-11 20:47:22 +00008063 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8064 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008065 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
8066 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +00008067 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008068
Chris Lattnera62fe662010-02-05 19:20:30 +00008069 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +00008070 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8071 DAG.getConstant(5, MVT::i32));
8072 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00008073 MachinePointerInfo(TrmpAddr, 5),
8074 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008075
Owen Anderson825b72b2009-08-11 20:47:22 +00008076 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8077 DAG.getConstant(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008078 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
8079 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +00008080 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008081
Dan Gohman475871a2008-07-27 21:46:04 +00008082 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00008083 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008084 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008085 }
8086}
8087
Dan Gohmand858e902010-04-17 15:26:15 +00008088SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
8089 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008090 /*
8091 The rounding mode is in bits 11:10 of FPSR, and has the following
8092 settings:
8093 00 Round to nearest
8094 01 Round to -inf
8095 10 Round to +inf
8096 11 Round to 0
8097
8098 FLT_ROUNDS, on the other hand, expects the following:
8099 -1 Undefined
8100 0 Round to 0
8101 1 Round to nearest
8102 2 Round to +inf
8103 3 Round to -inf
8104
8105 To perform the conversion, we do:
8106 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
8107 */
8108
8109 MachineFunction &MF = DAG.getMachineFunction();
8110 const TargetMachine &TM = MF.getTarget();
8111 const TargetFrameInfo &TFI = *TM.getFrameInfo();
8112 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00008113 EVT VT = Op.getValueType();
Chris Lattner2156b792010-09-22 01:11:26 +00008114 DebugLoc DL = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008115
8116 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +00008117 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +00008118 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008119
Michael J. Spencerec38de22010-10-10 22:04:20 +00008120
Chris Lattner2156b792010-09-22 01:11:26 +00008121 MachineMemOperand *MMO =
8122 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8123 MachineMemOperand::MOStore, 2, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008124
Chris Lattner2156b792010-09-22 01:11:26 +00008125 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
8126 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
8127 DAG.getVTList(MVT::Other),
8128 Ops, 2, MVT::i16, MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008129
8130 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +00008131 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Chris Lattner51abfe42010-09-21 06:02:19 +00008132 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008133
8134 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00008135 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +00008136 DAG.getNode(ISD::SRL, DL, MVT::i16,
8137 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00008138 CWD, DAG.getConstant(0x800, MVT::i16)),
8139 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00008140 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +00008141 DAG.getNode(ISD::SRL, DL, MVT::i16,
8142 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00008143 CWD, DAG.getConstant(0x400, MVT::i16)),
8144 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008145
Dan Gohman475871a2008-07-27 21:46:04 +00008146 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +00008147 DAG.getNode(ISD::AND, DL, MVT::i16,
8148 DAG.getNode(ISD::ADD, DL, MVT::i16,
8149 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +00008150 DAG.getConstant(1, MVT::i16)),
8151 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008152
8153
Duncan Sands83ec4b62008-06-06 12:08:01 +00008154 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +00008155 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008156}
8157
Dan Gohmand858e902010-04-17 15:26:15 +00008158SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008159 EVT VT = Op.getValueType();
8160 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008161 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008162 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00008163
8164 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008165 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00008166 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +00008167 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00008168 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008169 }
Evan Cheng18efe262007-12-14 02:13:44 +00008170
Evan Cheng152804e2007-12-14 08:30:15 +00008171 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008172 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008173 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00008174
8175 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00008176 SDValue Ops[] = {
8177 Op,
8178 DAG.getConstant(NumBits+NumBits-1, OpVT),
8179 DAG.getConstant(X86::COND_E, MVT::i8),
8180 Op.getValue(1)
8181 };
8182 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00008183
8184 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +00008185 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +00008186
Owen Anderson825b72b2009-08-11 20:47:22 +00008187 if (VT == MVT::i8)
8188 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008189 return Op;
8190}
8191
Dan Gohmand858e902010-04-17 15:26:15 +00008192SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008193 EVT VT = Op.getValueType();
8194 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008195 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008196 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00008197
8198 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008199 if (VT == MVT::i8) {
8200 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00008201 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008202 }
Evan Cheng152804e2007-12-14 08:30:15 +00008203
8204 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008205 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008206 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00008207
8208 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00008209 SDValue Ops[] = {
8210 Op,
8211 DAG.getConstant(NumBits, OpVT),
8212 DAG.getConstant(X86::COND_E, MVT::i8),
8213 Op.getValue(1)
8214 };
8215 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00008216
Owen Anderson825b72b2009-08-11 20:47:22 +00008217 if (VT == MVT::i8)
8218 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008219 return Op;
8220}
8221
Dan Gohmand858e902010-04-17 15:26:15 +00008222SDValue X86TargetLowering::LowerMUL_V2I64(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008223 EVT VT = Op.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00008224 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008225 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00008226
Mon P Wangaf9b9522008-12-18 21:42:19 +00008227 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
8228 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
8229 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
8230 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
8231 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
8232 //
8233 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
8234 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
8235 // return AloBlo + AloBhi + AhiBlo;
8236
8237 SDValue A = Op.getOperand(0);
8238 SDValue B = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008239
Dale Johannesene4d209d2009-02-03 20:21:25 +00008240 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008241 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
8242 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008243 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008244 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
8245 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008246 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008247 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008248 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008249 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008250 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008251 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008252 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008253 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008254 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008255 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008256 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
8257 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008258 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008259 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
8260 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008261 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
8262 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +00008263 return Res;
8264}
8265
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008266SDValue X86TargetLowering::LowerSHL(SDValue Op, SelectionDAG &DAG) const {
8267 EVT VT = Op.getValueType();
8268 DebugLoc dl = Op.getDebugLoc();
8269 SDValue R = Op.getOperand(0);
8270
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008271 LLVMContext *Context = DAG.getContext();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008272
Nate Begeman51409212010-07-28 00:21:48 +00008273 assert(Subtarget->hasSSE41() && "Cannot lower SHL without SSE4.1 or later");
8274
8275 if (VT == MVT::v4i32) {
8276 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8277 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
8278 Op.getOperand(1), DAG.getConstant(23, MVT::i32));
8279
8280 ConstantInt *CI = ConstantInt::get(*Context, APInt(32, 0x3f800000U));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008281
Nate Begeman51409212010-07-28 00:21:48 +00008282 std::vector<Constant*> CV(4, CI);
8283 Constant *C = ConstantVector::get(CV);
8284 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8285 SDValue Addend = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008286 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +00008287 false, false, 16);
8288
8289 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Addend);
8290 Op = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4f32, Op);
8291 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
8292 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
8293 }
8294 if (VT == MVT::v16i8) {
8295 // a = a << 5;
8296 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8297 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
8298 Op.getOperand(1), DAG.getConstant(5, MVT::i32));
8299
8300 ConstantInt *CM1 = ConstantInt::get(*Context, APInt(8, 15));
8301 ConstantInt *CM2 = ConstantInt::get(*Context, APInt(8, 63));
8302
8303 std::vector<Constant*> CVM1(16, CM1);
8304 std::vector<Constant*> CVM2(16, CM2);
8305 Constant *C = ConstantVector::get(CVM1);
8306 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8307 SDValue M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008308 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +00008309 false, false, 16);
8310
8311 // r = pblendv(r, psllw(r & (char16)15, 4), a);
8312 M = DAG.getNode(ISD::AND, dl, VT, R, M);
8313 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8314 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
8315 DAG.getConstant(4, MVT::i32));
8316 R = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8317 DAG.getConstant(Intrinsic::x86_sse41_pblendvb, MVT::i32),
8318 R, M, Op);
8319 // a += a
8320 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008321
Nate Begeman51409212010-07-28 00:21:48 +00008322 C = ConstantVector::get(CVM2);
8323 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8324 M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008325 MachinePointerInfo::getConstantPool(),
Chris Lattner51abfe42010-09-21 06:02:19 +00008326 false, false, 16);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008327
Nate Begeman51409212010-07-28 00:21:48 +00008328 // r = pblendv(r, psllw(r & (char16)63, 2), a);
8329 M = DAG.getNode(ISD::AND, dl, VT, R, M);
8330 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8331 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
8332 DAG.getConstant(2, MVT::i32));
8333 R = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8334 DAG.getConstant(Intrinsic::x86_sse41_pblendvb, MVT::i32),
8335 R, M, Op);
8336 // a += a
8337 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008338
Nate Begeman51409212010-07-28 00:21:48 +00008339 // return pblendv(r, r+r, a);
8340 R = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8341 DAG.getConstant(Intrinsic::x86_sse41_pblendvb, MVT::i32),
8342 R, DAG.getNode(ISD::ADD, dl, VT, R, R), Op);
8343 return R;
8344 }
8345 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008346}
Mon P Wangaf9b9522008-12-18 21:42:19 +00008347
Dan Gohmand858e902010-04-17 15:26:15 +00008348SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
Bill Wendling74c37652008-12-09 22:08:41 +00008349 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
8350 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +00008351 // looks for this combo and may remove the "setcc" instruction if the "setcc"
8352 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +00008353 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +00008354 SDValue LHS = N->getOperand(0);
8355 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +00008356 unsigned BaseOp = 0;
8357 unsigned Cond = 0;
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008358 DebugLoc dl = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +00008359
8360 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008361 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +00008362 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +00008363 // A subtract of one will be selected as a INC. Note that INC doesn't
8364 // set CF, so we can't do this for UADDO.
8365 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
8366 if (C->getAPIntValue() == 1) {
8367 BaseOp = X86ISD::INC;
8368 Cond = X86::COND_O;
8369 break;
8370 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008371 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +00008372 Cond = X86::COND_O;
8373 break;
8374 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008375 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +00008376 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00008377 break;
8378 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +00008379 // A subtract of one will be selected as a DEC. Note that DEC doesn't
8380 // set CF, so we can't do this for USUBO.
8381 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
8382 if (C->getAPIntValue() == 1) {
8383 BaseOp = X86ISD::DEC;
8384 Cond = X86::COND_O;
8385 break;
8386 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008387 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +00008388 Cond = X86::COND_O;
8389 break;
8390 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008391 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +00008392 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00008393 break;
8394 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00008395 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +00008396 Cond = X86::COND_O;
8397 break;
8398 case ISD::UMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00008399 BaseOp = X86ISD::UMUL;
Dan Gohman653456c2009-01-07 00:15:08 +00008400 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00008401 break;
8402 }
Bill Wendling3fafd932008-11-26 22:37:40 +00008403
Bill Wendling61edeb52008-12-02 01:06:39 +00008404 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008405 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008406 SDValue Sum = DAG.getNode(BaseOp, dl, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +00008407
Bill Wendling61edeb52008-12-02 01:06:39 +00008408 SDValue SetCC =
Dale Johannesene4d209d2009-02-03 20:21:25 +00008409 DAG.getNode(X86ISD::SETCC, dl, N->getValueType(1),
Owen Anderson825b72b2009-08-11 20:47:22 +00008410 DAG.getConstant(Cond, MVT::i32), SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +00008411
Bill Wendling61edeb52008-12-02 01:06:39 +00008412 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
8413 return Sum;
Bill Wendling41ea7e72008-11-24 19:21:46 +00008414}
8415
Eric Christopher9a9d2752010-07-22 02:48:34 +00008416SDValue X86TargetLowering::LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG) const{
8417 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00008418
Eric Christopherb6729dc2010-08-04 23:03:04 +00008419 if (!Subtarget->hasSSE2()) {
Eric Christopherc0b2a202010-08-14 21:51:50 +00008420 SDValue Chain = Op.getOperand(0);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008421 SDValue Zero = DAG.getConstant(0,
Eric Christopherb6729dc2010-08-04 23:03:04 +00008422 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Eric Christopherc0b2a202010-08-14 21:51:50 +00008423 SDValue Ops[] = {
8424 DAG.getRegister(X86::ESP, MVT::i32), // Base
8425 DAG.getTargetConstant(1, MVT::i8), // Scale
8426 DAG.getRegister(0, MVT::i32), // Index
8427 DAG.getTargetConstant(0, MVT::i32), // Disp
8428 DAG.getRegister(0, MVT::i32), // Segment.
8429 Zero,
8430 Chain
8431 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00008432 SDNode *Res =
Eric Christopherc0b2a202010-08-14 21:51:50 +00008433 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
8434 array_lengthof(Ops));
8435 return SDValue(Res, 0);
Eric Christopherb6729dc2010-08-04 23:03:04 +00008436 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00008437
Eric Christopher9a9d2752010-07-22 02:48:34 +00008438 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
Chris Lattner132929a2010-08-14 17:26:09 +00008439 if (!isDev)
Eric Christopher9a9d2752010-07-22 02:48:34 +00008440 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008441
Chris Lattner132929a2010-08-14 17:26:09 +00008442 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
8443 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
8444 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
8445 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
Michael J. Spencerec38de22010-10-10 22:04:20 +00008446
Chris Lattner132929a2010-08-14 17:26:09 +00008447 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
8448 if (!Op1 && !Op2 && !Op3 && Op4)
8449 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008450
Chris Lattner132929a2010-08-14 17:26:09 +00008451 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
8452 if (Op1 && !Op2 && !Op3 && !Op4)
8453 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008454
8455 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
Chris Lattner132929a2010-08-14 17:26:09 +00008456 // (MFENCE)>;
8457 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
Eric Christopher9a9d2752010-07-22 02:48:34 +00008458}
8459
Dan Gohmand858e902010-04-17 15:26:15 +00008460SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008461 EVT T = Op.getValueType();
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008462 DebugLoc DL = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +00008463 unsigned Reg = 0;
8464 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00008465 switch(T.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00008466 default:
8467 assert(false && "Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +00008468 case MVT::i8: Reg = X86::AL; size = 1; break;
8469 case MVT::i16: Reg = X86::AX; size = 2; break;
8470 case MVT::i32: Reg = X86::EAX; size = 4; break;
8471 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +00008472 assert(Subtarget->is64Bit() && "Node not type legal!");
8473 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +00008474 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00008475 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008476 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +00008477 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +00008478 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +00008479 Op.getOperand(1),
8480 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +00008481 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +00008482 cpIn.getValue(1) };
Owen Anderson825b72b2009-08-11 20:47:22 +00008483 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008484 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
8485 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
8486 Ops, 5, T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +00008487 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008488 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +00008489 return cpOut;
8490}
8491
Duncan Sands1607f052008-12-01 11:39:25 +00008492SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008493 SelectionDAG &DAG) const {
Duncan Sands1607f052008-12-01 11:39:25 +00008494 assert(Subtarget->is64Bit() && "Result not type legalized?");
Owen Anderson825b72b2009-08-11 20:47:22 +00008495 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Duncan Sands1607f052008-12-01 11:39:25 +00008496 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008497 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +00008498 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00008499 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
8500 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +00008501 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +00008502 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
8503 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +00008504 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +00008505 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +00008506 rdx.getValue(1)
8507 };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008508 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008509}
8510
Dale Johannesen7d07b482010-05-21 00:52:33 +00008511SDValue X86TargetLowering::LowerBIT_CONVERT(SDValue Op,
8512 SelectionDAG &DAG) const {
8513 EVT SrcVT = Op.getOperand(0).getValueType();
8514 EVT DstVT = Op.getValueType();
Michael J. Spencerec38de22010-10-10 22:04:20 +00008515 assert((Subtarget->is64Bit() && !Subtarget->hasSSE2() &&
Dale Johannesen7d07b482010-05-21 00:52:33 +00008516 Subtarget->hasMMX() && !DisableMMX) &&
8517 "Unexpected custom BIT_CONVERT");
Michael J. Spencerec38de22010-10-10 22:04:20 +00008518 assert((DstVT == MVT::i64 ||
Dale Johannesen7d07b482010-05-21 00:52:33 +00008519 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
8520 "Unexpected custom BIT_CONVERT");
8521 // i64 <=> MMX conversions are Legal.
8522 if (SrcVT==MVT::i64 && DstVT.isVector())
8523 return Op;
8524 if (DstVT==MVT::i64 && SrcVT.isVector())
8525 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +00008526 // MMX <=> MMX conversions are Legal.
8527 if (SrcVT.isVector() && DstVT.isVector())
8528 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +00008529 // All other conversions need to be expanded.
8530 return SDValue();
8531}
Dan Gohmand858e902010-04-17 15:26:15 +00008532SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen71d1bf52008-09-29 22:25:26 +00008533 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +00008534 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008535 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008536 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +00008537 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008538 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008539 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +00008540 Node->getOperand(0),
8541 Node->getOperand(1), negOp,
8542 cast<AtomicSDNode>(Node)->getSrcValue(),
8543 cast<AtomicSDNode>(Node)->getAlignment());
Mon P Wang63307c32008-05-05 19:05:59 +00008544}
8545
Evan Cheng0db9fe62006-04-25 20:13:52 +00008546/// LowerOperation - Provide custom lowering hooks for some operations.
8547///
Dan Gohmand858e902010-04-17 15:26:15 +00008548SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00008549 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008550 default: llvm_unreachable("Should not custom lower this!");
Eric Christopher9a9d2752010-07-22 02:48:34 +00008551 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op,DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008552 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
8553 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008554 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00008555 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008556 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
8557 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
8558 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
8559 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
8560 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
8561 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00008562 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +00008563 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +00008564 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008565 case ISD::SHL_PARTS:
8566 case ISD::SRA_PARTS:
8567 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
8568 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00008569 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008570 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00008571 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008572 case ISD::FABS: return LowerFABS(Op, DAG);
8573 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008574 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00008575 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman30a0de92008-07-17 16:51:19 +00008576 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00008577 case ISD::SELECT: return LowerSELECT(Op, DAG);
8578 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008579 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008580 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +00008581 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +00008582 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008583 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00008584 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
8585 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008586 case ISD::FRAME_TO_ARGS_OFFSET:
8587 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008588 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008589 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008590 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +00008591 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +00008592 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
8593 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Mon P Wangaf9b9522008-12-18 21:42:19 +00008594 case ISD::MUL: return LowerMUL_V2I64(Op, DAG);
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008595 case ISD::SHL: return LowerSHL(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +00008596 case ISD::SADDO:
8597 case ISD::UADDO:
8598 case ISD::SSUBO:
8599 case ISD::USUBO:
8600 case ISD::SMULO:
8601 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +00008602 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Dale Johannesen7d07b482010-05-21 00:52:33 +00008603 case ISD::BIT_CONVERT: return LowerBIT_CONVERT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008604 }
Chris Lattner27a6c732007-11-24 07:07:01 +00008605}
8606
Duncan Sands1607f052008-12-01 11:39:25 +00008607void X86TargetLowering::
8608ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00008609 SelectionDAG &DAG, unsigned NewOp) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008610 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008611 DebugLoc dl = Node->getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00008612 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +00008613
8614 SDValue Chain = Node->getOperand(0);
8615 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00008616 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00008617 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00008618 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00008619 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +00008620 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +00008621 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +00008622 SDValue Result =
8623 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
8624 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +00008625 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +00008626 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00008627 Results.push_back(Result.getValue(2));
8628}
8629
Duncan Sands126d9072008-07-04 11:47:58 +00008630/// ReplaceNodeResults - Replace a node with an illegal result type
8631/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00008632void X86TargetLowering::ReplaceNodeResults(SDNode *N,
8633 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00008634 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +00008635 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +00008636 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +00008637 default:
Duncan Sands1607f052008-12-01 11:39:25 +00008638 assert(false && "Do not know how to custom type legalize this operation!");
8639 return;
8640 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +00008641 std::pair<SDValue,SDValue> Vals =
8642 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +00008643 SDValue FIST = Vals.first, StackSlot = Vals.second;
8644 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +00008645 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +00008646 // Return a load from the stack slot.
Chris Lattner51abfe42010-09-21 06:02:19 +00008647 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
8648 MachinePointerInfo(), false, false, 0));
Duncan Sands1607f052008-12-01 11:39:25 +00008649 }
8650 return;
8651 }
8652 case ISD::READCYCLECOUNTER: {
Owen Anderson825b72b2009-08-11 20:47:22 +00008653 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Duncan Sands1607f052008-12-01 11:39:25 +00008654 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008655 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00008656 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +00008657 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00008658 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00008659 eax.getValue(2));
8660 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
8661 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +00008662 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00008663 Results.push_back(edx.getValue(1));
8664 return;
8665 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008666 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +00008667 EVT T = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008668 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
Duncan Sands1607f052008-12-01 11:39:25 +00008669 SDValue cpInL, cpInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00008670 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
8671 DAG.getConstant(0, MVT::i32));
8672 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
8673 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00008674 cpInL = DAG.getCopyToReg(N->getOperand(0), dl, X86::EAX, cpInL, SDValue());
8675 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl, X86::EDX, cpInH,
Duncan Sands1607f052008-12-01 11:39:25 +00008676 cpInL.getValue(1));
8677 SDValue swapInL, swapInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00008678 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
8679 DAG.getConstant(0, MVT::i32));
8680 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
8681 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00008682 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl, X86::EBX, swapInL,
Duncan Sands1607f052008-12-01 11:39:25 +00008683 cpInH.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00008684 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl, X86::ECX, swapInH,
Duncan Sands1607f052008-12-01 11:39:25 +00008685 swapInL.getValue(1));
8686 SDValue Ops[] = { swapInH.getValue(0),
8687 N->getOperand(1),
8688 swapInH.getValue(1) };
Owen Anderson825b72b2009-08-11 20:47:22 +00008689 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008690 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG8_DAG, dl, Tys, Ops, 3);
Dale Johannesendd64c412009-02-04 00:33:20 +00008691 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl, X86::EAX,
Owen Anderson825b72b2009-08-11 20:47:22 +00008692 MVT::i32, Result.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00008693 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl, X86::EDX,
Owen Anderson825b72b2009-08-11 20:47:22 +00008694 MVT::i32, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +00008695 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Owen Anderson825b72b2009-08-11 20:47:22 +00008696 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00008697 Results.push_back(cpOutH.getValue(1));
8698 return;
8699 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008700 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +00008701 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
8702 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008703 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +00008704 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
8705 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008706 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +00008707 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
8708 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008709 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +00008710 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
8711 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008712 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +00008713 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
8714 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008715 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +00008716 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
8717 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008718 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +00008719 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
8720 return;
Chris Lattner27a6c732007-11-24 07:07:01 +00008721 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00008722}
8723
Evan Cheng72261582005-12-20 06:22:03 +00008724const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
8725 switch (Opcode) {
8726 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +00008727 case X86ISD::BSF: return "X86ISD::BSF";
8728 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +00008729 case X86ISD::SHLD: return "X86ISD::SHLD";
8730 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00008731 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +00008732 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +00008733 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +00008734 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +00008735 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00008736 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00008737 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
8738 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
8739 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00008740 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00008741 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +00008742 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +00008743 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +00008744 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +00008745 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +00008746 case X86ISD::COMI: return "X86ISD::COMI";
8747 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +00008748 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +00008749 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Evan Cheng72261582005-12-20 06:22:03 +00008750 case X86ISD::CMOV: return "X86ISD::CMOV";
8751 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00008752 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00008753 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
8754 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +00008755 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00008756 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +00008757 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +00008758 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +00008759 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +00008760 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
8761 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +00008762 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +00008763 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Evan Cheng8ca29322006-11-10 21:43:37 +00008764 case X86ISD::FMAX: return "X86ISD::FMAX";
8765 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +00008766 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
8767 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00008768 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +00008769 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008770 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00008771 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008772 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +00008773 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
8774 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008775 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
8776 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
8777 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
8778 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
8779 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
8780 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +00008781 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
8782 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +00008783 case X86ISD::VSHL: return "X86ISD::VSHL";
8784 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +00008785 case X86ISD::CMPPD: return "X86ISD::CMPPD";
8786 case X86ISD::CMPPS: return "X86ISD::CMPPS";
8787 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
8788 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
8789 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
8790 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
8791 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
8792 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
8793 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
8794 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008795 case X86ISD::ADD: return "X86ISD::ADD";
8796 case X86ISD::SUB: return "X86ISD::SUB";
Bill Wendlingd350e022008-12-12 21:15:41 +00008797 case X86ISD::SMUL: return "X86ISD::SMUL";
8798 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +00008799 case X86ISD::INC: return "X86ISD::INC";
8800 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +00008801 case X86ISD::OR: return "X86ISD::OR";
8802 case X86ISD::XOR: return "X86ISD::XOR";
8803 case X86ISD::AND: return "X86ISD::AND";
Evan Cheng73f24c92009-03-30 21:36:47 +00008804 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +00008805 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008806 case X86ISD::TESTP: return "X86ISD::TESTP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00008807 case X86ISD::PALIGN: return "X86ISD::PALIGN";
8808 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
8809 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
8810 case X86ISD::PSHUFHW_LD: return "X86ISD::PSHUFHW_LD";
8811 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
8812 case X86ISD::PSHUFLW_LD: return "X86ISD::PSHUFLW_LD";
8813 case X86ISD::SHUFPS: return "X86ISD::SHUFPS";
8814 case X86ISD::SHUFPD: return "X86ISD::SHUFPD";
8815 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00008816 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00008817 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00008818 case X86ISD::MOVHLPD: return "X86ISD::MOVHLPD";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00008819 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
8820 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00008821 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
8822 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
8823 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
8824 case X86ISD::MOVSHDUP_LD: return "X86ISD::MOVSHDUP_LD";
8825 case X86ISD::MOVSLDUP_LD: return "X86ISD::MOVSLDUP_LD";
8826 case X86ISD::MOVSD: return "X86ISD::MOVSD";
8827 case X86ISD::MOVSS: return "X86ISD::MOVSS";
8828 case X86ISD::UNPCKLPS: return "X86ISD::UNPCKLPS";
8829 case X86ISD::UNPCKLPD: return "X86ISD::UNPCKLPD";
8830 case X86ISD::UNPCKHPS: return "X86ISD::UNPCKHPS";
8831 case X86ISD::UNPCKHPD: return "X86ISD::UNPCKHPD";
8832 case X86ISD::PUNPCKLBW: return "X86ISD::PUNPCKLBW";
8833 case X86ISD::PUNPCKLWD: return "X86ISD::PUNPCKLWD";
8834 case X86ISD::PUNPCKLDQ: return "X86ISD::PUNPCKLDQ";
8835 case X86ISD::PUNPCKLQDQ: return "X86ISD::PUNPCKLQDQ";
8836 case X86ISD::PUNPCKHBW: return "X86ISD::PUNPCKHBW";
8837 case X86ISD::PUNPCKHWD: return "X86ISD::PUNPCKHWD";
8838 case X86ISD::PUNPCKHDQ: return "X86ISD::PUNPCKHDQ";
8839 case X86ISD::PUNPCKHQDQ: return "X86ISD::PUNPCKHQDQ";
Dan Gohmand6708ea2009-08-15 01:38:56 +00008840 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00008841 case X86ISD::MINGW_ALLOCA: return "X86ISD::MINGW_ALLOCA";
Evan Cheng72261582005-12-20 06:22:03 +00008842 }
8843}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008844
Chris Lattnerc9addb72007-03-30 23:15:24 +00008845// isLegalAddressingMode - Return true if the addressing mode represented
8846// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +00008847bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerc9addb72007-03-30 23:15:24 +00008848 const Type *Ty) const {
8849 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00008850 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +00008851 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +00008852
Chris Lattnerc9addb72007-03-30 23:15:24 +00008853 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00008854 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +00008855 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00008856
Chris Lattnerc9addb72007-03-30 23:15:24 +00008857 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +00008858 unsigned GVFlags =
8859 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00008860
Chris Lattnerdfed4132009-07-10 07:38:24 +00008861 // If a reference to this global requires an extra load, we can't fold it.
8862 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +00008863 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00008864
Chris Lattnerdfed4132009-07-10 07:38:24 +00008865 // If BaseGV requires a register for the PIC base, we cannot also have a
8866 // BaseReg specified.
8867 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +00008868 return false;
Evan Cheng52787842007-08-01 23:46:47 +00008869
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00008870 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +00008871 if ((M != CodeModel::Small || R != Reloc::Static) &&
8872 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00008873 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +00008874 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008875
Chris Lattnerc9addb72007-03-30 23:15:24 +00008876 switch (AM.Scale) {
8877 case 0:
8878 case 1:
8879 case 2:
8880 case 4:
8881 case 8:
8882 // These scales always work.
8883 break;
8884 case 3:
8885 case 5:
8886 case 9:
8887 // These scales are formed with basereg+scalereg. Only accept if there is
8888 // no basereg yet.
8889 if (AM.HasBaseReg)
8890 return false;
8891 break;
8892 default: // Other stuff never works.
8893 return false;
8894 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008895
Chris Lattnerc9addb72007-03-30 23:15:24 +00008896 return true;
8897}
8898
8899
Evan Cheng2bd122c2007-10-26 01:56:11 +00008900bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00008901 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +00008902 return false;
Evan Chenge127a732007-10-29 07:57:50 +00008903 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
8904 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00008905 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +00008906 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00008907 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +00008908}
8909
Owen Andersone50ed302009-08-10 22:56:29 +00008910bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +00008911 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +00008912 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008913 unsigned NumBits1 = VT1.getSizeInBits();
8914 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00008915 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +00008916 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00008917 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00008918}
Evan Cheng2bd122c2007-10-26 01:56:11 +00008919
Dan Gohman97121ba2009-04-08 00:15:30 +00008920bool X86TargetLowering::isZExtFree(const Type *Ty1, const Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00008921 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00008922 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00008923}
8924
Owen Andersone50ed302009-08-10 22:56:29 +00008925bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00008926 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00008927 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00008928}
8929
Owen Andersone50ed302009-08-10 22:56:29 +00008930bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +00008931 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +00008932 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +00008933}
8934
Evan Cheng60c07e12006-07-05 22:17:51 +00008935/// isShuffleMaskLegal - Targets can use this to indicate that they only
8936/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
8937/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
8938/// are assumed to be legal.
8939bool
Eric Christopherfd179292009-08-27 18:07:15 +00008940X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +00008941 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +00008942 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +00008943 if (VT.getSizeInBits() == 64)
Eric Christophercff6f852010-04-15 01:40:20 +00008944 return isPALIGNRMask(M, VT, Subtarget->hasSSSE3());
Nate Begeman9008ca62009-04-27 18:41:29 +00008945
Nate Begemana09008b2009-10-19 02:17:23 +00008946 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +00008947 return (VT.getVectorNumElements() == 2 ||
8948 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
8949 isMOVLMask(M, VT) ||
8950 isSHUFPMask(M, VT) ||
8951 isPSHUFDMask(M, VT) ||
8952 isPSHUFHWMask(M, VT) ||
8953 isPSHUFLWMask(M, VT) ||
Nate Begemana09008b2009-10-19 02:17:23 +00008954 isPALIGNRMask(M, VT, Subtarget->hasSSSE3()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +00008955 isUNPCKLMask(M, VT) ||
8956 isUNPCKHMask(M, VT) ||
8957 isUNPCKL_v_undef_Mask(M, VT) ||
8958 isUNPCKH_v_undef_Mask(M, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00008959}
8960
Dan Gohman7d8143f2008-04-09 20:09:42 +00008961bool
Nate Begeman5a5ca152009-04-29 05:20:52 +00008962X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +00008963 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00008964 unsigned NumElts = VT.getVectorNumElements();
8965 // FIXME: This collection of masks seems suspect.
8966 if (NumElts == 2)
8967 return true;
8968 if (NumElts == 4 && VT.getSizeInBits() == 128) {
8969 return (isMOVLMask(Mask, VT) ||
8970 isCommutedMOVLMask(Mask, VT, true) ||
8971 isSHUFPMask(Mask, VT) ||
8972 isCommutedSHUFPMask(Mask, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00008973 }
8974 return false;
8975}
8976
8977//===----------------------------------------------------------------------===//
8978// X86 Scheduler Hooks
8979//===----------------------------------------------------------------------===//
8980
Mon P Wang63307c32008-05-05 19:05:59 +00008981// private utility function
8982MachineBasicBlock *
8983X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
8984 MachineBasicBlock *MBB,
8985 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +00008986 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008987 unsigned LoadOpc,
8988 unsigned CXchgOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008989 unsigned notOpc,
8990 unsigned EAXreg,
8991 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00008992 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00008993 // For the atomic bitwise operator, we generate
8994 // thisMBB:
8995 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00008996 // ld t1 = [bitinstr.addr]
8997 // op t2 = t1, [bitinstr.val]
8998 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00008999 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
9000 // bz newMBB
9001 // fallthrough -->nextMBB
9002 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9003 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009004 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00009005 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009006
Mon P Wang63307c32008-05-05 19:05:59 +00009007 /// First build the CFG
9008 MachineFunction *F = MBB->getParent();
9009 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009010 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9011 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9012 F->insert(MBBIter, newMBB);
9013 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009014
Dan Gohman14152b42010-07-06 20:24:04 +00009015 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9016 nextMBB->splice(nextMBB->begin(), thisMBB,
9017 llvm::next(MachineBasicBlock::iterator(bInstr)),
9018 thisMBB->end());
9019 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009020
Mon P Wang63307c32008-05-05 19:05:59 +00009021 // Update thisMBB to fall through to newMBB
9022 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009023
Mon P Wang63307c32008-05-05 19:05:59 +00009024 // newMBB jumps to itself and fall through to nextMBB
9025 newMBB->addSuccessor(nextMBB);
9026 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009027
Mon P Wang63307c32008-05-05 19:05:59 +00009028 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009029 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009030 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +00009031 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00009032 MachineOperand& destOper = bInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009033 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00009034 int numArgs = bInstr->getNumOperands() - 1;
9035 for (int i=0; i < numArgs; ++i)
9036 argOpers[i] = &bInstr->getOperand(i+1);
9037
9038 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009039 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009040 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00009041
Dale Johannesen140be2d2008-08-19 18:47:28 +00009042 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009043 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00009044 for (int i=0; i <= lastAddrIndx; ++i)
9045 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009046
Dale Johannesen140be2d2008-08-19 18:47:28 +00009047 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009048 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00009049 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009050 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009051 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009052 tt = t1;
9053
Dale Johannesen140be2d2008-08-19 18:47:28 +00009054 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +00009055 assert((argOpers[valArgIndx]->isReg() ||
9056 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00009057 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +00009058 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009059 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00009060 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009061 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009062 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +00009063 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009064
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009065 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +00009066 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009067
Dale Johannesene4d209d2009-02-03 20:21:25 +00009068 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +00009069 for (int i=0; i <= lastAddrIndx; ++i)
9070 (*MIB).addOperand(*argOpers[i]);
9071 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +00009072 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00009073 (*MIB).setMemRefs(bInstr->memoperands_begin(),
9074 bInstr->memoperands_end());
Mon P Wangf5952662008-07-17 04:54:06 +00009075
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009076 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +00009077 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +00009078
Mon P Wang63307c32008-05-05 19:05:59 +00009079 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009080 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00009081
Dan Gohman14152b42010-07-06 20:24:04 +00009082 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00009083 return nextMBB;
9084}
9085
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00009086// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +00009087MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009088X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
9089 MachineBasicBlock *MBB,
9090 unsigned regOpcL,
9091 unsigned regOpcH,
9092 unsigned immOpcL,
9093 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009094 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009095 // For the atomic bitwise operator, we generate
9096 // thisMBB (instructions are in pairs, except cmpxchg8b)
9097 // ld t1,t2 = [bitinstr.addr]
9098 // newMBB:
9099 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
9100 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +00009101 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009102 // mov ECX, EBX <- t5, t6
9103 // mov EAX, EDX <- t1, t2
9104 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
9105 // mov t3, t4 <- EAX, EDX
9106 // bz newMBB
9107 // result in out1, out2
9108 // fallthrough -->nextMBB
9109
9110 const TargetRegisterClass *RC = X86::GR32RegisterClass;
9111 const unsigned LoadOpc = X86::MOV32rm;
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009112 const unsigned NotOpc = X86::NOT32r;
9113 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9114 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
9115 MachineFunction::iterator MBBIter = MBB;
9116 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009117
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009118 /// First build the CFG
9119 MachineFunction *F = MBB->getParent();
9120 MachineBasicBlock *thisMBB = MBB;
9121 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9122 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9123 F->insert(MBBIter, newMBB);
9124 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009125
Dan Gohman14152b42010-07-06 20:24:04 +00009126 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9127 nextMBB->splice(nextMBB->begin(), thisMBB,
9128 llvm::next(MachineBasicBlock::iterator(bInstr)),
9129 thisMBB->end());
9130 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009131
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009132 // Update thisMBB to fall through to newMBB
9133 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009134
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009135 // newMBB jumps to itself and fall through to nextMBB
9136 newMBB->addSuccessor(nextMBB);
9137 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009138
Dale Johannesene4d209d2009-02-03 20:21:25 +00009139 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009140 // Insert instructions into newMBB based on incoming instruction
9141 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009142 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009143 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009144 MachineOperand& dest1Oper = bInstr->getOperand(0);
9145 MachineOperand& dest2Oper = bInstr->getOperand(1);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009146 MachineOperand* argOpers[2 + X86::AddrNumOperands];
9147 for (int i=0; i < 2 + X86::AddrNumOperands; ++i) {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009148 argOpers[i] = &bInstr->getOperand(i+2);
9149
Dan Gohman71ea4e52010-05-14 21:01:44 +00009150 // We use some of the operands multiple times, so conservatively just
9151 // clear any kill flags that might be present.
9152 if (argOpers[i]->isReg() && argOpers[i]->isUse())
9153 argOpers[i]->setIsKill(false);
9154 }
9155
Evan Chengad5b52f2010-01-08 19:14:57 +00009156 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009157 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +00009158
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009159 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009160 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009161 for (int i=0; i <= lastAddrIndx; ++i)
9162 (*MIB).addOperand(*argOpers[i]);
9163 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009164 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +00009165 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +00009166 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009167 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +00009168 MachineOperand newOp3 = *(argOpers[3]);
9169 if (newOp3.isImm())
9170 newOp3.setImm(newOp3.getImm()+4);
9171 else
9172 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009173 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +00009174 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009175
9176 // t3/4 are defined later, at the bottom of the loop
9177 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
9178 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009179 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009180 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009181 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009182 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
9183
Evan Cheng306b4ca2010-01-08 23:41:50 +00009184 // The subsequent operations should be using the destination registers of
9185 //the PHI instructions.
Scott Michelfdc40a02009-02-17 22:15:04 +00009186 if (invSrc) {
Evan Cheng306b4ca2010-01-08 23:41:50 +00009187 t1 = F->getRegInfo().createVirtualRegister(RC);
9188 t2 = F->getRegInfo().createVirtualRegister(RC);
9189 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t1).addReg(dest1Oper.getReg());
9190 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t2).addReg(dest2Oper.getReg());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009191 } else {
Evan Cheng306b4ca2010-01-08 23:41:50 +00009192 t1 = dest1Oper.getReg();
9193 t2 = dest2Oper.getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009194 }
9195
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009196 int valArgIndx = lastAddrIndx + 1;
9197 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +00009198 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009199 "invalid operand");
9200 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
9201 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009202 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009203 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009204 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009205 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +00009206 if (regOpcL != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00009207 MIB.addReg(t1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009208 (*MIB).addOperand(*argOpers[valArgIndx]);
9209 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00009210 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009211 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00009212 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009213 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009214 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009215 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009216 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +00009217 if (regOpcH != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00009218 MIB.addReg(t2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009219 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009220
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009221 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009222 MIB.addReg(t1);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009223 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009224 MIB.addReg(t2);
9225
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009226 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009227 MIB.addReg(t5);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009228 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009229 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +00009230
Dale Johannesene4d209d2009-02-03 20:21:25 +00009231 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009232 for (int i=0; i <= lastAddrIndx; ++i)
9233 (*MIB).addOperand(*argOpers[i]);
9234
9235 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00009236 (*MIB).setMemRefs(bInstr->memoperands_begin(),
9237 bInstr->memoperands_end());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009238
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009239 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009240 MIB.addReg(X86::EAX);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009241 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009242 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +00009243
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009244 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009245 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009246
Dan Gohman14152b42010-07-06 20:24:04 +00009247 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009248 return nextMBB;
9249}
9250
9251// private utility function
9252MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +00009253X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
9254 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009255 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00009256 // For the atomic min/max operator, we generate
9257 // thisMBB:
9258 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00009259 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +00009260 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +00009261 // cmp t1, t2
9262 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +00009263 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00009264 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
9265 // bz newMBB
9266 // fallthrough -->nextMBB
9267 //
9268 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9269 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009270 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00009271 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009272
Mon P Wang63307c32008-05-05 19:05:59 +00009273 /// First build the CFG
9274 MachineFunction *F = MBB->getParent();
9275 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009276 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9277 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9278 F->insert(MBBIter, newMBB);
9279 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009280
Dan Gohman14152b42010-07-06 20:24:04 +00009281 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9282 nextMBB->splice(nextMBB->begin(), thisMBB,
9283 llvm::next(MachineBasicBlock::iterator(mInstr)),
9284 thisMBB->end());
9285 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009286
Mon P Wang63307c32008-05-05 19:05:59 +00009287 // Update thisMBB to fall through to newMBB
9288 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009289
Mon P Wang63307c32008-05-05 19:05:59 +00009290 // newMBB jumps to newMBB and fall through to nextMBB
9291 newMBB->addSuccessor(nextMBB);
9292 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009293
Dale Johannesene4d209d2009-02-03 20:21:25 +00009294 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00009295 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009296 assert(mInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009297 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +00009298 MachineOperand& destOper = mInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009299 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00009300 int numArgs = mInstr->getNumOperands() - 1;
9301 for (int i=0; i < numArgs; ++i)
9302 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009303
Mon P Wang63307c32008-05-05 19:05:59 +00009304 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009305 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009306 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00009307
Mon P Wangab3e7472008-05-05 22:56:23 +00009308 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009309 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00009310 for (int i=0; i <= lastAddrIndx; ++i)
9311 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +00009312
Mon P Wang63307c32008-05-05 19:05:59 +00009313 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +00009314 assert((argOpers[valArgIndx]->isReg() ||
9315 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00009316 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +00009317
9318 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +00009319 if (argOpers[valArgIndx]->isReg())
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009320 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +00009321 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009322 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00009323 (*MIB).addOperand(*argOpers[valArgIndx]);
9324
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009325 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +00009326 MIB.addReg(t1);
9327
Dale Johannesene4d209d2009-02-03 20:21:25 +00009328 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +00009329 MIB.addReg(t1);
9330 MIB.addReg(t2);
9331
9332 // Generate movc
9333 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009334 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +00009335 MIB.addReg(t2);
9336 MIB.addReg(t1);
9337
9338 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +00009339 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +00009340 for (int i=0; i <= lastAddrIndx; ++i)
9341 (*MIB).addOperand(*argOpers[i]);
9342 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +00009343 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00009344 (*MIB).setMemRefs(mInstr->memoperands_begin(),
9345 mInstr->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +00009346
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009347 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +00009348 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +00009349
Mon P Wang63307c32008-05-05 19:05:59 +00009350 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009351 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00009352
Dan Gohman14152b42010-07-06 20:24:04 +00009353 mInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00009354 return nextMBB;
9355}
9356
Eric Christopherf83a5de2009-08-27 18:08:16 +00009357// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00009358// or XMM0_V32I8 in AVX all of this code can be replaced with that
9359// in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +00009360MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +00009361X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +00009362 unsigned numArgs, bool memArg) const {
Eric Christopherb120ab42009-08-18 22:50:32 +00009363
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00009364 assert((Subtarget->hasSSE42() || Subtarget->hasAVX()) &&
9365 "Target must have SSE4.2 or AVX features enabled");
9366
Eric Christopherb120ab42009-08-18 22:50:32 +00009367 DebugLoc dl = MI->getDebugLoc();
9368 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9369
9370 unsigned Opc;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00009371
9372 if (!Subtarget->hasAVX()) {
9373 if (memArg)
9374 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
9375 else
9376 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
9377 } else {
9378 if (memArg)
9379 Opc = numArgs == 3 ? X86::VPCMPISTRM128rm : X86::VPCMPESTRM128rm;
9380 else
9381 Opc = numArgs == 3 ? X86::VPCMPISTRM128rr : X86::VPCMPESTRM128rr;
9382 }
Eric Christopherb120ab42009-08-18 22:50:32 +00009383
9384 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(Opc));
9385
9386 for (unsigned i = 0; i < numArgs; ++i) {
9387 MachineOperand &Op = MI->getOperand(i+1);
9388
9389 if (!(Op.isReg() && Op.isImplicit()))
9390 MIB.addOperand(Op);
9391 }
9392
9393 BuildMI(BB, dl, TII->get(X86::MOVAPSrr), MI->getOperand(0).getReg())
9394 .addReg(X86::XMM0);
9395
Dan Gohman14152b42010-07-06 20:24:04 +00009396 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +00009397
9398 return BB;
9399}
9400
9401MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +00009402X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
9403 MachineInstr *MI,
9404 MachineBasicBlock *MBB) const {
9405 // Emit code to save XMM registers to the stack. The ABI says that the
9406 // number of registers to save is given in %al, so it's theoretically
9407 // possible to do an indirect jump trick to avoid saving all of them,
9408 // however this code takes a simpler approach and just executes all
9409 // of the stores if %al is non-zero. It's less code, and it's probably
9410 // easier on the hardware branch predictor, and stores aren't all that
9411 // expensive anyway.
9412
9413 // Create the new basic blocks. One block contains all the XMM stores,
9414 // and one block is the final destination regardless of whether any
9415 // stores were performed.
9416 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
9417 MachineFunction *F = MBB->getParent();
9418 MachineFunction::iterator MBBIter = MBB;
9419 ++MBBIter;
9420 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
9421 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
9422 F->insert(MBBIter, XMMSaveMBB);
9423 F->insert(MBBIter, EndMBB);
9424
Dan Gohman14152b42010-07-06 20:24:04 +00009425 // Transfer the remainder of MBB and its successor edges to EndMBB.
9426 EndMBB->splice(EndMBB->begin(), MBB,
9427 llvm::next(MachineBasicBlock::iterator(MI)),
9428 MBB->end());
9429 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
9430
Dan Gohmand6708ea2009-08-15 01:38:56 +00009431 // The original block will now fall through to the XMM save block.
9432 MBB->addSuccessor(XMMSaveMBB);
9433 // The XMMSaveMBB will fall through to the end block.
9434 XMMSaveMBB->addSuccessor(EndMBB);
9435
9436 // Now add the instructions.
9437 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9438 DebugLoc DL = MI->getDebugLoc();
9439
9440 unsigned CountReg = MI->getOperand(0).getReg();
9441 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
9442 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
9443
9444 if (!Subtarget->isTargetWin64()) {
9445 // If %al is 0, branch around the XMM save block.
9446 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009447 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +00009448 MBB->addSuccessor(EndMBB);
9449 }
9450
9451 // In the XMM save block, save all the XMM argument registers.
9452 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
9453 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +00009454 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +00009455 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +00009456 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +00009457 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +00009458 /*Size=*/16, /*Align=*/16);
Dan Gohmand6708ea2009-08-15 01:38:56 +00009459 BuildMI(XMMSaveMBB, DL, TII->get(X86::MOVAPSmr))
9460 .addFrameIndex(RegSaveFrameIndex)
9461 .addImm(/*Scale=*/1)
9462 .addReg(/*IndexReg=*/0)
9463 .addImm(/*Disp=*/Offset)
9464 .addReg(/*Segment=*/0)
9465 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +00009466 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +00009467 }
9468
Dan Gohman14152b42010-07-06 20:24:04 +00009469 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +00009470
9471 return EndMBB;
9472}
Mon P Wang63307c32008-05-05 19:05:59 +00009473
Evan Cheng60c07e12006-07-05 22:17:51 +00009474MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +00009475X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00009476 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +00009477 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9478 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +00009479
Chris Lattner52600972009-09-02 05:57:00 +00009480 // To "insert" a SELECT_CC instruction, we actually have to insert the
9481 // diamond control-flow pattern. The incoming instruction knows the
9482 // destination vreg to set, the condition code register to branch on, the
9483 // true/false values to select between, and a branch opcode to use.
9484 const BasicBlock *LLVM_BB = BB->getBasicBlock();
9485 MachineFunction::iterator It = BB;
9486 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +00009487
Chris Lattner52600972009-09-02 05:57:00 +00009488 // thisMBB:
9489 // ...
9490 // TrueVal = ...
9491 // cmpTY ccX, r1, r2
9492 // bCC copy1MBB
9493 // fallthrough --> copy0MBB
9494 MachineBasicBlock *thisMBB = BB;
9495 MachineFunction *F = BB->getParent();
9496 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
9497 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +00009498 F->insert(It, copy0MBB);
9499 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +00009500
Bill Wendling730c07e2010-06-25 20:48:10 +00009501 // If the EFLAGS register isn't dead in the terminator, then claim that it's
9502 // live into the sink and copy blocks.
9503 const MachineFunction *MF = BB->getParent();
9504 const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo();
9505 BitVector ReservedRegs = TRI->getReservedRegs(*MF);
Bill Wendling730c07e2010-06-25 20:48:10 +00009506
Dan Gohman14152b42010-07-06 20:24:04 +00009507 for (unsigned I = 0, E = MI->getNumOperands(); I != E; ++I) {
9508 const MachineOperand &MO = MI->getOperand(I);
9509 if (!MO.isReg() || !MO.isUse() || MO.isKill()) continue;
Bill Wendling730c07e2010-06-25 20:48:10 +00009510 unsigned Reg = MO.getReg();
9511 if (Reg != X86::EFLAGS) continue;
9512 copy0MBB->addLiveIn(Reg);
9513 sinkMBB->addLiveIn(Reg);
9514 }
9515
Dan Gohman14152b42010-07-06 20:24:04 +00009516 // Transfer the remainder of BB and its successor edges to sinkMBB.
9517 sinkMBB->splice(sinkMBB->begin(), BB,
9518 llvm::next(MachineBasicBlock::iterator(MI)),
9519 BB->end());
9520 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
9521
9522 // Add the true and fallthrough blocks as its successors.
9523 BB->addSuccessor(copy0MBB);
9524 BB->addSuccessor(sinkMBB);
9525
9526 // Create the conditional branch instruction.
9527 unsigned Opc =
9528 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
9529 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
9530
Chris Lattner52600972009-09-02 05:57:00 +00009531 // copy0MBB:
9532 // %FalseValue = ...
9533 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +00009534 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +00009535
Chris Lattner52600972009-09-02 05:57:00 +00009536 // sinkMBB:
9537 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
9538 // ...
Dan Gohman14152b42010-07-06 20:24:04 +00009539 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
9540 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +00009541 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
9542 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
9543
Dan Gohman14152b42010-07-06 20:24:04 +00009544 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +00009545 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +00009546}
9547
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00009548MachineBasicBlock *
9549X86TargetLowering::EmitLoweredMingwAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00009550 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00009551 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9552 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00009553
9554 // The lowering is pretty easy: we're just emitting the call to _alloca. The
9555 // non-trivial part is impdef of ESP.
9556 // FIXME: The code should be tweaked as soon as we'll try to do codegen for
9557 // mingw-w64.
9558
Dan Gohman14152b42010-07-06 20:24:04 +00009559 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00009560 .addExternalSymbol("_alloca")
9561 .addReg(X86::EAX, RegState::Implicit)
9562 .addReg(X86::ESP, RegState::Implicit)
9563 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
Anton Korobeynikov9f7f83b2010-08-25 07:50:11 +00009564 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
9565 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00009566
Dan Gohman14152b42010-07-06 20:24:04 +00009567 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00009568 return BB;
9569}
Chris Lattner52600972009-09-02 05:57:00 +00009570
9571MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +00009572X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
9573 MachineBasicBlock *BB) const {
9574 // This is pretty easy. We're taking the value that we received from
9575 // our load from the relocation, sticking it in either RDI (x86-64)
9576 // or EAX and doing an indirect call. The return value will then
9577 // be in the normal return register.
Michael J. Spencerec38de22010-10-10 22:04:20 +00009578 const X86InstrInfo *TII
Eric Christopher54415362010-06-08 22:04:25 +00009579 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +00009580 DebugLoc DL = MI->getDebugLoc();
9581 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +00009582
9583 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +00009584 assert(MI->getOperand(3).isGlobal() && "This should be a global");
Michael J. Spencerec38de22010-10-10 22:04:20 +00009585
Eric Christopher30ef0e52010-06-03 04:07:48 +00009586 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +00009587 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
9588 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +00009589 .addReg(X86::RIP)
9590 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +00009591 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +00009592 MI->getOperand(3).getTargetFlags())
9593 .addReg(0);
Eric Christopher722d3152010-09-27 06:01:51 +00009594 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +00009595 addDirectMem(MIB, X86::RDI);
Eric Christopher61025492010-06-15 23:08:42 +00009596 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +00009597 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
9598 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +00009599 .addReg(0)
9600 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +00009601 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher61025492010-06-15 23:08:42 +00009602 MI->getOperand(3).getTargetFlags())
9603 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +00009604 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +00009605 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +00009606 } else {
Dan Gohman14152b42010-07-06 20:24:04 +00009607 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
9608 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +00009609 .addReg(TII->getGlobalBaseReg(F))
9610 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +00009611 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +00009612 MI->getOperand(3).getTargetFlags())
9613 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +00009614 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +00009615 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +00009616 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00009617
Dan Gohman14152b42010-07-06 20:24:04 +00009618 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +00009619 return BB;
9620}
9621
9622MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00009623X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00009624 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +00009625 switch (MI->getOpcode()) {
9626 default: assert(false && "Unexpected instr type to insert");
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00009627 case X86::MINGW_ALLOCA:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00009628 return EmitLoweredMingwAlloca(MI, BB);
Eric Christopher30ef0e52010-06-03 04:07:48 +00009629 case X86::TLSCall_32:
9630 case X86::TLSCall_64:
9631 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +00009632 case X86::CMOV_GR8:
Evan Cheng60c07e12006-07-05 22:17:51 +00009633 case X86::CMOV_FR32:
9634 case X86::CMOV_FR64:
9635 case X86::CMOV_V4F32:
9636 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +00009637 case X86::CMOV_V2I64:
Chris Lattner314a1132010-03-14 18:31:44 +00009638 case X86::CMOV_GR16:
9639 case X86::CMOV_GR32:
9640 case X86::CMOV_RFP32:
9641 case X86::CMOV_RFP64:
9642 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00009643 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +00009644
Dale Johannesen849f2142007-07-03 00:53:03 +00009645 case X86::FP32_TO_INT16_IN_MEM:
9646 case X86::FP32_TO_INT32_IN_MEM:
9647 case X86::FP32_TO_INT64_IN_MEM:
9648 case X86::FP64_TO_INT16_IN_MEM:
9649 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +00009650 case X86::FP64_TO_INT64_IN_MEM:
9651 case X86::FP80_TO_INT16_IN_MEM:
9652 case X86::FP80_TO_INT32_IN_MEM:
9653 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +00009654 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9655 DebugLoc DL = MI->getDebugLoc();
9656
Evan Cheng60c07e12006-07-05 22:17:51 +00009657 // Change the floating point control register to use "round towards zero"
9658 // mode when truncating to an integer value.
9659 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +00009660 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +00009661 addFrameReference(BuildMI(*BB, MI, DL,
9662 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00009663
9664 // Load the old value of the high byte of the control word...
9665 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +00009666 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohman14152b42010-07-06 20:24:04 +00009667 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009668 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00009669
9670 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +00009671 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +00009672 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +00009673
9674 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +00009675 addFrameReference(BuildMI(*BB, MI, DL,
9676 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00009677
9678 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +00009679 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +00009680 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +00009681
9682 // Get the X86 opcode to use.
9683 unsigned Opc;
9684 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00009685 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +00009686 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
9687 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
9688 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
9689 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
9690 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
9691 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +00009692 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
9693 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
9694 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +00009695 }
9696
9697 X86AddressMode AM;
9698 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +00009699 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +00009700 AM.BaseType = X86AddressMode::RegBase;
9701 AM.Base.Reg = Op.getReg();
9702 } else {
9703 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +00009704 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +00009705 }
9706 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +00009707 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +00009708 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00009709 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +00009710 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +00009711 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00009712 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +00009713 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +00009714 AM.GV = Op.getGlobal();
9715 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +00009716 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00009717 }
Dan Gohman14152b42010-07-06 20:24:04 +00009718 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009719 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +00009720
9721 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +00009722 addFrameReference(BuildMI(*BB, MI, DL,
9723 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00009724
Dan Gohman14152b42010-07-06 20:24:04 +00009725 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +00009726 return BB;
9727 }
Eric Christopherb120ab42009-08-18 22:50:32 +00009728 // String/text processing lowering.
9729 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00009730 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +00009731 return EmitPCMP(MI, BB, 3, false /* in-mem */);
9732 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00009733 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +00009734 return EmitPCMP(MI, BB, 3, true /* in-mem */);
9735 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00009736 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +00009737 return EmitPCMP(MI, BB, 5, false /* in mem */);
9738 case X86::PCMPESTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00009739 case X86::VPCMPESTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +00009740 return EmitPCMP(MI, BB, 5, true /* in mem */);
9741
9742 // Atomic Lowering.
Mon P Wang63307c32008-05-05 19:05:59 +00009743 case X86::ATOMAND32:
9744 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00009745 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009746 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009747 X86::NOT32r, X86::EAX,
9748 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +00009749 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +00009750 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
9751 X86::OR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009752 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009753 X86::NOT32r, X86::EAX,
9754 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +00009755 case X86::ATOMXOR32:
9756 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00009757 X86::XOR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009758 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009759 X86::NOT32r, X86::EAX,
9760 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009761 case X86::ATOMNAND32:
9762 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009763 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009764 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009765 X86::NOT32r, X86::EAX,
9766 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +00009767 case X86::ATOMMIN32:
9768 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
9769 case X86::ATOMMAX32:
9770 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
9771 case X86::ATOMUMIN32:
9772 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
9773 case X86::ATOMUMAX32:
9774 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +00009775
9776 case X86::ATOMAND16:
9777 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
9778 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009779 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009780 X86::NOT16r, X86::AX,
9781 X86::GR16RegisterClass);
9782 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +00009783 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009784 X86::OR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009785 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009786 X86::NOT16r, X86::AX,
9787 X86::GR16RegisterClass);
9788 case X86::ATOMXOR16:
9789 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
9790 X86::XOR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009791 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009792 X86::NOT16r, X86::AX,
9793 X86::GR16RegisterClass);
9794 case X86::ATOMNAND16:
9795 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
9796 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009797 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009798 X86::NOT16r, X86::AX,
9799 X86::GR16RegisterClass, true);
9800 case X86::ATOMMIN16:
9801 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
9802 case X86::ATOMMAX16:
9803 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
9804 case X86::ATOMUMIN16:
9805 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
9806 case X86::ATOMUMAX16:
9807 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
9808
9809 case X86::ATOMAND8:
9810 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
9811 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009812 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009813 X86::NOT8r, X86::AL,
9814 X86::GR8RegisterClass);
9815 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +00009816 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009817 X86::OR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009818 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009819 X86::NOT8r, X86::AL,
9820 X86::GR8RegisterClass);
9821 case X86::ATOMXOR8:
9822 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
9823 X86::XOR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009824 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009825 X86::NOT8r, X86::AL,
9826 X86::GR8RegisterClass);
9827 case X86::ATOMNAND8:
9828 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
9829 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009830 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009831 X86::NOT8r, X86::AL,
9832 X86::GR8RegisterClass, true);
9833 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009834 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +00009835 case X86::ATOMAND64:
9836 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00009837 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009838 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +00009839 X86::NOT64r, X86::RAX,
9840 X86::GR64RegisterClass);
9841 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +00009842 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
9843 X86::OR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009844 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +00009845 X86::NOT64r, X86::RAX,
9846 X86::GR64RegisterClass);
9847 case X86::ATOMXOR64:
9848 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00009849 X86::XOR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009850 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +00009851 X86::NOT64r, X86::RAX,
9852 X86::GR64RegisterClass);
9853 case X86::ATOMNAND64:
9854 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
9855 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009856 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +00009857 X86::NOT64r, X86::RAX,
9858 X86::GR64RegisterClass, true);
9859 case X86::ATOMMIN64:
9860 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
9861 case X86::ATOMMAX64:
9862 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
9863 case X86::ATOMUMIN64:
9864 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
9865 case X86::ATOMUMAX64:
9866 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009867
9868 // This group does 64-bit operations on a 32-bit host.
9869 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00009870 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009871 X86::AND32rr, X86::AND32rr,
9872 X86::AND32ri, X86::AND32ri,
9873 false);
9874 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00009875 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009876 X86::OR32rr, X86::OR32rr,
9877 X86::OR32ri, X86::OR32ri,
9878 false);
9879 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00009880 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009881 X86::XOR32rr, X86::XOR32rr,
9882 X86::XOR32ri, X86::XOR32ri,
9883 false);
9884 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00009885 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009886 X86::AND32rr, X86::AND32rr,
9887 X86::AND32ri, X86::AND32ri,
9888 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009889 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00009890 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009891 X86::ADD32rr, X86::ADC32rr,
9892 X86::ADD32ri, X86::ADC32ri,
9893 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009894 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00009895 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009896 X86::SUB32rr, X86::SBB32rr,
9897 X86::SUB32ri, X86::SBB32ri,
9898 false);
Dale Johannesen880ae362008-10-03 22:25:52 +00009899 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00009900 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +00009901 X86::MOV32rr, X86::MOV32rr,
9902 X86::MOV32ri, X86::MOV32ri,
9903 false);
Dan Gohmand6708ea2009-08-15 01:38:56 +00009904 case X86::VASTART_SAVE_XMM_REGS:
9905 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +00009906 }
9907}
9908
9909//===----------------------------------------------------------------------===//
9910// X86 Optimization Hooks
9911//===----------------------------------------------------------------------===//
9912
Dan Gohman475871a2008-07-27 21:46:04 +00009913void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00009914 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00009915 APInt &KnownZero,
9916 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00009917 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +00009918 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +00009919 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +00009920 assert((Opc >= ISD::BUILTIN_OP_END ||
9921 Opc == ISD::INTRINSIC_WO_CHAIN ||
9922 Opc == ISD::INTRINSIC_W_CHAIN ||
9923 Opc == ISD::INTRINSIC_VOID) &&
9924 "Should use MaskedValueIsZero if you don't know whether Op"
9925 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +00009926
Dan Gohmanf4f92f52008-02-13 23:07:24 +00009927 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +00009928 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +00009929 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +00009930 case X86ISD::ADD:
9931 case X86ISD::SUB:
9932 case X86ISD::SMUL:
9933 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +00009934 case X86ISD::INC:
9935 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +00009936 case X86ISD::OR:
9937 case X86ISD::XOR:
9938 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +00009939 // These nodes' second result is a boolean.
9940 if (Op.getResNo() == 0)
9941 break;
9942 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009943 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00009944 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
9945 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +00009946 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +00009947 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +00009948}
Chris Lattner259e97c2006-01-31 19:43:35 +00009949
Owen Andersonbc146b02010-09-21 20:42:50 +00009950unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
9951 unsigned Depth) const {
9952 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
9953 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
9954 return Op.getValueType().getScalarType().getSizeInBits();
Michael J. Spencerec38de22010-10-10 22:04:20 +00009955
Owen Andersonbc146b02010-09-21 20:42:50 +00009956 // Fallback case.
9957 return 1;
9958}
9959
Evan Cheng206ee9d2006-07-07 08:33:52 +00009960/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +00009961/// node is a GlobalAddress + offset.
9962bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +00009963 const GlobalValue* &GA,
9964 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +00009965 if (N->getOpcode() == X86ISD::Wrapper) {
9966 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00009967 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00009968 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +00009969 return true;
9970 }
Evan Cheng206ee9d2006-07-07 08:33:52 +00009971 }
Evan Chengad4196b2008-05-12 19:56:52 +00009972 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +00009973}
9974
Evan Cheng206ee9d2006-07-07 08:33:52 +00009975/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
9976/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
9977/// if the load addresses are consecutive, non-overlapping, and in the right
Nate Begemanfdea31a2010-03-24 20:49:50 +00009978/// order.
Dan Gohman475871a2008-07-27 21:46:04 +00009979static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Nate Begeman9008ca62009-04-27 18:41:29 +00009980 const TargetLowering &TLI) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00009981 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00009982 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +00009983
Eli Friedman7a5e5552009-06-07 06:52:44 +00009984 if (VT.getSizeInBits() != 128)
9985 return SDValue();
9986
Nate Begemanfdea31a2010-03-24 20:49:50 +00009987 SmallVector<SDValue, 16> Elts;
9988 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00009989 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00009990
Nate Begemanfdea31a2010-03-24 20:49:50 +00009991 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00009992}
Evan Chengd880b972008-05-09 21:53:03 +00009993
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +00009994/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
9995/// generation and convert it from being a bunch of shuffles and extracts
9996/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +00009997static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
9998 const TargetLowering &TLI) {
9999 SDValue InputVector = N->getOperand(0);
10000
10001 // Only operate on vectors of 4 elements, where the alternative shuffling
10002 // gets to be more expensive.
10003 if (InputVector.getValueType() != MVT::v4i32)
10004 return SDValue();
10005
10006 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
10007 // single use which is a sign-extend or zero-extend, and all elements are
10008 // used.
10009 SmallVector<SDNode *, 4> Uses;
10010 unsigned ExtractedElements = 0;
10011 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
10012 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
10013 if (UI.getUse().getResNo() != InputVector.getResNo())
10014 return SDValue();
10015
10016 SDNode *Extract = *UI;
10017 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
10018 return SDValue();
10019
10020 if (Extract->getValueType(0) != MVT::i32)
10021 return SDValue();
10022 if (!Extract->hasOneUse())
10023 return SDValue();
10024 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
10025 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
10026 return SDValue();
10027 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
10028 return SDValue();
10029
10030 // Record which element was extracted.
10031 ExtractedElements |=
10032 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
10033
10034 Uses.push_back(Extract);
10035 }
10036
10037 // If not all the elements were used, this may not be worthwhile.
10038 if (ExtractedElements != 15)
10039 return SDValue();
10040
10041 // Ok, we've now decided to do the transformation.
10042 DebugLoc dl = InputVector.getDebugLoc();
10043
10044 // Store the value to a temporary stack slot.
10045 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000010046 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
10047 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010048
10049 // Replace each use (extract) with a load of the appropriate element.
10050 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
10051 UE = Uses.end(); UI != UE; ++UI) {
10052 SDNode *Extract = *UI;
10053
10054 // Compute the element's address.
10055 SDValue Idx = Extract->getOperand(1);
10056 unsigned EltSize =
10057 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
10058 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
10059 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
10060
Eric Christopher90eb4022010-07-22 00:26:08 +000010061 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, Idx.getValueType(),
Chris Lattner51abfe42010-09-21 06:02:19 +000010062 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010063
10064 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000010065 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000010066 ScalarAddr, MachinePointerInfo(),
10067 false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010068
10069 // Replace the exact with the load.
10070 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
10071 }
10072
10073 // The replacement was made in place; don't return anything.
10074 return SDValue();
10075}
10076
Chris Lattner83e6c992006-10-04 06:57:07 +000010077/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000010078static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner47b4ce82009-03-11 05:48:52 +000010079 const X86Subtarget *Subtarget) {
10080 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +000010081 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000010082 // Get the LHS/RHS of the select.
10083 SDValue LHS = N->getOperand(1);
10084 SDValue RHS = N->getOperand(2);
Eric Christopherfd179292009-08-27 18:07:15 +000010085
Dan Gohman670e5392009-09-21 18:03:22 +000010086 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000010087 // instructions match the semantics of the common C idiom x<y?x:y but not
10088 // x<=y?x:y, because of how they handle negative zero (which can be
10089 // ignored in unsafe-math mode).
Chris Lattner83e6c992006-10-04 06:57:07 +000010090 if (Subtarget->hasSSE2() &&
Owen Anderson825b72b2009-08-11 20:47:22 +000010091 (LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64) &&
Chris Lattner47b4ce82009-03-11 05:48:52 +000010092 Cond.getOpcode() == ISD::SETCC) {
10093 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010094
Chris Lattner47b4ce82009-03-11 05:48:52 +000010095 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000010096 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000010097 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
10098 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000010099 switch (CC) {
10100 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000010101 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000010102 // Converting this to a min would handle NaNs incorrectly, and swapping
10103 // the operands would cause it to handle comparisons between positive
10104 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000010105 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000010106 if (!UnsafeFPMath &&
10107 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
10108 break;
10109 std::swap(LHS, RHS);
10110 }
Dan Gohman670e5392009-09-21 18:03:22 +000010111 Opcode = X86ISD::FMIN;
10112 break;
10113 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000010114 // Converting this to a min would handle comparisons between positive
10115 // and negative zero incorrectly.
10116 if (!UnsafeFPMath &&
10117 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
10118 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010119 Opcode = X86ISD::FMIN;
10120 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000010121 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000010122 // Converting this to a min would handle both negative zeros and NaNs
10123 // incorrectly, but we can swap the operands to fix both.
10124 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000010125 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010126 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000010127 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010128 Opcode = X86ISD::FMIN;
10129 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010130
Dan Gohman670e5392009-09-21 18:03:22 +000010131 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000010132 // Converting this to a max would handle comparisons between positive
10133 // and negative zero incorrectly.
10134 if (!UnsafeFPMath &&
10135 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(LHS))
10136 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010137 Opcode = X86ISD::FMAX;
10138 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000010139 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000010140 // Converting this to a max would handle NaNs incorrectly, and swapping
10141 // the operands would cause it to handle comparisons between positive
10142 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000010143 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000010144 if (!UnsafeFPMath &&
10145 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
10146 break;
10147 std::swap(LHS, RHS);
10148 }
Dan Gohman670e5392009-09-21 18:03:22 +000010149 Opcode = X86ISD::FMAX;
10150 break;
10151 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000010152 // Converting this to a max would handle both negative zeros and NaNs
10153 // incorrectly, but we can swap the operands to fix both.
10154 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000010155 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010156 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010157 case ISD::SETGE:
10158 Opcode = X86ISD::FMAX;
10159 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000010160 }
Dan Gohman670e5392009-09-21 18:03:22 +000010161 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000010162 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
10163 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000010164 switch (CC) {
10165 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000010166 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000010167 // Converting this to a min would handle comparisons between positive
10168 // and negative zero incorrectly, and swapping the operands would
10169 // cause it to handle NaNs incorrectly.
10170 if (!UnsafeFPMath &&
10171 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000010172 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000010173 break;
10174 std::swap(LHS, RHS);
10175 }
Dan Gohman670e5392009-09-21 18:03:22 +000010176 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000010177 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010178 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000010179 // Converting this to a min would handle NaNs incorrectly.
10180 if (!UnsafeFPMath &&
10181 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
10182 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010183 Opcode = X86ISD::FMIN;
10184 break;
10185 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000010186 // Converting this to a min would handle both negative zeros and NaNs
10187 // incorrectly, but we can swap the operands to fix both.
10188 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000010189 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010190 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010191 case ISD::SETGE:
10192 Opcode = X86ISD::FMIN;
10193 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010194
Dan Gohman670e5392009-09-21 18:03:22 +000010195 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000010196 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000010197 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000010198 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010199 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000010200 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010201 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000010202 // Converting this to a max would handle comparisons between positive
10203 // and negative zero incorrectly, and swapping the operands would
10204 // cause it to handle NaNs incorrectly.
10205 if (!UnsafeFPMath &&
10206 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000010207 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000010208 break;
10209 std::swap(LHS, RHS);
10210 }
Dan Gohman670e5392009-09-21 18:03:22 +000010211 Opcode = X86ISD::FMAX;
10212 break;
10213 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000010214 // Converting this to a max would handle both negative zeros and NaNs
10215 // incorrectly, but we can swap the operands to fix both.
10216 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000010217 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010218 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000010219 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010220 Opcode = X86ISD::FMAX;
10221 break;
10222 }
Chris Lattner83e6c992006-10-04 06:57:07 +000010223 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010224
Chris Lattner47b4ce82009-03-11 05:48:52 +000010225 if (Opcode)
10226 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000010227 }
Eric Christopherfd179292009-08-27 18:07:15 +000010228
Chris Lattnerd1980a52009-03-12 06:52:53 +000010229 // If this is a select between two integer constants, try to do some
10230 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000010231 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
10232 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000010233 // Don't do this for crazy integer types.
10234 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
10235 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000010236 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000010237 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000010238
Chris Lattnercee56e72009-03-13 05:53:31 +000010239 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000010240 // Efficiently invertible.
10241 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
10242 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
10243 isa<ConstantSDNode>(Cond.getOperand(1))))) {
10244 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000010245 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000010246 }
Eric Christopherfd179292009-08-27 18:07:15 +000010247
Chris Lattnerd1980a52009-03-12 06:52:53 +000010248 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000010249 if (FalseC->getAPIntValue() == 0 &&
10250 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000010251 if (NeedsCondInvert) // Invert the condition if needed.
10252 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
10253 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000010254
Chris Lattnerd1980a52009-03-12 06:52:53 +000010255 // Zero extend the condition if needed.
10256 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000010257
Chris Lattnercee56e72009-03-13 05:53:31 +000010258 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000010259 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000010260 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000010261 }
Eric Christopherfd179292009-08-27 18:07:15 +000010262
Chris Lattner97a29a52009-03-13 05:22:11 +000010263 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000010264 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +000010265 if (NeedsCondInvert) // Invert the condition if needed.
10266 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
10267 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000010268
Chris Lattner97a29a52009-03-13 05:22:11 +000010269 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000010270 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
10271 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000010272 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000010273 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000010274 }
Eric Christopherfd179292009-08-27 18:07:15 +000010275
Chris Lattnercee56e72009-03-13 05:53:31 +000010276 // Optimize cases that will turn into an LEA instruction. This requires
10277 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000010278 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000010279 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000010280 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000010281
Chris Lattnercee56e72009-03-13 05:53:31 +000010282 bool isFastMultiplier = false;
10283 if (Diff < 10) {
10284 switch ((unsigned char)Diff) {
10285 default: break;
10286 case 1: // result = add base, cond
10287 case 2: // result = lea base( , cond*2)
10288 case 3: // result = lea base(cond, cond*2)
10289 case 4: // result = lea base( , cond*4)
10290 case 5: // result = lea base(cond, cond*4)
10291 case 8: // result = lea base( , cond*8)
10292 case 9: // result = lea base(cond, cond*8)
10293 isFastMultiplier = true;
10294 break;
10295 }
10296 }
Eric Christopherfd179292009-08-27 18:07:15 +000010297
Chris Lattnercee56e72009-03-13 05:53:31 +000010298 if (isFastMultiplier) {
10299 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
10300 if (NeedsCondInvert) // Invert the condition if needed.
10301 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
10302 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000010303
Chris Lattnercee56e72009-03-13 05:53:31 +000010304 // Zero extend the condition if needed.
10305 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
10306 Cond);
10307 // Scale the condition by the difference.
10308 if (Diff != 1)
10309 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
10310 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000010311
Chris Lattnercee56e72009-03-13 05:53:31 +000010312 // Add the base if non-zero.
10313 if (FalseC->getAPIntValue() != 0)
10314 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
10315 SDValue(FalseC, 0));
10316 return Cond;
10317 }
Eric Christopherfd179292009-08-27 18:07:15 +000010318 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000010319 }
10320 }
Eric Christopherfd179292009-08-27 18:07:15 +000010321
Dan Gohman475871a2008-07-27 21:46:04 +000010322 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000010323}
10324
Chris Lattnerd1980a52009-03-12 06:52:53 +000010325/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
10326static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
10327 TargetLowering::DAGCombinerInfo &DCI) {
10328 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +000010329
Chris Lattnerd1980a52009-03-12 06:52:53 +000010330 // If the flag operand isn't dead, don't touch this CMOV.
10331 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
10332 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +000010333
Chris Lattnerd1980a52009-03-12 06:52:53 +000010334 // If this is a select between two integer constants, try to do some
10335 // optimizations. Note that the operands are ordered the opposite of SELECT
10336 // operands.
10337 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
10338 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
10339 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
10340 // larger than FalseC (the false value).
10341 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
Eric Christopherfd179292009-08-27 18:07:15 +000010342
Chris Lattnerd1980a52009-03-12 06:52:53 +000010343 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
10344 CC = X86::GetOppositeBranchCondition(CC);
10345 std::swap(TrueC, FalseC);
10346 }
Eric Christopherfd179292009-08-27 18:07:15 +000010347
Chris Lattnerd1980a52009-03-12 06:52:53 +000010348 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000010349 // This is efficient for any integer data type (including i8/i16) and
10350 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000010351 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
10352 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +000010353 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
10354 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000010355
Chris Lattnerd1980a52009-03-12 06:52:53 +000010356 // Zero extend the condition if needed.
10357 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000010358
Chris Lattnerd1980a52009-03-12 06:52:53 +000010359 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
10360 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000010361 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000010362 if (N->getNumValues() == 2) // Dead flag value?
10363 return DCI.CombineTo(N, Cond, SDValue());
10364 return Cond;
10365 }
Eric Christopherfd179292009-08-27 18:07:15 +000010366
Chris Lattnercee56e72009-03-13 05:53:31 +000010367 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
10368 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000010369 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
10370 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +000010371 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
10372 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000010373
Chris Lattner97a29a52009-03-13 05:22:11 +000010374 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000010375 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
10376 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000010377 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
10378 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000010379
Chris Lattner97a29a52009-03-13 05:22:11 +000010380 if (N->getNumValues() == 2) // Dead flag value?
10381 return DCI.CombineTo(N, Cond, SDValue());
10382 return Cond;
10383 }
Eric Christopherfd179292009-08-27 18:07:15 +000010384
Chris Lattnercee56e72009-03-13 05:53:31 +000010385 // Optimize cases that will turn into an LEA instruction. This requires
10386 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000010387 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000010388 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000010389 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000010390
Chris Lattnercee56e72009-03-13 05:53:31 +000010391 bool isFastMultiplier = false;
10392 if (Diff < 10) {
10393 switch ((unsigned char)Diff) {
10394 default: break;
10395 case 1: // result = add base, cond
10396 case 2: // result = lea base( , cond*2)
10397 case 3: // result = lea base(cond, cond*2)
10398 case 4: // result = lea base( , cond*4)
10399 case 5: // result = lea base(cond, cond*4)
10400 case 8: // result = lea base( , cond*8)
10401 case 9: // result = lea base(cond, cond*8)
10402 isFastMultiplier = true;
10403 break;
10404 }
10405 }
Eric Christopherfd179292009-08-27 18:07:15 +000010406
Chris Lattnercee56e72009-03-13 05:53:31 +000010407 if (isFastMultiplier) {
10408 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
10409 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +000010410 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
10411 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000010412 // Zero extend the condition if needed.
10413 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
10414 Cond);
10415 // Scale the condition by the difference.
10416 if (Diff != 1)
10417 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
10418 DAG.getConstant(Diff, Cond.getValueType()));
10419
10420 // Add the base if non-zero.
10421 if (FalseC->getAPIntValue() != 0)
10422 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
10423 SDValue(FalseC, 0));
10424 if (N->getNumValues() == 2) // Dead flag value?
10425 return DCI.CombineTo(N, Cond, SDValue());
10426 return Cond;
10427 }
Eric Christopherfd179292009-08-27 18:07:15 +000010428 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000010429 }
10430 }
10431 return SDValue();
10432}
10433
10434
Evan Cheng0b0cd912009-03-28 05:57:29 +000010435/// PerformMulCombine - Optimize a single multiply with constant into two
10436/// in order to implement it with two cheaper instructions, e.g.
10437/// LEA + SHL, LEA + LEA.
10438static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
10439 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000010440 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
10441 return SDValue();
10442
Owen Andersone50ed302009-08-10 22:56:29 +000010443 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010444 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000010445 return SDValue();
10446
10447 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
10448 if (!C)
10449 return SDValue();
10450 uint64_t MulAmt = C->getZExtValue();
10451 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
10452 return SDValue();
10453
10454 uint64_t MulAmt1 = 0;
10455 uint64_t MulAmt2 = 0;
10456 if ((MulAmt % 9) == 0) {
10457 MulAmt1 = 9;
10458 MulAmt2 = MulAmt / 9;
10459 } else if ((MulAmt % 5) == 0) {
10460 MulAmt1 = 5;
10461 MulAmt2 = MulAmt / 5;
10462 } else if ((MulAmt % 3) == 0) {
10463 MulAmt1 = 3;
10464 MulAmt2 = MulAmt / 3;
10465 }
10466 if (MulAmt2 &&
10467 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
10468 DebugLoc DL = N->getDebugLoc();
10469
10470 if (isPowerOf2_64(MulAmt2) &&
10471 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
10472 // If second multiplifer is pow2, issue it first. We want the multiply by
10473 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
10474 // is an add.
10475 std::swap(MulAmt1, MulAmt2);
10476
10477 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000010478 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000010479 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000010480 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000010481 else
Evan Cheng73f24c92009-03-30 21:36:47 +000010482 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000010483 DAG.getConstant(MulAmt1, VT));
10484
Eric Christopherfd179292009-08-27 18:07:15 +000010485 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000010486 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000010487 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000010488 else
Evan Cheng73f24c92009-03-30 21:36:47 +000010489 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000010490 DAG.getConstant(MulAmt2, VT));
10491
10492 // Do not add new nodes to DAG combiner worklist.
10493 DCI.CombineTo(N, NewMul, false);
10494 }
10495 return SDValue();
10496}
10497
Evan Chengad9c0a32009-12-15 00:53:42 +000010498static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
10499 SDValue N0 = N->getOperand(0);
10500 SDValue N1 = N->getOperand(1);
10501 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
10502 EVT VT = N0.getValueType();
10503
10504 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
10505 // since the result of setcc_c is all zero's or all ones.
10506 if (N1C && N0.getOpcode() == ISD::AND &&
10507 N0.getOperand(1).getOpcode() == ISD::Constant) {
10508 SDValue N00 = N0.getOperand(0);
10509 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
10510 ((N00.getOpcode() == ISD::ANY_EXTEND ||
10511 N00.getOpcode() == ISD::ZERO_EXTEND) &&
10512 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
10513 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
10514 APInt ShAmt = N1C->getAPIntValue();
10515 Mask = Mask.shl(ShAmt);
10516 if (Mask != 0)
10517 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
10518 N00, DAG.getConstant(Mask, VT));
10519 }
10520 }
10521
10522 return SDValue();
10523}
Evan Cheng0b0cd912009-03-28 05:57:29 +000010524
Nate Begeman740ab032009-01-26 00:52:55 +000010525/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
10526/// when possible.
10527static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
10528 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +000010529 EVT VT = N->getValueType(0);
10530 if (!VT.isVector() && VT.isInteger() &&
10531 N->getOpcode() == ISD::SHL)
10532 return PerformSHLCombine(N, DAG);
10533
Nate Begeman740ab032009-01-26 00:52:55 +000010534 // On X86 with SSE2 support, we can transform this to a vector shift if
10535 // all elements are shifted by the same amount. We can't do this in legalize
10536 // because the a constant vector is typically transformed to a constant pool
10537 // so we have no knowledge of the shift amount.
Nate Begemanc2fd67f2009-01-26 03:15:31 +000010538 if (!Subtarget->hasSSE2())
10539 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000010540
Owen Anderson825b72b2009-08-11 20:47:22 +000010541 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
Nate Begemanc2fd67f2009-01-26 03:15:31 +000010542 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000010543
Mon P Wang3becd092009-01-28 08:12:05 +000010544 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +000010545 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +000010546 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +000010547 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +000010548 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
10549 unsigned NumElts = VT.getVectorNumElements();
10550 unsigned i = 0;
10551 for (; i != NumElts; ++i) {
10552 SDValue Arg = ShAmtOp.getOperand(i);
10553 if (Arg.getOpcode() == ISD::UNDEF) continue;
10554 BaseShAmt = Arg;
10555 break;
10556 }
10557 for (; i != NumElts; ++i) {
10558 SDValue Arg = ShAmtOp.getOperand(i);
10559 if (Arg.getOpcode() == ISD::UNDEF) continue;
10560 if (Arg != BaseShAmt) {
10561 return SDValue();
10562 }
10563 }
10564 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +000010565 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +000010566 SDValue InVec = ShAmtOp.getOperand(0);
10567 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
10568 unsigned NumElts = InVec.getValueType().getVectorNumElements();
10569 unsigned i = 0;
10570 for (; i != NumElts; ++i) {
10571 SDValue Arg = InVec.getOperand(i);
10572 if (Arg.getOpcode() == ISD::UNDEF) continue;
10573 BaseShAmt = Arg;
10574 break;
10575 }
10576 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
10577 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +000010578 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +000010579 if (C->getZExtValue() == SplatIdx)
10580 BaseShAmt = InVec.getOperand(1);
10581 }
10582 }
10583 if (BaseShAmt.getNode() == 0)
10584 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
10585 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +000010586 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +000010587 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000010588
Mon P Wangefa42202009-09-03 19:56:25 +000010589 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +000010590 if (EltVT.bitsGT(MVT::i32))
10591 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
10592 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +000010593 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +000010594
Nate Begemanc2fd67f2009-01-26 03:15:31 +000010595 // The shift amount is identical so we can do a vector shift.
10596 SDValue ValOp = N->getOperand(0);
10597 switch (N->getOpcode()) {
10598 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000010599 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +000010600 break;
10601 case ISD::SHL:
Owen Anderson825b72b2009-08-11 20:47:22 +000010602 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000010603 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010604 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000010605 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000010606 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000010607 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010608 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000010609 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000010610 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000010611 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010612 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000010613 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000010614 break;
10615 case ISD::SRA:
Owen Anderson825b72b2009-08-11 20:47:22 +000010616 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000010617 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010618 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000010619 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000010620 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000010621 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010622 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000010623 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000010624 break;
10625 case ISD::SRL:
Owen Anderson825b72b2009-08-11 20:47:22 +000010626 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000010627 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010628 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000010629 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000010630 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000010631 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010632 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000010633 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000010634 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000010635 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010636 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000010637 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000010638 break;
Nate Begeman740ab032009-01-26 00:52:55 +000010639 }
10640 return SDValue();
10641}
10642
Evan Cheng760d1942010-01-04 21:22:48 +000010643static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000010644 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000010645 const X86Subtarget *Subtarget) {
Evan Cheng39cfeec2010-04-28 02:25:18 +000010646 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000010647 return SDValue();
10648
Evan Cheng760d1942010-01-04 21:22:48 +000010649 EVT VT = N->getValueType(0);
Evan Cheng8b1190a2010-04-28 01:18:01 +000010650 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64)
Evan Cheng760d1942010-01-04 21:22:48 +000010651 return SDValue();
10652
10653 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
10654 SDValue N0 = N->getOperand(0);
10655 SDValue N1 = N->getOperand(1);
10656 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
10657 std::swap(N0, N1);
10658 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
10659 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000010660 if (!N0.hasOneUse() || !N1.hasOneUse())
10661 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000010662
10663 SDValue ShAmt0 = N0.getOperand(1);
10664 if (ShAmt0.getValueType() != MVT::i8)
10665 return SDValue();
10666 SDValue ShAmt1 = N1.getOperand(1);
10667 if (ShAmt1.getValueType() != MVT::i8)
10668 return SDValue();
10669 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
10670 ShAmt0 = ShAmt0.getOperand(0);
10671 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
10672 ShAmt1 = ShAmt1.getOperand(0);
10673
10674 DebugLoc DL = N->getDebugLoc();
10675 unsigned Opc = X86ISD::SHLD;
10676 SDValue Op0 = N0.getOperand(0);
10677 SDValue Op1 = N1.getOperand(0);
10678 if (ShAmt0.getOpcode() == ISD::SUB) {
10679 Opc = X86ISD::SHRD;
10680 std::swap(Op0, Op1);
10681 std::swap(ShAmt0, ShAmt1);
10682 }
10683
Evan Cheng8b1190a2010-04-28 01:18:01 +000010684 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000010685 if (ShAmt1.getOpcode() == ISD::SUB) {
10686 SDValue Sum = ShAmt1.getOperand(0);
10687 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000010688 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
10689 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
10690 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
10691 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000010692 return DAG.getNode(Opc, DL, VT,
10693 Op0, Op1,
10694 DAG.getNode(ISD::TRUNCATE, DL,
10695 MVT::i8, ShAmt0));
10696 }
10697 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
10698 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
10699 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000010700 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000010701 return DAG.getNode(Opc, DL, VT,
10702 N0.getOperand(0), N1.getOperand(0),
10703 DAG.getNode(ISD::TRUNCATE, DL,
10704 MVT::i8, ShAmt0));
10705 }
10706
10707 return SDValue();
10708}
10709
Chris Lattner149a4e52008-02-22 02:09:43 +000010710/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000010711static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000010712 const X86Subtarget *Subtarget) {
Chris Lattner149a4e52008-02-22 02:09:43 +000010713 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
10714 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000010715 // A preferable solution to the general problem is to figure out the right
10716 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000010717
10718 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng7e2ff772008-05-08 00:57:18 +000010719 StoreSDNode *St = cast<StoreSDNode>(N);
Owen Andersone50ed302009-08-10 22:56:29 +000010720 EVT VT = St->getValue().getValueType();
Evan Cheng536e6672009-03-12 05:59:15 +000010721 if (VT.getSizeInBits() != 64)
10722 return SDValue();
10723
Devang Patel578efa92009-06-05 21:57:13 +000010724 const Function *F = DAG.getMachineFunction().getFunction();
10725 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Eric Christopherfd179292009-08-27 18:07:15 +000010726 bool F64IsLegal = !UseSoftFloat && !NoImplicitFloatOps
Devang Patel578efa92009-06-05 21:57:13 +000010727 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +000010728 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000010729 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000010730 isa<LoadSDNode>(St->getValue()) &&
10731 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
10732 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000010733 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000010734 LoadSDNode *Ld = 0;
10735 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000010736 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000010737 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000010738 // Must be a store of a load. We currently handle two cases: the load
10739 // is a direct child, and it's under an intervening TokenFactor. It is
10740 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000010741 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000010742 Ld = cast<LoadSDNode>(St->getChain());
10743 else if (St->getValue().hasOneUse() &&
10744 ChainVal->getOpcode() == ISD::TokenFactor) {
10745 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000010746 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000010747 TokenFactorIndex = i;
10748 Ld = cast<LoadSDNode>(St->getValue());
10749 } else
10750 Ops.push_back(ChainVal->getOperand(i));
10751 }
10752 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000010753
Evan Cheng536e6672009-03-12 05:59:15 +000010754 if (!Ld || !ISD::isNormalLoad(Ld))
10755 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000010756
Evan Cheng536e6672009-03-12 05:59:15 +000010757 // If this is not the MMX case, i.e. we are just turning i64 load/store
10758 // into f64 load/store, avoid the transformation if there are multiple
10759 // uses of the loaded value.
10760 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
10761 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000010762
Evan Cheng536e6672009-03-12 05:59:15 +000010763 DebugLoc LdDL = Ld->getDebugLoc();
10764 DebugLoc StDL = N->getDebugLoc();
10765 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
10766 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
10767 // pair instead.
10768 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000010769 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000010770 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
10771 Ld->getPointerInfo(), Ld->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000010772 Ld->isNonTemporal(), Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000010773 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000010774 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000010775 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000010776 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000010777 Ops.size());
10778 }
Evan Cheng536e6672009-03-12 05:59:15 +000010779 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000010780 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000010781 St->isVolatile(), St->isNonTemporal(),
10782 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000010783 }
Evan Cheng536e6672009-03-12 05:59:15 +000010784
10785 // Otherwise, lower to two pairs of 32-bit loads / stores.
10786 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000010787 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
10788 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000010789
Owen Anderson825b72b2009-08-11 20:47:22 +000010790 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000010791 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000010792 Ld->isVolatile(), Ld->isNonTemporal(),
10793 Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000010794 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000010795 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000010796 Ld->isVolatile(), Ld->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000010797 MinAlign(Ld->getAlignment(), 4));
10798
10799 SDValue NewChain = LoLd.getValue(1);
10800 if (TokenFactorIndex != -1) {
10801 Ops.push_back(LoLd);
10802 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000010803 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000010804 Ops.size());
10805 }
10806
10807 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000010808 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
10809 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000010810
10811 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000010812 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000010813 St->isVolatile(), St->isNonTemporal(),
10814 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000010815 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000010816 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000010817 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000010818 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000010819 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000010820 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000010821 }
Dan Gohman475871a2008-07-27 21:46:04 +000010822 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000010823}
10824
Chris Lattner6cf73262008-01-25 06:14:17 +000010825/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
10826/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000010827static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000010828 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
10829 // F[X]OR(0.0, x) -> x
10830 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000010831 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
10832 if (C->getValueAPF().isPosZero())
10833 return N->getOperand(1);
10834 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
10835 if (C->getValueAPF().isPosZero())
10836 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000010837 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000010838}
10839
10840/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000010841static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000010842 // FAND(0.0, x) -> 0.0
10843 // FAND(x, 0.0) -> 0.0
10844 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
10845 if (C->getValueAPF().isPosZero())
10846 return N->getOperand(0);
10847 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
10848 if (C->getValueAPF().isPosZero())
10849 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000010850 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000010851}
10852
Dan Gohmane5af2d32009-01-29 01:59:02 +000010853static SDValue PerformBTCombine(SDNode *N,
10854 SelectionDAG &DAG,
10855 TargetLowering::DAGCombinerInfo &DCI) {
10856 // BT ignores high bits in the bit index operand.
10857 SDValue Op1 = N->getOperand(1);
10858 if (Op1.hasOneUse()) {
10859 unsigned BitWidth = Op1.getValueSizeInBits();
10860 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
10861 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000010862 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
10863 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000010864 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000010865 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
10866 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
10867 DCI.CommitTargetLoweringOpt(TLO);
10868 }
10869 return SDValue();
10870}
Chris Lattner83e6c992006-10-04 06:57:07 +000010871
Eli Friedman7a5e5552009-06-07 06:52:44 +000010872static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
10873 SDValue Op = N->getOperand(0);
10874 if (Op.getOpcode() == ISD::BIT_CONVERT)
10875 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000010876 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000010877 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000010878 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000010879 OpVT.getVectorElementType().getSizeInBits()) {
10880 return DAG.getNode(ISD::BIT_CONVERT, N->getDebugLoc(), VT, Op);
10881 }
10882 return SDValue();
10883}
10884
Evan Cheng2e489c42009-12-16 00:53:11 +000010885static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG) {
10886 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
10887 // (and (i32 x86isd::setcc_carry), 1)
10888 // This eliminates the zext. This transformation is necessary because
10889 // ISD::SETCC is always legalized to i8.
10890 DebugLoc dl = N->getDebugLoc();
10891 SDValue N0 = N->getOperand(0);
10892 EVT VT = N->getValueType(0);
10893 if (N0.getOpcode() == ISD::AND &&
10894 N0.hasOneUse() &&
10895 N0.getOperand(0).hasOneUse()) {
10896 SDValue N00 = N0.getOperand(0);
10897 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
10898 return SDValue();
10899 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
10900 if (!C || C->getZExtValue() != 1)
10901 return SDValue();
10902 return DAG.getNode(ISD::AND, dl, VT,
10903 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
10904 N00.getOperand(0), N00.getOperand(1)),
10905 DAG.getConstant(1, VT));
10906 }
10907
10908 return SDValue();
10909}
10910
Dan Gohman475871a2008-07-27 21:46:04 +000010911SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000010912 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000010913 SelectionDAG &DAG = DCI.DAG;
10914 switch (N->getOpcode()) {
10915 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010916 case ISD::EXTRACT_VECTOR_ELT:
10917 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, *this);
Chris Lattneraf723b92008-01-25 05:46:26 +000010918 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +000010919 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000010920 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000010921 case ISD::SHL:
10922 case ISD::SRA:
10923 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000010924 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000010925 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +000010926 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000010927 case X86ISD::FOR: return PerformFORCombine(N, DAG);
10928 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000010929 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000010930 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Evan Cheng2e489c42009-12-16 00:53:11 +000010931 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000010932 case X86ISD::SHUFPS: // Handle all target specific shuffles
10933 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +000010934 case X86ISD::PALIGN:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000010935 case X86ISD::PUNPCKHBW:
10936 case X86ISD::PUNPCKHWD:
10937 case X86ISD::PUNPCKHDQ:
10938 case X86ISD::PUNPCKHQDQ:
10939 case X86ISD::UNPCKHPS:
10940 case X86ISD::UNPCKHPD:
10941 case X86ISD::PUNPCKLBW:
10942 case X86ISD::PUNPCKLWD:
10943 case X86ISD::PUNPCKLDQ:
10944 case X86ISD::PUNPCKLQDQ:
10945 case X86ISD::UNPCKLPS:
10946 case X86ISD::UNPCKLPD:
10947 case X86ISD::MOVHLPS:
10948 case X86ISD::MOVLHPS:
10949 case X86ISD::PSHUFD:
10950 case X86ISD::PSHUFHW:
10951 case X86ISD::PSHUFLW:
10952 case X86ISD::MOVSS:
10953 case X86ISD::MOVSD:
10954 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, *this);
Evan Cheng206ee9d2006-07-07 08:33:52 +000010955 }
10956
Dan Gohman475871a2008-07-27 21:46:04 +000010957 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000010958}
10959
Evan Chenge5b51ac2010-04-17 06:13:15 +000010960/// isTypeDesirableForOp - Return true if the target has native support for
10961/// the specified value type and it is 'desirable' to use the type for the
10962/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
10963/// instruction encodings are longer and some i16 instructions are slow.
10964bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
10965 if (!isTypeLegal(VT))
10966 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000010967 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000010968 return true;
10969
10970 switch (Opc) {
10971 default:
10972 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000010973 case ISD::LOAD:
10974 case ISD::SIGN_EXTEND:
10975 case ISD::ZERO_EXTEND:
10976 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000010977 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000010978 case ISD::SRL:
10979 case ISD::SUB:
10980 case ISD::ADD:
10981 case ISD::MUL:
10982 case ISD::AND:
10983 case ISD::OR:
10984 case ISD::XOR:
10985 return false;
10986 }
10987}
10988
10989/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000010990/// beneficial for dag combiner to promote the specified node. If true, it
10991/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000010992bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000010993 EVT VT = Op.getValueType();
10994 if (VT != MVT::i16)
10995 return false;
10996
Evan Cheng4c26e932010-04-19 19:29:22 +000010997 bool Promote = false;
10998 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000010999 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000011000 default: break;
11001 case ISD::LOAD: {
11002 LoadSDNode *LD = cast<LoadSDNode>(Op);
11003 // If the non-extending load has a single use and it's not live out, then it
11004 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000011005 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
11006 Op.hasOneUse()*/) {
11007 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
11008 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
11009 // The only case where we'd want to promote LOAD (rather then it being
11010 // promoted as an operand is when it's only use is liveout.
11011 if (UI->getOpcode() != ISD::CopyToReg)
11012 return false;
11013 }
11014 }
Evan Cheng4c26e932010-04-19 19:29:22 +000011015 Promote = true;
11016 break;
11017 }
11018 case ISD::SIGN_EXTEND:
11019 case ISD::ZERO_EXTEND:
11020 case ISD::ANY_EXTEND:
11021 Promote = true;
11022 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000011023 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000011024 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000011025 SDValue N0 = Op.getOperand(0);
11026 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000011027 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000011028 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000011029 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000011030 break;
11031 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000011032 case ISD::ADD:
11033 case ISD::MUL:
11034 case ISD::AND:
11035 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000011036 case ISD::XOR:
11037 Commute = true;
11038 // fallthrough
11039 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000011040 SDValue N0 = Op.getOperand(0);
11041 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000011042 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000011043 return false;
11044 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000011045 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000011046 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000011047 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000011048 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000011049 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000011050 }
11051 }
11052
11053 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000011054 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000011055}
11056
Evan Cheng60c07e12006-07-05 22:17:51 +000011057//===----------------------------------------------------------------------===//
11058// X86 Inline Assembly Support
11059//===----------------------------------------------------------------------===//
11060
Chris Lattnerb8105652009-07-20 17:51:36 +000011061static bool LowerToBSwap(CallInst *CI) {
11062 // FIXME: this should verify that we are targetting a 486 or better. If not,
11063 // we will turn this bswap into something that will be lowered to logical ops
11064 // instead of emitting the bswap asm. For now, we don't support 486 or lower
11065 // so don't worry about this.
Eric Christopherfd179292009-08-27 18:07:15 +000011066
Chris Lattnerb8105652009-07-20 17:51:36 +000011067 // Verify this is a simple bswap.
Gabor Greife1c2b9c2010-06-30 13:03:37 +000011068 if (CI->getNumArgOperands() != 1 ||
Gabor Greif1cfe44a2010-06-26 11:51:52 +000011069 CI->getType() != CI->getArgOperand(0)->getType() ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000011070 !CI->getType()->isIntegerTy())
Chris Lattnerb8105652009-07-20 17:51:36 +000011071 return false;
Eric Christopherfd179292009-08-27 18:07:15 +000011072
Chris Lattnerb8105652009-07-20 17:51:36 +000011073 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
11074 if (!Ty || Ty->getBitWidth() % 16 != 0)
11075 return false;
Eric Christopherfd179292009-08-27 18:07:15 +000011076
Chris Lattnerb8105652009-07-20 17:51:36 +000011077 // Okay, we can do this xform, do so now.
11078 const Type *Tys[] = { Ty };
11079 Module *M = CI->getParent()->getParent()->getParent();
11080 Constant *Int = Intrinsic::getDeclaration(M, Intrinsic::bswap, Tys, 1);
Eric Christopherfd179292009-08-27 18:07:15 +000011081
Gabor Greif1cfe44a2010-06-26 11:51:52 +000011082 Value *Op = CI->getArgOperand(0);
Chris Lattnerb8105652009-07-20 17:51:36 +000011083 Op = CallInst::Create(Int, Op, CI->getName(), CI);
Eric Christopherfd179292009-08-27 18:07:15 +000011084
Chris Lattnerb8105652009-07-20 17:51:36 +000011085 CI->replaceAllUsesWith(Op);
11086 CI->eraseFromParent();
11087 return true;
11088}
11089
11090bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
11091 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
11092 std::vector<InlineAsm::ConstraintInfo> Constraints = IA->ParseConstraints();
11093
11094 std::string AsmStr = IA->getAsmString();
11095
11096 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000011097 SmallVector<StringRef, 4> AsmPieces;
Chris Lattnerb8105652009-07-20 17:51:36 +000011098 SplitString(AsmStr, AsmPieces, "\n"); // ; as separator?
11099
11100 switch (AsmPieces.size()) {
11101 default: return false;
11102 case 1:
11103 AsmStr = AsmPieces[0];
11104 AsmPieces.clear();
11105 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
11106
11107 // bswap $0
11108 if (AsmPieces.size() == 2 &&
11109 (AsmPieces[0] == "bswap" ||
11110 AsmPieces[0] == "bswapq" ||
11111 AsmPieces[0] == "bswapl") &&
11112 (AsmPieces[1] == "$0" ||
11113 AsmPieces[1] == "${0:q}")) {
11114 // No need to check constraints, nothing other than the equivalent of
11115 // "=r,0" would be valid here.
11116 return LowerToBSwap(CI);
11117 }
11118 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000011119 if (CI->getType()->isIntegerTy(16) &&
Chris Lattnerb8105652009-07-20 17:51:36 +000011120 AsmPieces.size() == 3 &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000011121 (AsmPieces[0] == "rorw" || AsmPieces[0] == "rolw") &&
Chris Lattnerb8105652009-07-20 17:51:36 +000011122 AsmPieces[1] == "$$8," &&
11123 AsmPieces[2] == "${0:w}" &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000011124 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
11125 AsmPieces.clear();
Benjamin Kramer018cbd52010-03-12 13:54:59 +000011126 const std::string &Constraints = IA->getConstraintString();
11127 SplitString(StringRef(Constraints).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000011128 std::sort(AsmPieces.begin(), AsmPieces.end());
11129 if (AsmPieces.size() == 4 &&
11130 AsmPieces[0] == "~{cc}" &&
11131 AsmPieces[1] == "~{dirflag}" &&
11132 AsmPieces[2] == "~{flags}" &&
11133 AsmPieces[3] == "~{fpsr}") {
11134 return LowerToBSwap(CI);
11135 }
Chris Lattnerb8105652009-07-20 17:51:36 +000011136 }
11137 break;
11138 case 3:
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000011139 if (CI->getType()->isIntegerTy(64) &&
Owen Anderson1d0be152009-08-13 21:58:54 +000011140 Constraints.size() >= 2 &&
Chris Lattnerb8105652009-07-20 17:51:36 +000011141 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
11142 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
11143 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
Benjamin Kramerd4f19592010-01-11 18:03:24 +000011144 SmallVector<StringRef, 4> Words;
Chris Lattnerb8105652009-07-20 17:51:36 +000011145 SplitString(AsmPieces[0], Words, " \t");
11146 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
11147 Words.clear();
11148 SplitString(AsmPieces[1], Words, " \t");
11149 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
11150 Words.clear();
11151 SplitString(AsmPieces[2], Words, " \t,");
11152 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
11153 Words[2] == "%edx") {
11154 return LowerToBSwap(CI);
11155 }
11156 }
11157 }
11158 }
11159 break;
11160 }
11161 return false;
11162}
11163
11164
11165
Chris Lattnerf4dff842006-07-11 02:54:03 +000011166/// getConstraintType - Given a constraint letter, return the type of
11167/// constraint it is for this target.
11168X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000011169X86TargetLowering::getConstraintType(const std::string &Constraint) const {
11170 if (Constraint.size() == 1) {
11171 switch (Constraint[0]) {
11172 case 'A':
Dale Johannesen330169f2008-11-13 21:52:36 +000011173 return C_Register;
Chris Lattnerfce84ac2008-03-11 19:06:29 +000011174 case 'f':
Chris Lattner4234f572007-03-25 02:14:49 +000011175 case 'r':
11176 case 'R':
11177 case 'l':
11178 case 'q':
11179 case 'Q':
11180 case 'x':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000011181 case 'y':
Chris Lattner4234f572007-03-25 02:14:49 +000011182 case 'Y':
11183 return C_RegisterClass;
Dale Johannesen78e3e522009-02-12 20:58:09 +000011184 case 'e':
11185 case 'Z':
11186 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000011187 default:
11188 break;
11189 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000011190 }
Chris Lattner4234f572007-03-25 02:14:49 +000011191 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000011192}
11193
John Thompsoneac6e1d2010-09-13 18:15:37 +000011194/// Examine constraint type and operand type and determine a weight value,
11195/// where: -1 = invalid match, and 0 = so-so match to 3 = good match.
11196/// This object must already have been set up with the operand type
11197/// and the current alternative constraint selected.
11198int X86TargetLowering::getSingleConstraintMatchWeight(
11199 AsmOperandInfo &info, const char *constraint) const {
11200 int weight = -1;
11201 Value *CallOperandVal = info.CallOperandVal;
11202 // If we don't have a value, we can't do a match,
11203 // but allow it at the lowest weight.
11204 if (CallOperandVal == NULL)
11205 return 0;
11206 // Look at the constraint type.
11207 switch (*constraint) {
11208 default:
11209 return TargetLowering::getSingleConstraintMatchWeight(info, constraint);
11210 break;
11211 case 'I':
11212 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
11213 if (C->getZExtValue() <= 31)
11214 weight = 3;
11215 }
11216 break;
11217 // etc.
11218 }
11219 return weight;
11220}
11221
Dale Johannesenba2a0b92008-01-29 02:21:21 +000011222/// LowerXConstraint - try to replace an X constraint, which matches anything,
11223/// with another that has more specific requirements based on the type of the
11224/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000011225const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000011226LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000011227 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
11228 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000011229 if (ConstraintVT.isFloatingPoint()) {
Dale Johannesenba2a0b92008-01-29 02:21:21 +000011230 if (Subtarget->hasSSE2())
Chris Lattner5e764232008-04-26 23:02:14 +000011231 return "Y";
11232 if (Subtarget->hasSSE1())
11233 return "x";
11234 }
Scott Michelfdc40a02009-02-17 22:15:04 +000011235
Chris Lattner5e764232008-04-26 23:02:14 +000011236 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000011237}
11238
Chris Lattner48884cd2007-08-25 00:47:38 +000011239/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
11240/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000011241void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattner48884cd2007-08-25 00:47:38 +000011242 char Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000011243 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000011244 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000011245 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000011246
Chris Lattner22aaf1d2006-10-31 20:13:11 +000011247 switch (Constraint) {
11248 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000011249 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000011250 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000011251 if (C->getZExtValue() <= 31) {
11252 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000011253 break;
11254 }
Devang Patel84f7fd22007-03-17 00:13:28 +000011255 }
Chris Lattner48884cd2007-08-25 00:47:38 +000011256 return;
Evan Cheng364091e2008-09-22 23:57:37 +000011257 case 'J':
11258 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000011259 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000011260 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
11261 break;
11262 }
11263 }
11264 return;
11265 case 'K':
11266 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000011267 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +000011268 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
11269 break;
11270 }
11271 }
11272 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000011273 case 'N':
11274 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000011275 if (C->getZExtValue() <= 255) {
11276 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000011277 break;
11278 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000011279 }
Chris Lattner48884cd2007-08-25 00:47:38 +000011280 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000011281 case 'e': {
11282 // 32-bit signed value
11283 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000011284 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
11285 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000011286 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000011287 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000011288 break;
11289 }
11290 // FIXME gcc accepts some relocatable values here too, but only in certain
11291 // memory models; it's complicated.
11292 }
11293 return;
11294 }
11295 case 'Z': {
11296 // 32-bit unsigned value
11297 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000011298 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
11299 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000011300 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
11301 break;
11302 }
11303 }
11304 // FIXME gcc accepts some relocatable values here too, but only in certain
11305 // memory models; it's complicated.
11306 return;
11307 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000011308 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000011309 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000011310 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000011311 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000011312 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000011313 break;
11314 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011315
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000011316 // In any sort of PIC mode addresses need to be computed at runtime by
11317 // adding in a register or some sort of table lookup. These can't
11318 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000011319 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000011320 return;
11321
Chris Lattnerdc43a882007-05-03 16:52:29 +000011322 // If we are in non-pic codegen mode, we allow the address of a global (with
11323 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000011324 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000011325 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000011326
Chris Lattner49921962009-05-08 18:23:14 +000011327 // Match either (GA), (GA+C), (GA+C1+C2), etc.
11328 while (1) {
11329 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
11330 Offset += GA->getOffset();
11331 break;
11332 } else if (Op.getOpcode() == ISD::ADD) {
11333 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
11334 Offset += C->getZExtValue();
11335 Op = Op.getOperand(0);
11336 continue;
11337 }
11338 } else if (Op.getOpcode() == ISD::SUB) {
11339 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
11340 Offset += -C->getZExtValue();
11341 Op = Op.getOperand(0);
11342 continue;
11343 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000011344 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000011345
Chris Lattner49921962009-05-08 18:23:14 +000011346 // Otherwise, this isn't something we can handle, reject it.
11347 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000011348 }
Eric Christopherfd179292009-08-27 18:07:15 +000011349
Dan Gohman46510a72010-04-15 01:51:59 +000011350 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000011351 // If we require an extra load to get this address, as in PIC mode, we
11352 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000011353 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
11354 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000011355 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000011356
Devang Patel0d881da2010-07-06 22:08:15 +000011357 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
11358 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000011359 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000011360 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000011361 }
Scott Michelfdc40a02009-02-17 22:15:04 +000011362
Gabor Greifba36cb52008-08-28 21:40:38 +000011363 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000011364 Ops.push_back(Result);
11365 return;
11366 }
Dale Johannesen1784d162010-06-25 21:55:36 +000011367 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000011368}
11369
Chris Lattner259e97c2006-01-31 19:43:35 +000011370std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +000011371getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000011372 EVT VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +000011373 if (Constraint.size() == 1) {
11374 // FIXME: not handling fp-stack yet!
Chris Lattner259e97c2006-01-31 19:43:35 +000011375 switch (Constraint[0]) { // GCC X86 Constraint Letters
Chris Lattnerf4dff842006-07-11 02:54:03 +000011376 default: break; // Unknown constraint letter
Evan Cheng47e9fab2009-07-17 22:13:25 +000011377 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
11378 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000011379 if (VT == MVT::i32)
Evan Cheng47e9fab2009-07-17 22:13:25 +000011380 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX,
11381 X86::ESI, X86::EDI, X86::R8D, X86::R9D,
11382 X86::R10D,X86::R11D,X86::R12D,
11383 X86::R13D,X86::R14D,X86::R15D,
11384 X86::EBP, X86::ESP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000011385 else if (VT == MVT::i16)
Evan Cheng47e9fab2009-07-17 22:13:25 +000011386 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX,
11387 X86::SI, X86::DI, X86::R8W,X86::R9W,
11388 X86::R10W,X86::R11W,X86::R12W,
11389 X86::R13W,X86::R14W,X86::R15W,
11390 X86::BP, X86::SP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000011391 else if (VT == MVT::i8)
Evan Cheng47e9fab2009-07-17 22:13:25 +000011392 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL,
11393 X86::SIL, X86::DIL, X86::R8B,X86::R9B,
11394 X86::R10B,X86::R11B,X86::R12B,
11395 X86::R13B,X86::R14B,X86::R15B,
11396 X86::BPL, X86::SPL, 0);
11397
Owen Anderson825b72b2009-08-11 20:47:22 +000011398 else if (VT == MVT::i64)
Evan Cheng47e9fab2009-07-17 22:13:25 +000011399 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX,
11400 X86::RSI, X86::RDI, X86::R8, X86::R9,
11401 X86::R10, X86::R11, X86::R12,
11402 X86::R13, X86::R14, X86::R15,
11403 X86::RBP, X86::RSP, 0);
11404
11405 break;
11406 }
Eric Christopherfd179292009-08-27 18:07:15 +000011407 // 32-bit fallthrough
Chris Lattner259e97c2006-01-31 19:43:35 +000011408 case 'Q': // Q_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +000011409 if (VT == MVT::i32)
Chris Lattner80a7ecc2006-05-06 00:29:37 +000011410 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000011411 else if (VT == MVT::i16)
Chris Lattner80a7ecc2006-05-06 00:29:37 +000011412 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000011413 else if (VT == MVT::i8)
Evan Cheng12914382007-08-13 23:27:11 +000011414 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000011415 else if (VT == MVT::i64)
Chris Lattner03e6c702007-11-04 06:51:12 +000011416 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
11417 break;
Chris Lattner259e97c2006-01-31 19:43:35 +000011418 }
11419 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011420
Chris Lattner1efa40f2006-02-22 00:56:39 +000011421 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +000011422}
Chris Lattnerf76d1802006-07-31 23:26:50 +000011423
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011424std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000011425X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000011426 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000011427 // First, see if this is a constraint that directly corresponds to an LLVM
11428 // register class.
11429 if (Constraint.size() == 1) {
11430 // GCC Constraint Letters
11431 switch (Constraint[0]) {
11432 default: break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000011433 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000011434 case 'l': // INDEX_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +000011435 if (VT == MVT::i8)
Chris Lattner0f65cad2007-04-09 05:49:22 +000011436 return std::make_pair(0U, X86::GR8RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000011437 if (VT == MVT::i16)
Chris Lattner1fa71982008-10-17 18:15:05 +000011438 return std::make_pair(0U, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000011439 if (VT == MVT::i32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +000011440 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +000011441 return std::make_pair(0U, X86::GR64RegisterClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000011442 case 'R': // LEGACY_REGS
11443 if (VT == MVT::i8)
11444 return std::make_pair(0U, X86::GR8_NOREXRegisterClass);
11445 if (VT == MVT::i16)
11446 return std::make_pair(0U, X86::GR16_NOREXRegisterClass);
11447 if (VT == MVT::i32 || !Subtarget->is64Bit())
11448 return std::make_pair(0U, X86::GR32_NOREXRegisterClass);
11449 return std::make_pair(0U, X86::GR64_NOREXRegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000011450 case 'f': // FP Stack registers.
11451 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
11452 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000011453 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000011454 return std::make_pair(0U, X86::RFP32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000011455 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000011456 return std::make_pair(0U, X86::RFP64RegisterClass);
11457 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000011458 case 'y': // MMX_REGS if MMX allowed.
11459 if (!Subtarget->hasMMX()) break;
11460 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000011461 case 'Y': // SSE_REGS if SSE2 allowed
11462 if (!Subtarget->hasSSE2()) break;
11463 // FALL THROUGH.
11464 case 'x': // SSE_REGS if SSE1 allowed
11465 if (!Subtarget->hasSSE1()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000011466
Owen Anderson825b72b2009-08-11 20:47:22 +000011467 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000011468 default: break;
11469 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000011470 case MVT::f32:
11471 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +000011472 return std::make_pair(0U, X86::FR32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000011473 case MVT::f64:
11474 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +000011475 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000011476 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000011477 case MVT::v16i8:
11478 case MVT::v8i16:
11479 case MVT::v4i32:
11480 case MVT::v2i64:
11481 case MVT::v4f32:
11482 case MVT::v2f64:
Chris Lattner0f65cad2007-04-09 05:49:22 +000011483 return std::make_pair(0U, X86::VR128RegisterClass);
11484 }
Chris Lattnerad043e82007-04-09 05:11:28 +000011485 break;
11486 }
11487 }
Scott Michelfdc40a02009-02-17 22:15:04 +000011488
Chris Lattnerf76d1802006-07-31 23:26:50 +000011489 // Use the default implementation in TargetLowering to convert the register
11490 // constraint into a member of a register class.
11491 std::pair<unsigned, const TargetRegisterClass*> Res;
11492 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000011493
11494 // Not found as a standard register?
11495 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000011496 // Map st(0) -> st(7) -> ST0
11497 if (Constraint.size() == 7 && Constraint[0] == '{' &&
11498 tolower(Constraint[1]) == 's' &&
11499 tolower(Constraint[2]) == 't' &&
11500 Constraint[3] == '(' &&
11501 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
11502 Constraint[5] == ')' &&
11503 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000011504
Chris Lattner56d77c72009-09-13 22:41:48 +000011505 Res.first = X86::ST0+Constraint[4]-'0';
11506 Res.second = X86::RFP80RegisterClass;
11507 return Res;
11508 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000011509
Chris Lattner56d77c72009-09-13 22:41:48 +000011510 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000011511 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000011512 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +000011513 Res.second = X86::RFP80RegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000011514 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000011515 }
Chris Lattner56d77c72009-09-13 22:41:48 +000011516
11517 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000011518 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000011519 Res.first = X86::EFLAGS;
11520 Res.second = X86::CCRRegisterClass;
11521 return Res;
11522 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000011523
Dale Johannesen330169f2008-11-13 21:52:36 +000011524 // 'A' means EAX + EDX.
11525 if (Constraint == "A") {
11526 Res.first = X86::EAX;
Dan Gohman68a31c22009-07-30 17:02:08 +000011527 Res.second = X86::GR32_ADRegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000011528 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000011529 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000011530 return Res;
11531 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011532
Chris Lattnerf76d1802006-07-31 23:26:50 +000011533 // Otherwise, check to see if this is a register class of the wrong value
11534 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
11535 // turn into {ax},{dx}.
11536 if (Res.second->hasType(VT))
11537 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011538
Chris Lattnerf76d1802006-07-31 23:26:50 +000011539 // All of the single-register GCC register classes map their values onto
11540 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
11541 // really want an 8-bit or 32-bit register, map to the appropriate register
11542 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +000011543 if (Res.second == X86::GR16RegisterClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000011544 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000011545 unsigned DestReg = 0;
11546 switch (Res.first) {
11547 default: break;
11548 case X86::AX: DestReg = X86::AL; break;
11549 case X86::DX: DestReg = X86::DL; break;
11550 case X86::CX: DestReg = X86::CL; break;
11551 case X86::BX: DestReg = X86::BL; break;
11552 }
11553 if (DestReg) {
11554 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000011555 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000011556 }
Owen Anderson825b72b2009-08-11 20:47:22 +000011557 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000011558 unsigned DestReg = 0;
11559 switch (Res.first) {
11560 default: break;
11561 case X86::AX: DestReg = X86::EAX; break;
11562 case X86::DX: DestReg = X86::EDX; break;
11563 case X86::CX: DestReg = X86::ECX; break;
11564 case X86::BX: DestReg = X86::EBX; break;
11565 case X86::SI: DestReg = X86::ESI; break;
11566 case X86::DI: DestReg = X86::EDI; break;
11567 case X86::BP: DestReg = X86::EBP; break;
11568 case X86::SP: DestReg = X86::ESP; break;
11569 }
11570 if (DestReg) {
11571 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000011572 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000011573 }
Owen Anderson825b72b2009-08-11 20:47:22 +000011574 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000011575 unsigned DestReg = 0;
11576 switch (Res.first) {
11577 default: break;
11578 case X86::AX: DestReg = X86::RAX; break;
11579 case X86::DX: DestReg = X86::RDX; break;
11580 case X86::CX: DestReg = X86::RCX; break;
11581 case X86::BX: DestReg = X86::RBX; break;
11582 case X86::SI: DestReg = X86::RSI; break;
11583 case X86::DI: DestReg = X86::RDI; break;
11584 case X86::BP: DestReg = X86::RBP; break;
11585 case X86::SP: DestReg = X86::RSP; break;
11586 }
11587 if (DestReg) {
11588 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000011589 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000011590 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000011591 }
Chris Lattner6ba50a92008-08-26 06:19:02 +000011592 } else if (Res.second == X86::FR32RegisterClass ||
11593 Res.second == X86::FR64RegisterClass ||
11594 Res.second == X86::VR128RegisterClass) {
11595 // Handle references to XMM physical registers that got mapped into the
11596 // wrong class. This can happen with constraints like {xmm0} where the
11597 // target independent register mapper will just pick the first match it can
11598 // find, ignoring the required type.
Owen Anderson825b72b2009-08-11 20:47:22 +000011599 if (VT == MVT::f32)
Chris Lattner6ba50a92008-08-26 06:19:02 +000011600 Res.second = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +000011601 else if (VT == MVT::f64)
Chris Lattner6ba50a92008-08-26 06:19:02 +000011602 Res.second = X86::FR64RegisterClass;
11603 else if (X86::VR128RegisterClass->hasType(VT))
11604 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000011605 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011606
Chris Lattnerf76d1802006-07-31 23:26:50 +000011607 return Res;
11608}